Skip to content

Commit a2898a8

Browse files
author
EliuX
committed
Refactor codebase to use solution of #25
1 parent 80c41eb commit a2898a8

22 files changed

+440
-179
lines changed

cli.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
from flask_script import Manager
77

88
from time_tracker_api import create_app
9-
from time_tracker_api.api import api
109

1110
app = create_app()
11+
12+
from time_tracker_api.api import api
13+
1214
cli_manager = Manager(app)
1315

1416

@@ -44,6 +46,23 @@ def gen_postman_collection(filename='timetracker-api-postman-collection.json',
4446
save_data(parsed_json, filename)
4547

4648

49+
@cli_manager.command
50+
def seed():
51+
from time_tracker_api.database import seeder as seed
52+
seed()
53+
54+
55+
@cli_manager.command
56+
def re_create_db():
57+
print('This command will drop all tables and seed again the database')
58+
confirm_answer = input('Do you confirm (Y) you want to remove all your data?\n')
59+
if confirm_answer.upper() == 'Y':
60+
from time_tracker_api.database import seeder
61+
seeder.fresh()
62+
else:
63+
print('\nThe process was cancelled!')
64+
65+
4766
def save_data(data: str, filename: str) -> None:
4867
""" Save text content to a file """
4968
if filename:

requirements/dev.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@ pytest-mock==2.0.0
1212
Faker==4.0.2
1313

1414
# Coverage
15-
coverage==4.5.1
16-
17-
# SQL database (MS SQL)
18-
flask_sqlalchemy==2.4.1
15+
coverage==4.5.1

requirements/prod.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ Jinja2==2.11.1
1313
gunicorn==20.0.4
1414

1515
#Swagger support for Restful API
16-
flask-restplus==0.13.0
16+
flask-restplus==0.12.1
1717

1818
#CLI support
19-
Flask-Script==2.0.6
19+
Flask-Script==2.0.6
20+
21+
# SQL database (MS SQL)
22+
flask_sqlalchemy==2.4.1
23+
24+
# Handling requests
25+
requests==2.23.0

tests/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def client(app: Flask) -> FlaskClient:
2424
@pytest.fixture(scope="module")
2525
def sql_repository(app: Flask):
2626
with app.app_context():
27-
from .resources import TestModel
27+
from .resources import SQLPersonModel
28+
from time_tracker_api.database import seeder
2829
from time_tracker_api.sql_repository import db
2930

30-
db.create_all()
31-
print("Models for test created!")
31+
seeder.fresh()
3232

3333
from time_tracker_api.sql_repository import SQLRepository
34-
yield SQLRepository(TestModel)
34+
yield SQLRepository(SQLPersonModel)
3535

36-
print("Models for test removed!")
3736
db.drop_all()
37+
print("Models for test removed!")
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
from flask import json
2+
from flask.testing import FlaskClient
3+
from pytest_mock import MockFixture
24

35

4-
def test_list_should_return_nothing(client):
5-
"""Should return an empty array"""
6+
def test_list_all_elements(client: FlaskClient, mocker: MockFixture):
7+
"""Should return all elements in a list"""
8+
from time_tracker_api.projects.projects_namespace import project_dao
9+
repository_find_all_mock = mocker.patch.object(project_dao.repository, 'find_all', return_value=[])
10+
611
response = client.get("/projects", follow_redirects=True)
712

813
assert 200 == response.status_code
914

1015
json_data = json.loads(response.data)
1116
assert [] == json_data
17+
repository_find_all_mock.assert_called_once()

tests/resources.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from time_tracker_api.sql_repository import db
1+
from time_tracker_api.database import AuditedModel
2+
from time_tracker_api.sql_repository import db, SQLAuditedModel
23

34

4-
class TestModel(db.Model):
5+
class SQLPersonModel(db.Model, SQLAuditedModel):
6+
__tablename__ = 'tests'
57
id = db.Column(db.Integer, primary_key=True)
6-
name = db.Column(db.String(80), unique=True, nullable=False)
8+
name = db.Column(db.String(80), unique=False, nullable=False)
79
email = db.Column(db.String(120), unique=True, nullable=False)
810
age = db.Column(db.Integer, nullable=False)
911

tests/smoke_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
def test_app_exists(app):
32
"""Does app exists"""
43
assert app is not None

tests/sql_repository_test.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88

99
def test_create(sql_repository):
1010
"""Should create a new Entry"""
11-
from .resources import TestModel
1211
global sample_element
13-
sample_element = TestModel(name=fake.name(),
14-
email=fake.safe_email(),
15-
age=fake.pyint(min_value=10, max_value=80))
12+
sample_element = dict(name=fake.name(),
13+
email=fake.safe_email(),
14+
age=fake.pyint(min_value=10, max_value=80))
1615

1716
result = sql_repository.create(sample_element)
1817

1918
assert result is not None
2019
assert result.id is not None
20+
assert result.created_at is not None
21+
assert result.created_by is not None
22+
assert result.updated_at is None
23+
assert result.updated_by is None
2124

2225
existing_elements_registry.append(result)
2326

@@ -43,6 +46,9 @@ def test_update(sql_repository):
4346
assert updated_element.id == existing_element.id
4447
assert updated_element.name == "Jon Snow"
4548
assert updated_element.age == 34
49+
assert updated_element.updated_at is not None
50+
assert updated_element.updated_at > updated_element.created_at
51+
assert updated_element.updated_by is not None
4652

4753

4854
def test_find_all(sql_repository):
@@ -54,10 +60,10 @@ def test_find_all(sql_repository):
5460

5561
def test_find_all_that_contains_property_with_string(sql_repository):
5662
"""Find all elements that have a property that partially contains a string (case-insensitive)"""
57-
from .resources import TestModel
58-
new_element = TestModel(name='Ramsay Snow',
59-
email=fake.safe_email(),
60-
age=fake.pyint(min_value=10, max_value=80))
63+
fake_name = fake.name()
64+
new_element = dict(name="%s Snow" % fake_name,
65+
email=fake.safe_email(),
66+
age=fake.pyint(min_value=10, max_value=80))
6167
sql_repository.create(new_element)
6268
existing_elements_registry.append(new_element)
6369

@@ -67,8 +73,8 @@ def test_find_all_that_contains_property_with_string(sql_repository):
6773
search_jon_result = sql_repository.find_all_contain_str('name', 'Jon')
6874
assert len(search_jon_result) == 1
6975

70-
search_ram_result = sql_repository.find_all_contain_str('name', 'RAM')
71-
assert search_ram_result[0] is new_element
76+
search_ram_result = sql_repository.find_all_contain_str('name', fake_name)
77+
assert search_ram_result[0].name == new_element['name']
7278

7379

7480
def test_delete_existing_element(sql_repository):

tests/time_entries/__init__.py

Whitespace-only changes.

tests/time_entries/time_entries_namespace_test.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)