-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_projectGo.py
More file actions
146 lines (125 loc) · 6.91 KB
/
test_projectGo.py
File metadata and controls
146 lines (125 loc) · 6.91 KB
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
import unittest
import sqlite3
import projectGo
from flask import request, jsonify
import json
import tempfile
import shutil
class Test(unittest.TestCase):
#simple set up for self.app to mean something...
def setUp(self):
self.app = projectGo.app.test_client()
#setup the test DB file
self.temp_folder_path = tempfile.mkdtemp()
self.temp_db_filepath = self.temp_folder_path + 'unit_test_db.db'
test_db_file = open(self.temp_db_filepath, 'a')
test_db_file.close()
self.db_connection = sqlite3.connect(self.temp_db_filepath)
self.db_cursor = self.db_connection.cursor()
#create the test DB tables
self.db_cursor.execute('create table users(userid integer primary key not null, userName varchar(15))')
self.db_cursor.execute('create table userinfo(userid int primary key not null, name varchar(50), address varchar(50), birthday text, foreign key(userid) references users(userid))')
self.db_cursor.execute('create table obligation(obligationid integer primary key not null, userid int not null, name varchar(30), description varchar(200), starttime text, endtime text, priority int, status int, category int, foreign key(userid) references users(userid))')
#populate the test DB tables
self.db_cursor.execute("insert into users values(1, 'UserName')")
self.db_cursor.execute("insert into users values(2, 'UserName2')")
self.db_cursor.execute("insert into users values(3, 'UserName3')")
self.db_cursor.execute("insert into obligation values(1, 1, 'Dude name', 'This is a long description', '2014-01-01 00:00:00.000', '2014-01-01 01:00:00.000', 1, 1, 1)")
self.db_cursor.execute("insert into obligation values(2, 2, 'Dude name2', 'This is another long description', '2014-01-01 00:00:00.000', '2014-01-01 01:00:00.000', 1, 1, 1)")
self.db_cursor.execute("insert into obligation values(3, 1, 'Dude name', 'shorter description', '2014-01-01 00:00:00.000', '2014-01-01 01:00:00.000', 1, 1, 1)")
self.db_cursor.execute("insert into obligation values(4, 3, 'Test', 'Longggggggggggggggggggggggggg description', '2014-01-01 00:00:00.000', '2014-01-01 01:00:00.000', 1, 2, 3)")
self.db_cursor.execute("insert into obligation values(5, 4, 'Test2', '', '2014-01-05 00:00:00.000', '2014-01-10 01:00:00.000', 1, 4, 9)")
self.db_cursor.execute("insert into obligation values(6, 4, 'Obg1', '', '2014-03-04 00:00:00.000', '2014-01-10 01:00:00.000', 1, 2, 9)")
self.db_cursor.execute("insert into obligation values(7, 4, 'Obg2', '', '2014-03-05 00:00:00.000', '2014-01-10 01:00:00.000', 1, 4, 9)")
self.db_connection.commit()
#re-route projectGo to use the test DB
projectGo.applicationInfo.database_filepath = self.temp_db_filepath
projectGo.applicationInfo.OBLIGATION_TABLE_NAME = 'obligation'
projectGo.applicationInfo.OBLIGATION_ID_NAME = 'obligationid'
def tearDown(self):
#remove the test DB
shutil.rmtree(self.temp_folder_path)
#test of POST method without sending data for create_obligation
def test_create_obligation_no_data(self):
result = self.app.post('/obligations')
self.assertEqual(result.status_code, 400)
#test of POST method creating an obligation with working data
def test_create_obligation_working_data(self):
data_to_send = {
'userid': 1,
'name': 'flask-test',
'description': 'flask-test-description',
'starttime': '2014-03-05 00:00:00.000',
'endtime': '2014-03-05 00:00:00.000',
'priority': 1,
'status': 1,
'category': 1
}
result = self.app.post('/obligations', data=data_to_send)
self.assertEqual(result.status_code, 200)
jsonData = json.loads(result.data)
self.assertTrue('obligation_id' in jsonData)
test_id = jsonData['obligation_id']
my_query = "select * from obligation where obligationid =" + str(test_id)
row = self.db_cursor.execute(my_query).fetchall()
self.assertTrue(len(row) > 0)
if (len(row) > 0):
row = row[0]
self.assertEquals(row[2], 'flask-test')
#test of direct method for get_obligation with entry that doesn't exist
def test_go_get_obligation_not_exists(self):
#Test case 1
jsonData = json.loads(self.app.get('/obligations/9999999').data)
self.assertEqual(jsonData['error'],1)
#Test case 2
jsonData = json.loads(self.app.get('/obligations/0').data)
self.assertEqual(jsonData['error'],1)
#test of GET method for get_obligation
def test_get_obligation(self):
#Test case 1
result = self.app.get('/obligations/1')
self.assertEquals(result.status, '200 OK')
jsonData = json.loads(result.data)
self.assertEqual(jsonData['obligationid'], 1)
self.assertEqual(jsonData['userid'],1)
self.assertEqual(jsonData['name'],"Dude name")
self.assertEqual(jsonData['starttime'],"2014-01-01 00:00:00.000")
self.assertEqual(jsonData['endtime'],"2014-01-01 01:00:00.000")
self.assertEqual(jsonData['priority'],1)
#Test case 2
result = self.app.get('/obligations/2')
self.assertEquals(result.status, '200 OK')
jsonData = json.loads(result.data)
self.assertEqual(jsonData['obligationid'], 2)
self.assertEqual(jsonData['userid'], 2)
self.assertEqual(jsonData['name'],"Dude name2")
self.assertEqual(jsonData['starttime'],"2014-01-01 00:00:00.000")
self.assertEqual(jsonData['endtime'],"2014-01-01 01:00:00.000")
self.assertEqual(jsonData['priority'],1)
#test of GET method for get_all_obligations
def test_get_all_obligations(self):
result = self.app.get('/obligations')
self.assertEquals(result.status, '200 OK')
jsonData = json.loads(result.data)
self.assertTrue(len(jsonData) > 0)
entry1 = jsonData[0]
self.assertTrue('obligationid' in entry1)
#Test of GET method for obligations on a given day
def test_get_date_obligations(self):
result = self.app.get('/obligations/2014-01-01')
self.assertEquals(result.status, '200 OK')
result = self.app.get('/obligations/2019-01-01')
self.assertEquals(result.status, '404 NOT FOUND')
#test of DELETE method for delete_obligation
def test_delete_obligation(self):
#typical delete case of existing obligation
result = self.app.delete('/obligations/1')
self.assertEquals(result.status, '200 OK')
#delete case for non existing obligation
result = self.app.delete('/obligations/1')
self.assertEquals(result.status, '404 NOT FOUND')
#delete case for invalid obligation
result = self.app.delete('/obligations/abcdefg')
self.assertEquals(result.status, '405 METHOD NOT ALLOWED')
if __name__ == '__main__':
unittest.main()