/
items_iterator_test.go
107 lines (90 loc) · 3.3 KB
/
items_iterator_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
package appx_test
import (
"appengine/aetest"
"appengine/datastore"
"github.com/drborges/appx"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestItemsIterator(t *testing.T) {
c, _ := aetest.NewContext(nil)
defer c.Close()
tags := []*Tag{
&Tag{Name: "golang", Owner: "Borges"},
&Tag{Name: "ruby", Owner: "Borges"},
&Tag{Name: "scala", Owner: "Borges"},
&Tag{Name: "swift", Owner: "Diego"},
}
createAll(c, tags...)
// TODO fix non deterministic issue
Convey("ItemsIterator", t, func() {
Convey("Given I have an items iterator with 3 pages each with 1 item", func() {
q := datastore.NewQuery(new(Tag).KeySpec().Kind).Filter("Owner=", "Borges").Limit(1)
iter := appx.NewDatastore(c).Query(q).ItemsIterator()
tagsFromIterator := []*Tag{&Tag{}, &Tag{}, &Tag{}}
Convey("Then I can load the first item", func() {
So(iter.HasNext(), ShouldBeTrue)
So(iter.Cursor(), ShouldBeEmpty)
So(iter.LoadNext(tagsFromIterator[0]), ShouldBeNil)
So(iter.HasNext(), ShouldBeTrue)
So(iter.Cursor(), ShouldNotBeEmpty)
So(tagsFromIterator[0], ShouldResemble, tags[0])
Convey("Then I can load the second item", func() {
So(iter.LoadNext(tagsFromIterator[1]), ShouldBeNil)
So(iter.HasNext(), ShouldBeTrue)
So(iter.Cursor(), ShouldNotBeEmpty)
So(tagsFromIterator[1], ShouldResemble, tags[2])
Convey("Then I can load the third item", func() {
So(iter.LoadNext(tagsFromIterator[2]), ShouldBeNil)
So(iter.HasNext(), ShouldBeTrue)
So(iter.Cursor(), ShouldNotBeEmpty)
So(tagsFromIterator[2], ShouldResemble, tags[1])
Convey("Then I cannot load more items", func() {
So(iter.LoadNext(&Tag{}), ShouldEqual, datastore.Done)
So(iter.HasNext(), ShouldBeFalse)
So(iter.Cursor(), ShouldBeEmpty)
})
})
})
Convey("I can create a new iterator using the cursor from the previous one", func() {
iterWithCursor := appx.NewDatastore(c).Query(q).StartFrom(iter.Cursor()).ItemsIterator()
Convey("I can load the second item", func() {
So(iterWithCursor.LoadNext(tagsFromIterator[1]), ShouldBeNil)
So(iterWithCursor.HasNext(), ShouldBeTrue)
So(iter.Cursor(), ShouldNotBeEmpty)
So(tagsFromIterator[1], ShouldResemble, tags[1])
})
})
})
Convey("Then I can load items until iterator has no more items", func() {
items := []*Tag{}
for iter.HasNext() {
item := &Tag{}
if err := iter.LoadNext(item); err == nil {
items = append(items, item)
}
}
So(len(items), ShouldEqual, 3)
So(items[0], ShouldResemble, tags[0])
So(items[1], ShouldResemble, tags[1])
So(items[2], ShouldResemble, tags[2])
})
})
Convey("Given I have an items iterator with zero items", func() {
q := datastore.NewQuery(new(Tag).KeySpec().Kind).Filter("Owner=", "non existent").Limit(1)
iter := appx.NewDatastore(c).Query(q).ItemsIterator()
Convey("When I load the next item", func() {
firstItem := Tag{}
So(iter.Cursor(), ShouldBeEmpty)
So(iter.LoadNext(&firstItem), ShouldEqual, datastore.Done)
So(iter.Cursor(), ShouldBeEmpty)
Convey("Then the item is not populated", func() {
So(firstItem, ShouldResemble, Tag{})
Convey("Then it has no more results", func() {
So(iter.HasNext(), ShouldBeFalse)
})
})
})
})
})
}