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_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 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 __setitem__(self, item, value): """Set an item, storing it into the _columns backing store.""" value = self.sanitize(value) # If this doesn't change anything, don't record it _orig = self._original.get(item, None) if _orig and _orig.value == value: return dict.__setitem__(self, item, value) if item not in self._columns: self._columns[item] = Column(name=item) if item in self._deleted: del self._deleted[item] self._modified[item] = True # Copy-on-write, saves 100% for r/o records if self._columns[item] is _orig: col = copy.copy(_orig) self._columns[item] = col assert self._columns[item] is not self._original.get(item, None), \ "Whoops, not modifying the original column." self._columns[item].value = value self._columns[item].timestamp = self.timestamp()
def test_revert(self): data = {'id': 'eggs', 'title': 'bacon'} for k in data: self.object._original[k] = Column(name=k, value=data[k]) self.object.revert() for k in data: self.assert_(self.object[k] == data[k])
def get_slice(self, *args, **kwargs): [_last_cols.pop() for i in range(len(_last_cols))] cols = [] for i in range(random.randrange(1, 15)): cols.append( ColumnOrSuperColumn(column=Column(name=uuid.uuid4().hex, value=uuid.uuid4().hex, timestamp=util.timestamp()))) _last_cols.extend(cols) return cols
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 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 insert(self, key, columns, write_consistency_level=None): """ Insert or update columns for a key Parameters ---------- key : str The key to insert or update the columns at columns : dict Column: {'column': 'value'} SuperColumn: {'column': {'subcolumn': 'value'}} The columns or supercolumns to insert or update write_consistency_level : ConsistencyLevel Affects the guaranteed replication factor before returning from any write operation Returns ------- int timestamp """ timestamp = self.timestamp() cols = [] for c, v in columns.iteritems(): if self.super: subc = [Column(name=subname, value=subvalue, timestamp=timestamp) \ for subname, subvalue in v.iteritems()] column = SuperColumn(name=c, columns=subc) cols.append( Mutation(column_or_supercolumn=ColumnOrSuperColumn( super_column=column))) else: column = Column(name=c, value=v, timestamp=timestamp) cols.append( Mutation(column_or_supercolumn=ColumnOrSuperColumn( column=column))) self.client.batch_mutate(self.keyspace, {key: { self.column_family: cols }}, self._wcl(write_consistency_level)) return timestamp
def test_tuples(self): """Test iterators.tuples.""" cols = [Column(str(uuid.uuid4()), random.randint(0, 10000), ts) for ts in range(100)] tuples = iterators.tuples(cols) self.assert_(isinstance(tuples, types.GeneratorType)) tuples = list(tuples) self.assert_(len(tuples) == len(cols)) for (idx, (name, value)) in enumerate(tuples): self.assert_(cols[idx].name == name) self.assert_(cols[idx].value == value)
def append(self, record): """Append a record to a view""" assert isinstance(record, Record), \ "Can't append non-record type %s to view %s" % \ (record.__class__, self.__class__) path = self.key.get_path(column=self._record_key(record)) col = Column(path.column, record.key.key, record.timestamp()) self._get_cas().insert( self.key.key, path, col, self.consistency)
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 __base_view_test(self, view, ncols, chunk_size): """Base test for View._keys iteration.""" cols = map(ColumnOrSuperColumn, [Column(name=x, value=x) for x in range(ncols)]) def get_slice(instance, keyspace, key, parent, predicate, level): try: start = int(predicate.slice_range.start) except ValueError: start = 0 count = int(predicate.slice_range.count) return cols[start:start + count] view.chunk_size = chunk_size MockClient.get_slice = get_slice keys = tuple(view._keys()) self.assert_(len(keys) == len(cols), "Got %s keys instead of of %s" % (len(keys), len(cols))) self.assert_(len(set(keys)) == len(keys), "Duplicates present in output")
def test_setitem_getitem(self): data = {'id': 'eggs', 'title': 'bacon'} self.assertRaises(exc.ErrorInvalidValue, self.object.__setitem__, "eggs", None) for k in data: self.object[k] = data[k] self.assert_(self.object[k] == data[k], "Data not set in Record") self.assert_(k in self.object._columns, "Data not set in Record.columns") self.assert_(self.object._columns[k].__class__ is Column, "Record._columns[%s] is %s, not Column" % \ (type(self.object._columns[k]), k)) self.assert_(self.object._columns[k].value == data[k], "Value mismatch in Column, got `%s', expected `%s'" \ % (self.object._columns[k].value, data[k])) now = self.object.timestamp() self.assert_(self.object._columns[k].timestamp <= now, "Expected timestamp <= %s, got %s" \ % (now, self.object._columns[k].timestamp)) self.assert_(k not in self.object._deleted, "Key was marked as deleted.") self.assert_(k in self.object._modified, "Key not in modified list") del self.object[k] self.object[k] = data[k] self.assert_(k not in self.object._deleted) # Make sure setting an identical original value doesn't change self.object._inject( self.object.key, {"eggs": Column(name="eggs", value="bacon", timestamp=0)}) self.assert_(self.object["eggs"] == "bacon") self.assert_(self.object._original["eggs"].timestamp == 0) self.object["eggs"] = "bacon" self.assert_(self.object._original["eggs"].timestamp == 0)
def columns(seq, ts=None): """Yield Column instances form from seq of (name, value).""" ts = util.timestamp() if ts is None else ts return (Column(name, value, ts) for (name, value) in seq)