2016-09-06 22:40:32 +03:00
|
|
|
#!/usr/bin/env python2.7
|
|
|
|
|
2016-09-10 01:24:06 +03:00
|
|
|
import random
|
2016-09-06 22:40:32 +03:00
|
|
|
import unittest
|
2016-09-10 01:24:06 +03:00
|
|
|
from contextlib import contextmanager
|
2016-09-06 22:40:32 +03:00
|
|
|
|
|
|
|
import silenttestrunner
|
|
|
|
|
|
|
|
import ctreemanifest
|
|
|
|
|
|
|
|
class FakeStore(object):
|
|
|
|
def get(self, xyz):
|
|
|
|
return "abcabcabc"
|
|
|
|
|
2016-09-10 01:24:06 +03:00
|
|
|
@contextmanager
|
2016-09-10 01:24:17 +03:00
|
|
|
def hashflags(requireflag=False):
|
2016-09-10 01:24:06 +03:00
|
|
|
h = ''.join([chr(random.randint(0, 255)) for x in range(20)])
|
2016-09-10 01:24:17 +03:00
|
|
|
if random.randint(0, 1) == 0 and requireflag is False:
|
2016-09-10 01:24:06 +03:00
|
|
|
f = ''
|
|
|
|
else:
|
|
|
|
f = chr(random.randint(0, 255))
|
|
|
|
yield (h, f)
|
|
|
|
|
2016-09-06 22:40:32 +03:00
|
|
|
class ctreemanifesttests(unittest.TestCase):
|
|
|
|
def testInitialization(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
|
2016-09-18 23:40:14 +03:00
|
|
|
def testEmptyFlag(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, _):
|
|
|
|
f = ''
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
def testNullFlag(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, _):
|
|
|
|
f = '\0'
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
2016-09-10 01:24:06 +03:00
|
|
|
def testSetGet(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
def testUpdate(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
def testConflict(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
lambda: a.set("abc/def", h, f)
|
|
|
|
)
|
|
|
|
|
2016-09-10 01:24:17 +03:00
|
|
|
def testDeeplyNested(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc/def/ghi/jkl", h, f)
|
|
|
|
out = a.find("abc/def/ghi/jkl")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc/def/ghi/jkl2", h, f)
|
|
|
|
out = a.find("abc/def/ghi/jkl2")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
def testDeeplyNested(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc/def/ghi/jkl", h, f)
|
|
|
|
out = a.find("abc/def/ghi/jkl")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc/def/ghi/jkl2", h, f)
|
|
|
|
out = a.find("abc/def/ghi/jkl2")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
def testBushyTrees(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
nodes = {}
|
|
|
|
for ix in range(111):
|
2016-09-13 10:41:06 +03:00
|
|
|
with hashflags() as (h, f):
|
|
|
|
nodes["abc/def/ghi/jkl%d" % ix] = (h, f)
|
2016-09-10 01:24:17 +03:00
|
|
|
|
|
|
|
for fp, (h, f) in nodes.items():
|
|
|
|
a.set(fp, h, f)
|
|
|
|
|
|
|
|
for fp, (h, f) in nodes.items():
|
2016-09-13 10:41:06 +03:00
|
|
|
out = a.find(fp)
|
2016-09-10 01:24:17 +03:00
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
def testFlagChanges(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
|
|
|
|
# go from no flags to with flags, back to no flags.
|
|
|
|
with hashflags(requireflag=True) as (h, f):
|
|
|
|
self.assertEquals(len(f), 1)
|
|
|
|
|
|
|
|
a.set("abc", h, '')
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals(h, out[0])
|
|
|
|
self.assertEquals('', out[1])
|
|
|
|
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals(h, out[0])
|
|
|
|
self.assertEquals(f, out[1])
|
|
|
|
|
|
|
|
a.set("abc", h, '')
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals(h, out[0])
|
|
|
|
self.assertEquals('', out[1])
|
|
|
|
|
2016-09-11 02:27:40 +03:00
|
|
|
def testSetRemove(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
2016-09-13 10:41:06 +03:00
|
|
|
a.set("abc", None, None)
|
2016-09-11 02:27:40 +03:00
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((None, None), out)
|
|
|
|
|
2016-09-18 23:40:28 +03:00
|
|
|
def testCleanupAfterRemove(self):
|
|
|
|
a = ctreemanifest.treemanifest(FakeStore())
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc/def/ghi", h, f)
|
|
|
|
out = a.find("abc/def/ghi")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
|
|
|
a.set("abc/def/ghi", None, None)
|
|
|
|
|
|
|
|
with hashflags() as (h, f):
|
|
|
|
a.set("abc", h, f)
|
|
|
|
out = a.find("abc")
|
|
|
|
self.assertEquals((h, f), out)
|
|
|
|
|
2016-09-06 22:40:32 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
silenttestrunner.main(__name__)
|