Improved ItemCollectionDeduplication

More tests for object equality method
This commit is contained in:
Marius Orcsik 2020-05-15 15:17:35 +02:00
parent fac868b7c2
commit 342a261a17
No known key found for this signature in database
GPG key ID: 77618B618F79EB72
11 changed files with 86 additions and 46 deletions

View file

@ -400,8 +400,7 @@ func (a *Activity) Recipients() ItemCollection {
if len(alwaysRemove) > 0 {
removeFromAudience(a, alwaysRemove...)
}
rec, _ := ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience)
return rec
return ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience)
}
// Clean removes Bto and BCC properties

View file

@ -306,8 +306,7 @@ func ServiceNew(id ID) *Service {
}
func (a *Actor) Recipients() ItemCollection {
rec, _ := ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience)
return rec
return ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience)
}
func (a *Actor) Clean() {

View file

@ -135,10 +135,7 @@ type (
// Recipients performs recipient de-duplication on the Activity's To, Bto, CC and BCC properties
func (i *IntransitiveActivity) Recipients() ItemCollection {
actor := make(ItemCollection, 0)
actor.Append(i.Actor)
rec, _ := ItemCollectionDeduplication(&actor, &i.To, &i.Bto, &i.CC, &i.BCC, &i.Audience)
return rec
return ItemCollectionDeduplication(&ItemCollection{i.Actor}, &i.To, &i.Bto, &i.CC, &i.BCC, &i.Audience)
}
// Clean removes Bto and BCC properties

View file

@ -89,7 +89,7 @@ func (i ItemCollection) Contains(r Item) bool {
}
// ItemCollectionDeduplication normalizes the received arguments lists into a single unified one
func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, error) {
func ItemCollectionDeduplication(recCols ...*ItemCollection) ItemCollection {
rec := make(ItemCollection, 0)
for _, recCol := range recCols {
@ -105,14 +105,14 @@ func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, er
}
var testIt IRI
if cur.IsObject() {
testIt = IRI(cur.GetID())
testIt = cur.GetID()
} else if cur.IsLink() {
testIt = cur.GetLink()
} else {
continue
}
for _, it := range rec {
if testIt.Equals(IRI(it.GetID()), false) {
if testIt.Equals(it.GetID(), false) {
// mark the element for removal
toRemove = append(toRemove, i)
save = false
@ -128,7 +128,7 @@ func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, er
*recCol = append((*recCol)[:idx], (*recCol)[idx+1:]...)
}
}
return rec, nil
return rec
}
// FlattenItemCollection flattens the Collection's properties from Object type to IRI

View file

@ -477,9 +477,10 @@ func TestNaturalLanguageValues_Equals(t *testing.T) {
want bool
}{
{
name: "equal-key-value", n: NaturalLanguageValues{LangRefValue{
Ref: "en",
Value: "test123#",
name: "equal-key-value",
n: NaturalLanguageValues{LangRefValue{
Ref: "en",
Value: "test123#",
}},
args: args{
with: NaturalLanguageValues{LangRefValue{
@ -490,9 +491,10 @@ func TestNaturalLanguageValues_Equals(t *testing.T) {
want: true,
},
{
name: "not-equal-key", n: NaturalLanguageValues{LangRefValue{
Ref: "en",
Value: "test123#",
name: "not-equal-key",
n: NaturalLanguageValues{LangRefValue{
Ref: "en",
Value: "test123#",
}},
args: args{
with: NaturalLanguageValues{LangRefValue{
@ -503,9 +505,10 @@ func TestNaturalLanguageValues_Equals(t *testing.T) {
want: false,
},
{
name: "not-equal-value", n: NaturalLanguageValues{LangRefValue{
Ref: "en",
Value: "test123#",
name: "not-equal-value",
n: NaturalLanguageValues{LangRefValue{
Ref: "en",
Value: "test123#",
}},
args: args{
with: NaturalLanguageValues{LangRefValue{

View file

@ -278,8 +278,7 @@ func (o Object) MarshalJSON() ([]byte, error) {
// Recipients performs recipient de-duplication on the Object's To, Bto, CC and BCC properties
func (o *Object) Recipients() ItemCollection {
var aud ItemCollection
rec, _ := ItemCollectionDeduplication(&aud, &o.To, &o.Bto, &o.CC, &o.BCC, &o.Audience)
return rec
return ItemCollectionDeduplication(&aud, &o.To, &o.Bto, &o.CC, &o.BCC, &o.Audience)
}
// Clean removes Bto and BCC properties
@ -569,6 +568,10 @@ func (o Object) Equals(with Item) bool {
}
}
if w.URL != nil {
if o.URL == nil {
result = false
return nil
}
if !w.URL.GetLink().Equals(o.URL.GetLink(), false) {
result = false
return nil

View file

@ -272,11 +272,6 @@ func TestRecipients(t *testing.T) {
if len(second) != 0 {
t.Errorf("Second Objects array should have exactly 0(zero) elements, not %d", len(second))
}
_, err := ItemCollectionDeduplication(&first, &second, nil)
if err != nil {
t.Errorf("Deduplication with empty array failed")
}
}
func validateEmptyObject(o Object, t *testing.T) {
@ -751,16 +746,16 @@ func TestSource_MarshalJSON(t *testing.T) {
wantErr: false,
},
{
name: "MediaType",
fields: fields{
name: "MediaType",
fields: fields{
MediaType: MimeType("blank"),
},
want: []byte(`{"mediaType":"blank"}`),
wantErr: false,
},
{
name: "OneContentValue",
fields: fields{
name: "OneContentValue",
fields: fields{
Content: NaturalLanguageValues{
{Value: "test"},
},
@ -769,8 +764,8 @@ func TestSource_MarshalJSON(t *testing.T) {
wantErr: false,
},
{
name: "MultipleContentValues",
fields: fields{
name: "MultipleContentValues",
fields: fields{
Content: NaturalLanguageValues{
{
Ref: "en",
@ -803,3 +798,55 @@ func TestSource_MarshalJSON(t *testing.T) {
})
}
}
func TestObject_Equals(t *testing.T) {
type args struct {
with Object
}
tests := []struct {
name string
o Object
args args
want bool
}{
{
name: "equal-empty-object",
o: Object{},
args: args{
with: Object{},
},
want: true,
},
{
name: "equal-object-just-id",
o: Object{ID: "test"},
args: args{
with: Object{ID: "test"},
},
want: true,
},
{
name: "equal-object-id",
o: Object{ID: "test", URL: IRI("example.com")},
args: args{
with: Object{ID: "test"},
},
want: true,
},
{
name: "equal-false-with-id-and-url",
o: Object{ID: "test"},
args: args{
with: Object{ID: "test", URL: IRI("example.com")},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.o.Equals(tt.args.with); got != tt.want {
t.Errorf("Equals() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -192,9 +192,7 @@ func (p Place) MarshalJSON() ([]byte, error) {
// Recipients performs recipient de-duplication on the Place object's To, Bto, CC and BCC properties
func (p *Place) Recipients() ItemCollection {
var aud ItemCollection
rec, _ := ItemCollectionDeduplication(&aud, &p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience)
return rec
return ItemCollectionDeduplication(&p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience)
}
// Clean removes Bto and BCC properties

View file

@ -164,9 +164,7 @@ func (p Profile) MarshalJSON() ([]byte, error) {
// Recipients performs recipient de-duplication on the Profile object's To, Bto, CC and BCC properties
func (p *Profile) Recipients() ItemCollection {
var aud ItemCollection
rec, _ := ItemCollectionDeduplication(&aud, &p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience)
return rec
return ItemCollectionDeduplication(&p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience)
}
// Clean removes Bto and BCC properties

View file

@ -181,9 +181,7 @@ func (r Relationship) MarshalJSON() ([]byte, error) {
// Recipients performs recipient de-duplication on the Relationship object's To, Bto, CC and BCC properties
func (r *Relationship) Recipients() ItemCollection {
var aud ItemCollection
rec, _ := ItemCollectionDeduplication(&aud, &r.To, &r.Bto, &r.CC, &r.BCC, &r.Audience)
return rec
return ItemCollectionDeduplication(&r.To, &r.Bto, &r.CC, &r.BCC, &r.Audience)
}
// Clean removes Bto and BCC properties

View file

@ -170,9 +170,7 @@ func (t Tombstone) MarshalJSON() ([]byte, error) {
// Recipients performs recipient de-duplication on the Tombstone object's To, Bto, CC and BCC properties
func (t *Tombstone) Recipients() ItemCollection {
var aud ItemCollection
rec, _ := ItemCollectionDeduplication(&aud, &t.To, &t.Bto, &t.CC, &t.BCC, &t.Audience)
return rec
return ItemCollectionDeduplication(&t.To, &t.Bto, &t.CC, &t.BCC, &t.Audience)
}
// Clean removes Bto and BCC properties