-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
135 lines (109 loc) · 3.62 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import json
import bcrypt
from .models import Book, Author, AuthorBook, User
from django.test import TestCase
from django.test import Client
from unittest.mock import patch, MagicMock
class AuthorTest(TestCase):
def setUp(self):
Author.objects.create(
name = 'Brendan Eich',
email = 'BrendanEich@gmail.com'
)
def tearDown(self):
Author.objects.all().delete()
def test_authorkview_post_success(self):
client = Client()
author = {
'name' : 'Guido van Rossum',
'email' : 'GuidovanRossum@gmail.com'
}
response = client.post('/book/author', json.dumps(author), content_type='application/json')
self.assertEqual(response.status_code, 200)
def test_authorkview_post_duplicated_name(self):
client = Client()
author = {
'name' : 'Brendan Eich',
'email' : 'GuidovanRossum@gmail.com'
}
response = client.post('/book/author', json.dumps(author), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json(),
{
'message':'DUPLICATED_NAME'
}
)
def test_authorkview_post_invalid_keys(self):
client = Client()
author = {
'first_name' : 'Guido van Rossum',
'email' : 'GuidovanRossum@gmail.com'
}
response = client.post('/book/author', json.dumps(author), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json(),
{
'message':'INVALID_KEYS'
}
)
class AuthorBookTest(TestCase):
def setUp(self):
client = Client()
Book.objects.create(
id = 1,
title = 'python'
)
Book.objects.create(
id =2,
title = 'javascript'
)
Author.objects.create(
id = 1,
name = 'Guido van Rossum',
email = 'GuidovanRossum@gmail.com'
)
Author.objects.create(
id =2,
name = 'Brendan Eich',
email = 'BrendanEich@gmail.com'
)
AuthorBook.objects.create(
book = Book.objects.get(id=1),
author = Author.objects.get(id=1)
)
AuthorBook.objects.create(
book = Book.objects.get(id=1),
author = Author.objects.get(id=2)
)
AuthorBook.objects.create(
book = Book.objects.get(id=2),
author = Author.objects.get(id=1)
)
AuthorBook.objects.create(
book = Book.objects.get(id=2),
author = Author.objects.get(id=2)
)
def tearDown(self):
Book.objects.all().delete()
Author.objects.all().delete()
AuthorBook.objects.all().delete()
def test_authorbook_get_success(self):
client = Client()
response = client.get('/book/author-book/python')
self.assertEqual(response.json(),
{
}
)
self.assertEqual(response.status_code, 200)
def test_authorbook_get_fail(self):
client = Client()
response = client.get('/book/author-book/c-language')
self.assertEqual(response.json(),
{
}
)
self.assertEqual(response.status_code, 400)
def test_authorbook_get_not_found(self):
client = Client()
response = client.get('/book/author-book?book=python')
self.assertEqual(response.status_code, 400)