Example #1
0
    def test_post_crud_methods(self):
        message = 'Test message'
        user = UserFactory.create(remote_id=TRAVIS_USER_ID)
        mock_post = PostFactory.create(text=message, wall_owner=user)

        #create by objects api
        post = Post.objects.create(
                **PostFactory.get_mock_params_dict(
                    mock_post, commit_remote=True)
        )

        self.assertTrue(post.remote_id > 0)
        self.assertEqual(post.text, message)

        fetched_post = Post.remote.fetch(ids=[post.remote_id]).first()
        self.assertEqual(fetched_post.text, post.text)

        # Update
        edited_message = 'Edited message with CRUD'
        post = Post.objects.get(id=post.id)
        post.text = edited_message
        post.save(commit_remote=True)
        self.assertEqual(post.text, edited_message)

        fetched_post = Post.remote.fetch(ids=[post.remote_id]).first()
        self.assertEqual(fetched_post.text, edited_message)

        # Delete
        post.delete()
        post1 = Post.objects.get(id=post.id)
        self.assertTrue(post1.archived)

        fetched_post = Post.remote.fetch(ids=[post.remote_id])
        self.assertFalse(fetched_post)

        # Restore
        post.restore()
        post1 = Post.objects.get(id=post.id)
        self.assertFalse(post1.archived)

        fetched_post = Post.remote.fetch(ids=[post1.remote_id])
        self.assertTrue(fetched_post)

        post.delete()

        # Create with save()
        post = Post()
        post.__dict__.update(PostFactory.get_mock_params_dict(mock_post))
        post.text = message + message
        post.save(commit_remote=True)

        self.assertTrue(post.remote_id > 0)
        self.assertEqual(post.text, message + message)

        fetched_post = Post.remote.fetch(ids=[post.remote_id]).first()
        self.assertEqual(fetched_post.text, post.text)

        post.delete()