def test_batch_insert_if_not_exists(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) as assertion: b.execute() self.assertEqual(assertion.exception.existing, { 'count': 8, 'id': id, 'text': '123456789', '[applied]': False, }) q = TestIfNotExistsModel.objects(id=id) self.assertEqual(len(q), 1) tm = q.first() self.assertEqual(tm.count, 8) self.assertEqual(tm.text, '123456789')
def comment_on_video(self, video_id, user_id, comment_id, comment): #Checking values have been provided if not video_id: raise ValueError('video_id should be provided to submit a comment') elif not user_id: raise ValueError('user_id should be provided to submit a comment') elif not comment_id: raise ValueError( 'comment_id should be provided to submit a comment') elif not comment: raise ValueError('comment should be provided to submit a comment') #Needs to insert into both tables to be successful batch_query = BatchQuery() CommentsByVideoModel.batch(batch_query).create(video_id=video_id, comment_id=comment_id, user_id=user_id, comment=comment) CommentsByUserModel.batch(batch_query).create(user_id=user_id, comment_id=comment_id, video_id=video_id, comment=comment) batch_query.execute() #Publish UserCommentedOnVideo event self.user_comments_publisher.publish_user_comment_added_event( video_id=video_id, user_id=user_id, comment_id=comment_id) return
def test_insert_success_case(self): b = BatchQuery() TestMultiKeyModel.batch(b).create(partition=self.pkey, cluster=2, count=3, text='4') with self.assertRaises(TestMultiKeyModel.DoesNotExist): TestMultiKeyModel.get(partition=self.pkey, cluster=2) b.execute() TestMultiKeyModel.get(partition=self.pkey, cluster=2)
def test_update_success_case(self): inst = TestMultiKeyModel.create(partition=self.pkey, cluster=2, count=3, text='4') b = BatchQuery() inst.count = 4 inst.batch(b).save() inst2 = TestMultiKeyModel.get(partition=self.pkey, cluster=2) self.assertEqual(inst2.count, 3) b.execute() inst3 = TestMultiKeyModel.get(partition=self.pkey, cluster=2) self.assertEqual(inst3.count, 4)
def test_callbacks_properly_execute_callables_and_tuples(self): call_history = [] def my_callback(*args, **kwargs): call_history.append(args) # adding on init: batch = BatchQuery() batch.add_callback(my_callback) batch.add_callback(my_callback, 'more', 'args') batch.execute() self.assertEqual(len(call_history), 2) self.assertEqual([(), ('more', 'args')], call_history)
def test_batch_update_conditional(self): t = TestConditionalModel.create(text='something', count=5) id = t.id with BatchQuery() as b: t.batch(b).iff(count=5).update(text='something else') updated = TestConditionalModel.objects(id=id).first() self.assertEqual(updated.text, 'something else') b = BatchQuery() updated.batch(b).iff(count=6).update(text='and another thing') with self.assertRaises(LWTException) as assertion: b.execute() self.assertEqual(assertion.exception.existing, { 'id': id, 'count': 5, '[applied]': False, }) updated = TestConditionalModel.objects(id=id).first() self.assertEqual(updated.text, 'something else')
def test_empty_batch(self): b = BatchQuery() b.execute() with BatchQuery() as b: pass
def submit_youtube_video(self, video_id, user_id, name, description, tags, you_tube_video_id): # validate inputs if not video_id: raise ValueError( 'video_id should be provided for submit youtube video request') elif not user_id: raise ValueError( 'user_id should be provided for submit youtube video request') elif not name: raise ValueError( 'video name should be provided for submit youtube video request' ) elif not description: raise ValueError( 'video description should be provided for submit youtube video request' ) elif not you_tube_video_id: raise ValueError( 'video YouTube id should be provided for submit youtube video request' ) # Formulate the preview location preview_image_location = '//img.youtube.com/vi/' + you_tube_video_id + '/hqdefault.jpg' # formulate the time-based values now = datetime.utcnow() yyyymmdd = now.strftime('%Y%m%d') # create and execute batch statement to insert into multiple tables batch_query = BatchQuery(timestamp=now) VideosModel.batch(batch_query).create( video_id=video_id, user_id=user_id, name=name, description=description, location=you_tube_video_id, location_type=YOUTUBE, preview_image_location=preview_image_location, tags=tags, added_date=now) LatestVideosModel.batch(batch_query).create( yyyymmdd=yyyymmdd, added_date=now, video_id=video_id, user_id=user_id, name=name, preview_image_location=preview_image_location) UserVideosModel.batch(batch_query).create( user_id=user_id, added_date=now, video_id=video_id, name=name, preview_image_location=preview_image_location) batch_query.execute() # publish YouTubeVideoAdded event self.video_catalog_publisher.publish_youtube_video_added_event( video_id=video_id, user_id=user_id, name=name, description=description, tags=tags, location=you_tube_video_id, preview_image_location=preview_image_location, added_date=now, timestamp=now)