-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
166 lines (145 loc) · 5.58 KB
/
commands.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
157
158
159
160
161
162
163
164
165
166
import unittest, click
from flask.cli import FlaskGroup
import app
from app.utils import LOGGER, from_json
from app.extensions import db
from app.models.flashcard_model import FlashcardModel, FigureModel
cli = FlaskGroup(app)
@cli.command("clear_database")
def clear_database():
"""Initializes database and cleans up old tables"""
try:
print("Clearing Database ...")
db.drop_all()
print("Creating Tables ...")
db.create_all()
print("Successfully re-initialized the database!")
LOGGER.info(f"Successfully re-initialized the database!")
return 0
except Exception as e:
LOGGER.error(f"An error occurred when initializing database: {e}")
return 1
# ==============================================================================================================
@cli.command("create_database")
def init_database():
"""Initializes database by creating tables"""
try:
print("Creating Tables ...")
db.create_all()
print("Successfully initialized the database!")
LOGGER.info(f"Successfully initialized the database!")
return 0
except Exception as e:
LOGGER.error(f"An error occurred when initializing database: {e}")
return 1
# ==============================================================================================================
@cli.command("test")
def test():
'''
Runs all the unit tests
'''
tests = unittest.TestLoader().discover("tests")
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
else:
return 1
# ==============================================================================================================
@cli.command("test_routes")
@click.argument('route_name', required=False)
def test_routes(route_name):
'''
Runs the unit tests for routes: Main, Manage
'''
if not route_name:
tests = unittest.TestLoader().discover("tests/test_routes")
elif route_name == 'main':
tests = unittest.TestLoader().discover("tests/test_routes", pattern="test_main_routes.py")
elif route_name == 'manage':
tests = unittest.TestLoader().discover("tests/test_routes", pattern="test_manage_routes.py")
else:
print(f"Invalid argument: {route_name}!")
return 1
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
else:
return 1
# ==============================================================================================================
@cli.command("test_forms")
@click.argument('form_name', required=False)
def test_forms(form_name):
'''
Runs the unit tests for forms: FlashcardForm, SearchForm
'''
if not form_name:
tests = unittest.TestLoader().discover("tests/test_forms")
elif form_name == 'flashcard':
tests = unittest.TestLoader().discover("tests/test_forms", pattern="test_flashcard_form.py")
elif form_name == 'search':
tests = unittest.TestLoader().discover("tests/test_forms", pattern="test_search_form.py")
else:
print(f"Invalid argument: {form_name}!")
return 1
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
else:
return 1
# ==============================================================================================================
@cli.command("test_models")
@click.argument('model_name', required=False)
def test_models(model_name):
'''
Runs the unit tests for models: FlashcardModel, FigureModel
'''
if not model_name:
tests = unittest.TestLoader().discover("tests/test_models")
elif model_name == 'flashcard':
tests = unittest.TestLoader().discover("tests/test_models", pattern="test_flashcard_model.py")
elif model_name == 'figure':
tests = unittest.TestLoader().discover("tests/test_models", pattern="test_figure_model.py")
else:
print(f"Invalid argument: {model_name}!")
return 1
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
else:
return 1
# ==============================================================================================================
@cli.command("import_json")
@click.argument('filename', required=False)
def import_json(filename):
'''
Runs the unit tests for models: FlashcardModel, FigureModel
'''
try:
if not filename:
# NOTE: file must be in the data directory and named data.json
print("Importing JSON File: data.json ...")
LOGGER.info("Importing JSON File: data.json ...")
data = from_json()
else:
print(f"Importing JSON File: {filename} ...")
LOGGER.info(f"Importing JSON File: {filename} ...")
data = from_json(filename=filename)
for card in data:
FlashcardModel(
category=card['category'],
question=card['question'],
answer=card['answer'],
q_code_type=card['q_code_type'],
q_code_example=card['q_code_block'],
a_code_type=card['a_code_type'],
a_code_example=card['a_code_block'],
)
print(f"Successfully imported JSON file.")
LOGGER.info(f"Successfully imported JSON file.")
return 0
except Exception as e:
print(f"Failed to import JSON file: {e}")
LOGGER.error(f"Failed to import JSON file: {e}")
return 1
if __name__ == "__main__":
cli()