-
Notifications
You must be signed in to change notification settings - Fork 0
Creates repository sql database#25 #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,34 @@ | ||
| import pytest | ||
| from _pytest.fixtures import FixtureRequest | ||
| from flask import Flask | ||
| from flask.testing import FlaskClient | ||
|
|
||
| from time_tracker_api import create_app | ||
|
|
||
| CONFIGURATIONS = ['AzureSQLDatabaseDevelopTestConfig'] | ||
|
|
||
| @pytest.fixture(scope='session') | ||
| def app() -> Flask: | ||
| """An instance of the app for tests""" | ||
| return create_app() | ||
|
|
||
| @pytest.fixture(scope='session', params=CONFIGURATIONS) | ||
| def app(request: FixtureRequest) -> Flask: | ||
| return create_app("time_tracker_api.config.%s" % request.param) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(app: Flask) -> FlaskClient: | ||
| """A test client for the app.""" | ||
| with app.test_client() as c: | ||
| return c | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
enriquezrene marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def sql_repository(): | ||
| from .resources import PersonSQLModel | ||
| from time_tracker_api.database import seeder | ||
| from time_tracker_api.sql_repository import db | ||
|
|
||
| seeder.fresh() | ||
|
|
||
| from time_tracker_api.sql_repository import SQLRepository | ||
| yield SQLRepository(PersonSQLModel) | ||
|
|
||
| db.drop_all() | ||
| print("Models for test removed!") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,16 @@ | ||
| from flask import json | ||
| from flask.testing import FlaskClient | ||
| from pytest_mock import MockFixture | ||
|
|
||
|
|
||
| def test_list_should_return_nothing(client): | ||
| """Should return an empty array""" | ||
| def test_list_all_elements(client: FlaskClient, mocker: MockFixture): | ||
enriquezrene marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from time_tracker_api.projects.projects_namespace import project_dao | ||
| repository_find_all_mock = mocker.patch.object(project_dao.repository, 'find_all', return_value=[]) | ||
|
|
||
| response = client.get("/projects", follow_redirects=True) | ||
|
|
||
| assert 200 == response.status_code | ||
|
|
||
| json_data = json.loads(response.data) | ||
| assert [] == json_data | ||
| repository_find_all_mock.assert_called_once() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from time_tracker_api.sql_repository import db, AuditedSQLModel | ||
|
|
||
|
|
||
| class PersonSQLModel(db.Model, AuditedSQLModel): | ||
| __tablename__ = 'tests' | ||
| id = db.Column(db.Integer, primary_key=True) | ||
| name = db.Column(db.String(80), unique=False, nullable=False) | ||
| email = db.Column(db.String(120), unique=True, nullable=False) | ||
| age = db.Column(db.Integer, nullable=False) | ||
|
|
||
| def __repr__(self): | ||
| return '<Test Model %r>' % self.name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,2 @@ | ||
|
|
||
| def test_app_exists(app): | ||
| """Does app exists""" | ||
| assert app is not None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from faker import Faker | ||
|
|
||
| fake = Faker() | ||
|
|
||
| existing_elements_registry = [] | ||
| last_deleted_element = None | ||
|
|
||
|
|
||
| def test_create(sql_repository): | ||
| global sample_element | ||
| sample_element = dict(name=fake.name(), | ||
| email=fake.safe_email(), | ||
| age=fake.pyint(min_value=10, max_value=80)) | ||
|
|
||
| result = sql_repository.create(sample_element) | ||
|
|
||
| assert result is not None | ||
| assert result.id is not None | ||
| assert result.created_at is not None | ||
| assert result.created_by is not None | ||
| assert result.updated_at is None | ||
| assert result.updated_by is None | ||
|
|
||
| existing_elements_registry.append(result) | ||
|
|
||
|
|
||
| def test_find(sql_repository): | ||
| existing_element = existing_elements_registry[0] | ||
enriquezrene marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| found_element = sql_repository.find(existing_element.id) | ||
|
|
||
| assert found_element is not None | ||
| assert found_element.id is not None | ||
|
|
||
|
|
||
| def test_update(sql_repository): | ||
| existing_element = existing_elements_registry[0] | ||
|
|
||
| updated_element = sql_repository.update(existing_element.id, | ||
| dict(name="Jon Snow", age=34)) | ||
|
|
||
| assert updated_element is not None | ||
| assert updated_element.id == existing_element.id | ||
| assert updated_element.name == "Jon Snow" | ||
| assert updated_element.age == 34 | ||
enriquezrene marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert updated_element.updated_at is not None | ||
| assert updated_element.updated_at > updated_element.created_at | ||
| assert updated_element.updated_by is not None | ||
|
|
||
|
|
||
| def test_find_all(sql_repository): | ||
| existing_elements = sql_repository.find_all() | ||
|
|
||
| assert all(e in existing_elements_registry for e in existing_elements) | ||
|
|
||
|
|
||
| def test_find_all_that_contains_property_with_string_case_insensitive(sql_repository): | ||
| fake_name = fake.name() | ||
| new_element = dict(name="%s Snow" % fake_name, | ||
| email=fake.safe_email(), | ||
| age=fake.pyint(min_value=10, max_value=80)) | ||
| sql_repository.create(new_element) | ||
| existing_elements_registry.append(new_element) | ||
|
|
||
| search_snow_result = sql_repository.find_all_contain_str('name', 'Snow') | ||
| assert len(search_snow_result) == 2 | ||
|
|
||
| search_jon_result = sql_repository.find_all_contain_str('name', 'Jon') | ||
| assert len(search_jon_result) == 1 | ||
|
|
||
| search_ram_result = sql_repository.find_all_contain_str('name', fake_name) | ||
| assert search_ram_result[0].name == new_element['name'] | ||
|
|
||
|
|
||
| def test_delete_existing_element(sql_repository): | ||
| existing_element = existing_elements_registry[0] | ||
|
|
||
| result = sql_repository.remove(existing_element.id) | ||
|
|
||
| assert result is None | ||
|
|
||
| global last_deleted_model | ||
| last_deleted_model = existing_elements_registry.pop() | ||
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.