def test_composed_iteritems_test(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab12") tablename = "tab12" pd = StorageDict(tablename, [('pid', 'int'), ('time', 'int')], [('value', 'text'), ('x', 'double'), ('y', 'double'), ('z', 'double')]) what_should_be = {} for i in range(100): pd[i, i + 100] = ('ciao' + str(i), i * 0.1, i * 0.2, i * 0.3) what_should_be[i, i + 100] = ('ciao' + str(i), i * 0.1, i * 0.2, i * 0.3) del pd count, = config.session.execute('SELECT count(*) FROM my_app.tab12')[0] self.assertEqual(count, 100) pd = StorageDict(tablename, [('pid', 'int'), ('time', 'int')], [('value', 'text'), ('x', 'double'), ('y', 'double'), ('z', 'double')]) count = 0 res = {} for key, val in pd.iteritems(): res[key] = val count += 1 self.assertEqual(count, 100) delta = 0.000001 for i in range(100): a = what_should_be[i, i + 100] b = res[i, i + 100] self.assertEqual(a[0], b.value) self.assertAlmostEquals(a[1], b.x, delta=delta) self.assertAlmostEquals(a[2], b.y, delta=delta) self.assertAlmostEquals(a[3], b.z, delta=delta)
def test_update(self): tablename = "test_update1" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd[0] = 'prev_a' pd[1] = 'prev_b' self.assertEquals(pd[0], 'prev_a') self.assertEquals(pd[1], 'prev_b') pd.update({0: 'a', 1: 'b'}) time.sleep(1) self.assertEquals(pd[0], 'a') self.assertEquals(pd[1], 'b') pd.update({2: 'c', 3: 'd'}) time.sleep(1) self.assertEquals(pd[0], 'a') self.assertEquals(pd[1], 'b') self.assertEquals(pd[2], 'c') self.assertEquals(pd[3], 'd') tablename = "test_update2" pd2 = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd2[0] = 'final_a' pd2[4] = 'final_4' pd.update(pd2) time.sleep(1) self.assertEquals(pd[0], 'final_a') self.assertEquals(pd[4], 'final_4') pd.delete_persistent() pd2.delete_persistent()
def test_simple_values_test(self): tablename = "test_simple_values_test" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) what_should_be = set() for i in range(100): pd[i] = 'ciao' + str(i) what_should_be.add('ciao' + str(i)) del pd import gc gc.collect() count, = config.session.execute('SELECT count(*) FROM ' + self.current_ksp + '.' + tablename)[0] self.assertEqual(count, 100) pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) count = 0 res = set() for val in pd.values(): res.add(val) count += 1 self.assertEqual(count, 100) self.assertEqual(what_should_be, res) pd.delete_persistent()
def test_update(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab_a4") config.session.execute("DROP TABLE IF EXISTS my_app.tab_a5") tablename = "tab_a4" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd[0] = 'prev_a' pd[1] = 'prev_b' self.assertEquals(pd[0], 'prev_a') self.assertEquals(pd[1], 'prev_b') pd.update({0: 'a', 1: 'b'}) time.sleep(1) self.assertEquals(pd[0], 'a') self.assertEquals(pd[1], 'b') pd.update({2: 'c', 3: 'd'}) time.sleep(1) self.assertEquals(pd[0], 'a') self.assertEquals(pd[1], 'b') self.assertEquals(pd[2], 'c') self.assertEquals(pd[3], 'd') tablename = "tab_a5" pd2 = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd2[0] = 'final_a' pd2[4] = 'final_4' pd.update(pd2) time.sleep(1) self.assertEquals(pd[0], 'final_a') self.assertEquals(pd[4], 'final_4')
def test_simple_contains(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab_a4") tablename = "tab_a4" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) for i in range(100): pd[i] = 'ciao' + str(i) del pd count, = config.session.execute( 'SELECT count(*) FROM my_app.tab_a4')[0] self.assertEqual(count, 100) pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) for i in range(100): self.assertTrue(i in pd)
def test_init_empty_def_keyspace(self): tablename = "test_init_empty_def_keyspace" tokens = [(1, 2), (2, 3), (3, 4)] nopars = StorageDict(tablename, [('position', 'int')], [('value', 'int')], tokens=tokens) self.assertEqual(tablename, nopars._table) self.assertEqual(config.execution_name, nopars._ksp) nopars.sync() # Wait until the data is persisted res = config.session.execute( 'SELECT storage_id, primary_keys, columns, class_name, name, tokens, istorage_props,indexed_on ' + 'FROM hecuba.istorage WHERE storage_id = %s', [nopars.storage_id])[0] self.assertEqual( uuid.uuid5(uuid.NAMESPACE_DNS, config.execution_name + '.' + tablename), nopars.storage_id) self.assertEqual(nopars.__class__.__module__, 'hecuba.hdict') self.assertEqual(nopars.__class__.__name__, 'StorageDict') rebuild = build_remotely(res._asdict()) self.assertEqual(rebuild._built_remotely, True) self.assertEqual(tablename, rebuild._table) self.assertEqual(config.execution_name, rebuild._ksp) self.assertEqual( uuid.uuid5(uuid.NAMESPACE_DNS, config.execution_name + '.' + tablename), rebuild.storage_id) self.assertEqual(nopars.storage_id, rebuild.storage_id) rebuild.delete_persistent()
def test_init_empty_def_keyspace(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab1") tablename = "tab1" tokens = [(1l, 2l), (2l, 3l), (3l, 4l)] nopars = StorageDict(tablename, [('position', 'int')], [('value', 'int')], tokens) self.assertEqual("tab1", nopars._table) self.assertEqual(config.execution_name, nopars._ksp) res = config.session.execute( 'SELECT storage_id, primary_keys, columns, class_name, name, tokens, istorage_props,indexed_on ' + 'FROM hecuba.istorage WHERE storage_id = %s', [nopars._storage_id])[0] self.assertEqual( uuid.uuid3(uuid.NAMESPACE_DNS, config.execution_name + '.' + tablename), nopars._storage_id) self.assertEqual(nopars.__class__.__module__, 'hecuba.hdict') self.assertEqual(nopars.__class__.__name__, 'StorageDict') rebuild = StorageDict.build_remotely(res) self.assertEqual('tab1', rebuild._table) self.assertEqual(config.execution_name, rebuild._ksp) self.assertEqual( uuid.uuid3(uuid.NAMESPACE_DNS, config.execution_name + '.' + tablename), rebuild._storage_id) self.assertEqual(nopars._is_persistent, rebuild._is_persistent)
def test_get_strs(self): tablename = "tab10" config.session.execute("DROP TABLE IF EXISTS my_app." + tablename) pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd[0] = 'str1' self.assertEquals(pd[0], 'str1') '''
def test_simple_contains(self): tablename = "test_simple_contains" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) for i in range(100): pd[i] = 'ciao' + str(i) del pd import gc gc.collect() count, = config.session.execute('SELECT count(*) FROM ' + self.current_ksp + '.' + tablename)[0] self.assertEqual(count, 100) pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) for i in range(100): self.assertTrue(i in pd) pd.delete_persistent()
def test_deleteitem_nonpersistent(self): pd = StorageDict(None, [('position', 'int')], [('value', 'text')]) pd[0] = 'to_delete' del pd[0] def del_val(): val = pd[0] self.assertRaises(KeyError, del_val) pd = StorageDict(None, [('position', 'text')], [('value', 'int')]) pd['pos0'] = 0 del pd['pos0'] def del_val(): val = pd['pos0'] self.assertRaises(KeyError, del_val)
def test_paranoid_setitemdouble_multiple_persistent(self): tablename = "test_prnoid_set_2_N_p" pd = StorageDict(tablename, [('position1', 'int'), ('position2', 'text')], [('value1', 'text'), ('value2', 'double')]) pd[0, 'pos1'] = ['bla', 1.0] time.sleep(2) self.assertEquals(pd[0, 'pos1'], ('bla', 1.0)) pd.delete_persistent()
def test_simple_insertions(self): tablename = "test_simple_insertions" tokens = [(1, 2), (2, 3), (3, 4)] pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')], tokens=tokens) for i in range(100): pd[i] = 'ciao' + str(i) del pd import gc gc.collect() count, = config.session.execute('SELECT count(*) FROM ' + self.current_ksp + '.' + tablename)[0] self.assertEqual(count, 100) #clean up pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')], tokens=tokens) pd.delete_persistent()
def test_paranoid_setitemdouble_multiple_persistent(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab_a4") config.hecuba_type_checking = True pd = StorageDict("tab_a4", [('position1', 'int'), ('position2', 'text')], [('value1', 'text'), ('value2', 'double')]) pd[0, 'pos1'] = ['bla', 1.0] time.sleep(2) self.assertEquals(pd[0, 'pos1'], ('bla', 1.0)) config.hecuba_type_checking = False
def test_iter_and_get_sets(self): """ The iterator should read the same elements I can get with a __getitem__ :return: """ from hecuba.hdict import StorageDict b = StorageDict(None, [('pk1', 'str')], [('val', 'int')]) b['test1'] = 123124 self.assertEqual(123124, b['test1'])
def test_simple_insertions(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab10") tablename = "tab10" tokens = [(1l, 2l), (2l, 3l), (3l, 4l)] pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')], tokens) for i in range(100): pd[i] = 'ciao' + str(i) del pd count, = config.session.execute('SELECT count(*) FROM my_app.tab10')[0] self.assertEqual(count, 100)
def test_simple_iterkeys_test(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab_a3") tablename = "tab_a3" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) what_should_be = set() for i in range(100): pd[i] = 'ciao' + str(i) what_should_be.add(i) del pd count, = config.session.execute( 'SELECT count(*) FROM my_app.tab_a3')[0] self.assertEqual(count, 100) pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) count = 0 res = set() for val in pd.iterkeys(): res.add(val) count += 1 self.assertEqual(count, 100) self.assertEqual(what_should_be, res)
def test_parse_2(self): comment = ''' @TypeSpec dict<<partid:int>,x:int,y:int,z:int> ''' pd = StorageDict(None, [('pk1', 'int')], [('val1', 'text')]) p = pd._parse_comments(comment) should_be = { 'columns': [('x', 'int'), ('y', 'int'), ('z', 'int')], 'primary_keys': [('partid', 'int')], 'type': 'StorageDict' } self.assertEquals(p, should_be)
def test_simple_iteritems_test(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab_a1") pd = StorageDict("tab_a1", [('position', 'int')], [('value', 'text')]) what_should_be = {} for i in range(100): pd[i] = 'ciao' + str(i) what_should_be[i] = 'ciao' + str(i) del pd count, = config.session.execute( 'SELECT count(*) FROM my_app.tab_a1')[0] self.assertEqual(count, 100) pd = StorageDict("tab_a1", [('position', 'int')], [('value', 'text')]) count = 0 res = {} for key, val in pd.iteritems(): res[key] = val count += 1 self.assertEqual(count, 100) self.assertEqual(what_should_be, res)
def test_composed_key_return_list_items_test(self): tablename = "test_comkey_ret_list" pd = StorageDict(tablename, primary_keys=[('pid', 'int'), ('time', 'double')], columns=[('value', 'text'), ('x', 'double'), ('y', 'double'), ('z', 'double')]) what_should_be = {} for i in range(100): pd[i, i + 100.0] = ['ciao' + str(i), i * 0.1, i * 0.2, i * 0.3] what_should_be[i, i + 100.0] = [ 'ciao' + str(i), i * 0.1, i * 0.2, i * 0.3 ] del pd import gc gc.collect() count, = config.session.execute('SELECT count(*) FROM ' + self.current_ksp + '.' + tablename)[0] self.assertEqual(count, 100) pd = StorageDict(tablename, [('pid', 'int')], [('time', 'double'), ('value', 'text'), ('x', 'double'), ('y', 'double'), ('z', 'double')]) count = 0 res = {} for key, val in pd.items(): self.assertTrue(isinstance(key, int)) self.assertTrue(isinstance(val[0], float)) res[key] = val count += 1 self.assertEqual(count, 100) # casting to avoid 1.0000001 float python problem data = set([(key, int(val.time), val.value, int(val.x), int(val.y), int(val.z)) for key, val in pd.items()]) data2 = set([(key[0], int(key[1]), val[0], int(val[1]), int(val[2]), int(val[3])) for key, val in what_should_be.items()]) self.assertEqual(data, data2) pd.delete_persistent()
def test_getID(self): """ Checks that the id is the same :return: """ from hecuba.hdict import StorageDict old = StorageDict.__init__ StorageDict.__init__ = Mock(return_value=None) bl = StorageDict() u = uuid.uuid4() bl.storage_id = u self.assertEquals(u, bl.storage_id) StorageDict.__init__ = old
def test_deleteitem_persistent(self): tablename = "test_deleteitem_persistent_1" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd[0] = 'to_delete' del pd[0] def del_val(): val = pd[0] self.assertRaises(KeyError, del_val) pd.delete_persistent() tablename = "test_deleteitem_persistent_2" pd = StorageDict(tablename, [('position', 'text')], [('value', 'int')]) pd['pos1'] = 0 del pd['pos1'] def del_val(): val = pd['pos1'] self.assertRaises(KeyError, del_val) pd.delete_persistent()
def test_deleteitem_persistent(self): tablename = "tab_a5" config.session.execute("DROP TABLE IF EXISTS my_app." + tablename) pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd[0] = 'to_delete' del pd[0] def del_val(): val = pd[0] self.assertRaises(KeyError, del_val) tablename = "tab_a6" config.session.execute("DROP TABLE IF EXISTS my_app." + tablename) pd = StorageDict(tablename, [('position', 'text')], [('value', 'int')]) pd['pos1'] = 0 del pd['pos1'] def del_val(): val = pd['pos1'] self.assertRaises(KeyError, del_val)
def test_update_kwargs(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab_a6") tablename = "tab_a6" pd = StorageDict(tablename, [('position', 'text')], [('value', 'text')]) pd['val1'] = 'old_a' pd['val2'] = 'old_b' time.sleep(2) self.assertEquals(pd['val1'], 'old_a') self.assertEquals(pd['val2'], 'old_b') pd.update(val1='new_a', val2='new_b') time.sleep(2) self.assertEquals(pd['val1'], 'new_a') self.assertEquals(pd['val2'], 'new_b')
def test_update_kwargs(self): tablename = "test_update_kwargs" pd = StorageDict(tablename, [('position', 'text')], [('value', 'text')]) pd['val1'] = 'old_a' pd['val2'] = 'old_b' time.sleep(2) self.assertEquals(pd['val1'], 'old_a') self.assertEquals(pd['val2'], 'old_b') pd.update(val1='new_a', val2='new_b') time.sleep(2) self.assertEquals(pd['val1'], 'new_a') self.assertEquals(pd['val2'], 'new_b') pd.delete_persistent()
def test_composed_key_return_list_iteritems_test(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab13") tablename = "tab13" pd = StorageDict(tablename, [('pid', 'int'), ('time', 'double')], [('value', 'text'), ('x', 'double'), ('y', 'double'), ('z', 'double')]) what_should_be = {} for i in range(100): pd[i, i + 100.0] = ('ciao' + str(i), i * 0.1, i * 0.2, i * 0.3) what_should_be[i, i + 100.0] = ('ciao' + str(i), i * 0.1, i * 0.2, i * 0.3) del pd count, = config.session.execute('SELECT count(*) FROM my_app.tab13')[0] self.assertEqual(count, 100) pd = StorageDict(tablename, [('pid', 'int')], [('time', 'double'), ('value', 'text'), ('x', 'double'), ('y', 'double'), ('z', 'double')]) count = 0 res = {} for key, val in pd.iteritems(): self.assertTrue(isinstance(key, int)) self.assertTrue(isinstance(val[0], float)) res[key] = val count += 1 self.assertEqual(count, 100) # casting to avoid 1.0000001 float python problem data = set([(key, int(val.time), val.value, int(val.x), int(val.y), int(val.z)) for key, val in pd.iteritems()]) data2 = set([(key[0], int(key[1]), val[0], int(val[1]), int(val[2]), int(val[3])) for key, val in what_should_be.iteritems()]) self.assertEqual(data, data2)
def test_composed_items_test(self): tablename = "test_composed_items_test" pd = StorageDict(tablename, primary_keys=[('pid', 'int'), ('time', 'int')], columns=[('value', 'text'), ('x', 'double'), ('y', 'double'), ('z', 'double')]) what_should_be = {} for i in range(100): pd[i, i + 100] = ['ciao' + str(i), i * 0.1, i * 0.2, i * 0.3] what_should_be[i, i + 100] = ['ciao' + str(i), i * 0.1, i * 0.2, i * 0.3] del pd import gc gc.collect() count, = config.session.execute('SELECT count(*) FROM ' + self.current_ksp + '.' + tablename)[0] self.assertEqual(count, 100) pd = StorageDict(tablename) count = 0 res = {} for key, val in pd.items(): res[key] = val count += 1 self.assertEqual(count, 100) delta = 0.000001 for i in range(100): a = what_should_be[i, i + 100] b = res[i, i + 100] self.assertEqual(a[0], b.value) self.assertAlmostEquals(a[1], b.x, delta=delta) self.assertAlmostEquals(a[2], b.y, delta=delta) self.assertAlmostEquals(a[3], b.z, delta=delta) pd.delete_persistent()
def test_paranoid_setitem_persistent(self): config.session.execute("DROP TABLE IF EXISTS my_app.tab_a1") config.hecuba_type_checking = True pd = StorageDict("tab_a1", [('position', 'int')], [('value', 'text')]) pd[0] = 'bla' result = config.session.execute( 'SELECT value FROM my_app.tab_a1 WHERE position = 0') for row in result: self.assertEquals(row.value, 'bla') def set_wrong_val_test(): pd[0] = 1 self.assertRaises(ValueError, set_wrong_val_test) config.hecuba_type_checking = False
def test_paranoid_setitem_nonpersistent(self): tablename = "test_prnoid_set_nonp" pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) pd[0] = 'bla' self.assertEquals(pd[0], 'bla') def set_wrong_val_1(): pd[0] = 1 self.assertRaises(TypeError, set_wrong_val_1) def set_wrong_val_2(): pd['bla'] = 'bla' self.assertRaises(TypeError, set_wrong_val_2) pd.delete_persistent()
def test_paranoid_setitem_nonpersistent(self): config.hecuba_type_checking = True pd = StorageDict(None, [('position', 'int')], [('value', 'text')]) pd[0] = 'bla' self.assertEquals(pd[0], 'bla') def set_wrong_val_1(): pd[0] = 1 self.assertRaises(ValueError, set_wrong_val_1) def set_wrong_val_2(): pd['bla'] = 'bla' self.assertRaises(KeyError, set_wrong_val_2) config.hecuba_type_checking = False
def test_dict_print(self): tablename = "tab10" config.session.execute("DROP TABLE IF EXISTS my_app." + tablename) pd = StorageDict(tablename, [('position', 'int')], [('value', 'text')]) self.assertEquals(pd.__repr__(), "") pd[0] = 'a' self.assertEquals(pd.__repr__(), "{0: 'a'}") pd[1] = 'b' self.assertEquals(pd.__repr__(), "{0: 'a', 1: 'b'}") for i in range(1100): pd[i] = str(i) self.assertEquals(pd.__repr__().count(':'), 1000)