-
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 1 commit
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
Next
Next commit
Setup SQL Alchemny and tests for sql repository
- Loading branch information
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
| 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: | ||
|
|
||
| @pytest.fixture(scope='session', params=CONFIGURATIONS) | ||
| def app(request: FixtureRequest) -> Flask: | ||
| """An instance of the app for tests""" | ||
| return create_app() | ||
| 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() | ||
| def repository(app: Flask): | ||
| with app.app_context(): | ||
| from .resources import TestModel | ||
| from time_tracker_api.sql_repository import db | ||
|
|
||
| db.create_all() | ||
| print("Models for test created!") | ||
|
|
||
| from time_tracker_api.sql_repository import SQLRepository | ||
| return SQLRepository(TestModel) |
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,11 @@ | ||
| from time_tracker_api.sql_repository import db | ||
|
|
||
|
|
||
| class TestModel(db.Model): | ||
| id = db.Column(db.Integer, primary_key=True) | ||
| name = db.Column(db.String(80), unique=True, 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from faker import Faker | ||
| from flask import Flask | ||
|
|
||
| fake = Faker() | ||
|
|
||
|
|
||
| def test_create_entry(repository, app: Flask): | ||
| """Should create a new Entry""" | ||
| assert repository is not None | ||
| from .resources import TestModel | ||
| sample_model = TestModel(name=fake.name(), | ||
| email=fake.safe_email(), | ||
| age = fake.pyint(min_value=10, max_value=80)) | ||
|
|
||
| repository.create(sample_model) | ||
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,12 +1,27 @@ | ||
| import os | ||
|
|
||
|
|
||
| class Config: | ||
| DEBUG = False | ||
|
|
||
|
|
||
| class WhateverDevelopConfig(Config): | ||
| class DevelopConfig(Config): | ||
| DEBUG = True | ||
| FLASK_DEBUG = True | ||
| FLASK_ENV = "develop" | ||
| DATABASE = "whatever" | ||
| DATABASE_URI = os.environ.get('DATABASE_URI') | ||
|
|
||
|
|
||
| class AzureSQLDatabaseDevelopConfig(DevelopConfig): | ||
| DATABASE = 'sql' | ||
| TEST_TABLE = 'tests' | ||
| SQLALCHEMY_COMMIT_ON_TEARDOWN = True | ||
| SQLALCHEMY_TRACK_MODIFICATIONS = False | ||
|
|
||
|
|
||
| class AzureSQLDatabaseDevelopTestConfig(AzureSQLDatabaseDevelopConfig): | ||
| DEBUG = True | ||
| TESTING = True | ||
|
|
||
|
|
||
| DefaultConfig = WhateverDevelopConfig | ||
| DefaultConfig = AzureSQLDatabaseDevelopConfig |
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import urllib.parse | ||
|
|
||
| from flask import Flask | ||
| from flask_sqlalchemy import SQLAlchemy | ||
|
|
||
| db = None | ||
|
|
||
|
|
||
| def init_app(app: Flask) -> None: | ||
| app.config['SQLALCHEMY_DATABASE_URI'] = app.config['DATABASE_URI'] | ||
| global db | ||
| db = SQLAlchemy(app) | ||
|
|
||
|
|
||
| class SQLRepository(): | ||
| def __init__(self, model_type: type): | ||
| self.model_type = model_type | ||
|
|
||
| def create(self, model: dict) -> dict: | ||
| db.session.add(model) | ||
| db.session.commit() | ||
|
|
||
| def find_all(self): | ||
| return self.model_type.query.all() | ||
|
|
||
|
|
||
| repository_model = SQLRepository |
This file was deleted.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not naming the function/method name as
test_creates_a_new_entryThen you can get rid of comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function just tests the function create of the repository, this is a simple and unique name. As I explained before the comment in the function is providing a verbose description of the test.