def test_json_no_posts(self):
     title = 'Test'
     author = 'first last'
     new_blog = Blog(title, author)
     expected = {'title': title,
                 'author': 'First Last',
                 'posts': []}
     # Tests
     self.assertDictEqual(expected, new_blog.json())
 def test_create_post_in_blog(self):
     title = 'Test'
     author = 'first last'
     new_blog = Blog(title, author)
     new_blog.create_post('Test post', 'Test content')
     # Tests
     self.assertEqual(1, len(new_blog.posts))
     self.assertEqual('Test Post', new_blog.posts[0].title_field)
     self.assertEqual('Test content', new_blog.posts[0].content_field)
 def test_json(self):
     title = 'Test'
     author = 'first last'
     new_blog = Blog(title, author)
     new_blog.create_post('Test post', 'Test content')
     expected = {'title': title,
                 'author': 'First Last',
                 'posts': [{'title': 'Test Post',
                            'content': 'Test content'}]}
     # Tests
     self.assertDictEqual(expected, new_blog.json())
class BlogTestCase(unittest.TestCase):

    print("Running unit tests from: " + os.path.dirname(__file__) + '\\' +
          os.path.basename(__file__) + "\n")

    def setUp(self):
        self.title = 'Test'
        self.author = 'First Last'
        self.new_blog = Blog(self.title, self.author)

    def test_blog_title(self):
        self.assertEqual(self.title, self.new_blog.title)

    def test_blog_title_capitalized(self):
        title = 'test title'
        author = 'First Last'
        new_blog = Blog(title, author)
        expected_title = 'Test Title'
        self.assertEqual(expected_title, new_blog.title)

    def test_blog_author(self):
        self.assertEqual(self.author, self.new_blog.author)

    def test_blog_author_capitalized(self):
        title = 'Test'
        author = 'first last'
        new_blog = Blog(title, author)
        expected = 'First Last'
        self.assertEqual(expected, new_blog.author)

    def test_initial_posts_length(self):
        expected_length = 0
        self.assertEqual(expected_length, len(self.new_blog.posts))

    def test_initial_posts_list(self):
        expected_list = []
        self.assertListEqual(expected_list, self.new_blog.posts)

    def test_multiple_posts_list(self):
        self.new_blog.posts = ['post #1', 'post #2']
        expected_list = ['post #1', 'post #2']
        self.assertListEqual(expected_list, self.new_blog.posts)

    def test_repr(self):
        expected = 'Test by First Last (0 post)'
        self.assertEqual(expected, self.new_blog.__repr__())

    def test_repr_multiple_posts(self):
        self.new_blog.posts = ['post #1', 'post #2']
        expected = 'Test by First Last (2 posts)'
        self.assertEqual(expected, self.new_blog.__repr__())
 def test_blog_author_capitalized(self):
     title = 'Test'
     author = 'first last'
     new_blog = Blog(title, author)
     expected = 'First Last'
     self.assertEqual(expected, new_blog.author)
 def test_blog_title_capitalized(self):
     title = 'test title'
     author = 'First Last'
     new_blog = Blog(title, author)
     expected_title = 'Test Title'
     self.assertEqual(expected_title, new_blog.title)
 def setUp(self):
     self.title = 'Test'
     self.author = 'First Last'
     self.new_blog = Blog(self.title, self.author)
