22
33
44import unittest
5- import coverage
65
7- from flask_script import Manager
8- from flask_migrate import Migrate , MigrateCommand
6+ import coverage
7+ from flask . cli import FlaskGroup
98
109from project .server import create_app , db
1110from project .server .models import User
1211
12+
13+ app = create_app ()
14+ cli = FlaskGroup (create_app = create_app )
15+
1316# code coverage
1417COV = coverage .coverage (
1518 branch = True ,
2225)
2326COV .start ()
2427
25- app = create_app ()
26- migrate = Migrate (app , db )
27- manager = Manager (app )
28+ @cli .command ()
29+ def create_db ():
30+ db .drop_all ()
31+ db .create_all ()
32+ db .session .commit ()
33+
34+
35+ @cli .command ()
36+ def drop_db ():
37+ """Drops the db tables."""
38+ db .drop_all ()
39+
40+
41+ @cli .command ()
42+ def create_admin ():
43+ """Creates the admin user."""
44+ db .
session .
add (
User (
email = '[email protected] ' ,
password = 'admin' ,
admin = True ))
45+ db .session .commit ()
46+
2847
29- # migrations
30- manager .add_command ('db' , MigrateCommand )
48+ @cli .command ()
49+ def create_data ():
50+ """Creates sample data."""
51+ pass
3152
3253
33- @manager .command
54+ @cli .command ()
3455def test ():
3556 """Runs the unit tests without test coverage."""
3657 tests = unittest .TestLoader ().discover ('project/tests' , pattern = 'test*.py' )
@@ -40,7 +61,7 @@ def test():
4061 return 1
4162
4263
43- @manager .command
64+ @cli .command ()
4465def cov ():
4566 """Runs the unit tests with coverage."""
4667 tests = unittest .TestLoader ().discover ('project/tests' )
@@ -56,30 +77,6 @@ def cov():
5677 return 1
5778
5879
59- @manager .command
60- def create_db ():
61- """Creates the db tables."""
62- db .create_all ()
63-
64-
65- @manager .command
66- def drop_db ():
67- """Drops the db tables."""
68- db .drop_all ()
69-
70-
71- @manager .command
72- def create_admin ():
73- """Creates the admin user."""
74- db .
session .
add (
User (
email = '[email protected] ' ,
password = 'admin' ,
admin = True ))
75- db .session .commit ()
76-
77-
78- @manager .command
79- def create_data ():
80- """Creates sample data."""
81- pass
82-
8380
8481if __name__ == '__main__' :
85- manager . run ()
82+ cli ()
0 commit comments