def test_load(self): test_data = (Column(name="eggs", value="1"), Column(name="bacon", value="2"), Column(name="spam", value="3")) real_slice = lazyboy.record.iterators.slice_iterator try: lazyboy.record.iterators.slice_iterator = lambda *args: test_data key = Key(keyspace='eggs', column_family='bacon', key='tomato') self.object.make_key = lambda *args, **kwargs: key self.object.load('tomato') self.assert_(self.object.key is key) key = Key(keyspace='eggs', column_family='bacon', key='tomato') self.object.load(key) finally: lazyboy.record.iterators.slice_iterator = real_slice self.assert_(self.object.key is key) cols = dict([[column.name, column] for column in test_data]) self.assert_(self.object._original == cols) for col in cols.values(): self.assert_( self.object[col.name] == col.value, "Expected %r, got %r for col %s" % (col.value, self.object[col.name], col.name)) self.assert_(self.object._columns[col.name] == col)
def setUp(self): """Prepare the text fixture.""" obj = view.View() obj.key = Key(keyspace='eggs', column_family='bacon', key='dummy_view') obj.record_key = Key(keyspace='spam', column_family='tomato') self.client = MockClient(['localhost:1234']) obj._get_cas = lambda: self.client self.object = obj
def test_get_path(self): base_key = dict(keyspace='eggs', key='spam', column_family='bacon') key = Key(**base_key) path = key.get_path() self.assert_(isinstance(path, ColumnPath)) self.assert_(path.column_family == key.column_family) self.assert_(base_key['keyspace'] == key.keyspace) keey = key.clone() path2 = keey.get_path() self.assert_(path2 == path) path = key.get_path(column="foo") self.assert_(path.column == "foo")
def setUp(self): """Prepare the text fixture.""" obj = view.PartitionedView() obj.view_key = Key(keyspace='eggs', column_family='bacon', key='dummy_view') obj.view_class = view.View self.object = obj
def test_iter(self): mg = view.multigetterator self.object = view.BatchLoadingView(None, Key("Eggs", "Bacon")) # self.object._keys = lambda: [Key("Eggs", "Bacon", x) # for x in range(25)] self.object._cols = lambda start=None, end=None: \ [Column("name:%s" % x, "val:%s" % x) for x in range(25)] columns = [Column(x, x * x) for x in range(10)] data = {'Digg': {'Users': {}}} for key in self.object._keys(): data['Digg']['Users'][key] = iter(columns) try: view.multigetterator = lambda *args, **kwargs: data self.assert_(isinstance(self.object.__iter__(), types.GeneratorType)) for record in self.object: self.assert_(isinstance(record, Record)) self.assert_(hasattr(record, 'key')) self.assert_(record.key.keyspace == "Digg") self.assert_(record.key.column_family == "Users") self.assert_(record.key.key in self.object.keys()) for x in range(10): self.assert_(x in record) self.assert_(record[x] == x * x) finally: view.multigetterator = mg
def test_append(self): view = self.object MockClient.insert = \ lambda conn, keyspace, key, path, value, time, x: True rec = Record() rec.key = Key(keyspace="eggs", column_family="bacon", key="tomato") self.object.append(rec)
def test_remove_key(self): """Test remove_key.""" client = MockClient('eggs', ['localhost:1234']) key = Key("123", "456") with save(lazyboy.record, ('get_pool', )): lazyboy.record.get_pool = lambda keyspace: client Record.remove_key(key)
def test_batch_load(self): records, keys = [], [] for x in range(10): record = Record() record.key = Key('eggs', 'bacon') record['number'] = x record['square'] = x * x records.append(record) keys.append(record.key) backing = {} for record in records: backing[record.key.key] = [ColumnOrSuperColumn(col) for col in record._columns.values()] mock_client = MockClient("Eggs", []) mock_client.multiget_slice = \ lambda keys, parent, pred, clvl: backing sets.itr.get_pool = lambda ks: mock_client out_records = self.object._batch_load(Record, keys) for record in out_records: self.assert_(isinstance(record, Record)) self.assert_(record.key in keys) orig = records[records.index(record)] self.assert_(orig['number'] == record['number']) self.assert_(orig['square'] == record['square'])
def test_sparse_get(self): """Test sparse_get.""" key = Key(keyspace="eggs", column_family="bacon", key="tomato") getter = iterators.sparse_get(key, ['eggs', 'bacon']) self.assert_(isinstance(getter, types.GeneratorType)) for col in getter: self.assert_(isinstance(col, ttypes.Column))
def test_slice_iterator(self): """Test slice_iterator.""" key = Key(keyspace="eggs", column_family="bacon", key="tomato") slice_iterator = iterators.slice_iterator(key, ConsistencyLevel.ONE) self.assert_(isinstance(slice_iterator, types.GeneratorType)) for col in slice_iterator: self.assert_(isinstance(col, ttypes.Column))
def test_get_batch_args(self): columns = (Column(name="eggs", value="1"), Column(name="bacon", value="2"), Column(name="sausage", value="3")) column_names = [col.name for col in columns] data = {'eggs': "1", 'bacon': "2", 'sausage': "3"} key = Key(keyspace='eggs', column_family='bacon', key='tomato') args = self.object._get_batch_args(key, columns) # Make sure the key is correct self.assert_(args[0] is key.keyspace) self.assert_(args[1] is key.key) self.assert_(isinstance(args[2], dict)) keys = args[2].keys() self.assert_(len(keys) == 1) self.assert_(keys[0] == key.column_family) self.assert_(not isinstance(args[2][key.column_family], types.GeneratorType)) for val in args[2][key.column_family]: self.assert_(isinstance(val, ColumnOrSuperColumn)) self.assert_(val.column in columns) self.assert_(val.super_column is None) key.super_column = "spam" args = self.object._get_batch_args(key, columns) self.assert_(args[0] is key.keyspace) self.assert_(args[1] is key.key) self.assert_(isinstance(args[2], dict)) keys = args[2].keys() self.assert_(len(keys) == 1) self.assert_(keys[0] == key.column_family) self.assert_(not isinstance(args[2][key.column_family], types.GeneratorType)) for val in args[2][key.column_family]: self.assert_(isinstance(val, ColumnOrSuperColumn)) self.assert_(val.column is None) self.assert_(isinstance(val.super_column, SuperColumn)) self.assert_(val.super_column.name is key.super_column) self.assert_(hasattr(val.super_column.columns, '__iter__')) for col in val.super_column.columns: self.assert_(col.name in data.keys()) self.assert_(col.value == data[col.name])
def test_clone(self): pk = Key(keyspace='eggs', key='spam', column_family='bacon') ppkk = pk.clone() self.assert_(isinstance(ppkk, Key)) self.assert_(repr(pk) == repr(ppkk)) for k in ('keyspace', 'key', 'column_family'): self.assert_(getattr(pk, k) == getattr(ppkk, k)) # Changes override base keys, but don't change them. _pk = pk.clone(key='sausage') self.assert_(hasattr(_pk, 'keyspace')) self.assert_(hasattr(_pk, 'column_family')) self.assert_(hasattr(_pk, 'key')) self.assert_(_pk.key == 'sausage') self.assert_(pk.key == 'spam') _pk = pk.clone(super_column='tomato') self.assert_(_pk.super_column == 'tomato') self.assertRaises(AttributeError, _pk.__getattr__, 'sopdfj') self.assert_(hasattr(pk, 'key')) # Changes to the base propagate to cloned PKs. pk.keyspace = 'beans' self.assert_(_pk.keyspace == 'beans') __pk = _pk.clone() self.assert_(__pk.keyspace == 'beans') pk.keyspace = 'tomato' self.assert_(__pk.keyspace == 'tomato')
def test_remove(self): data = {'eggs': "1", 'bacon': "2", 'sausage': "3"} self.object.update(data) self.object.key = Key("eggs", "bacon", "tomato") self.object._get_cas = self.get_mock_cassandra self.assert_(self.object.remove() is self.object) self.assert_(not self.object.is_modified()) for (key, val) in data.iteritems(): self.assert_(key not in self.object)
def test_issue_17(self): """Make sure newly-added columns additions aren't deleted. See: http://github.com/digg/lazyboy/issues#issue/17 """ self.object.set_key(Key("ks", "cf", "issue_17")) self.object['column'] = "value" del self.object['column'] changes = self.object._marshal() self.assert_(not changes['deleted'])
def test_get_batch_args(self): columns = (Column(name="eggs", value="1"), Column(name="bacon", value="2"), Column(name="sausage", value="3")) column_names = [col.name for col in columns] data = {'eggs': "1", 'bacon': "2", 'sausage': "3"} key = Key(keyspace='eggs', column_family='bacon', key='tomato') args = self.object._get_batch_args(key, columns) # Make sure the key is correct self.assert_(args[0] is key.key) self.assert_(isinstance(args[1], dict)) keys = args[1].keys() self.assert_(len(keys) == 1) self.assert_(keys[0] == key.column_family) self.assert_( not isinstance(args[1][key.column_family], types.GeneratorType)) for val in args[1][key.column_family]: self.assert_(isinstance(val, ColumnOrSuperColumn)) self.assert_(val.column in columns) self.assert_(val.super_column is None) key.super_column = "spam" args = self.object._get_batch_args(key, columns) self.assert_(args[0] is key.key) self.assert_(isinstance(args[1], dict)) keys = args[1].keys() self.assert_(len(keys) == 1) self.assert_(keys[0] == key.column_family) self.assert_( not isinstance(args[1][key.column_family], types.GeneratorType)) for val in args[1][key.column_family]: self.assert_(isinstance(val, ColumnOrSuperColumn)) self.assert_(val.column is None) self.assert_(isinstance(val.super_column, SuperColumn)) self.assert_(val.super_column.name is key.super_column) self.assert_(hasattr(val.super_column.columns, '__iter__')) for col in val.super_column.columns: self.assert_(col.name in data.keys()) self.assert_(col.value == data[col.name])
def make_key(self, key=None, super_column=None, **kwargs): """Return a new key.""" args = { 'keyspace': self._keyspace, 'column_family': self._column_family, 'key': key, 'super_column': super_column } args.update(**kwargs) return Key(**args)
def test_init(self): for args in self.allowed: pk = Key(**args) for k in args: self.assert_(getattr(pk, k) == args[k], "Expected `%s', for key `%s', got `%s'" % \ (args[k], k, getattr(pk, k))) for args in self.denied: self.assertRaises(ErrorIncompleteKey, Key, **args)
def test_sparse_multiget(self): """Test sparse_multiget.""" key = Key(keyspace="eggs", column_family="bacon", key="tomato") row_keys = ['cleese', 'jones', 'gilliam', 'chapman', 'idle', 'palin'] keys = (key.clone(key=row_key) for row_key in row_keys) cols = self.client.get_slice() res = dict((row_key, cols) for row_key in row_keys) self.client.multiget_slice = lambda *args: res getter = iterators.sparse_multiget(keys, ['eggs', 'bacon']) for (row_key, cols) in getter.iteritems(): self.assert_(isinstance(row_key, str)) self.assert_(row_key in row_keys) self.assert_(isinstance(cols, list)) for col in cols: self.assert_(isinstance(col, ttypes.Column))
def test_modified_nreference(self): """Make sure original column values don't change.""" rec = Record()._inject(Key('foo', 'bar', 'baz'), (Column("username", "whaddup"), )) orig = copy.copy(rec._original['username']) rec['username'] = "******" self.assert_( rec._original['username'] == orig, "Original column mutated, expected %r, got %r" % (orig, rec._original['username']))
def test_get_cas(self): self.assertRaises(ErrorIncompleteKey, self.object._get_cas) # Get with explicit tale self.assert_(self.object._get_cas('foo') == "Test") # Implicit based on pk self.object.pk = Key(keyspace='eggs', column_family="bacon", key='sausage') self.assert_(self.object._get_cas('foo') == "Test")
def _get_records(self, count, **kwargs): """Return records for testing.""" records = [] for n in range(count): record = Record() if kwargs: record.key = Key(**kwargs) records.append(record) return records
def _batch_load(self, record_class, keys, consistency=None): """Return an iterator of records for the given keys.""" consistency = consistency or self.consistency data = itr.multigetterator(keys, consistency) for (keyspace, col_fams) in data.iteritems(): for (col_fam, rows) in col_fams.iteritems(): for (row_key, cols) in rows.iteritems(): yield record_class()._inject( Key(keyspace=keyspace, column_family=col_fam, key=row_key), cols)
def setUp(self): """Prepare the test environment.""" num_records = 15 keys = [Key(keyspace='spam', column_family='bacon') for x in range(num_records)] records = [] for key in keys: r = Record() r.key = key records.append(r) self.records = records
def test_key_range_iterator(self): """Test key_range_iterator.""" key = Key(keyspace="eggs", column_family="bacon", key="tomato") keys = ['spam', 'eggs', 'sausage', 'tomato', 'spam'] real_key_range = iterators.key_range iterators.key_range = lambda *args: keys try: key_iter = iterators.key_range_iterator(key) self.assert_(isinstance(key_iter, types.GeneratorType)) for key in key_iter: self.assert_(isinstance(key, Key)) finally: iterators.key_range = real_key_range
def test_slice_iterator_supercolumns(self): """Test slice_iterator with supercolumns.""" key = Key(keyspace="eggs", column_family="bacon", key="tomato", super_column="spam") cols = [bla.column for bla in self.client.get_slice()] scol = ttypes.SuperColumn(name="spam", columns=cols) corsc = ttypes.ColumnOrSuperColumn(super_column=scol) self.client.get_slice = lambda *args: [corsc] slice_iterator = iterators.slice_iterator(key, ConsistencyLevel.ONE) self.assert_(isinstance(slice_iterator, types.GeneratorType)) for col in slice_iterator: self.assert_(isinstance(col, ttypes.SuperColumn))
def test_delitem(self): data = {'id': 'eggs', 'title': 'bacon'} self.object._inject( Key("keyspace", "colfam", "foods"), [Column(name, value) for (name, value) in data.iteritems()]) for k in data: self.object[k] = data[k] self.assert_(self.object[k] == data[k], "Data not set in Record") del self.object[k] self.assert_(k not in self.object, "Key was not deleted.") self.assert_(k in self.object._deleted, "Key was not marked as deleted.") self.assert_(k not in self.object._modified, "Deleted key in modified list") self.assert_(k not in self.object._columns, "Column was not deleted.")
def test_slice_iterator_error(self): class Client(object): def get_slice(*args, **kwargs): return None key = Key(keyspace="eggs", column_family="bacon", key="tomato") real_get_pool = iterators.get_pool try: iterators.get_pool = lambda pool: Client() self.assertRaises(exc.ErrorNoSuchRecord, iterators.slice_iterator, key, ConsistencyLevel.ONE) finally: iterators.get_pool = real_get_pool
def test_multigetterator(self): """Test multigetterator.""" keys = [Key("eggs", "bacon", "cleese"), Key("eggs", "bacon", "gilliam"), Key("eggs", "spam", "jones"), Key("eggs", "spam", "idle"), Key("tomato", "sausage", "chapman"), Key("tomato", "sausage", "palin")] def pack(cols): return list(iterators.pack(cols)) data = {'eggs': # Keyspace {'bacon': # Column Family {'cleese': pack([Column(name="john", value="cleese")]), 'gilliam': pack([Column(name="terry", value="gilliam")])}, 'spam': # Column Family {'jones': pack([Column(name="terry", value="jones")]), 'idle': pack([Column(name="eric", value="idle")])}}, 'tomato': # Keyspace {'sausage': # Column Family {'chapman': pack([SuperColumn(name="chap_scol", columns=[])]), 'palin': pack([SuperColumn(name="pal_scol", columns=[])])}}} def gen_ms(keyspace): def multiget_slice(keys, column_parent, predicate, consistencylevel): return data[keyspace][column_parent.column_family] return multiget_slice for keyspace in ("eggs", "tomato"): self.clients[keyspace].multiget_slice = gen_ms(keyspace) res = iterators.multigetterator(keys, ConsistencyLevel.ONE) self.assert_(isinstance(res, dict)) for (keyspace, col_fams) in res.iteritems(): self.assert_(keyspace in res) self.assert_(isinstance(col_fams, dict)) for (col_fam, rows) in col_fams.iteritems(): self.assert_(col_fam in data[keyspace]) self.assert_(isinstance(rows, dict)) for (row_key, columns) in rows.iteritems(): self.assert_(row_key in data[keyspace][col_fam]) for column in columns: if keyspace == 'tomato': self.assert_(isinstance(column, SuperColumn)) else: self.assert_(isinstance(column, Column))
def test_save(self): self.assertRaises(exc.ErrorMissingField, self.object.save) data = {'eggs': "1", 'bacon': "2", 'sausage': "3"} self.object.update(data) self.assertRaises(exc.ErrorMissingKey, self.object.save) key = Key(keyspace='eggs', column_family='bacon', key='tomato') self.object.key = key self.object._get_cas = self.get_mock_cassandra del self.object['bacon'] # FIXME – This doesn't really work, in the sense that # self.fail() is never called, but it still triggers an error # which makes the test fail, due to incorrect arity in the # arguments to the lambda. MockClient.remove = lambda self, a, b, c, d, e: None self.object.load = lambda self: None res = self.object.save() self.assert_(res == self.object, "Self not returned from Record.save") cfmap = _inserts[-1] self.assert_(isinstance(cfmap, dict)) self.assert_( self.object.key.column_family in cfmap, "PK family %s not in cfmap" % (self.object.key.column_family, )) for corsc in cfmap[self.object.key.column_family]: self.assert_(corsc.__class__ == ColumnOrSuperColumn) self.assert_(corsc.column and not corsc.super_column) col = corsc.column self.assert_(col.name in data, "Column %s wasn't set from update()" % \ (col.name)) self.assert_(data[col.name] == col.value, "Value of column %s is wrong, %s ≠ %s" % \ (col.name, data[col.name], col.value)) self.assert_(col == self.object._columns[col.name], "Column from cf._columns wasn't used in mutation_t")
def __init__(self, key=None): Key.__init__(self, "UserData", "UserViews", key)
def test_repr(self): pk = Key(keyspace='eggs', key='spam', column_family='bacon') self.assert_(unicode(pk) == repr(pk))
def test_unicode(self): pk = Key(keyspace='eggs', key='spam', column_family='bacon') x = pk.__unicode__() self.assert_(type(x) is unicode) self.assert_(str(x) == str(pk))
def test_gen_uuid(self): key = Key(keyspace="eggs", column_family="bacon") self.assert_(type(key._gen_uuid()) == str) self.assert_(key._gen_uuid() != key._gen_uuid(), "Unique IDs aren't very unique.")
def __init__(self, key=None): Key.__init__(self, "Keyspace1", "Standard1", key)