-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
146 lines (120 loc) · 3.09 KB
/
manage.py
File metadata and controls
146 lines (120 loc) · 3.09 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
# manage.py
import datetime
import unittest
import coverage
from flask.cli import FlaskGroup
from faker import Faker
from project.server import create_app, db
from project.server.models import User, Micropost
import subprocess
import sys
app = create_app()
cli = FlaskGroup(create_app=create_app)
# code coverage
COV = coverage.coverage(
branch=True,
include="project/*",
omit=[
"project/tests/*",
"project/server/config.py",
"project/server/*/__init__.py",
],
)
COV.start()
@cli.command()
def create_db():
db.drop_all()
db.create_all()
db.session.commit()
@cli.command()
def drop_db():
"""Drops the db tables."""
db.drop_all()
@cli.command()
def create_admin():
"""Creates the admin user."""
db.session.add(User(
name="admin",
email="ad@min.com",
password="admin",
admin=True,
activated=True,
activated_on=datetime.datetime.now()
))
db.session.commit()
@cli.command()
def create_data():
"""Creates sample data."""
user = User(
name="Example User",
email="example@railstutorial.org",
password="foobar",
admin=False,
activated=True,
activated_on=datetime.datetime.now()
)
db.session.add(user)
names = [user.name]
for n in range(99):
fake = Faker()
while len(fake.name()) < 51 and fake.name() in names:
fake = Faker()
name = fake.name()
names.append(name)
email = f"example-{n+1}@railstutorial.org"
password = "password"
db.session.add(User(
name=name,
email=email,
password=password,
admin=False,
activated=True,
activated_on=datetime.datetime.now()
))
db.session.commit()
users = User.query.limit(6)
for user in users:
fake = Faker()
content = fake.sentence()
db.session.add(
Micropost(content=content, user_id=user.id)
)
db.session.commit()
users = User.query.all()
user = users[0]
following = users[2:50]
followers = users[3:40]
for followed in following:
user.follow(followed)
for follower in followers:
follower.follow(user)
@cli.command()
def test():
"""Runs the unit tests without test coverage."""
tests = unittest.TestLoader().discover("project/tests", pattern="test*.py")
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)
@cli.command()
def cov():
"""Runs the unit tests with coverage."""
tests = unittest.TestLoader().discover("project/tests")
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
COV.stop()
COV.save()
print("Coverage Summary:")
COV.report()
COV.html_report()
COV.erase()
sys.exit(0)
else:
sys.exit(1)
@cli.command()
def flake():
"""Runs flake8 on the project."""
subprocess.run(["flake8", "project"])
if __name__ == "__main__":
cli()