Skip to content

Commit

Permalink
Merge pull request #33 from stanislawK/26_tests_for_models_and_db_2
Browse files Browse the repository at this point in the history
[26]tests for models and db
  • Loading branch information
w1stler authored May 29, 2019
2 parents e4777cf + 99dd8fc commit dd43c6d
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ stop: ## Stop the environment

bash: ## Go to the backend container
docker exec -ti codeforpoznanpl_v3_backend_1 bash
psql: ## Go to the db and make SQL queries

psql: ## Go to the db and make SQL queries
docker exec -ti codeforpoznanpl_v3_db_1 psql -U cfp_v3
53 changes: 53 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import datetime
import tempfile

import pytest
Expand Down Expand Up @@ -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
59 changes: 59 additions & 0 deletions backend/tests/test_db.py
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)
39 changes: 39 additions & 0 deletions backend/tests/test_models.py
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)

0 comments on commit dd43c6d

Please sign in to comment.