generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
156 lines (137 loc) · 4.6 KB
/
test.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
FreeFrom Test Python module
Version 1.1.1
"""
# Import dependencies
import os
import unittest
import mongomock
from unittest import mock
from products import (
product_get_id, product_get_name,
product_get_search_dict, product_process_search_results)
from bson.objectid import ObjectId
class MockedMongoClient(mongomock.MongoClient):
"""
MockedMongoClient class
"""
def init_app(self, app):
pass
class AppTests(unittest.TestCase):
"""
AppTests class
"""
@classmethod
def setUpClass(cls) -> None:
"""
Set up class prior to each test
"""
# Replace flask_pymongo.PyMongo with a fake client
cls.patcher = mock.patch(
'flask_pymongo.PyMongo', return_value=MockedMongoClient())
cls.patcher.start()
# This will instantiate Mongo, so mongomock is required before
os.environ['SECRET_KEY'] = 'test'
from app import app
cls.app = app.test_client()
@classmethod
def tearDownClass(cls) -> None:
"""
Runs at the end of each test
"""
cls.patcher.stop()
def test_signout_before_signin(self):
"""
Test signout before sign in
"""
response = self.app.get("/signout")
self.assertEqual(response.status_code, 500)
self.assertIn('Oops.... something went wrong', str(response.data))
def test_successful_register_post(self):
"""
Test succesful user registration
"""
username = "testuser"
response = self.app.post("/register", data={
"username": username,
"email": "test@gmail.com",
"password": "test123",
"confirmpassword": "test123",
})
self.assertEqual(response.status_code, 302)
from database import mongo
user = mongo.db.users.find_one({"username": username})
self.assertIsNotNone(user)
def test_successful_signin_post(self):
"""
Test successful user signin
"""
username = "testadmin1"
password = "testadmin1"
response = self.app.post("/signin", data={
"username": username,
"password": password
})
self.assertEqual(response.status_code, 302)
# Test the session cookie has been created
self.assertIn("session", (response.headers["Set-Cookie"]))
def test_successful_product_search_post(self):
"""
Test successful product search
"""
response = self.app.post("/search", data={
"search": "digestives",
"categorySelector": "biscuits"
})
self.assertEqual(response.status_code, 200)
self.assertIn("digestives", str(response.data))
def test_successful_product_view_post(self):
"""
Test successful product view
"""
response = self.app.post("/view/60c30bdc8c137a1d611addaf")
self.assertEqual(response.status_code, 200)
self.assertIn("digestives", str(response.data))
def test_get_product_id(self):
"""
Test get product id
"""
result = product_get_id("digestives")
self.assertIsNotNone(result)
self.assertEqual(result, ObjectId("60c30bdc8c137a1d611addaf"))
def test_get_product_name(self):
"""
Test get product name
"""
result = product_get_name(ObjectId("60c30bdc8c137a1d611addaf"))
self.assertIsNotNone(result)
self.assertEqual(result, "digestives")
def test_product_get_search_dict(self):
"""
Test product process get search dictionary
"""
result = product_get_search_dict(
"digestives", ObjectId("609e4a13011739b2627fd835"),
[ObjectId("609e499b011739b2627fd82e")])
print(result)
self.assertIsNotNone(result)
self.assertEqual(result, {
"$text": {"$search": "digestives"},
"category_id": ObjectId("609e4a13011739b2627fd835"),
"free_from_allergens": {
"$all": [ObjectId("609e499b011739b2627fd82e")]}})
def test_product_process_search_results(self):
"""
Test product process search results
"""
from database import mongo
search_dict = product_get_search_dict(
"digestives", ObjectId("609e4a13011739b2627fd835"),
[ObjectId("609e499b011739b2627fd82e")])
products = mongo.db.products.find(search_dict)
result = product_process_search_results(products)
self.assertIsNotNone(result)
if __name__ == '__main__':
unittest.main()