Skip to content

Commit 16c1ea8

Browse files
committed
improvements on unit test
1 parent 550f71e commit 16c1ea8

File tree

7 files changed

+31
-21
lines changed

7 files changed

+31
-21
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ migrate_db:
1919
start:
2020
python run.py runserver
2121

22+
test: export STAGE=test
23+
test:
24+
rm -Rf migrations/
25+
python run.py db init
26+
python run.py db migrate
27+
python run.py db upgrade
28+
python -m utils.scripts.insertData
29+
2230
test:
23-
ENV=test
2431
PYTHONPATH=. pytest

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URI', default)
1616
SQLALCHEMY_TEST_DATABASE_URI = os.getenv('DATABASE_TEST_URI', default)
1717

18-
if os.environ.get("ENV", None) == "test":
18+
if os.environ.get("STAGE", None) == "test":
1919
SQLALCHEMY_DATABASE_URI = SQLALCHEMY_TEST_DATABASE_URI
2020

2121
SQLALCHEMY_TRACK_MODIFICATIONS = True

tests/conftest.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,36 @@
77

88
from config import BASE_DIR
99

10-
@pytest.fixture
11-
def app():
12-
return _app
1310

14-
@pytest.fixture
15-
def db(app):
16-
with app.app_context():
11+
def pytest_sessionstart(session):
12+
with _app.app_context():
1713
_db.drop_all()
1814
_db.create_all()
1915

20-
session = db.session()
21-
sql_file = open(BASE_DIR + "/utils/scripts/db/dml.sql", "r")
16+
session = _db.session()
17+
18+
sql_file = open(BASE_DIR + "/utils/scripts/db/cidade_uf.sql", "r")
2219
escaped_sql = text(sql_file.read())
2320
session.execute(escaped_sql)
2421
session.flush()
2522

26-
sql_file = open(BASE_DIR + "/utils/scripts/db/tests.sql", "r")
23+
sql_file = open(BASE_DIR + "/utils/scripts/db/rules.sql", "r")
2724
escaped_sql = text(sql_file.read())
2825
session.execute(escaped_sql)
2926
session.commit()
3027

3128
session.close()
32-
yield _db
33-
_db.drop_all()
34-
_db.session.commit()
3529

3630

31+
def pytest_sessionfinish(session, exitstatus):
32+
_db.drop_all()
33+
_db.session.commit()
34+
35+
36+
@pytest.fixture
37+
def app():
38+
return _app
39+
3740

3841
@pytest.fixture
3942
def admin_login(app):

tests/test_cadastro_controller.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pprint import pprint
66

7-
def test_get_perfis(app, db, admin_login):
7+
def test_get_perfis(app, admin_login):
88
with app.app_context():
99
client = app.test_client()
1010

@@ -14,7 +14,7 @@ def test_get_perfis(app, db, admin_login):
1414
assert not output["error"]
1515
assert response.status_code == 200
1616

17-
def test_post_perfil(app, db, admin_login):
17+
def test_post_perfil(app, admin_login):
1818
with app.app_context():
1919
client = app.test_client()
2020

@@ -24,7 +24,7 @@ def test_post_perfil(app, db, admin_login):
2424
assert not output["error"]
2525
assert response.status_code == 200
2626

27-
def test_view_perfil(app, db, admin_login):
27+
def test_view_perfil(app, admin_login):
2828
with app.app_context():
2929
client = app.test_client()
3030

tests/test_user_controller.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pprint import pprint
66

7-
def test_get_usuarios(app, db, admin_login):
7+
def test_get_usuarios(app, admin_login):
88
with app.app_context():
99
client = app.test_client()
1010

@@ -14,7 +14,7 @@ def test_get_usuarios(app, db, admin_login):
1414
assert not output["error"]
1515
assert response.status_code == 200
1616

17-
def test_post_usuario(app, db, admin_login):
17+
def test_post_usuario(app, admin_login):
1818
with app.app_context():
1919
client = app.test_client()
2020

@@ -24,7 +24,7 @@ def test_post_usuario(app, db, admin_login):
2424
assert not output["error"]
2525
assert response.status_code == 200
2626

27-
def test_view_usuario(app, db, admin_login, created_login):
27+
def test_view_usuario(app, admin_login, created_login):
2828
with app.app_context():
2929
client = app.test_client()
3030

File renamed without changes.

utils/scripts/insertData.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def insert(filePath, output):
2929

3030
insert("db/cidade_uf.sql", output)
3131

32-
insert("db/dml_acl.sql", output)
32+
insert("db/rules.sql", output)
3333

3434
try:
3535
db.session.commit()

0 commit comments

Comments
 (0)