def test_group_migration(self): for i in range(1,7): UserFactory.create(remote_id=i) group = Group.objects.create(remote_id=GROUP_ID) stat1 = GroupStatMembers.objects.create(group=group, members=',1,2,3,4,5,') stat1.save_final() stat2 = GroupStatMembers.objects.create(group=group, members='1,2,3,4,6') stat2.save_final() stat3 = GroupStatMembers.objects.create(group=group, members='1,2,3,5,7') stat3.save_final() self.assertEqual(stat1.members, '1,3,2,5,4') self.assertEqual(sorted(stat1.members_ids), sorted([int(i) for i in '1,2,3,4,5'.split(',')])) self.assertEqual(sorted(stat2.members_ids), sorted([int(i) for i in '1,2,3,4,6'.split(',')])) self.assertEqual(sorted(stat3.members_ids), sorted([int(i) for i in '1,2,3,5,7'.split(',')])) self.assertEqual(stat2.members_entered_ids, [6]) self.assertEqual(stat2.members_left_ids, [5]) self.assertEqual(stat3.members_entered_ids, [5,7]) self.assertEqual(stat3.members_left_ids, [4,6]) stat2.delete() stat3 = GroupStatMembers.objects.get(id=stat3.id) self.assertEqual(stat3.members_entered_ids, [7]) self.assertEqual(stat3.members_left_ids, [4])
def test_deleting_hiding_migration(self): for i in range(1,7): UserFactory.create(remote_id=i) group = GroupFactory.create(remote_id=GROUP_ID) stat1 = GroupMigrationFactory(group=group, time=datetime.now()-timedelta(10), members_ids=[1,2,3,4,5]) stat1.save_final() stat2 = GroupMigrationFactory(group=group, time=datetime.now()-timedelta(9), members_ids=[1,2,3,4,6]) stat2.save_final() stat3 = GroupMigrationFactory(group=group, time=datetime.now()-timedelta(8), members_ids=[1,2,3,5,7]) stat3.save_final() # difference between stat2 and stat1 self.assertItemsEqual(stat2.members_entered_ids, [6]) self.assertItemsEqual(stat2.members_left_ids, [5]) # difference between stat3 and stat2 self.assertItemsEqual(stat3.members_entered_ids, [5,7]) self.assertItemsEqual(stat3.members_left_ids, [4,6]) stat2.delete() stat3 = GroupMigration.objects.get(id=stat3.id) # difference between stat3 and stat1 self.assertItemsEqual(stat3.members_entered_ids, [7]) self.assertItemsEqual(stat3.members_left_ids, [4]) stat4 = GroupMigrationFactory(group=group, time=datetime.now()-timedelta(7), members_ids=[1,2,3,4,6]) stat4.save_final() # difference between stat4 and stat3 self.assertItemsEqual(stat4.members_entered_ids, [4,6]) self.assertItemsEqual(stat4.members_left_ids, [5,7]) stat3.hide() stat4 = GroupMigration.objects.get(id=stat4.id) # difference between stat4 and stat1 self.assertItemsEqual(stat4.members_entered_ids, [6]) self.assertItemsEqual(stat4.members_left_ids, [5]) stat5 = GroupMigrationFactory(group=group, time=datetime.now()-timedelta(6), members_ids=[1,2,3,5,7]) stat5.save_final() # difference between stat5 and stat4 self.assertItemsEqual(stat5.members_entered_ids, [5,7]) self.assertItemsEqual(stat5.members_left_ids, [4,6]) stat4.hidden = True stat4.save() stat5 = GroupMigration.objects.get(id=stat5.id) # difference between stat5 and stat1 self.assertItemsEqual(stat5.members_entered_ids, [7]) self.assertItemsEqual(stat5.members_left_ids, [4])
def test_parse_album(self): response = '''{"response":[{"aid":"16178407","thumb_id":"96509883","owner_id":"6492","title":"qwerty", "description":"desc","created":"1298365200","updated":"1298365201","size":"3", "privacy":"3"},{"aid":"17071606","thumb_id":"98054577","owner_id":"-6492", "title":"","description":"","created":"1204576880","updated":"1229532461", "size":"3","privacy":"0"}]} ''' instance = Album() owner = UserFactory.create(remote_id=6492) instance.parse(json.loads(response)['response'][0]) instance.save() self.assertEqual(instance.remote_id, '6492_16178407') self.assertEqual(instance.thumb_id, 96509883) self.assertEqual(instance.owner, owner) self.assertEqual(instance.title, 'qwerty') self.assertEqual(instance.description, 'desc') self.assertEqual(instance.created, datetime(2011,2,22,9,0,0)) self.assertEqual(instance.updated, datetime(2011,2,22,9,0,1)) self.assertEqual(instance.size, 3) self.assertEqual(instance.privacy, 3) instance = Album() group = GroupFactory.create(remote_id=6492) instance.parse(json.loads(response)['response'][1]) instance.save() self.assertEqual(instance.remote_id, '-6492_17071606') self.assertEqual(instance.group, group)
def test_parse_album(self): response = '''{"response":[{"id":16178407,"thumb_id":"96509883","owner_id":6492,"title":"qwerty", "description":"desc","created":"1298365200","updated":"1298365201","size":"3", "privacy":"3"},{"id":"17071606","thumb_id":"98054577","owner_id":-6492, "title":"","description":"","created":"1204576880","updated":"1229532461", "size":"3","privacy":"0"}]} ''' instance = Album() owner = UserFactory(remote_id=6492) instance.parse(json.loads(response)['response'][0]) instance.save() self.assertEqual(instance.remote_id, 16178407) self.assertEqual(instance.thumb_id, 96509883) self.assertEqual(instance.owner, owner) self.assertEqual(instance.title, 'qwerty') self.assertEqual(instance.description, 'desc') self.assertEqual(instance.size, 3) self.assertEqual(instance.privacy, 3) self.assertIsNotNone(instance.created) self.assertIsNotNone(instance.updated) instance = Album() group = GroupFactory(remote_id=6492) instance.parse(json.loads(response)['response'][1]) instance.save() self.assertEqual(instance.remote_id, 17071606) self.assertEqual(instance.owner, group)
def test_fetch_by_user_parameter(self): user = UserFactory(remote_id=13312307) album = AlbumFactory(remote_id=55976289, owner=user) video = VideoFactory(remote_id=165144348, album=album, owner=user) comments = video.fetch_comments() self.assertGreater(len(comments), 0) self.assertEqual(Comment.objects.count(), len(comments)) self.assertEqual(comments[0].object, video) self.assertEqual(comments[0].author, user)
def test_upload_to_user_album(self): user = UserFactory(remote_id=201164356) album = AlbumFactory(remote_id=209873101, owner=user) caption = 'test_upload' photos = album.upload_photos(self.files, caption=caption) self.objects_to_delete += photos # delete photos self.assertEqual(len(photos), len(self.files)) self.assertEqual(photos[0].text, caption)
def test_comment_wall_crud_methods(self): group = GroupFactory(remote_id=GROUP_CRUD_ID) post = PostFactory(remote_id=POST_CRUD_ID, text='', owner=group) user = UserFactory(remote_id=self.token_user_id) self.assertNoCommentsForObject(post) # create Comment.remote.fetch_by_object(object=post) self.assertEqual(Comment.objects.count(), 0) # create comment = Comment(text='Test comment', object=post, owner=group, author=user, date=timezone.now()) comment.save(commit_remote=True) self.objects_to_delete += [comment] self.assertEqual(Comment.objects.count(), 1) self.assertNotEqual(len(comment.remote_id), 0) self.assertCommentTheSameEverywhere(comment) # create by manager comment = Comment.objects.create( text='Test comment created by manager', object=post, owner=group, author=user, date=timezone.now(), commit_remote=True) self.objects_to_delete += [comment] self.assertEqual(Comment.objects.count(), 2) self.assertNotEqual(len(comment.remote_id), 0) self.assertCommentTheSameEverywhere(comment) # update comment.text = 'Test comment updated' comment.save(commit_remote=True) self.assertEqual(Comment.objects.count(), 2) self.assertCommentTheSameEverywhere(comment) # delete comment.delete(commit_remote=True) self.assertEqual(Comment.objects.count(), 2) self.assertTrue(comment.archived) self.assertEqual(Comment.remote.fetch_by_object( object=comment.object).filter(remote_id=comment.remote_id).count(), 0) # restore comment.restore(commit_remote=True) self.assertFalse(comment.archived) self.assertEqual(Comment.objects.count(), 2) self.assertCommentTheSameEverywhere(comment)
def test_parse_photo(self): response = '''{"response":[{"pid":"146771291","aid":"100001227","owner_id":"6492", "src":"http://cs9231.vkontakte.ru/u06492/100001227/m_7875d2fb.jpg", "src_big":"http://cs9231.vkontakte.ru/u06492/100001227/x_cd563004.jpg", "src_small":"http://cs9231.vkontakte.ru/u06492/100001227/s_c3bba2a8.jpg", "src_xbig":"http://cs9231.vkontakte.ru/u06492/100001227/y_62a74569.jpg", "src_xxbig":"http://cs9231.vkontakte.ru/u06492/100001227/z_793e9682.jpg", "text":"test","user_id":6492,"width":10,"height":10, "created":"1298365200"},{"pid":"146772677","aid":"100001227","owner_id":-6492, "src":"http://cs9231.vkontakte.ru/u06492/100001227/m_fd092958.jpg", "src_big":"http://cs9231.vkontakte.ru/u06492/100001227/x_1f8ec9b8.jpg", "src_small":"http://cs9231.vkontakte.ru/u06492/100001227/s_603d27ab.jpg", "src_xbig":"http://cs9231.vkontakte.ru/u06492/100001227/y_6938f576.jpg", "src_xxbig":"http://cs9231.vkontakte.ru/u06492/100001227/z_6a27e9fd.jpg", "text":"test","user_id":6492,"width":10,"height":10, "created":"1260887080"}]} ''' instance = Photo() owner = UserFactory(remote_id=6492) album = AlbumFactory(remote_id='6492_100001227') instance.parse(json.loads(response)['response'][0]) instance.save() self.assertEqual(instance.remote_id, '6492_146771291') self.assertEqual(instance.album, album) self.assertEqual(instance.owner, owner) self.assertEqual( instance.src, 'http://cs9231.vkontakte.ru/u06492/100001227/m_7875d2fb.jpg') self.assertEqual(instance.text, 'test') self.assertEqual(instance.width, 10) self.assertEqual(instance.height, 10) self.assertIsNotNone(instance.created) instance = Photo() group = GroupFactory(remote_id=6492) album = AlbumFactory(remote_id='-6492_100001227') instance.parse(json.loads(response)['response'][1]) instance.save() self.assertEqual(instance.remote_id, '-6492_146772677') self.assertEqual(instance.album, album) self.assertEqual(instance.group, group)
def test_parse_photo(self): response = '''{"response":[{"pid":"146771291","aid":"100001227","owner_id":"6492", "src":"http://cs9231.vkontakte.ru/u06492/100001227/m_7875d2fb.jpg", "src_big":"http://cs9231.vkontakte.ru/u06492/100001227/x_cd563004.jpg", "src_small":"http://cs9231.vkontakte.ru/u06492/100001227/s_c3bba2a8.jpg", "src_xbig":"http://cs9231.vkontakte.ru/u06492/100001227/y_62a74569.jpg", "src_xxbig":"http://cs9231.vkontakte.ru/u06492/100001227/z_793e9682.jpg", "text":"test","user_id":6492,"width":10,"height":10, "created":"1298365200"},{"pid":"146772677","aid":"100001227","owner_id":-6492, "src":"http://cs9231.vkontakte.ru/u06492/100001227/m_fd092958.jpg", "src_big":"http://cs9231.vkontakte.ru/u06492/100001227/x_1f8ec9b8.jpg", "src_small":"http://cs9231.vkontakte.ru/u06492/100001227/s_603d27ab.jpg", "src_xbig":"http://cs9231.vkontakte.ru/u06492/100001227/y_6938f576.jpg", "src_xxbig":"http://cs9231.vkontakte.ru/u06492/100001227/z_6a27e9fd.jpg", "text":"test","user_id":6492,"width":10,"height":10, "created":"1260887080"}]} ''' instance = Photo() owner = UserFactory.create(remote_id=6492) album = AlbumFactory.create(remote_id='6492_100001227') instance.parse(json.loads(response)['response'][0]) instance.save() self.assertEqual(instance.remote_id, '6492_146771291') self.assertEqual(instance.album, album) self.assertEqual(instance.owner, owner) self.assertEqual(instance.created, datetime(2011,2,22,9,0,0)) self.assertEqual(instance.src, 'http://cs9231.vkontakte.ru/u06492/100001227/m_7875d2fb.jpg') self.assertEqual(instance.text, 'test') self.assertEqual(instance.width, 10) self.assertEqual(instance.height, 10) instance = Photo() group = GroupFactory.create(remote_id=6492) album = AlbumFactory.create(remote_id='-6492_100001227') instance.parse(json.loads(response)['response'][1]) instance.save() self.assertEqual(instance.remote_id, '-6492_146772677') self.assertEqual(instance.album, album) self.assertEqual(instance.group, group)
def test_parse_photo(self): response = '''{"response":[{"id":"146771291","album_id":"100001227","owner_id":6492, "photo_130":"http://cs9231.vkontakte.ru/u06492/100001227/m_7875d2fb.jpg", "text":"test","user_id":6492,"width":10,"height":10, "date":"1298365200"}, {"id":"146772677","album_id":"100001227","owner_id":-6492, "photo_130":"http://cs9231.vkontakte.ru/u06492/100001227/m_fd092958.jpg", "text":"test","user_id":6492,"width":10,"height":10, "date":"1260887080"}]} ''' instance = Photo() owner = UserFactory(remote_id=6492) album = AlbumFactory(remote_id=100001227, owner=owner) instance.parse(json.loads(response)['response'][0]) instance.save() self.assertEqual(instance.remote_id, 146771291) self.assertEqual(instance.album, album) self.assertEqual(instance.owner, owner) self.assertEqual(instance.src, instance.photo_130) self.assertEqual( instance.src, 'http://cs9231.vkontakte.ru/u06492/100001227/m_7875d2fb.jpg') self.assertEqual(instance.text, 'test') self.assertEqual(instance.width, 10) self.assertEqual(instance.height, 10) self.assertIsNotNone(instance.created) instance = Photo() group = GroupFactory(remote_id=6492) #album = AlbumFactory(remote_id=100001227, owner=owner) instance.parse(json.loads(response)['response'][1]) instance.save() self.assertEqual(instance.remote_id, 146772677) self.assertEqual(instance.album, album) self.assertEqual(instance.owner, group)
class VkontaktePollsTest(VkontakteApiTestCase): def test_parse_poll(self): response = u''' {"response":{"answer_id": 0, "answers": [{"id": 266067655, "rate": 26.76, "text": "Да, профессионально!", "votes": 569}, {"id": 266067661, "rate": 5.41, "text": "Свой вариант (расскажу в комментариях).", "votes": 115}], "created": 1365411542, "owner_id": -16297716, "id": 83838453, "question": "А ты занимаешься спортом? (открытое голосование)", "votes": 2126}}''' group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(owner=group) instance = Poll.remote.parse_response_dict( json.loads(response)['response'], {'post_id': post.id}) instance.save() self.assertEqual(instance.pk, POLL_ID) self.assertEqual(instance.question, u'А ты занимаешься спортом? (открытое голосование)') self.assertEqual(instance.owner, group) self.assertEqual(instance.post, post) self.assertEqual(instance.votes_count, 2126) self.assertIsNotNone(instance.created) self.assertEqual(instance.answers.count(), 2) answer = instance.answers.get(pk=266067661) self.assertEqual(answer.text, u'Свой вариант (расскажу в комментариях).') self.assertEqual(answer.votes_count, 115) self.assertEqual(answer.rate, 5.41) answer = instance.answers.get(pk=266067655) self.assertEqual(answer.text, u'Да, профессионально!') self.assertEqual(answer.votes_count, 569) self.assertEqual(answer.rate, 26.76) def test_fetching_poll(self): group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(remote_id=POST_ID, owner=group) instance = Poll.remote.fetch(POLL_ID, post) self.assertEqual(instance.pk, POLL_ID) self.assertEqual(instance.question, u'А ты занимаешься спортом?') self.assertEqual(instance.owner, group) self.assertEqual(instance.post, post) self.assertGreater(instance.votes_count, 2126) self.assertIsNotNone(instance.created) self.assertEqual(instance.answers.count(), 7) answer = instance.answers.get(pk=266067661) self.assertEqual(answer.text, u'Свой вариант (расскажу в комментариях).') self.assertGreater(answer.votes_count, 100) self.assertIsNotNone(answer.rate) answer = instance.answers.get(pk=266067655) self.assertEqual(answer.text, u'Да, профессионально!') self.assertGreater(answer.votes_count, 560) self.assertIsNotNone(answer.rate) @mock.patch('vkontakte_users.models.User.remote.get_by_slug', side_effect=lambda s: UserFactory.create()) def test_fetching_answer_users_by_parser(self, *args, **kwargs): group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(remote_id=POST_ID, owner=group) poll = PollFactory.create(remote_id=POLL_ID, owner=group, post=post) answer = AnswerFactory.create(pk=266067661, poll=poll) answer.fetch_voters_by_parser() self.assertEqual(answer.voters.count(), answer.votes_count) self.assertGreaterEqual(answer.voters.count(), 155) self.assertGreater(answer.rate, 0) def test_fetch_group_post_with_poll(self, *args, **kwargs): self.assertEqual(Poll.objects.count(), 0) self.assertEqual(Answer.objects.count(), 0) Post.remote.fetch(ids=[POST_ID]) #post = Post.remote.fetch(ids=[POST_ID]) self.assertEqual(Poll.objects.count(), 1) self.assertEqual(Answer.objects.count(), 7) def test_fetch_user_post_with_poll(self, *args, **kwargs): group = GroupFactory.create(remote_id=GROUP2_ID) self.assertEqual(Post.objects.count(), 0) self.assertEqual(Poll.objects.count(), 0) self.assertEqual(Answer.objects.count(), 0) posts = group.fetch_posts(own=True) self.assertGreaterEqual(posts.count(), 14) self.assertTrue( GROUP2_POLLPOST_ID in [post.remote_id for post in posts]) self.assertGreater(Post.objects.count(), 0) # TODO: error with fetching right owenr of post for repost with poll # self.assertGreater(Poll.objects.count(), 0) # self.assertGreater(Answer.objects.count(), 0) @mock.patch('vkontakte_users.models.User.remote.get_by_slug', side_effect=lambda s: UserFactory.create()) def test_fetching_answer_users_by_api(self, *args, **kwargs): def calc_percentage(poll, answer_votes): pp = Poll.remote.fetch(poll.pk, poll.post) if pp and pp.votes_count: return answer_votes * 100. / pp.votes_count return 0 group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(remote_id=POST_ID, owner=group) poll = PollFactory.create(remote_id=POLL_ID, owner=group, post=post) answer = AnswerFactory.create(pk=266067661, poll=poll) answer.fetch_voters_by_api() self.assertEqual(answer.voters.count(), answer.votes_count) self.assertGreaterEqual(answer.voters.count(), 155) self.assertGreater(answer.rate, 0) percentage = calc_percentage(poll, answer.votes_count) self.assertEqual(answer.rate, percentage) @mock.patch('vkontakte_users.models.User.remote.get_by_slug', side_effect=lambda s: UserFactory.create()) def test_fetching_answer_users(self, *args, **kwargs): group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(remote_id=POST_ID, owner=group) poll = PollFactory.create(remote_id=POLL_ID, owner=group, post=post) answer = AnswerFactory.create(pk=266067661, poll=poll) answer.fetch_voters(source='api') self.assertEqual(answer.voters.count(), answer.votes_count) self.assertGreaterEqual(answer.voters.count(), 155) self.assertGreater(answer.rate, 0) answer.fetch_voters(source=None) self.assertEqual(answer.voters.count(), answer.votes_count) self.assertGreaterEqual(answer.voters.count(), 155) self.assertGreater(answer.rate, 0)
class VkontaktePollsTest(TestCase): def test_parse_poll(self): response = u''' {"response":{"answer_id": 0, "answers": [{"id": 266067655, "rate": 26.76, "text": "Да, профессионально!", "votes": 569}, {"id": 266067661, "rate": 5.41, "text": "Свой вариант (расскажу в комментариях).", "votes": 115}], "created": 1365411542, "owner_id": -16297716, "poll_id": 83838453, "question": "А ты занимаешься спортом? (открытое голосование)", "votes": 2126}}''' group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(wall_owner=group) instance = Poll.remote.parse_response_dict( json.loads(response)['response'], {'post_id': post.id}) instance.save() self.assertEqual(instance.pk, POLL_ID) self.assertEqual(instance.question, u'А ты занимаешься спортом? (открытое голосование)') self.assertEqual(instance.owner, group) self.assertEqual(instance.post, post) self.assertEqual(instance.votes_count, 2126) self.assertIsNotNone(instance.created) self.assertEqual(instance.answers.count(), 2) answer = instance.answers.get(pk=266067661) self.assertEqual(answer.text, u'Свой вариант (расскажу в комментариях).') self.assertEqual(answer.votes_count, 115) self.assertEqual(answer.rate, 5.41) answer = instance.answers.get(pk=266067655) self.assertEqual(answer.text, u'Да, профессионально!') self.assertEqual(answer.votes_count, 569) self.assertEqual(answer.rate, 26.76) def test_fetching_poll(self): group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(remote_id=POST_ID, wall_owner=group) instance = Poll.remote.fetch(POLL_ID, group, post) self.assertEqual(instance.pk, POLL_ID) self.assertEqual(instance.question, u'А ты занимаешься спортом? (открытое голосование)') self.assertEqual(instance.owner, group) self.assertEqual(instance.post, post) self.assertTrue(instance.votes_count > 2126) self.assertIsNotNone(instance.created) self.assertEqual(instance.answers.count(), 7) answer = instance.answers.get(pk=266067661) self.assertEqual(answer.text, u'Свой вариант (расскажу в комментариях).') self.assertTrue(answer.votes_count > 100) self.assertIsNotNone(answer.rate) answer = instance.answers.get(pk=266067655) self.assertEqual(answer.text, u'Да, профессионально!') self.assertTrue(answer.votes_count > 560) self.assertIsNotNone(answer.rate) @mock.patch('vkontakte_users.models.User.remote.get_by_slug', side_effect=lambda s: UserFactory.create()) def test_fetching_answer_users(self, *args, **kwargs): group = GroupFactory.create(remote_id=GROUP_ID) post = PostFactory.create(remote_id=POST_ID, wall_owner=group) poll = PollFactory.create(remote_id=POLL_ID, owner=group, post=post) answer = AnswerFactory.create(pk=266067661, poll=poll) answer.fetch_voters() self.assertEqual(answer.voters.count(), answer.votes_count) self.assertTrue(answer.voters.count() > 110) self.assertTrue(answer.rate > 0) def test_fetch_group_post_with_poll(self, *args, **kwargs): group = GroupFactory.create(remote_id=GROUP_ID) self.assertEqual(Poll.objects.count(), 0) self.assertEqual(Answer.objects.count(), 0) group.fetch_posts(after=datetime(2013, 4, 8), own=True) self.assertTrue(Poll.objects.count() > 0) self.assertTrue(Answer.objects.count() > 0) def test_fetch_user_post_with_poll(self, *args, **kwargs): group = GroupFactory.create(remote_id=GROUP2_ID) self.assertEqual(Post.objects.count(), 0) self.assertEqual(Poll.objects.count(), 0) self.assertEqual(Answer.objects.count(), 0) group.fetch_posts(own=True) self.assertTrue(Post.objects.count() > 0) self.assertTrue(Poll.objects.count() > 0) self.assertTrue(Answer.objects.count() > 0)