def test_match_on(self): entity_one = self.ds.put({'foo_id': schemaless.guid(), 'bar': 1, 'm': 'left'}) entity_two = self.ds.put({'foo_id': schemaless.guid(), 'bar': 1, 'm': 'right'}) # only this should match rows = self.foo.query(c.bar == 1) self.assert_len(1, rows) self.assert_equal(rows[0].foo_id, entity_two.foo_id)
def test_in_queries(self): user_ids = [self.entity.user_id] user_ids.append(self.ds.put({'user_id': schemaless.guid()}).user_id) rows = self.user.query(c.user_id.in_(user_ids)) self.assert_len(2, rows) self.assert_equal(set(user_ids), set(row['user_id'] for row in rows))
def test_query(self): self.assert_len(1, self.user.query(c.user_id == self.entity.user_id)) self.assert_len(1, self.user_name.query(c.first_name == 'evan', c.last_name == 'klitzke')) new_entity = self.ds.put({'user_id': schemaless.guid(), 'first_name': 'george'}) self.assert_len(1, self.user.query(c.user_id == new_entity.user_id)) self.assert_len(0, self.user_name.query(c.first_name == 'george')) # didn't have a full index
def test_create_object_save_delete(self): # create a new, empty object u = self.User() assert not u._saveable() assert u.is_dirty # populate some, but not all of the fields; the object should be dirty, # but not saveable u.user_id = schemaless.guid() u.first_name = 'evan' assert not u._saveable() assert u.is_dirty user = self.User.get(c.user_id == u.user_id) assert not user # finish populating the fields, check that the object is saveable u.last_name = 'klitzke' assert u._saveable() assert u.is_dirty # persist the object, check that it made it to the datastore u.save() assert u._saveable() assert not u.is_dirty user = self.User.get(c.user_id == u.user_id) assert user # delete the object, check that it's deleted from the datastore u.delete() assert u._saveable() assert not u.is_dirty user = self.User.get(c.user_id == u.user_id) assert not user
def test_converter(self): u = self.User(user_id=schemaless.guid(), first_name='foo', last_name='bar') u.save() self.assert_(isinstance(u.time_created, datetime.datetime)) v = self.User.get(c.user_id == u.user_id) self.assert_(isinstance(v.time_created, datetime.datetime))
def test_update_preserves_id(self): u = self.User(user_id=schemaless.guid(), first_name='foo', last_name='bar') u.save() orig_id = u.id u.first_name = 'baz' u.save() self.assert_equal(orig_id, u.id)
def setUp(self): super(SchemalessTestCase, self).setUp() self.ds = schemaless.DataStore(mysql_shards=['localhost:3306'], user='******', password='******', database='test') self.user = self.ds.define_index('index_user_id', ['user_id']) self.user_name = self.ds.define_index('index_user_name', ['first_name', 'last_name']) self.foo = self.ds.define_index('index_foo', ['bar'], {'m': 'right'}) self.clear_tables(self.ds) self.entity = self.ds.put({'user_id': schemaless.guid(), 'first_name': 'evan', 'last_name': 'klitzke'})
def test_index_update(self): u = self.User(user_id=schemaless.guid(), first_name='evan', last_name='klitzke') u.save() self.assert_equal(self.get_index_count('index_birthdate'), 0) u.birthdate = '1986-09-19' u.save() self.assert_equal(self.get_index_count('index_birthdate'), 1)
def test_update_multiple(self): user_id = schemaless.guid() item1 = self.ToDo(user_id=user_id, action='buy groceries').save() item2 = self.ToDo(user_id=user_id, action='buy groceries').save() self.assert_equal(self.get_index_count('index_todo_user_id'), 2) item1.completion_time = datetime.datetime.now() item1.save() self.assert_equal(self.get_index_count('index_todo_user_id'), 2)
def test_in_query(self): user_ids = [] users = [] for x in range(5): u = self.User(user_id=schemaless.guid(), first_name='foo', last_name='bar') user_ids.append(u.user_id) users.append(u) self.session.save() fetched_users = self.User.query(c.user_id.in_(user_ids[:3])) self.assert_used_index(self.User, 'index_user_id') self.assert_equal(set(user_ids[:3]), set(u.user_id for u in users[:3]))
def test_update(self): u = self.User(user_id=schemaless.guid(), first_name='foo', last_name='bar') u.save() v = self.User.get(c.first_name == 'foo', c.last_name == 'bar') self.assert_used_index(self.User, 'index_user_name') self.assert_equal(u.id, v.id) self.assert_equal(u.user_id, v.user_id) u.first_name = 'baz' u.save() v = self.User.get(c.first_name == 'foo', c.last_name == 'bar') self.assert_used_index(self.User, 'index_user_name') self.assert_equal(None, v) v = self.User.get(c.first_name == 'baz', c.last_name == 'bar') self.assert_used_index(self.User, 'index_user_name') self.assert_equal(u.id, v.id) self.assert_equal(u.user_id, v.user_id)
def reply(cls, post_id, author, content): return cls(comment_id=schemaless.guid(), post_id=post_id, author=author, content=content).save()
def new_post(cls, title, content): return cls(post_id=schemaless.guid(), title=title, content=content).save()
def test_double_delete(self): u = self.User(user_id=schemaless.guid(), first_name='foo', last_name='bar') u.save() u.delete() u.delete()
def add_biz(self, city='Oakland', state='CA'): return self.Business(business_id=schemaless.guid(), city='Oakland', state='CA').save()
def new_post(cls, title, content, active=False): now = datetime.datetime.now() return cls(post_id=schemaless.guid(), title=title, content=content, time_created=now, time_updated=now, active=active).save()