8000 Moved folders around a bit to make space for a new section. · senarclens/testing-python-apps@f118947 · GitHub
[go: up one dir, main page]

Skip to content

Commit f118947

Browse files
committed
Moved folders around a bit to make space for a new section.
1 parent 1fef2bb commit f118947

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+280
-168
lines changed

section1/video_code/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ def ask_create_post():
6262
blogs[blog].create_post(title, content)
6363

6464

65-
menu()
65+
if __name__ == '__main__':
66+
menu()

section1/video_code/tests/integration/blog_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_create_post_in_blog(self):
1313
def test_blog_repr(self):
1414
b = Blog('Test', 'Test author')
1515

16-
self.assertEqual(str(b), "Test by Test author (0 posts)")
16+
self.assertEqual(str(b), 'Test by Test author (0 posts)')
1717

1818
def test_json_with_posts(self):
1919
b = Blog('Test', 'Test author')
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
4+
from section1.video_code import app
5+
from blog import Blog
6+
from post import Post
7+
8+
9+
class AppTest(TestCase):
10+
def setUp(self):
11+
blog = Blog('Test', 'Test Author')
12+
app.blogs = {'Test': blog}
13+
14+
def test_menu_prints_blogs(self):
15+
with patch('builtins.print') as mocked_print:
16+
with patch('builtins.input', return_value='q'):
17+
app.menu()
18+
19+
mocked_print.assert_called_with('- Test by Test Author (0 posts)')
20+
21+
def test_menu_prints_prompt(self):
22+
with patch('builtins.input', return_value='q') as mocked_input:
23+
app.menu()
24+
25+
mocked_input.assert_called_with(app.MENU_PROMPT)
26+
27+
def test_menu_calls_create_blog(self):
28+
with patch('builtins.input') as mocked_input:
29+
mocked_input.side_effect = ('c', 'Test Two', 'Test Author Two', 'q')
30+
app.menu()
31+
32+
self.assertIsNotNone(app.blogs['Test Two'])
33+
34+
def test_menu_calls_print_blogs(self):
35+
with patch('builtins.input') as mocked_input:
36+
with patch('section1.video_code.app.print_blogs') as mocked_print_blogs:
37+
mocked_input.side_effect = ('l', 'q')
38+
app.menu()
39+
40+
mocked_print_blogs.assert_called()
41+
42+
def test_menu_calls_ask_read_blogs(self):
43+
with patch('builtins.input') as mocked_input:
44+
with patch('section1.video_code.app.ask_read_blog') as mocked_ask_read_blog:
45+
mocked_input.side_effect = ('r', 'Test', 'q')
46+
app.menu()
47+
48+
mocked_ask_read_blog.assert_called()
49+
50+
def test_menu_calls_ask_create_post(self):
51+
with patch('builtins.input') as mocked_input:
52+
with patch('section1.video_code.app.ask_create_post') as mocked_ask_create_post:
53+
mocked_input.side_effect = ('p', 'Test', 'New Post', 'New Content', 'q')
54+
app.menu()
55+
56+
mocked_ask_create_post.assert_called()
57+
58+
def test_print_blogs(self):
59+
with patch('builtins.print') as mocked_print:
60+
app.print_blogs()
61+
mocked_print.assert_called_with('- Test by Test Author (0 posts)')
62+
63+
def test_ask_create_blog(self):
64+
with patch('builtins.input') as mocked_input:
65+
mocked_input.side_effect = ('Test', 'Author')
66+
67+
app.ask_create_blog()
68+
69+
self.assertIsNotNone(app.blogs.get('Test'))
70+
self.assertEqual(app.blogs.get('Test').title, 'Test')
71+
self.assertEqual(app.blogs.get('Test').author, 'Author')
72+
73+
def test_ask_read_blog(self):
74+
with patch('builtins.input', return_value='Test'):
75+
with patch('section1.video_code.app.print_posts') as mocked_print_posts:
76+
app.ask_read_blog()
77+
78+
mocked_print_posts.assert_called_with(app.blogs['Test'])
79+
80+
def test_print_posts(self):
81+
blog = app.blogs['Test']
82+
blog.create_post('Post title', 'Post content')
83+
84+
with patch('section1.video_code.app.print_post') as mocked_print_post:
85+
app.print_posts(blog)
86+
87+
mocked_print_post.assert_called_with(blog.posts[0])
88+
89+
def test_print_post(self):
90+
post = Post('Post title', 'Post content')
91+
expected_print = """
92+
--- Post title ---
93+
94+
Post content
95+
96+
"""
97+
98+
with patch('builtins.print') as mocked_print:
99+
app.print_post(post)
100+
101+
mocked_print.assert_called_with(expected_print)
102+
103+
def test_ask_create_post(self):
104+
blog = app.blogs['Test']
105+
with patch('builtins.input') as mocked_input:
106+
mocked_input.side_effect = ('Test', 'Test Title', 'Test Content')
107+
108+
app.ask_create_post()
109+
110+
self.assertEqual(blog.posts[0].title, 'Test Title')
111+
self.assertEqual(blog.posts[0].content, 'Test Content')

section2/video_code/app.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

section2/video_code/readme.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

section2/video_code/requirements.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

section3/video_code/readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@
33
This is built with Flask, Flask-RESTful, Flask-JWT, and Flask-SQLAlchemy.
44

55
Deployed on Heroku.
6+
7+
To get started:
8+
9+
- Create a virtualenv for this project
10+
- Install requirements using `pip install -r requirements.txt`
11+
12+
When you've created the first test, you'll also need to create a correct runtime configuration in PyCharm.
13+
14+
Create a sample unittest configuration, and choose:
15+
16+
- `Path` as as target, with your project's `/tests` folder.
17+
- `Pattern` being `_test.py`

section3/video_code/resources/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_restful import Resource
1+
from flask_restful import Resource, reqparse
22
from models.store import StoreModel
33

44

section3/video_code/tests/unit/models/item_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
from unittest import TestCase
2+
13
from models.item import ItemModel
2-
from tests.base_test import BaseTest
34

45

5-
class ItemTest(BaseTest):
6+
class ItemTest(TestCase):
67
def test_create_item(self):
8+
# Notice this won't work with PostgreSQL, because of
9+
# foreign key constraints.
10+
# The store doesn't exist yet, so it would raise an error.
11+
# SQLite has no foreign key constraint enforcement, so it works there.
712
item = ItemModel('test', 19.99, 1)
813

914
self.assertEqual(item.name, 'test',

section3/video_code/tests/unit/models/store_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from unittest import TestCase
2+
13
from models.store import StoreModel
2-
from tests.base_test import BaseTest
34

45

5-
class StoreTest(BaseTest):
6+
class StoreTest(TestCase):
67
def test_create_store(self):
78
store = StoreModel('test')
89

0 commit comments

Comments
 (0)
0