def test_batch_update_transaction(self): t = TestTransactionModel.create(text='something', count=5) id = t.id with BatchQuery() as b: t.batch(b).iff(count=5).update(text='something else') updated = TestTransactionModel.objects(id=id).first() self.assertEqual(updated.text, 'something else') b = BatchQuery() updated.batch(b).iff(count=6).update(text='and another thing') self.assertRaises(LWTException, b.execute) updated = TestTransactionModel.objects(id=id).first() self.assertEqual(updated.text, 'something else')
def test_batch_consistency(self): with mock.patch.object(self.session, 'execute') as m: with BatchQuery(consistency=ALL) as b: TestConsistencyModel.batch(b).create(text="monkey") args = m.call_args self.assertEqual(ALL, args[0][0].consistency_level) with mock.patch.object(self.session, 'execute') as m: with BatchQuery() as b: TestConsistencyModel.batch(b).create(text="monkey") args = m.call_args self.assertNotEqual(ALL, args[0][0].consistency_level)
def trim(self, key, length, batch_interface=None): last_activity = self.get_slice_from_storage(key, 0, length)[-1] if last_activity: with BatchQuery(): for activity in self.model.filter( feed_id=key, activity_id__lt=last_activity[0])[:1000]: activity.delete()
def test_batch_if_not_exists(self): """ ensure 'IF NOT EXISTS' exists in statement when in batch """ with mock.patch.object(self.session, 'execute') as m: with BatchQuery() as b: TestIfNotExistsModel.batch(b).if_not_exists().create(count=8) self.assertIn("IF NOT EXISTS", m.call_args[0][0].query_string)
def test_instance_update_in_batch(self): with mock.patch.object(self.session, "execute") as m, BatchQuery() as b: self.instance.batch(b).timestamp( timedelta(seconds=30)).update(count=2) query = m.call_args[0][0].query_string "USING TIMESTAMP".should.be.within(query)
def test_batch(self): with mock.patch.object(self.session, "execute") as m, BatchQuery() as b: TestTimestampModel.timestamp( timedelta(seconds=10)).batch(b).create(count=1) query = m.call_args[0][0].query_string query.should.match(r"INSERT.*USING TIMESTAMP") query.should_not.match(r"TIMESTAMP.*INSERT")
def test_batch_insert_if_not_exists_success(self): """ tests that batch insertion with if_not_exists work as expected """ id = uuid4() with BatchQuery() as b: TestIfNotExistsModel.batch(b).if_not_exists().create(id=id, count=8, text='123456789') b = BatchQuery() TestIfNotExistsModel.batch(b).if_not_exists().create(id=id, count=9, text='111111111111') with self.assertRaises(LWTException): b.execute() q = TestIfNotExistsModel.objects(id=id) self.assertEqual(len(q), 1) tm = q.first() self.assertEquals(tm.count, 8) self.assertEquals(tm.text, '123456789')
def test_batch_insert_if_not_exists_failure(self): """ tests that batch insertion with if_not_exists failure """ id = uuid4() with BatchQuery() as b: TestIfNotExistsModel.batch(b).create(id=id, count=8, text='123456789') with BatchQuery() as b: TestIfNotExistsModel.batch(b).create(id=id, count=9, text='111111111111') q = TestIfNotExistsModel.objects(id=id) self.assertEquals(len(q), 1) tm = q.first() self.assertEquals(tm.count, 9) self.assertEquals(tm.text, '111111111111')
def test_batch_insert_if_not_exists_success(self): """ tests that batch insertion with if_not_exists work as expected """ id = uuid4() with BatchQuery() as b: TestIfNotExistsModel.batch(b).if_not_exists().create( id=id, count=8, text='123456789') b = BatchQuery() TestIfNotExistsModel.batch(b).if_not_exists().create( id=id, count=9, text='111111111111') self.assertRaises(LWTException, b.execute) q = TestIfNotExistsModel.objects(id=id) self.assertEqual(len(q), 1) tm = q.first() self.assertEquals(tm.count, 8) self.assertEquals(tm.text, '123456789')
def remove_from_storage(self, key, activities, batch_interface=None, *args, **kwargs): batch = batch_interface or BatchQuery() for activity_id in activities.keys(): self.model(feed_id=key, activity_id=activity_id).batch(batch).delete() if batch_interface is None: batch.execute()
def new_story(self): Story.create(id=self.id, actor=self.actor, type=self.type, target=self.target) followers = self.find_followers() with BatchQuery(execute_on_exception=True) as b: for follower_id in followers: Feed.batch(b).create(story=self.id, user=follower_id, actor=self.actor)
def add_to_storage(self, key, activities, batch_interface=None, *args, **kwargs): ''' Insert multiple columns using client.insert or batch_interface.insert ''' batch = batch_interface or BatchQuery() activity_chunks = chunks(activities.itervalues(), 50) for activity_chunk in activity_chunks: for model_instance in activity_chunk: model_instance.feed_id = str(key) model_instance.batch(batch).save() if batch_interface is None: batch.execute()
def test_batch_is_included(self): with mock.patch.object(self.session, "execute") as m, BatchQuery( timestamp=timedelta(seconds=30)) as b: TestTimestampModel.batch(b).create(count=1) "USING TIMESTAMP".should.be.within(m.call_args[0][0].query_string)
def get_batch_interface(self): return BatchQuery()