This repository has been archived on 2022-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
activitypub/collection_test.go

241 lines
5.7 KiB
Go
Raw Normal View History

package activitypub
2017-09-11 21:16:43 +00:00
import (
"reflect"
"testing"
)
2017-09-11 21:16:43 +00:00
2017-10-01 12:55:39 +00:00
func TestCollectionNew(t *testing.T) {
2019-12-05 18:02:15 +00:00
var testValue = ID("test")
2017-09-11 21:16:43 +00:00
c := CollectionNew(testValue)
if c.ID != testValue {
t.Errorf("APObject Id '%v' different than expected '%v'", c.ID, testValue)
2017-09-11 21:16:43 +00:00
}
if c.Type != CollectionType {
t.Errorf("APObject Type '%v' different than expected '%v'", c.Type, CollectionType)
2017-09-11 21:16:43 +00:00
}
}
2018-11-04 18:06:46 +00:00
func TestCollection_Append(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2019-12-05 18:02:15 +00:00
val := Object{ID: ID("grrr")}
c := CollectionNew(id)
c.Append(val)
if c.Count() != 1 {
t.Errorf("Inbox collectionPath of %q should have one element", c.GetID())
}
if !reflect.DeepEqual(c.Items[0], val) {
t.Errorf("First item in Inbox is does not match %q", val.ID)
}
}
2018-11-04 18:06:46 +00:00
func TestCollection_Collection(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2018-11-04 18:06:46 +00:00
c := CollectionNew(id)
if !reflect.DeepEqual(c.Collection(), c.Items) {
t.Errorf("Collection items should be equal %v %v", c.Collection(), c.Items)
2018-11-04 18:06:46 +00:00
}
}
func TestCollection_GetID(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2018-11-04 18:06:46 +00:00
c := CollectionNew(id)
if c.GetID() != id {
t.Errorf("GetID should return %s, received %s", id, c.GetID())
2018-11-04 18:06:46 +00:00
}
}
func TestCollection_GetLink(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2018-11-04 18:06:46 +00:00
link := IRI(id)
c := CollectionNew(id)
if c.GetLink() != link {
t.Errorf("GetLink should return %q, received %q", link, c.GetLink())
}
}
func TestCollection_GetType(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2018-11-04 18:06:46 +00:00
c := CollectionNew(id)
if c.GetType() != CollectionType {
t.Errorf("Collection Type should be %q, received %q", CollectionType, c.GetType())
}
}
func TestCollection_IsLink(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2018-11-04 18:06:46 +00:00
c := CollectionNew(id)
if c.IsLink() != false {
t.Errorf("Collection should not be a link, received %t", c.IsLink())
}
}
func TestCollection_IsObject(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2018-11-04 18:06:46 +00:00
c := CollectionNew(id)
if c.IsObject() != true {
t.Errorf("Collection should be an object, received %t", c.IsObject())
}
}
func TestCollection_UnmarshalJSON(t *testing.T) {
2018-11-04 19:07:26 +00:00
c := Collection{}
2018-11-04 18:06:46 +00:00
2018-11-04 19:07:26 +00:00
dataEmpty := []byte("{}")
c.UnmarshalJSON(dataEmpty)
if c.ID != "" {
t.Errorf("Unmarshaled object should have empty ID, received %q", c.ID)
2018-11-04 19:07:26 +00:00
}
if c.Type != "" {
t.Errorf("Unmarshaled object should have empty Type, received %q", c.Type)
2018-11-04 19:07:26 +00:00
}
if c.AttributedTo != nil {
t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", c.AttributedTo)
2018-11-04 19:07:26 +00:00
}
if len(c.Name) != 0 {
t.Errorf("Unmarshaled object should have empty Name, received %q", c.Name)
2018-11-04 19:07:26 +00:00
}
if len(c.Summary) != 0 {
t.Errorf("Unmarshaled object should have empty Summary, received %q", c.Summary)
2018-11-04 19:07:26 +00:00
}
if len(c.Content) != 0 {
t.Errorf("Unmarshaled object should have empty Content, received %q", c.Content)
2018-11-04 19:07:26 +00:00
}
if c.TotalItems != 0 {
t.Errorf("Unmarshaled object should have empty TotalItems, received %d", c.TotalItems)
2018-11-04 19:07:26 +00:00
}
if len(c.Items) > 0 {
t.Errorf("Unmarshaled object should have empty Items, received %v", c.Items)
2018-11-04 19:07:26 +00:00
}
if c.URL != nil {
t.Errorf("Unmarshaled object should have empty URL, received %v", c.URL)
2018-11-04 19:07:26 +00:00
}
if !c.Published.IsZero() {
t.Errorf("Unmarshaled object should have empty Published, received %q", c.Published)
2018-11-04 19:07:26 +00:00
}
if !c.StartTime.IsZero() {
t.Errorf("Unmarshaled object should have empty StartTime, received %q", c.StartTime)
2018-11-04 19:07:26 +00:00
}
if !c.Updated.IsZero() {
t.Errorf("Unmarshaled object should have empty Updated, received %q", c.Updated)
2018-11-04 19:07:26 +00:00
}
2018-11-04 18:06:46 +00:00
}
func TestCollection_Count(t *testing.T) {
2019-12-05 18:02:15 +00:00
id := ID("test")
2019-07-09 11:30:02 +00:00
c := CollectionNew(id)
if c.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", c.TotalItems)
}
if len(c.Items) > 0 {
t.Errorf("Empty object should have empty Items, received %v", c.Items)
}
if c.Count() != uint(len(c.Items)) {
t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.Items))
}
c.Append(IRI("test"))
if c.TotalItems != 0 {
t.Errorf("Empty object should have empty TotalItems, received %d", c.TotalItems)
}
if c.Count() != uint(len(c.Items)) {
t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.Items))
}
}
func TestCollection_Contains(t *testing.T) {
t.Skipf("TODO")
}
2019-12-15 18:01:29 +00:00
func TestCollection_IsCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestFollowersNew(t *testing.T) {
t.Skipf("TODO")
}
func TestFollowingNew(t *testing.T) {
t.Skipf("TODO")
}
2020-04-13 10:00:26 +00:00
func TestCollection_MarshalJSON(t *testing.T) {
t.Skipf("TODO")
}
func TestCollection_ItemMatches(t *testing.T) {
t.Skipf("TODO")
}
func TestToCollection(t *testing.T) {
err := func(it Item) error { return ErrorInvalidType[Collection](it) }
tests := map[string]struct {
it Item
want *Collection
wantErr error
}{
"Collection": {
2021-11-12 17:26:50 +00:00
it: new(Collection),
want: new(Collection),
wantErr: nil,
},
"CollectionPage": {
2021-11-12 17:26:50 +00:00
it: new(CollectionPage),
want: new(Collection),
wantErr: nil,
},
"OrderedCollectionPage": {
2021-11-12 17:26:50 +00:00
it: new(OrderedCollectionPage),
want: new(Collection),
wantErr: err(new(OrderedCollectionPage)),
},
"OrderedCollection": {
2021-11-12 17:26:50 +00:00
it: new(OrderedCollection),
want: new(Collection),
wantErr: err(new(OrderedCollection)),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := ToCollection(tt.it)
if tt.wantErr != nil && err == nil {
t.Errorf("ToCollection() no error returned, wanted error = [%T]%s", tt.wantErr, tt.wantErr)
return
}
if err != nil {
if tt.wantErr == nil {
t.Errorf("ToCollection() returned unexpected error[%T]%s", err, err)
return
}
if !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("ToCollection() received error %v, wanted error %v", err, tt.wantErr)
return
}
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToCollection() got = %v, want %v", got, tt.want)
}
})
}
}