Example #8
0
class AppTestCase(unittest.TestCase):

    print("Running unit tests from: " + os.path.dirname(__file__) + '\\' +
          os.path.basename(__file__) + "\n")

    def setUp(self) -> None:
        self.bloger_name = 'First Last'
        self.app = App()
        self.blog_name = "Blog Name"
        self.blog = Blog(self.blog_name, self.bloger_name)
        self.app.add_blog(self.blog_name, self.blog)

    def test_main_calls_print_blogs(self):
        with patch(
                'blog_console_app.app.App.print_blogs') as mocked_print_blogs:
            with patch('builtins.input', return_value='q'):
                main.main()
                mocked_print_blogs.assert_called()

    def test_app_print_blogs_0_posts(self):
        expected = '- {0} by {1} (0 post)'.format(self.blog_name,
                                                  self.bloger_name)
        with patch('builtins.print') as mocked_print:
            self.app.print_blogs()
            mocked_print.assert_called_with(expected)

    def test_app_print_blogs_1_post(self):
        self.blog.create_post("Test post", "Test Content")
        expected = '- {0} by {1} (1 post)'.format(self.blog_name,
                                                  self.bloger_name)
        with patch('builtins.print') as mocked_print:
            self.app.print_blogs()
            mocked_print.assert_called_with(expected)

    def test_ask_create_blog(self):
        with patch('builtins.input') as mocked_input:
            mocked_input.side_effect = ('Test', 'Test Author')
            self.app.ask_create_blog()
            self.assertIsNotNone(self.app.blogs.get('Test'))

    def test_ask_read_blog(self):
        with patch('builtins.input', return_value=self.blog_name):
            with patch('blog_console_app.app.App.print_posts'
                       ) as mocked_print_posts:
                self.app.ask_read_blog()
                mocked_print_posts.assert_called_with(self.blog)

    def test_print_posts(self):
        self.blog.create_post("Test post", "Test Content")
        with patch('blog_console_app.app.App.print_post') as mocked_print_post:
            self.app.print_posts(self.blog)
            mocked_print_post.assert_called_with(self.blog.posts[0])

    def test_print_post(self):
        title = "Post title"
        content = "Post content"
        post = Post(title, content)
        expected = '''
                    --- {0} ---
        
                    {1}
                
                    '''.format(
            ' '.join([word.capitalize() for word in title.split(' ')]),
            content)
        with patch('builtins.print') as mocked_print:
            self.app.print_post(post)
            mocked_print.assert_called_with(expected)

    def test_ask_create_post(self):
        expected = {
            'title': "Post Title",
            'content': "Post Content",
        }
        with patch('builtins.input') as mocked_input:
            # blog_name, post_title, post_content
            post_title = "Post Title"
            post_content = "Post Content"
            mocked_input.side_effect = (self.blog_name, post_title,
                                        post_content)
            self.app.ask_create_post()
            self.assertIsNotNone(self.app.blogs["Blog Name"].posts[0])
            self.assertDictEqual(expected,
                                 self.app.blogs["Blog Name"].posts[0].json())
            self.assertEqual(self.app.blogs["Blog Name"].posts[0].title_field,
                             post_title)
            self.assertEqual(
                self.app.blogs["Blog Name"].posts[0].content_field,
                post_content)

    def test_menu_create_blog(self):
        with patch('builtins.input') as mocked_input:
            user_selection = 'c'
            blog_name = 'Blog Title'
            author_name = 'Author Name'
            mocked_input.side_effect = (user_selection, blog_name, author_name,
                                        'q')
            main.main()
            self.assertIsNotNone(main.app.blogs[blog_name])
            self.assertEqual(main.app.blogs[blog_name].author, author_name)
            self.assertEqual(main.app.blogs[blog_name].title_field, blog_name)

    def test_menu_calls_ask_create_blog(self):
        with patch('builtins.input') as mocked_input:
            with patch('blog_console_app.app.App.ask_create_blog'
                       ) as mocked_ask_create_blog:
                user_selection = 'c'
                mocked_input.side_effect = (user_selection, 'q')
                main.main()
                mocked_ask_create_blog.assert_called()

    def test_menu_calls_print_blogs(self):
        with patch('builtins.input') as mocked_input:
            with patch('blog_console_app.app.App.print_blogs'
                       ) as mocked_print_blogs:
                user_selection = 'l'
                mocked_input.side_effect = (user_selection, 'q')
                main.main()
                mocked_print_blogs.assert_called()

    def test_menu_calls_ask_read_blog(self):
        with patch('builtins.input') as mocked_input:
            with patch('blog_console_app.app.App.ask_read_blog'
                       ) as mocked_ask_read_blog:
                user_selection = 'r'
                mocked_input.side_effect = (user_selection, 'q')
                main.main()
                mocked_ask_read_blog.assert_called()

    def test_menu_calls_ask_create_post(self):
        with patch('builtins.input') as mocked_input:
            with patch('blog_console_app.app.App.ask_create_post'
                       ) as mocked_ask_create_post:
                user_selection = 'p'
                mocked_input.side_effect = (user_selection, 'q')
                main.main()
                mocked_ask_create_post.assert_called()
Example #9
0
 def setUp(self) -> None:
     self.bloger_name = 'First Last'
     self.app = App()
     self.blog_name = "Blog Name"
     self.blog = Blog(self.blog_name, self.bloger_name)
     self.app.add_blog(self.blog_name, self.blog)
Example #10
0
 def ask_create_blog(self):
     title = input(self.PROMPT_FOR_BLOG_TITLE)
     author = input(self.PROMPT_FOR_AUTHOR_NAME)
     self.blogs[title] = Blog(title, author)