Skip to content

Commit 4fe58ca

Browse files
committed
add tests
1 parent 9dfaccc commit 4fe58ca

File tree

3 files changed

+152
-4
lines changed

3 files changed

+152
-4
lines changed

Diff for: flask_test.py

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from src.app import create_app
6+
7+
8+
class TestCaseManagementTestCase(unittest.TestCase):
9+
"""This class represents the trivia test case"""
10+
11+
def setUp(self):
12+
"""Define test variables and initialize app."""
13+
config_override = {"TESTING": True}
14+
os.environ["DATABASE_FILENAME"] = "testdb.db"
15+
self.app = create_app(config_override)
16+
self.client = self.app.test_client
17+
18+
self.new_test_case = {"name": "New Test Case", "description": "New Test Case Description"}
19+
20+
self.new_execution = {"asset_id": "1", "test_case_id": "1", "status": True, "details": "Success"}
21+
22+
def tearDown(self):
23+
"""Executed after each test"""
24+
pass
25+
26+
def test_retrieve_tests(self):
27+
"""Test retrieve tests"""
28+
res = self.client().get("/tests")
29+
data = json.loads(res.data)
30+
31+
self.assertEqual(res.status_code, 200)
32+
self.assertEqual(data["success"], True)
33+
self.assertTrue(data["test_cases"])
34+
35+
def test_405_using_wrong_method_to_retrieve_tests(self):
36+
"""Test 405 using wrong method to retrieve tests"""
37+
res = self.client().patch("/tests")
38+
data = json.loads(res.data)
39+
40+
self.assertEqual(res.status_code, 405)
41+
self.assertEqual(data["success"], False)
42+
self.assertTrue(data["message"])
43+
44+
def test_create_new_test(self):
45+
"""test create new test"""
46+
res = self.client().post("/tests", json=self.new_test_case)
47+
data = json.loads(res.data)
48+
49+
self.assertEqual(res.status_code, 200)
50+
self.assertEqual(data["success"], True)
51+
52+
def test_400_create_new_test_without_name(self):
53+
"""test create new test without providing name"""
54+
res = self.client().post("/tests", json={"testing": "xxx"})
55+
data = json.loads(res.data)
56+
57+
self.assertEqual(res.status_code, 400)
58+
self.assertEqual(data["success"], False)
59+
self.assertTrue(data["message"])
60+
61+
def test_405_creation_not_allowed(self):
62+
"""test 405 creation not allowed"""
63+
res = self.client().post("/tests/45", json=self.new_test_case)
64+
data = json.loads(res.data)
65+
66+
self.assertEqual(res.status_code, 405)
67+
self.assertEqual(data["success"], False)
68+
self.assertTrue(data["message"])
69+
70+
def test_get_specific_test(self):
71+
"""Test get specific test with id"""
72+
res = self.client().get("/tests/1")
73+
data = json.loads(res.data)
74+
75+
self.assertEqual(res.status_code, 200)
76+
self.assertEqual(data["success"], True)
77+
self.assertTrue(len(data["test_case"]))
78+
79+
def test_get_nonexistent_test(self):
80+
"""Test get non existent test"""
81+
res = self.client().get("/tests/10000")
82+
data = json.loads(res.data)
83+
84+
self.assertEqual(res.status_code, 404)
85+
self.assertEqual(data["success"], False)
86+
self.assertTrue(data["message"])
87+
88+
def test_update_test(self):
89+
"""Test update test"""
90+
res = self.client().patch("/tests/1", json={"name": "Updated Test Case"})
91+
data = json.loads(res.data)
92+
93+
self.assertEqual(res.status_code, 200)
94+
self.assertEqual(data["success"], True)
95+
self.assertTrue(data["test_case"])
96+
97+
def test_update_test_without_name(self):
98+
"""Test update test without providing name"""
99+
res = self.client().patch("/tests/1", json={"testing": "Updated Test Case"})
100+
data = json.loads(res.data)
101+
102+
self.assertEqual(res.status_code, 400)
103+
self.assertEqual(data["success"], False)
104+
self.assertTrue(data["message"])
105+
106+
def test_delete_test_case(self):
107+
"""Test delete test case"""
108+
res = self.client().delete("/tests/3")
109+
data = json.loads(res.data)
110+
111+
self.assertEqual(res.status_code, 200)
112+
self.assertEqual(data["success"], True)
113+
self.assertEqual(data["deleted_test_case_id"], 5)
114+
self.assertTrue(data["total_test_cases"])
115+
116+
def test_404_delete_nonexistent_test(self):
117+
"""test 404 delete nonexistent test"""
118+
res = self.client().delete("/tests/10000")
119+
data = json.loads(res.data)
120+
121+
self.assertEqual(res.status_code, 404)
122+
self.assertEqual(data["success"], False)
123+
self.assertTrue(data["message"])
124+
125+
def test_get_execution_results(self):
126+
"""Test get execution results"""
127+
res = self.client().get("/executions/1")
128+
data = json.loads(res.data)
129+
130+
self.assertEqual(res.status_code, 200)
131+
self.assertEqual(data["success"], True)
132+
self.assertTrue(data["executions"])
133+
self.assertTrue(data["asset"])
134+
self.assertTrue(data["total_executions"])
135+
136+
def test_add_execution_results(self):
137+
"""Test add execution result"""
138+
res = self.client().post("/executions", json=self.new_execution)
139+
data = json.loads(res.data)
140+
141+
self.assertEqual(res.status_code, 200)
142+
self.assertEqual(data["success"], True)
143+
self.assertTrue(data["execution"])
144+
self.assertTrue(data["total_executions"])
145+
146+
147+
# Make the tests conveniently executable
148+
if __name__ == "__main__":
149+
unittest.main()

Diff for: mock_up_data.sql

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
GRANT ALL PRIVILEGES ON DATABASE testdb TO john;
2-
31
-- TestCase
42
INSERT INTO `test_case` VALUES (1, 'First Test', 'First Test Description');
53
INSERT INTO `test_case` VALUES (2, 'Second Test', NULL);

Diff for: src/app.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23

34
from flask import Flask, abort, jsonify, request
@@ -30,12 +31,12 @@ def create_app(test_config=None):
3031
# Load configuration for prod vs. dev
3132
is_prod_env = "RUNNING_IN_PRODUCTION" in os.environ
3233
if not is_prod_env:
33-
print("Loading config.development.")
34+
logging.info("Loading config.development.")
3435
app.config.from_object("src.config.development")
3536
setup_db(app)
3637
# db_drop_and_create_all(app)
3738
else:
38-
print("Loading config.production.")
39+
logging.info("Loading config.production.")
3940
app.config.from_object("src.config.production")
4041
setup_db(app)
4142
# db_drop_and_create_all(app)

0 commit comments

Comments
 (0)