From c79d9a0ebb71547558345a9f1607cb4873ba0738 Mon Sep 17 00:00:00 2001 From: stanislawK Date: Sun, 17 Feb 2019 12:52:17 +0100 Subject: [PATCH 1/3] added initial test for models, and db --- backend/requirements.txt | 1 + backend/tests/conftest.py | 49 +++++++++++++++++++++++++++++ backend/tests/test_db.py | 60 ++++++++++++++++++++++++++++++++++++ backend/tests/test_models.py | 45 +++++++++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 backend/tests/test_db.py create mode 100644 backend/tests/test_models.py diff --git a/backend/requirements.txt b/backend/requirements.txt index b32a0d3c..89fca973 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -4,3 +4,4 @@ Flask-Migrate==2.3.0 Flask-Script==2.0.6 psycopg2==2.7.6 pytest==4.1.0 +pytest-flask-sqlalchemy==1.0.0 diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index c39abda1..553bbb5c 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -1,4 +1,5 @@ import os +import datetime import tempfile import pytest @@ -28,3 +29,51 @@ 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': 'test@test.com', + 'phone': '123456789'} + return participant + + +@pytest.fixture +def new_hacknight(): + hacknight = {'date': datetime.date(2000, 10, 10)} + return hacknight diff --git a/backend/tests/test_db.py b/backend/tests/test_db.py new file mode 100644 index 00000000..0d7a8ca5 --- /dev/null +++ b/backend/tests/test_db.py @@ -0,0 +1,60 @@ +import datetime +import pytest + +from backend.models import User, Participant, Hacknight + + +def test_add_new_user_to_db(db_session, new_user): + """ + GIVEN mocked db session, and user data + WHEN a new user added to db + THEN check the user data from 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) + + user = db_session.query(User).filter_by(username="TestName").first() + + assert user.username == "TestName" + assert user.password == "TestPassword" + + +def test_add_new_participant_to_db(db_session, new_participant): + """ + GIVEN mocked db session, and participant data + WHEN a new participant added to db + THEN check the participant data from 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) + + participant = db_session.query(Participant).filter_by(name="Jon").first() + + assert participant.name == 'Jon' + assert participant.lastname == 'Doe' + assert participant.email == 'test@test.com' + assert participant.phone == '123456789' + + +def test_add_new_hacknight_to_db(db_session, new_hacknight): + """ + GIVEN mocked db session, and hacknight data + WHEN a new hacknight added to db + THEN check the hacknight data from db + """ + assert len(db_session.query(Hacknight).all()) == 0 + + new_hacknight = Hacknight(date=new_hacknight['date']) + db_session.add(new_hacknight) + + hacknight = db_session.query(Hacknight).all()[0] + + assert hacknight.date == datetime.date(2000, 10, 10) diff --git a/backend/tests/test_models.py b/backend/tests/test_models.py new file mode 100644 index 00000000..88349ae6 --- /dev/null +++ b/backend/tests/test_models.py @@ -0,0 +1,45 @@ +import datetime +import pytest + +from backend.models import User, Participant, Hacknight + + +def test_create_new_user(new_user): + """ + GIVEN a User data + WHEN a new User is created + THEN check the username and password + """ + new_user = User(username=new_user['username'], + password=new_user['password']) + + assert new_user.username == 'TestName' + assert new_user.password == 'TestPassword' + + +def test_create_new_participant(new_participant): + """ + GIVEN a Participant data + WHEN a new Participant is created + THEN check the fullname, email and phone + """ + 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 == 'test@test.com' + assert new_participant.phone == '123456789' + + +def test_create_new_hacknight(new_hacknight): + """ + GIVEN a Hacknight data + WHEN a new Hacknight is created + THEN check the hacknight date + """ + new_hacknight = Hacknight(date=new_hacknight['date']) + + assert new_hacknight.date == datetime.date(2000, 10, 10) From f6aface64d60ed5dede2a7f4386e3fcac39b555b Mon Sep 17 00:00:00 2001 From: stanislawK Date: Wed, 22 May 2019 20:24:42 +0200 Subject: [PATCH 2/3] chenged intentations and dockstrings --- Makefile | 4 ++-- backend/tests/conftest.py | 18 +++++++++++------- backend/tests/test_db.py | 26 +++++++++----------------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index c8754629..0957efbc 100644 --- a/Makefile +++ b/Makefile @@ -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 - docker exec -ti codeforpoznanpl_v3_db_1 psql -U cfp_v3 + docker exec -ti codeforpoznanpl_v3_db_1 psql -U cfp_v3 diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 553bbb5c..0cb576c7 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -39,7 +39,7 @@ def client(app): @pytest.fixture def _db(): - """Create and configure a new db instance for pytest-flask-sqlalchemy""" + """Create and configure a new db instance for pytest-flask-sqlalchemy.""" db_fd, db_path = tempfile.mkstemp() app = create_app() app.config['TESTING'] = True @@ -59,17 +59,21 @@ def _db(): @pytest.fixture def new_user(): - user = {'username': 'TestName', - 'password': 'TestPassword'} + user = { + 'username': 'TestName', + 'password': 'TestPassword' + } return user @pytest.fixture def new_participant(): - participant = {'name': 'Jon', - 'lastname': 'Doe', - 'email': 'test@test.com', - 'phone': '123456789'} + participant = { + 'name': 'Jon', + 'lastname': 'Doe', + 'email': 'test@test.com', + 'phone': '123456789' + } return participant diff --git a/backend/tests/test_db.py b/backend/tests/test_db.py index 52e036d5..18627b0f 100644 --- a/backend/tests/test_db.py +++ b/backend/tests/test_db.py @@ -5,15 +5,14 @@ def test_add_new_user_to_db(_db, new_user): - """ - GIVEN mocked db session, and user data - WHEN a new user added to db - THEN check the user data from db - """ + """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']) + new_user = User( + username=new_user['username'], + password=new_user['password'] + ) db.session.add(new_user) db.session.commit() @@ -24,11 +23,8 @@ def test_add_new_user_to_db(_db, new_user): def test_add_new_participant_to_db(_db, new_participant): - """ - GIVEN mocked db session, and participant data - WHEN a new participant added to db - THEN check the participant data from db - """ + """Test addind participant do DB with valid data.""" + db = _db assert len(db.session.query(Participant).all()) == 0 @@ -48,11 +44,7 @@ def test_add_new_participant_to_db(_db, new_participant): def test_add_new_hacknight_to_db(_db, new_hacknight): - """ - GIVEN mocked db session, and hacknight data - WHEN a new hacknight added to db - THEN check the hacknight data from db - """ + """Test addind hacknight do DB with valid data.""" db = _db assert len(db.session.query(Hacknight).all()) == 0 From df8ffa7d1d3b20c9493ddde05a7bcbff3776b7cd Mon Sep 17 00:00:00 2001 From: stanislawK Date: Wed, 22 May 2019 20:32:15 +0200 Subject: [PATCH 3/3] more indentations and dockstring fixes --- backend/tests/test_db.py | 10 ++++++---- backend/tests/test_models.py | 36 +++++++++++++++--------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/backend/tests/test_db.py b/backend/tests/test_db.py index 18627b0f..4d980ad4 100644 --- a/backend/tests/test_db.py +++ b/backend/tests/test_db.py @@ -28,10 +28,12 @@ def test_add_new_participant_to_db(_db, new_participant): 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']) + 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() diff --git a/backend/tests/test_models.py b/backend/tests/test_models.py index 99ecbea7..b999601c 100644 --- a/backend/tests/test_models.py +++ b/backend/tests/test_models.py @@ -5,28 +5,25 @@ def test_create_new_user(new_user): - """ - GIVEN a User data - WHEN a new User is created - THEN check the username and password - """ - new_user = User(username=new_user['username'], - password=new_user['password']) + """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): - """ - GIVEN a Participant data - WHEN a new Participant is created - THEN check the fullname, email and phone - """ - new_participant = Participant(name=new_participant['name'], - lastname=new_participant['lastname'], - email=new_participant['email'], - phone=new_participant['phone']) + """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' @@ -35,11 +32,8 @@ def test_create_new_participant(new_participant): def test_create_new_hacknight(new_hacknight): - """ - GIVEN a Hacknight data - WHEN a new Hacknight is created - THEN check the hacknight date - """ + """Test hacknight model.""" + new_hacknight = Hacknight(date=new_hacknight['date']) assert new_hacknight.date == datetime.date(2000, 10, 10)