8000 Moved folders around a bit to make space for a new section. · bbdapps/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

section3/video_code/tests/unit/models/user_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.user import UserModel
2-
from tests.base_test import BaseTest
34

45

5-
class UserTest(BaseTest):
6+
class UserTest(TestCase):
67
def test_create_user(self):
78
user = UserModel('test', 'abcd')
89

File renamed without changes.

section4/video_code/app.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22

3-
from flask import Flask, jsonify
3+
from flask import Flask
44
from flask_restful import Api
5-
from flask_jwt import JWT, JWTError
5+
from flask_jwt import JWT
66

77
from security import authenticate, identity
88
from resources.user import UserRegister
@@ -15,7 +15,6 @@
1515

1616
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
1717
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18-
app.config['PROPAGATE_EXCEPTIONS'] = True
1918
app.secret_key = 'jose'
2019
api = Api(app)
2120

@@ -28,11 +27,6 @@
2827

2928
api.add_resource(UserRegister, '/register')
3029

31-
32-
@app.errorhandler(JWTError)
33-
def auth_error(err):
34-
return jsonify({'message': 'Could not authorize. Did you include a valid Authorization header?'}), 401
35-
3630
if __name__ == '__main__':
3731
from db import db
3832

section4/video_code/tests/base_test.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@
1414
class BaseTest(TestCase):
1515
SQLALCHEMY_DATABASE_URI = "sqlite://"
1616

17-
@classmethod
18-
def setUpClass(cls):
17+
def setUp(self):
1918
app.config['SQLALCHEMY_DATABASE_URI'] = BaseTest.SQLALCHEMY_DATABASE_URI
20-
app.config['DEBUG'] = False
2119
with app.app_context():
2220
db.init_app(app)
23-
24-
def setUp(self):
25-
with app.app_context():
2621
db.create_all()
27-
self.app = app.test_client
22+
self.app = app.test_client()
2823
self.app_context = app.app_context
2924

3025
def tearDown(self):

section5/video_code/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
def auth_error(err):
3434
return jsonify({'message': 'Could not authorize. Did you include a valid Authorization header?'}), 401
3535

36-
3736
if __name__ == '__main__':
3837
from db import db
3938

section5/video_code/models/item.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, name, price, store_id):
1717
self.store_id = store_id
1818

1919
def json(self):
20-
return {'id': self.id, 'name': self.name, 'price': self.price}
20+
return {'name': self.name, 'price': self.price}
2121

2222
@classmethod
2323
def find_by_name(cls, name):

section5/video_code/models/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, name):
1313
self.name = name
1414

1515
def json(self):
16-
return {'id': self.id, 'name': self.name, 'items': [item.json() for item in self.items.all()]}
16+
return {'name': self.name, 'items': [item.json() for item in self.items.all()]}
1717

1818
@classmethod
1919
def find_by_name(cls, name):
File renamed without changes.

section6/video_code/README.md

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
1-
# Set up
1+
# Stores REST Api
22

3-
We'll need a few things to install for this section:
3+
This is built with Flask, Flask-RESTful, Flask-JWT, and Flask-SQLAlchemy.
44

5-
- https://sites.google.com/a/chromium.org/chromedriver/downloads
6-
- behave (http://pythonhosted.org/behave/)
7-
- selenium (http://selenium-python.readthedocs.io/installation.html)
8-
9-
10-
## Running the tests
11-
12-
To run the tests, you'll need to do this in a terminal (but remember to have the Flask app running!):
13-
14-
```bash
15-
source venv/bin/activate
16-
cd section6/video_code/
17-
python -m behave tests/acceptance
18-
```
19-
20-
If you want to run the tests in PyCharm, you'll need to create appropriate configurations.
21-
22-
Create a Python configuration for the app.
23-
24-
[]
25-
26-
Create a Python configuration for the running of behave tests.
27-
28-
[]
5+
Deployed on Heroku.
File renamed without changes.

0 commit comments

Comments
 (0)
0