class MergeCounterChangesMadeInJSONTestCase(unittest.TestCase): def setUp(self): self.dc = DocumentCollection() self.dc.Register(CounterTestContainer) def runTest(self): #Create an object and set some values test1 = CounterTestContainer(None) test1id = test1.id self.dc.AddDocumentObject(test1) test1.testcounter.add(1) self.assertEqual(test1.testcounter.get(), 1) olddc = self.dc sharedcurrentnode = test1.currentnode #Simulate sending the object to another user via conversion to JSON and emailing jsontext = self.dc.asJSON() #Simulate making local conflicting changes test1.testcounter.subtract(1) self.assertEqual(test1.testcounter.get(), 0) #Simulate the other user (who received the email with the edges) getting the document and loading it into memory self.dc = DocumentCollection() self.dc.Register(CounterTestContainer) self.dc.LoadFromJSON(jsontext) self.assertEqual(jsontext, self.dc.asJSON()) tpo1s = self.dc.GetByClass(CounterTestContainer) self.assertEqual(len(tpo1s), 1) test2 = tpo1s[0] self.assertEqual(sharedcurrentnode, test2.currentnode) #The second user makes some changes and sends them back to the first test2.testcounter.add(1) self.assertEqual(test2.testcounter.get(), 2) edgenext = test2.history.edgesbyendnode[test2.currentnode] #Simulate the first user received the second users changes out of order #the second edge is received first. Test it is right self.dc = olddc self.dc.LoadFromJSON(JSONEncoder().encode({ "history": [edgenext.asTuple()], "immutableobjects": [] })) test2s = self.dc.GetByClass(CounterTestContainer) self.assertEqual(len(test2s), 1) test2 = test2s[0] #print "test2 edges=",[str(edge) for edge in test2.history.GetAllEdges()] self.assertEqual(test2.testcounter.get(), 1)
class StoreObjectsInJSONTestCase(unittest.TestCase): def setUp(self): self.dc = DocumentCollection() self.dc.Register(TestPropertyOwner1) self.dc.Register(TestPropertyOwner2) def runTest(self): #Test writing the history to a sql lite database test1 = TestPropertyOwner1(None) self.dc.AddDocumentObject(test1) test1id = test1.id testitem1 = TestPropertyOwner2(None) testitem1id = testitem1.id test1.propertyowner2s.add(testitem1) test2 = test1.Clone() testitem1.cover = 3 test2.covers = 2 self.assertEqual(len(test1.propertyowner2s), 1) test3 = test2.Merge(test1) self.assertEqual(len(test3.propertyowner2s), 1) for item1 in test3.propertyowner2s: self.assertEqual(item1.cover, 3) self.assertEqual(test3.covers, 2) self.dc.AddDocumentObject(test3) test1s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(test1s), 1) jsontext = self.dc.asJSON() self.dc = DocumentCollection() self.dc.Register(TestPropertyOwner1) self.dc.Register(TestPropertyOwner2) self.dc.LoadFromJSON(jsontext) test1s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(test1s), 1) test1 = test1s[0] test1id = test1.id self.assertEqual(len(test1.propertyowner2s), 1) for testitem1 in test3.propertyowner2s: self.assertEqual(testitem1id, testitem1.id) self.assertEqual(testitem1.cover, 3) self.assertEqual(test1.covers, 2)
class StoreImmutableObjectsInJSONTestCase(unittest.TestCase): def setUp(self): self.dc = DocumentCollection() self.dc.Register(MessageTest) def runTest(self): #Test writing the immutable object to an sql lite database t = int(round(time.time() * 1000)) m = MessageTest(messagetime=t, text="Hello") self.dc.AddImmutableObject(m) test1id = m.GetHash() test1s = self.dc.GetByClass(MessageTest) self.assertEqual(len(test1s), 1) jsontext = self.dc.asJSON() self.dc = DocumentCollection() self.dc.Register(MessageTest) self.dc.LoadFromJSON(jsontext) test1s = self.dc.GetByClass(MessageTest) self.assertEqual(len(test1s), 1) test1 = test1s[0] self.assertEqual(test1id, test1.GetHash())
class MergeAdvancedChangesMadeInJSONTestCase(unittest.TestCase): #Similar to merge changes test but testing things such as out of order reception of edges #Orphaned edges and partially orphaned merge edges def setUp(self): self.dc = DocumentCollection() self.dc.Register(TestPropertyOwner1) self.dc.Register(TestPropertyOwner2) def runTest(self): #Create an object and set some values test1 = TestPropertyOwner1(None) test1id = test1.id testitem1 = TestPropertyOwner2(None) testitem1id = testitem1.id test1.propertyowner2s.add(testitem1) testitem1.cover = 3 test1.covers = 2 self.dc.AddDocumentObject(test1) olddc = self.dc #Simulate sending the object to another user via conversion to JSON and emailing jsontext = self.dc.asJSON() #Simulate the other user (who received the email with the edges) getting the document and loading it into memory self.dc = DocumentCollection() self.dc.Register(TestPropertyOwner1) self.dc.Register(TestPropertyOwner2) self.dc.LoadFromJSON(jsontext) tpo1s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(tpo1s), 1) test2 = tpo1s[0] self.assertEqual(len(test2.propertyowner2s), 1) #The second user makes some changes and sends them back to the first for testitem2 in test2.propertyowner2s: testitem2.cover = 4 edge4 = test2.history.edgesbyendnode[test2.currentnode] test2.covers = 3 edge3 = test2.history.edgesbyendnode[test2.currentnode] #Simulate the first user received the second users changes out of order #the second edge is received first. Test it is right self.dc = olddc self.dc.LoadFromJSON(JSONEncoder().encode({ "history": [edge3.asTuple()], "immutableobjects": [] })) test2s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(test2s), 1) test2 = test2s[0] self.assertEqual(test2.covers, 2) for testitem2 in test2.propertyowner2s: self.assertEqual(testitem2.cover, 3) self.assertEqual(testitem2.cover, 3) #Simulate the first user received the second users changes out of order #the first edge is not received make sure everything self.dc.LoadFromJSON(JSONEncoder().encode({ "history": [edge4.asTuple()], "immutableobjects": [] })) test2s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(test2s), 1) test2 = test2s[0] self.assertEqual(test2.covers, 3) for testitem2 in test2.propertyowner2s: self.assertEqual(testitem2.cover, 4) self.assertEqual(testitem2.cover, 4) oldnode = test2.currentnode dummysha1 = hashlib.sha256('Invalid node 1').hexdigest() dummysha2 = hashlib.sha256('Invalid node 2').hexdigest() edgenull1 = HistoryEdgeMerge({dummysha1, dummysha2}, "", "", "", "", test2.id, test2.__class__.__name__) edgenull2 = HistoryEdgeMerge( {test2.currentnode, edgenull1.GetEndNode()}, "", "", "", "", test2.id, test2.__class__.__name__) self.dc.LoadFromJSON(JSONEncoder().encode({ "history": [edgenull2.asTuple()], "immutableobjects": [] })) test2s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(test2s), 1) test2 = test2s[0] self.assertEqual(oldnode, test2.currentnode) self.assertEqual(test2.covers, 3) for testitem2 in test2.propertyowner2s: self.assertEqual(testitem2.cover, 4) self.assertEqual(testitem2.cover, 4) self.dc.LoadFromJSON(JSONEncoder().encode({ "history": [edgenull1.asTuple()], "immutableobjects": [] })) test2s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(test2s), 1) test2 = test2s[0] self.assertEqual(oldnode, test2.currentnode) self.assertEqual(test2.covers, 3) for testitem2 in test2.propertyowner2s: self.assertEqual(testitem2.cover, 4) self.assertEqual(testitem2.cover, 4)
class MergeChangesMadeInJSONTestCase(unittest.TestCase): def setUp(self): self.dc = DocumentCollection() self.dc.Register(TestPropertyOwner1) self.dc.Register(TestPropertyOwner2) def runTest(self): #Create an object and set some values test1 = TestPropertyOwner1(None) self.dc.AddDocumentObject(test1) test1id = test1.id testitem1 = TestPropertyOwner2(None) testitem1id = testitem1.id test1.propertyowner2s.add(testitem1) testitem1.cover = 3 test1.covers = 2 self.dc.AddDocumentObject(test1) #Simulate sending the object to another user via conversion to JSON and emailing jsontext = self.dc.asJSON() #Make some local changes test1.covers = 4 #Simulate the other user (who received the email with the edges) getting the document and loading it into memory self.dc = DocumentCollection() self.dc.Register(TestPropertyOwner1) self.dc.Register(TestPropertyOwner2) self.dc.LoadFromJSON(jsontext) tpo1s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(tpo1s), 1) test2 = tpo1s[0] self.assertEqual(len(test2.propertyowner2s), 1) #The second user makes some changes and sends them back to the first for testitem2 in test2.propertyowner2s: testitem2.cover = 4 test2.covers = 3 jsontext = self.dc.asJSON() #Simulate the first user received the second users changes self.dc = DocumentCollection() self.dc.Register(TestPropertyOwner1) self.dc.Register(TestPropertyOwner2) self.dc.LoadFromJSON(jsontext) test2s = self.dc.GetByClass(TestPropertyOwner1) self.assertEqual(len(test2s), 1) test2 = test2s[0] self.assertEqual(test2.covers, 3) for testitem2 in test2.propertyowner2s: self.assertEqual(testitem2.cover, 4) self.assertEqual(testitem2.cover, 4) #The first user merges the changes back with his own test3 = test2.Merge(test1) self.assertEqual(len(test3.propertyowner2s), 1) for testitem3 in test3.propertyowner2s: self.assertEqual(testitem3.cover, 4) self.assertEqual(test3.covers, 4)