github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/nib/nib_test.go (about) 1 package nib 2 3 import ( 4 "bytes" 5 "time" 6 7 . "gopkg.in/check.v1" 8 ) 9 10 type NIBTests struct{} 11 12 var _ = Suite(&NIBTests{}) 13 14 func (t *NIBTests) TestEncode(c *C) { 15 n := NIB{} 16 n.ID = "1234" 17 n.HistoryOffset = 30 18 buf := &bytes.Buffer{} 19 written, err := n.WriteTo(buf) 20 c.Assert(err, IsNil) 21 n2 := NIB{} 22 read, err := n2.ReadFrom(buf) 23 c.Assert(err, IsNil) 24 c.Assert(written, Equals, read) 25 c.Assert(n, DeepEquals, n2) 26 } 27 28 func (t *NIBTests) TestRevisionEnDecode(c *C) { 29 r := &Revision{MetadataID: "1234"} 30 r.AddContentID("5678") 31 r.UTCTimestamp = time.Now().UnixNano() 32 r.DeviceID = "localhost" 33 n := NIB{} 34 n.AppendRevision(r) 35 buf := &bytes.Buffer{} 36 written, err := n.WriteTo(buf) 37 c.Assert(err, IsNil) 38 n2 := NIB{} 39 read, err := n2.ReadFrom(buf) 40 c.Assert(err, IsNil) 41 c.Assert(written, Equals, read) 42 r2, err := n2.LatestRevision() 43 c.Assert(err, IsNil) 44 c.Assert(r, DeepEquals, r2) 45 } 46 47 func (t *NIBTests) TestLatestRevisionFailure(c *C) { 48 n := NIB{} 49 r, err := n.LatestRevision() 50 c.Assert(r, IsNil) 51 c.Assert(err, NotNil) 52 } 53 54 func (t *NIBTests) TestLatestRevisionWithContent(c *C) { 55 n := &NIB{} 56 wanted := []string{"a", "b"} 57 n.AppendRevision(&Revision{MetadataID: "foo", ContentIDs: []string{"x", "y"}}) 58 n.AppendRevision(&Revision{MetadataID: "foo", ContentIDs: wanted}) 59 n.AppendRevision(&Revision{MetadataID: "foo", ContentIDs: []string{"c", "d"}}) 60 n.AppendRevision(&Revision{MetadataID: "foo", ContentIDs: []string{"a", "b", "c"}}) 61 rev, err := n.LatestRevisionWithContent(wanted) 62 c.Assert(err, IsNil) 63 c.Assert(rev.ContentIDs, DeepEquals, wanted) 64 } 65 66 func (t *NIBTests) TestLatestRevisionWithContentFail(c *C) { 67 n := &NIB{} 68 wanted := []string{"a", "b"} 69 n.AppendRevision(&Revision{MetadataID: "foo", ContentIDs: []string{"x", "y"}}) 70 n.AppendRevision(&Revision{MetadataID: "foo", ContentIDs: []string{"c", "d"}}) 71 n.AppendRevision(&Revision{MetadataID: "foo", ContentIDs: []string{"a", "b", "c"}}) 72 rev, err := n.LatestRevisionWithContent(wanted) 73 c.Assert(err, Equals, ErrNoRevision) 74 c.Assert(rev, IsNil) 75 } 76 77 func (t *NIBTests) TestRevisionsTotalSimple(c *C) { 78 n := NIB{} 79 n.AppendRevision(&Revision{}) 80 n.AppendRevision(&Revision{}) 81 c.Assert(n.RevisionsTotal(), Equals, int64(2)) 82 } 83 84 func (t *NIBTests) TestRevisionsTotalhWithOffset(c *C) { 85 n := NIB{ 86 HistoryOffset: 1097, 87 } 88 n.AppendRevision(&Revision{}) 89 n.AppendRevision(&Revision{}) 90 c.Assert(n.RevisionsTotal(), Equals, int64(1097+2)) 91 } 92 93 func (t *NIBTests) TestAllObjectIDs(c *C) { 94 n := &NIB{} 95 n.AppendRevision(&Revision{MetadataID: "meta1", 96 ContentIDs: []string{"content1", "content2"}}) 97 n.AppendRevision(&Revision{MetadataID: "meta2", 98 ContentIDs: []string{"content3", "content3"}}) 99 c.Assert(n.AllObjectIDs(), DeepEquals, 100 []string{"meta1", "content1", "content2", "meta2", "content3"}) 101 } 102 103 func (t *NIBTests) TestIsParentOf(c *C) { 104 oldNIB := &NIB{} 105 oldNIB.AppendRevision(&Revision{MetadataID: "meta1", 106 ContentIDs: []string{"content1", "content2"}}) 107 108 newNIB := &NIB{} 109 newNIB.AppendRevision(&Revision{MetadataID: "meta1", 110 ContentIDs: []string{"content1", "content2"}}) 111 newNIB.AppendRevision(&Revision{MetadataID: "meta2", 112 ContentIDs: []string{"content3", "content3"}}) 113 114 c.Assert(oldNIB.IsParentOf(newNIB), Equals, true) 115 } 116 117 func (t *NIBTests) TestIsParentOfSameNIB(c *C) { 118 oldNIB := NIB{} 119 oldNIB.AppendRevision(&Revision{MetadataID: "meta1", 120 ContentIDs: []string{"content1", "content2"}}) 121 oldNIB.AppendRevision(&Revision{MetadataID: "meta2", 122 ContentIDs: []string{"content3", "content3"}}) 123 124 newNIB := oldNIB 125 c.Assert(oldNIB.IsParentOf(&newNIB), Equals, true) 126 } 127 128 func (t *NIBTests) TestIsParentOfLongerOfShorter(c *C) { 129 oldNIB := &NIB{} 130 oldNIB.AppendRevision(&Revision{MetadataID: "meta1", 131 ContentIDs: []string{"content1", "content2"}}) 132 oldNIB.AppendRevision(&Revision{MetadataID: "meta2", 133 ContentIDs: []string{"content3", "content3"}}) 134 135 newNIB := &NIB{} 136 newNIB.AppendRevision(&Revision{MetadataID: "meta1", 137 ContentIDs: []string{"content1", "content2"}}) 138 139 c.Assert(oldNIB.IsParentOf(newNIB), Equals, false) 140 } 141 142 func (t *NIBTests) TestIsParentOfDifferentMetadata(c *C) { 143 oldNIB := &NIB{} 144 oldNIB.AppendRevision(&Revision{MetadataID: "meta1", 145 ContentIDs: []string{"content1", "content2"}}) 146 147 newNIB := &NIB{} 148 newNIB.AppendRevision(&Revision{MetadataID: "meta2", 149 ContentIDs: []string{"content1", "content2"}}) 150 151 c.Assert(oldNIB.IsParentOf(newNIB), Equals, false) 152 } 153 154 func (t *NIBTests) TestIsParentOfZeroLengthParent(c *C) { 155 oldNIB := &NIB{} 156 157 newNIB := &NIB{} 158 newNIB.AppendRevision(&Revision{MetadataID: "meta2", 159 ContentIDs: []string{"content1", "content2"}}) 160 161 c.Assert(oldNIB.IsParentOf(newNIB), Equals, true) 162 }