class GetPostsTests(TestCase): def setUp(self): self.blogger = EasyBlogger("id", "secret", "1234") self.blogger.service = Mock() self.posts = self.blogger.service.posts.return_value def tearDown(self): self.blogger = None def test_should_get_blog_by_labels(self): # Arrange request = self.posts.list.return_value request.execute.return_value = {"items": []} self.posts.list_next.return_value = None # act for x in self.blogger.getPosts(labels="abc", maxResults=4): pass # assert self.posts.list.assert_called_with(blogId="1234", labels="abc", maxResults=4) def test_should_default_search_by_labels(self): req = self.posts.list.return_value req.execute.return_value = {"items": []} self.posts.list_next.return_value = None for x in self.blogger.getPosts(): pass self.posts.list.assert_called_with(blogId=self.blogger.blogId, labels="", maxResults=None) req.execute.assert_called() def test_should_use_search_when_query_is_provided(self): req = self.posts.search.return_value req.execute.return_value = {"items": []} self.posts.list_next.return_value = None for x in self.blogger.getPosts(query="test"): pass self.posts.search.assert_called_with(blogId=self.blogger.blogId, q="test") req.execute.assert_called() def test_should_get_blog_by_id(self): req = self.posts.get.return_value item = {"id": 23234} req.execute.return_value = item post = [p for p in self.blogger.getPosts(postId="234")] self.posts.get.assert_called_with(blogId="1234", postId="234", view="AUTHOR") req.execute.assert_called() assert len(post) == 1 assert post[0] == item def test_should_get_blog_by_url(self): req = self.posts.getByPath.return_value item = {"id": 23234} req.execute.return_value = item post = [ x for x in self.blogger.getPosts( url="https://somedomain.com/some/path.html") ] self.posts.getByPath.assert_called_with(blogId="1234", path="/some/path.html") req.execute.assert_called() assert len(post) == 1 assert post[0] == item def test_should_return_empty_array_when_id_not_found(self): req = self.posts.get.return_value resp = Mock() resp.status = 404 req.execute.side_effect = HttpError(resp, content) post = [x for x in self.blogger.getPosts(postId="234")] self.posts.get.assert_called_with(blogId="1234", postId="234", view="AUTHOR") req.execute.assert_called() assert len(post) == 0 def test_should_rethrow_exception_other_than_404(self): req = self.posts.get.return_value resp = Mock() resp.status = 401 req.execute.side_effect = HttpError(resp, content) with self.assertRaises(HttpError): for x in self.blogger.getPosts(postId="234"): pass self.posts.get.assert_called_with(blogId="1234", postId="234", view="AUTHOR") req.execute.assert_called()
def setUp(self): self.blogger = EasyBlogger("id", "secret", "1234") self.blogger.service = Mock() self.posts = self.blogger.service.posts.return_value
class UpdateDeleteTests(TestCase): def setUp(self): self.blogger = EasyBlogger("id", "secret", "1234") self.blogger.service = Mock() self.posts = self.blogger.service.posts.return_value def test_should_update_draft(self): def validateBody(blogId, postId, body, revert, publish): assert blogId == "1234" assert postId == "4321" assert body["title"] == "t" assert body["content"] == "c" assert body["labels"] == ["l"] assert revert assert not publish return DEFAULT self.posts.patch.side_effect = validateBody getreq = self.posts.get.return_value getreq.execute.return_value = {"status": "DRAFT"} self.blogger.updatePost("4321", "t", "c", "l") req = self.posts.patch.return_value self.posts.patch.assert_called() assert self.posts.patch.call_count == 1 req.execute.assert_called() def test_publish_a_draft(self): def validateBody(blogId, postId, body, revert, publish): assert not revert assert publish return DEFAULT self.posts.patch.side_effect = validateBody getreq = self.posts.get.return_value getreq.execute.return_value = {"status": "DRAFT"} self.blogger.updatePost("4321", "t", "c", "l", isDraft=False) req = self.posts.patch.return_value self.posts.patch.assert_called() assert self.posts.patch.call_count == 1 req.execute.assert_called() def test_must_revert_live_post_on_update(self): def validateBody(blogId, postId, body, revert, publish): assert revert assert not publish return DEFAULT self.posts.patch.side_effect = validateBody getreq = self.posts.get.return_value getreq.execute.return_value = {"status": "LIVE"} self.blogger.updatePost("4321", "t", "c", "l", isDraft=True) req = self.posts.patch.return_value self.posts.patch.assert_called() assert self.posts.patch.call_count == 1 req.execute.assert_called() def test_should_update_post(self): def validateBody(blogId, postId, body, revert, publish): assert blogId == "1234" assert postId == "4321" assert body["title"] == "t" assert body["content"] == "c" assert body["labels"] == ["l"] assert revert assert not publish return DEFAULT self.posts.patch.side_effect = validateBody getreq = self.posts.get.return_value getreq.execute.return_value = {"status": "LIVE"} self.blogger.updatePost("4321", "t", "c", "l") req = self.posts.patch.return_value self.posts.patch.assert_called() assert self.posts.patch.call_count == 1 req.execute.assert_called() def test_update_post_should_take_label_array(self): def validateBody(blogId, postId, body, revert, publish): assert blogId == "1234" assert postId == "4321" assert body["title"] == "t" assert body["content"] == "c" assert body["labels"] == ["l"] assert revert assert not publish return DEFAULT self.posts.patch.side_effect = validateBody getreq = self.posts.get.return_value getreq.execute.return_value = {"status": "LIVE"} self.blogger.updatePost("4321", "t", "c", ["l"]) req = self.posts.patch.return_value self.posts.patch.assert_called() assert self.posts.patch.call_count == 1 req.execute.assert_called() def test_update_should_fail_if_nothing_specified(self): with self.assertRaises(ValueError): self.blogger.updatePost("4321") def test_should_delete_post(self): req = self.posts.delete.return_value self.blogger.deletePost("12345") self.posts.delete.assert_called_with(blogId="1234", postId="12345") req.execute.assert_called()
def setUp(self): self.blogger = EasyBlogger("id", "secret", "1234") self.blogger.service = Mock()
class GetPostsTests(TestCase): def setUp(self): self.blogger = EasyBlogger("id", "secret", "1234") self.blogger.service = Mock() def tearDown(self): self.blogger = None def test_should_get_blog_by_labels(self): # Arrange posts = self.blogger.service.posts.return_value # act self.blogger.getPosts(labels="abc", maxResults=4) # assert posts.list.assert_called_with(blogId="1234", labels="abc", maxResults=4) def test_should_default_search_by_labels(self): posts = self.blogger.service \ .posts \ .return_value req = posts.list.return_value self.blogger.getPosts() posts.list.assert_called_with(blogId=self.blogger.blogId, labels="", maxResults=1) req.execute.assert_called() def test_should_use_search_when_query_is_provided(self): posts = self.blogger.service \ .posts \ .return_value req = posts.search.return_value self.blogger.getPosts(query="test") posts.search.assert_called_with(blogId=self.blogger.blogId, q="test") req.execute.assert_called() def test_should_get_blog_by_id(self): posts = self.blogger.service \ .posts \ .return_value req = posts.get.return_value item = {"id": 23234} req.execute.return_value = item post = self.blogger.getPosts(postId="234") posts.get.assert_called_with(blogId="1234", postId="234", view="AUTHOR") req.execute.assert_called() assert "items" in post assert len(post["items"]) == 1 assert post["items"][0] == item def test_should_return_empty_array_when_id_not_found(self): posts = self.blogger.service \ .posts \ .return_value req = posts.get.return_value resp = Mock() resp.status = 404 req.execute.side_effect = HttpError(resp, content) post = self.blogger.getPosts(postId="234") posts.get.assert_called_with(blogId="1234", postId="234", view="AUTHOR") req.execute.assert_called() assert "items" in post assert len(post["items"]) == 0 def test_should_rethrow_exception_other_than_404(self): posts = self.blogger.service \ .posts \ .return_value req = posts.get.return_value resp = Mock() resp.status = 401 req.execute.side_effect = HttpError(resp, content) with self.assertRaises(HttpError): post = self.blogger.getPosts(postId="234") posts.get.assert_called_with(blogId="1234", postId="234", view="AUTHOR") req.execute.assert_called()
class PostsTests(TestCase): def setUp(self): self.blogger = EasyBlogger("id", "secret", "1234") self.blogger.service = Mock() self.posts = self.blogger.service.posts.return_value def test_should_post(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "c" assert body["labels"] == ["l"] assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value self.blogger.post("t", "c", "l") assert self.posts.insert.call_count == 1 self.posts.insert.assert_called() req.execute.assert_called() def test_labels_should_be_included_only_if_provided(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "c" self.assertTrue(body["labels"] is None) assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value self.blogger.post("t", "c", None) self.posts.insert.assert_called() req.execute.assert_called() def test_labels_should_be_split_if_provided(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "c" assert len(body["labels"]) == 3 assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value self.blogger.post("t", "c", "a,b,c") self.posts.insert.assert_called() req.execute.assert_called() def test_should_read_content_from_file(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "filecontent" assert len(body["labels"]) == 3 assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value fileMock = Mock() fileMock.read.return_value = "filecontent" self.blogger.post("t", fileMock, "a,b,c") fileMock.read.assert_called() self.posts.insert.assert_called() req.execute.assert_called() def test_should_convert_to_markup(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "<filecontent>" assert len(body["labels"]) == 3 return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value fileMock = Mock() fileMock.read.return_value = "filecontent" converterMock = Mock() self.blogger.converter = converterMock converterMock.convert.return_value = "<filecontent>" self.blogger.post("t", fileMock, "a,b,c", fmt="markdown") fileMock.read.assert_called() converterMock.convert.assert_called_with( "filecontent", 'html', filters=[], format="markdown") self.posts.insert.assert_called() req.execute.assert_called() @patch('tempfile.NamedTemporaryFile', autospec=True) @patch('subprocess.check_output', autospec=True) def test_should_convert_to_asciidoc_markup(self, mock_cp, mock_ntf): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "<filecontent>" assert len(body["labels"]) == 3 return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value fileMock = Mock() fileMock.read.return_value = "filecontent" self.blogger.namedTemporaryFile = mock_ntf self.blogger.open = Mock() htmlFile = self.blogger.open.return_value mockTempFile = mock_ntf.return_value.__enter__.return_value htmlFile.read.return_value = "<filecontent>" type(mockTempFile).name = PropertyMock( return_value="c:/some/path/file.adoc") self.blogger.check_output = mock_cp self.blogger.post("t", fileMock, "a,b,c", fmt="asciidoc") fileMock.read.assert_called() assert mock_cp.call_count == 1 list, kwargs = mock_cp.call_args assert list[0][0] == 'asciidoctor' assert list[0][-1] == 'c:/some/path/file.adoc' self.posts.insert.assert_called() req.execute.assert_called()
class PostsTests(TestCase): def setUp(self): self.blogger = EasyBlogger("id", "secret", "1234") self.blogger.service = Mock() self.posts = self.blogger.service.posts.return_value def test_should_post(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "c" assert body["labels"] == ["l"] assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value self.blogger.post("t", "c", "l") assert self.posts.insert.call_count == 1 self.posts.insert.assert_called() req.execute.assert_called() def test_labels_should_be_included_only_if_provided(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "c" self.assertTrue(body["labels"] is None) assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value self.blogger.post("t", "c", None) self.posts.insert.assert_called() req.execute.assert_called() def test_labels_should_be_split_if_provided(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "c" assert len(body["labels"]) == 3 assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value self.blogger.post("t", "c", "a,b,c") self.posts.insert.assert_called() req.execute.assert_called() def test_should_read_content_from_file(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "filecontent" assert len(body["labels"]) == 3 assert isDraft return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value fileMock = Mock() fileMock.read.return_value = "filecontent" self.blogger.post("t", fileMock, "a,b,c") fileMock.read.assert_called() self.posts.insert.assert_called() req.execute.assert_called() def test_should_convert_to_markup(self): def validateBody(blogId=None, body=None, isDraft=True): assert body["title"] == "t" assert body["content"] == "<filecontent>" assert len(body["labels"]) == 3 return DEFAULT self.posts.insert.side_effect = validateBody req = self.posts.insert.return_value fileMock = Mock() fileMock.read.return_value = "filecontent" converterMock = Mock() self.blogger.converter = converterMock converterMock.convert.return_value = "<filecontent>" self.blogger.post("t", fileMock, "a,b,c", fmt="markdown") fileMock.read.assert_called() converterMock.convert.assert_called_with("filecontent", 'html', filters=[], format="markdown") self.posts.insert.assert_called() req.execute.assert_called()