This repository was archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_item_test.go
144 lines (118 loc) · 3.43 KB
/
collection_item_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package core
import (
"fmt"
"github.com/datatogether/sql_datastore"
"github.com/ipfs/go-datastore"
"testing"
)
// confirm CollectionItem conforms to sql datastore mode
var _ = sql_datastore.Model(&CollectionItem{})
func TestCollectionItemStorage(t *testing.T) {
// store := datastore.NewLogDatastore(datastore.NewMapDatastore(), "CollectionItemTest")
store := datastore.NewMapDatastore()
c := &Collection{Title: "test collection"}
if err := c.Save(store); err != nil {
t.Error(err.Error())
return
}
u := Url{Url: "http://example.url.test.com"}
item := &CollectionItem{collectionId: c.Id, Url: u, Index: 0, Description: "Description"}
if err := item.Save(store); err != nil {
t.Error(err.Error())
return
}
item2 := &CollectionItem{collectionId: c.Id, Url: item.Url}
if err := item2.Read(store); err != nil {
t.Error(err.Error())
return
}
if err := CompareCollectionItems(item, item2); err != nil {
t.Error(err.Error())
return
}
item.Description = "Updated Description"
if err := item.Save(store); err != nil {
t.Error(err.Error())
return
}
item2 = &CollectionItem{collectionId: c.Id, Url: item.Url}
if err := item2.Read(store); err != nil {
t.Error(err.Error())
return
}
if err := CompareCollectionItems(item, item2); err != nil {
t.Error(err.Error())
return
}
if err := item.Delete(store); err != nil {
t.Error(err.Error())
return
}
}
func TestCollectionItemSQLStorage(t *testing.T) {
// defer resetTestData(appDB, "collections")
store := sql_datastore.NewDatastore(appDB)
if err := store.Register(&Collection{}, &CollectionItem{}, &Url{}); err != nil {
t.Error(err.Error())
return
}
c := &Collection{Title: "test collection"}
if err := c.Save(store); err != nil {
t.Error(err.Error())
return
}
item := &CollectionItem{collectionId: c.Id, Url: Url{Url: "http://test.url.two.example.test"}, Index: 0, Description: "item description"}
if err := item.Save(store); err != nil {
t.Error(err.Error())
return
}
item.Description = "updated item description"
if err := item.Save(store); err != nil {
t.Error(err.Error())
return
}
item2 := &CollectionItem{collectionId: c.Id, Url: Url{Id: item.Id}}
if err := item2.Read(store); err != nil {
t.Error(err.Error())
return
}
if err := CompareCollectionItems(item, item2); err != nil {
t.Error(err.Error())
return
}
urlItem := &CollectionItem{collectionId: c.Id, Url: Url{Url: item.Url.Url}}
if err := urlItem.Read(store); err != nil {
t.Error(err.Error())
return
}
if err := CompareCollectionItems(item, urlItem); err != nil {
t.Error(err.Error())
return
}
if err := item.Delete(store); err != nil {
t.Error(err.Error())
return
}
}
func CompareCollectionItems(a, b *CollectionItem) error {
// TODO - can't compare full urls b/c we don't always fill the whole thing out
// if err := CompareUrls(&a.Url, &b.Url); err != nil {
// return fmt.Errorf("url mismatch: %s", err.Error())
// }
if a.Url.Id != b.Url.Id {
return fmt.Errorf("url id mismatch %s != %s", a.Url.Id, b.Url.Id)
}
if a.Url.Url != b.Url.Url {
return fmt.Errorf("url mismatch %s != %s", a.Url.Url, b.Url.Url)
}
if a.collectionId != b.collectionId {
return fmt.Errorf("collectionId mismatch: %d != %d", a.Index, b.Index)
}
if a.Index != b.Index {
return fmt.Errorf("Index mismatch: %d != %d", a.Index, b.Index)
}
if a.Description != b.Description {
return fmt.Errorf("Description mistmatch: %s != %s", a.Description, b.Description)
}
return nil
}