8000 Added new section 1 with an introduction to automated testing in Python. · coduya/testing-python-apps@1fef2bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fef2bb

Browse files
committed
Added new section 1 with an introduction to automated testing in Python.
1 parent fa856ae commit 1fef2bb

File tree

11 files changed

+158
-0
lines changed

11 files changed

+158
-0
lines changed

section1/video_code/app.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from blog import Blog
2+
3+
4+
MENU_PROMPT = '\nEnter "c" to create a blog, "l" to list them, "r" to read one, "p" to write a post, or "q" to quit: '
5+
POST_TEMPLATE = """
6+
--- {} ---
7+
8+
{}
9+
10+
"""
11+
12+
blogs = dict()
13+
14+
15+
def menu():
16+
print_blogs()
17+
selection = input(MENU_PROMPT)
18+
while selection != 'q':
19+
if selection == 'c':
20+
ask_create_blog()
21+
elif selection == 'l':
22+
print_blogs()
23+
elif selection == 'r':
24+
ask_read_blog()
25+
elif selection == 'p':
26+
ask_create_post()
27+
selection = input(MENU_PROMPT)
28+
29+
30+
def print_blogs():
31+
for key, blog in blogs.items():
32+
print('- {}'.format(blog))
33+
34+
35+
def ask_create_blog():
36+
title = input("Enter your blog title: ")
37+
author = input("Enter your name: ")
38+
39+
blogs[title] = Blog(title, author)
40+
41+
42+
def ask_read_blog():
43+
title = input("Enter the blog title you want to read: ")
44+
45+
print_posts(blogs[title])
46+
47+
48+
def print_posts(blog):
49+
for post in blog.posts:
50+
print_post(post)
51+
52+
53+
def print_post(post):
54+
print(POST_TEMPLATE.format(post.title, post.content))
55+
56+
57+
def ask_create_post():
58+
blog = input("Enter the blog title you want to create a post in: ")
59+
title = input("Enter your post title: ")
60+
content = input("Enter your post content: ")
61+
62+
blogs[blog].create_post(title, content)
63+
64+
65+
menu()

section1/video_code/blog.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from post import Post
2+
3+
4+
class Blog:
5+
def __init__(self, title, author):
6+
self.title = title
7+
self.author = author
8+
self.posts = []
9+
10+
def __repr__(self):
11+
return '{} by {} ({} posts)'.format(self.title, self.author, len(self.posts))
12+
13+
def create_post(self, title, content):
14+
self.posts.append(Post(title, content))
15+
16+
def json(self):
17+
return {
18+
'title': self.title,
19+
'author': self.author,
20+
'posts': [post.json() for post in self.posts],
21+
}

section1/video_code/post.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Post:
2+
def __init__(self, title, content):
3+
self.title = title
4+
self.content = content
5+
6+
def json(self):
7+
return {
8+
'title': self.title,
9+
'content': self.content,
10+
}

section1/video_code/tests/__init__.py

Whitespace-only changes.

section1/video_code/tests/integration/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from unittest import TestCase
2+
from blog import Blog
3+
4+
5+
class BlogTest(TestCase):
6+
def test_create_post_in_blog(self):
7+
b = Blog('Test', 'Test author')
8+
b.create_post('Test Post', 'Test Content')
9+
10+
self.assertEqual(b.posts[0].title, 'Test Post')
11+
self.assertEqual(b.posts[0].content, 'Test Content')
12+
13+
def test_blog_repr(self):
14+
b = Blog('Test', 'Test author')
15+
16+
self.assertEqual(str(b), "Test by Test author (0 posts)")
17+
18+
def test_json_with_posts(self):
19+
b = Blog('Test', 'Test author')
20+
b.create_post('Test Post', 'Test Content')
21+
22+
self.assertDictEqual(b.json(), {
23+
'title': b.title,
24+
'author': b.author,
25+
'posts': [{'title': 'Test Post', 'content': 'Test Content'}],
26+
})

section1/video_code/tests/system/__init__.py

Whitespace-only changes.

section1/video_code/tests/system/app_test.py

Whitespace-only changes.

section1/video_code/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from unittest import TestCase
2+
from blog import Blog
3+
4+
5+
class BlogTest(TestCase):
6+
def test_create_blog(self):
7+
b = Blog('Test', 'Test author')
8+
9+
self.assertEqual('Test', b.title)
10+
self.assertEqual('Test author', b.author)
11+
self.assertEqual(0, len(b.posts))
12+
self.assertListEqual([], b.posts)
13+
14+
def test_json_no_posts(self):
15+
b = Blog('Test', 'Test author')
16+
17+
self.assertDictEqual(b.json(), {
18+
'title': b.title,
19+
'author': b.author,
20+
'posts': [],
21+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from unittest import TestCase
2+
from post import Post
3+
4+
5+
class PostTest(TestCase):
6+
def test_create_post(self):
7+
p = Post('Test', 'Test content')
8+
9+
self.assertEqual('Test', p.title)
10+
self.assertEqual('Test content', p.content)
11+
12+
def test_json(self):
13+
p = Post('Test', 'Test content')
14+
15+
self.assertDictEqual({'title': p.title, 'content': p.content}, p.json())

0 commit comments

Comments
 (0)
0