-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
110 lines (95 loc) · 4.89 KB
/
test.py
File metadata and controls
110 lines (95 loc) · 4.89 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
import unittest
import project
import argparse
import sys
import io
class TestProject(unittest.TestCase):
def setUp(self):
self.parser = argparse.ArgumentParser()
self.parser.add_argument('excel', type=str, action='store',
help='path to xlsx file', nargs=1)
self.parser.add_argument('-c', '--create', action='store_true',
help='flag if entered file is to add new table to database')
self.parser.add_argument('-ai', '--add_index', action='store', type=int,
help="adds index to the entered column by its number, if table and column exists",
nargs=1)
self.parser.add_argument('-upd', '--update', action='store_true',
help="flag if entered file is to update the existing table")
def test_unexisting_args(self): # testing with no existing argument, argparse systemexit error == 2
# redirect error outputs of argparse lib to nowhere, they are catched, that's ok.
data = ["./test_data/test1.xlsx", "-dagshi"]
sys.stderr = io.StringIO()
data = []
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
sys.stderr = sys.__stderr__
self.assertEqual(cm.exception.code, 2)
def test_no_args(self): # testing with no arguments, argparse systemexit error == 2
# redirect error outputs of argparse lib to nowhere, they are catched, that's ok.
sys.stderr = io.StringIO()
data = []
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
sys.stderr = sys.__stderr__
self.assertEqual(cm.exception.code, 2)
def test_help(self): # testing argparse --help systemexit == 0 and all ok
data = ["-h"]
# output suppressed
sys.stdout = io.StringIO()
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
sys.stdout = sys.__stdout__
self.assertEqual(cm.exception.code, 0)
def test_file_not_found(self): # testing invalid file exception openpyxl error
data = ["./invalid_file.xl"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'Error InvalidFileException: Enter file path carefully once more')
def test_file_does_not_exist(self): # testing invalid file exception found error
data = ["./file_does_not_exist.xlsx"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'Error FileNotFound: Enter file path carefully once more')
def test_two_flags(self):
data = ["./test_data/test1.xlsx", "-c", "-upd"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'Please, select only ONE of the flag arguments: create or update')
def test_no_action(self):
data = ["./test_data/test1.xlsx"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'No action specified')
def test_empty_file(self): # testing if we have have not entered path to data
data = ["./test_data/test1.xlsx", "-c"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'Table given is empty. Enter any data and try again.')
def test_empty_data(self): # testing if we have have not entered path to data
data = ["./test_data/test2.xlsx", "-c"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'Table given is empty. Add some lines and try again.')
def test_create_table(self): # testing if we have have not entered path to data
data = ["./test_data/test3.xlsx", "-c"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'Table created successfully and filled with data')
def test_update_table(self): # testing if we have have not entered path to data
data = ["./test_data/test4.xlsx", "-upd"]
with self.assertRaises(SystemExit) as cm:
args = self.parser.parse_args(data)
project.main(args)
self.assertEqual(cm.exception.code, 'Data successfully updated')
if __name__ == '__main__':
unittest.main()