-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from stanislawK/26_tests_for_models_and_db_2
[26]tests for models and db
- Loading branch information
Showing
4 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,5 @@ | ||
import os | ||
import datetime | ||
import tempfile | ||
|
||
import pytest | ||
|
@@ -28,3 +29,55 @@ def app(): | |
# close and remove the temporary db | ||
os.close(db_fd) | ||
os.unlink(db_path) | ||
|
||
|
||
@pytest.fixture | ||
def client(app): | ||
"""A test client for the app.""" | ||
return app.test_client() | ||
|
||
|
||
@pytest.fixture | ||
def _db(): | ||
"""Create and configure a new db instance for pytest-flask-sqlalchemy.""" | ||
db_fd, db_path = tempfile.mkstemp() | ||
app = create_app() | ||
app.config['TESTING'] = True | ||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
app.config['DATABASE'] = db_path | ||
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://" | ||
|
||
with app.app_context(): | ||
db.init_app(app) | ||
db.create_all() | ||
|
||
yield db | ||
|
||
os.close(db_fd) | ||
os.unlink(db_path) | ||
|
||
|
||
@pytest.fixture | ||
def new_user(): | ||
user = { | ||
'username': 'TestName', | ||
'password': 'TestPassword' | ||
} | ||
return user | ||
|
||
|
||
@pytest.fixture | ||
def new_participant(): | ||
participant = { | ||
'name': 'Jon', | ||
'lastname': 'Doe', | ||
'email': '[email protected]', | ||
'phone': '123456789' | ||
} | ||
return participant | ||
|
||
|
||
@pytest.fixture | ||
def new_hacknight(): | ||
hacknight = {'date': datetime.date(2000, 10, 10)} | ||
return hacknight |
This file contains 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,59 @@ | ||
import datetime | ||
import pytest | ||
|
||
from backend.models import User, Participant, Hacknight | ||
|
||
|
||
def test_add_new_user_to_db(_db, new_user): | ||
"""Test adding user to DB with valid data.""" | ||
|
||
db = _db | ||
assert len(db.session.query(User).all()) == 0 | ||
new_user = User( | ||
username=new_user['username'], | ||
password=new_user['password'] | ||
) | ||
db.session.add(new_user) | ||
db.session.commit() | ||
|
||
user = db.session.query(User).filter_by(username="TestName").first() | ||
|
||
assert user.username == "TestName" | ||
assert user.check_password('TestPassword') | ||
|
||
|
||
def test_add_new_participant_to_db(_db, new_participant): | ||
"""Test addind participant do DB with valid data.""" | ||
|
||
db = _db | ||
assert len(db.session.query(Participant).all()) == 0 | ||
|
||
new_participant = Participant( | ||
name=new_participant['name'], | ||
lastname=new_participant['lastname'], | ||
email=new_participant['email'], | ||
phone=new_participant['phone'] | ||
) | ||
db.session.add(new_participant) | ||
db.session.commit() | ||
|
||
participant = db.session.query(Participant).filter_by(name="Jon").first() | ||
|
||
assert participant.name == 'Jon' | ||
assert participant.lastname == 'Doe' | ||
assert participant.email == '[email protected]' | ||
assert participant.phone == '123456789' | ||
|
||
|
||
def test_add_new_hacknight_to_db(_db, new_hacknight): | ||
"""Test addind hacknight do DB with valid data.""" | ||
db = _db | ||
assert len(db.session.query(Hacknight).all()) == 0 | ||
|
||
new_hacknight = Hacknight(date=new_hacknight['date']) | ||
db.session.add(new_hacknight) | ||
db.session.commit() | ||
|
||
hacknight = db.session.query(Hacknight).all()[0] | ||
|
||
assert hacknight.date == datetime.date(2000, 10, 10) |
This file contains 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,39 @@ | ||
import datetime | ||
import pytest | ||
|
||
from backend.models import User, Participant, Hacknight | ||
|
||
|
||
def test_create_new_user(new_user): | ||
"""Test user model.""" | ||
|
||
new_user = User( | ||
username=new_user['username'], | ||
password=new_user['password'] | ||
) | ||
|
||
assert new_user.username == 'TestName' | ||
assert new_user.check_password('TestPassword') | ||
|
||
|
||
def test_create_new_participant(new_participant): | ||
"""Test participant model.""" | ||
new_participant = Participant( | ||
name=new_participant['name'], | ||
lastname=new_participant['lastname'], | ||
email=new_participant['email'], | ||
phone=new_participant['phone'] | ||
) | ||
|
||
assert new_participant.name == 'Jon' | ||
assert new_participant.lastname == 'Doe' | ||
assert new_participant.email == '[email protected]' | ||
assert new_participant.phone == '123456789' | ||
|
||
|
||
def test_create_new_hacknight(new_hacknight): | ||
"""Test hacknight model.""" | ||
|
||
new_hacknight = Hacknight(date=new_hacknight['date']) | ||
|
||
assert new_hacknight.date == datetime.date(2000, 10, 10) |