def test_namespace(self): k1 = Key('/c/d') k2 = Key('/a/b') k3 = Key('/a/b/c/d') ds = DictDatastore() nd = datastore.NamespaceDatastore(k2, ds) ds.put(k1, 'cd') ds.put(k3, 'abcd') self.assertEqual(ds.get(k1), 'cd') self.assertFalse(ds.contains(k2)) self.assertEqual(ds.get(k3), 'abcd') self.assertEqual(nd.get(k1), 'abcd') self.assertFalse(nd.contains(k2)) self.assertFalse(nd.contains(k3)) def test(key, val): nd.put(key, val) self.assertEqual(nd.get(key), val) self.assertFalse(ds.contains(key)) self.assertFalse(nd.contains(k2.child(key))) self.assertEqual(ds.get(k2.child(key)), val) for i in range(0, 10): test(Key(str(i)), 'val%d' % i)
def test_lowercase(self): ds = DictDatastore() lds = datastore.LowercaseKeyDatastore(ds) k1 = Key('hello') k2 = Key('HELLO') k3 = Key('HeLlo') ds.put(k1, 'world') ds.put(k2, 'WORLD') self.assertEqual(ds.get(k1), 'world') self.assertEqual(ds.get(k2), 'WORLD') self.assertFalse(ds.contains(k3)) self.assertEqual(lds.get(k1), 'world') self.assertEqual(lds.get(k2), 'world') self.assertEqual(lds.get(k3), 'world') def test(key, val): lds.put(key, val) self.assertEqual(lds.get(k1), val) self.assertEqual(lds.get(k2), val) self.assertEqual(lds.get(k3), val) test(k1, 'a') test(k2, 'b') test(k3, 'c')
def test_lowercase_transform(self): def transform(key): return Key(str(key).lower()) ds = DictDatastore() lds = datastore.KeyTransformDatastore(ds, keytransform=transform) k1 = Key('hello') k2 = Key('HELLO') k3 = Key('HeLlo') ds.put(k1, 'world') ds.put(k2, 'WORLD') self.assertEqual(ds.get(k1), 'world') self.assertEqual(ds.get(k2), 'WORLD') self.assertFalse(ds.contains(k3)) self.assertEqual(lds.get(k1), 'world') self.assertEqual(lds.get(k2), 'world') self.assertEqual(lds.get(k3), 'world') def test(key, val): lds.put(key, val) self.assertEqual(lds.get(k1), val) self.assertEqual(lds.get(k2), val) self.assertEqual(lds.get(k3), val) test(k1, 'a') test(k2, 'b') test(k3, 'c')
def test_reverse_transform(self): def transform(key): return key.reverse ds = DictDatastore() kt = datastore.KeyTransformDatastore(ds, keytransform=transform) k1 = Key('/a/b/c') k2 = Key('/c/b/a') self.assertFalse(ds.contains(k1)) self.assertFalse(ds.contains(k2)) self.assertFalse(kt.contains(k1)) self.assertFalse(kt.contains(k2)) ds.put(k1, 'abc') self.assertEqual(ds.get(k1), 'abc') self.assertFalse(ds.contains(k2)) self.assertFalse(kt.contains(k1)) self.assertEqual(kt.get(k2), 'abc') kt.put(k1, 'abc') self.assertEqual(ds.get(k1), 'abc') self.assertEqual(ds.get(k2), 'abc') self.assertEqual(kt.get(k1), 'abc') self.assertEqual(kt.get(k2), 'abc') ds.delete(k1) self.assertFalse(ds.contains(k1)) self.assertEqual(ds.get(k2), 'abc') self.assertEqual(kt.get(k1), 'abc') self.assertFalse(kt.contains(k2)) kt.delete(k1) self.assertFalse(ds.contains(k1)) self.assertFalse(ds.contains(k2)) self.assertFalse(kt.contains(k1)) self.assertFalse(kt.contains(k2))
def test_tiered(self): s1 = DictDatastore() s2 = DictDatastore() s3 = DictDatastore() ts = datastore.TieredDatastore([s1, s2, s3]) k1 = Key('1') k2 = Key('2') k3 = Key('3') s1.put(k1, '1') s2.put(k2, '2') s3.put(k3, '3') self.assertTrue(s1.contains(k1)) self.assertFalse(s2.contains(k1)) self.assertFalse(s3.contains(k1)) self.assertTrue(ts.contains(k1)) self.assertEqual(ts.get(k1), '1') self.assertEqual(s1.get(k1), '1') self.assertFalse(s2.contains(k1)) self.assertFalse(s3.contains(k1)) self.assertFalse(s1.contains(k2)) self.assertTrue(s2.contains(k2)) self.assertFalse(s3.contains(k2)) self.assertTrue(ts.contains(k2)) self.assertEqual(s2.get(k2), '2') self.assertFalse(s1.contains(k2)) self.assertFalse(s3.contains(k2)) self.assertEqual(ts.get(k2), '2') self.assertEqual(s1.get(k2), '2') self.assertEqual(s2.get(k2), '2') self.assertFalse(s3.contains(k2)) self.assertFalse(s1.contains(k3)) self.assertFalse(s2.contains(k3)) self.assertTrue(s3.contains(k3)) self.assertTrue(ts.contains(k3)) self.assertEqual(s3.get(k3), '3') self.assertFalse(s1.contains(k3)) self.assertFalse(s2.contains(k3)) self.assertEqual(ts.get(k3), '3') self.assertEqual(s1.get(k3), '3') self.assertEqual(s2.get(k3), '3') self.assertEqual(s3.get(k3), '3') ts.delete(k1) ts.delete(k2) ts.delete(k3) self.assertFalse(ts.contains(k1)) self.assertFalse(ts.contains(k2)) self.assertFalse(ts.contains(k3)) self.subtest_simple([ts])
def test_symlink_internals(self): dds = DictDatastore() sds = datastore.SymlinkDatastore(dds) a = datastore.Key('/A') b = datastore.Key('/B') c = datastore.Key('/C') d = datastore.Key('/D') lva = sds._link_value_for_key(a) lvb = sds._link_value_for_key(b) lvc = sds._link_value_for_key(c) lvd = sds._link_value_for_key(d) # helper to check queries sds_query = lambda: list(sds.query(Query(Key('/')))) dds_query = lambda: list(dds.query(Query(Key('/')))) # ensure _link_value_for_key and _link_for_value work self.assertEqual(lva, str(a.child(sds.sentinel))) self.assertEqual(a, sds._link_for_value(lva)) # adding a value should work like usual sds.put(a, 1) self.assertEqual(sds.get(a), 1) self.assertEqual(sds.get(b), None) self.assertNotEqual(sds.get(b), sds.get(a)) self.assertEqual(dds.get(a), 1) self.assertEqual(dds.get(b), None) self.assertEqual(sds_query(), [1]) self.assertEqual(dds_query(), [1]) # _follow_link(sds._link_value_for_key(a)) should == get(a) self.assertEqual(sds._follow_link(lva), 1) self.assertEqual(list(sds._follow_link_gen([lva])), [1]) # linking keys should work sds.link(a, b) self.assertEqual(sds.get(a), 1) self.assertEqual(sds.get(b), 1) self.assertEqual(sds.get(a), sds.get(b)) self.assertEqual(dds.get(a), 1) self.assertEqual(dds.get(b), lva) self.assertEqual(sds_query(), [1, 1]) self.assertEqual(dds_query(), [1, lva]) # changing link should affect source sds.put(b, 2) self.assertEqual(sds.get(a), 2) self.assertEqual(sds.get(b), 2) self.assertEqual(sds.get(a), sds.get(b)) self.assertEqual(dds.get(a), 2) self.assertEqual(dds.get(b), lva) self.assertEqual(sds_query(), [2, 2]) self.assertEqual(dds_query(), [2, lva]) # deleting source should affect link sds.delete(a) self.assertEqual(sds.get(a), None) self.assertEqual(sds.get(b), None) self.assertEqual(sds.get(b), sds.get(a)) self.assertEqual(dds.get(a), None) self.assertEqual(dds.get(b), lva) self.assertEqual(sds_query(), [None]) self.assertEqual(dds_query(), [lva]) # putting back source should yield working link sds.put(a, 3) self.assertEqual(sds.get(a), 3) self.assertEqual(sds.get(b), 3) self.assertEqual(sds.get(b), sds.get(a)) self.assertEqual(dds.get(a), 3) self.assertEqual(dds.get(b), lva) self.assertEqual(sds_query(), [3, 3]) self.assertEqual(dds_query(), [3, lva]) # deleting link should not affect source sds.delete(b) self.assertEqual(sds.get(a), 3) self.assertEqual(sds.get(b), None) self.assertNotEqual(sds.get(b), sds.get(a)) self.assertEqual(dds.get(a), 3) self.assertEqual(dds.get(b), None) self.assertEqual(sds_query(), [3]) self.assertEqual(dds_query(), [3]) # linking should bring back to normal sds.link(a, b) self.assertEqual(sds.get(a), 3) self.assertEqual(sds.get(b), 3) self.assertEqual(sds.get(b), sds.get(a)) self.assertEqual(dds.get(a), 3) self.assertEqual(dds.get(b), lva) self.assertEqual(sds_query(), [3, 3]) self.assertEqual(dds_query(), [3, lva]) # Adding another link should not affect things. sds.link(a, c) self.assertEqual(sds.get(a), 3) self.assertEqual(sds.get(b), 3) self.assertEqual(sds.get(c), 3) self.assertEqual(sds.get(a), sds.get(b)) self.assertEqual(sds.get(a), sds.get(c)) self.assertEqual(dds.get(a), 3) self.assertEqual(dds.get(b), lva) self.assertEqual(dds.get(c), lva) self.assertEqual(sds_query(), [3, 3, 3]) self.assertEqual(dds_query(), [3, lva, lva]) # linking should be transitive sds.link(b, c) sds.link(c, d) self.assertEqual(sds.get(a), 3) self.assertEqual(sds.get(b), 3) self.assertEqual(sds.get(c), 3) self.assertEqual(sds.get(d), 3) self.assertEqual(sds.get(a), sds.get(b)) self.assertEqual(sds.get(a), sds.get(c)) self.assertEqual(sds.get(a), sds.get(d)) self.assertEqual(dds.get(a), 3) self.assertEqual(dds.get(b), lva) self.assertEqual(dds.get(c), lvb) self.assertEqual(dds.get(d), lvc) self.assertEqual(sds_query(), [3, 3, 3, 3]) self.assertEqual(set(dds_query()), set([3, lva, lvb, lvc])) self.assertRaises(AssertionError, sds.link, d, a)
def subtest_nested_path_ds(self, **kwargs): k1 = kwargs.pop('k1') k2 = kwargs.pop('k2') k3 = kwargs.pop('k3') k4 = kwargs.pop('k4') ds = DictDatastore() np = datastore.NestedPathDatastore(ds, **kwargs) self.assertFalse(ds.contains(k1)) self.assertFalse(ds.contains(k2)) self.assertFalse(ds.contains(k3)) self.assertFalse(ds.contains(k4)) self.assertFalse(np.contains(k1)) self.assertFalse(np.contains(k2)) self.assertFalse(np.contains(k3)) self.assertFalse(np.contains(k4)) np.put(k1, k1) np.put(k2, k2) self.assertFalse(ds.contains(k1)) self.assertFalse(ds.contains(k2)) self.assertTrue(ds.contains(k3)) self.assertTrue(ds.contains(k4)) self.assertTrue(np.contains(k1)) self.assertTrue(np.contains(k2)) self.assertFalse(np.contains(k3)) self.assertFalse(np.contains(k4)) self.assertEqual(np.get(k1), k1) self.assertEqual(np.get(k2), k2) self.assertEqual(ds.get(k3), k1) self.assertEqual(ds.get(k4), k2) np.delete(k1) np.delete(k2) self.assertFalse(ds.contains(k1)) self.assertFalse(ds.contains(k2)) self.assertFalse(ds.contains(k3)) self.assertFalse(ds.contains(k4)) self.assertFalse(np.contains(k1)) self.assertFalse(np.contains(k2)) self.assertFalse(np.contains(k3)) self.assertFalse(np.contains(k4)) ds.put(k3, k1) ds.put(k4, k2) self.assertFalse(ds.contains(k1)) self.assertFalse(ds.contains(k2)) self.assertTrue(ds.contains(k3)) self.assertTrue(ds.contains(k4)) self.assertTrue(np.contains(k1)) self.assertTrue(np.contains(k2)) self.assertFalse(np.contains(k3)) self.assertFalse(np.contains(k4)) self.assertEqual(np.get(k1), k1) self.assertEqual(np.get(k2), k2) self.assertEqual(ds.get(k3), k1) self.assertEqual(ds.get(k4), k2) ds.delete(k3) ds.delete(k4) self.assertFalse(ds.contains(k1)) self.assertFalse(ds.contains(k2)) self.assertFalse(ds.contains(k3)) self.assertFalse(ds.contains(k4)) self.assertFalse(np.contains(k1)) self.assertFalse(np.contains(k2)) self.assertFalse(np.contains(k3)) self.assertFalse(np.contains(k4))