-
Notifications
You must be signed in to change notification settings - Fork 10
/
tests.py
84 lines (73 loc) · 2.83 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from unittest import TestCase, main
from dbConnect import DBConnect
import os
USERS = [
{'name': 'Linus Torvals', 'email': '[email protected]'},
{'name': 'Guido van Rossum', 'email': '[email protected]'},
{'name': 'Kenneth Reitz', 'email': '[email protected]'},
]
class DBTest(TestCase):
def setUp(self):
"""Prepare for Test."""
environment = os.environ.get('CI_ENV', "local")
if environment == "local":
self.database = DBConnect('test/test_credentials.json')
elif environment == "Gitlab":
self.database = DBConnect('test/gitlab_credentials.json')
elif environment == "Travis":
self.database = DBConnect('test/travis_credentials.json')
def tearDown(self):
"""Finish Testing."""
# Delete all created rows
self.database.cursor.execute("truncate test")
self.database.commit()
# Disconnect from database
self.database.disconnect()
def test_insert(self):
"""Test inserting information into database."""
new_user = {
'name': 'Emin Mastizada',
'email': '[email protected]',
'views': 6,
}
result = self.database.insert(new_user, 'test')
self.assertTrue(result["status"], "Insert Failed with message %s" % result["message"])
def test_commit(self):
"""Test committing all users at once."""
for user in USERS:
result = self.database.insert(user, 'test', commit=False)
self.assertTrue(result["status"], "Insert Failed with message %s" % result["message"])
self.database.commit()
# Now there should be 3 users in table with views=0
result = self.database.fetch(
'test',
fields=['count(id)'],
filters={'views': 0}
)
self.assertTrue(len(result), "Fetch returned empty results")
if len(result):
self.assertTrue('count(id)' in result[0].keys(), "Requested field 'count(id)' missing in result keys")
if 'count(id)' in result[0].keys():
self.assertEqual(result[0]['count(id)'], 3, "Number of new users in table should be 3")
def test_fetch(self):
"""Test fetching information from database."""
pass
def test_sum(self):
"""Test value_sum functionality."""
counter = 1
for user in USERS[:3]:
user['views'] = counter
self.database.insert(user, 'test')
counter += 1
sum_result = self.database.value_sum(
'test',
fields=['views']
)
# views = 1 + 2 + 3 = 6
self.assertEqual(
sum_result['views'], 6, "Sum of 3 new users should be 6"
)
if __name__ == '__main__':
main()