diff --git a/fixtures/location/all_locations_fixtures.py b/fixtures/location/all_locations_fixtures.py index 9ad244615..7f2e6f65a 100644 --- a/fixtures/location/all_locations_fixtures.py +++ b/fixtures/location/all_locations_fixtures.py @@ -50,6 +50,11 @@ } ] }, + { + "name": "Kigali", + "abbreviation": "KGL", + "rooms": [] + }, { "name": "Lagos", "abbreviation": "LOS", @@ -112,6 +117,7 @@ }, ]}, {'rooms': []}, + {'rooms': []}, {'rooms': []} ] } diff --git a/fixtures/response/room_response_fixture.py b/fixtures/response/room_response_fixture.py index d73de27c6..783e3df43 100644 --- a/fixtures/response/room_response_fixture.py +++ b/fixtures/response/room_response_fixture.py @@ -2,9 +2,11 @@ null = None -start_date = (datetime.date.today() - datetime.timedelta( +created_at = 'Nov 30 2019' +created_at_datetime = datetime.datetime.strptime(created_at, '%b %d %Y') +start_date = (created_at_datetime - datetime.timedelta( days=2)).strftime('%b %d %Y') -end_date = datetime.date.today().strftime('%b %d %Y') +end_date = created_at_datetime.strftime('%b %d %Y') invalid_start_date = (datetime.date.today() + datetime.timedelta( days=10)).strftime('%b %d %Y') invalid_end_date = (datetime.date.today() + datetime.timedelta( diff --git a/test_data/devices_table.json b/test_data/devices_table.json new file mode 100644 index 000000000..018dd39a0 --- /dev/null +++ b/test_data/devices_table.json @@ -0,0 +1,14 @@ +{ + "table_name": "devices", + "data": [ + { + "last_seen":"2018-06-08T11:17:58.785136", + "date_added":"2018-06-08T11:17:58.785136", + "name":"Samsung", + "location":"Kampala", + "device_type":"External Display", + "room_id":1, + "state":"active" + } + ] +} diff --git a/test_data/events_table.json b/test_data/events_table.json new file mode 100644 index 000000000..7103c9201 --- /dev/null +++ b/test_data/events_table.json @@ -0,0 +1,15 @@ +{ + "table_name": "events", + "data": [ + { + "event_id":"test_id5", + "room_id":1, + "event_title":"Onboarding", + "start_time":"2018-07-11T09:00:00Z", + "end_time":"2018-07-11T09:45:00Z", + "number_of_participants":4, + "checked_in":false, + "cancelled":false + } + ] +} diff --git a/test_data/locations_table.json b/test_data/locations_table.json new file mode 100644 index 000000000..3074ee2b2 --- /dev/null +++ b/test_data/locations_table.json @@ -0,0 +1,21 @@ +{ + "table_name": "locations", + "data": [ + { + "name":"Kampala", + "abbreviation":"KLA" + }, + { + "name": "Nairobi", + "abbreviation": "NBO" + }, + { + "name": "Lagos", + "abbreviation": "LOS" + }, + { + "name": "Kigali", + "abbreviation": "KGL" + } + ] +} diff --git a/test_data/office_structures_table.json b/test_data/office_structures_table.json new file mode 100644 index 000000000..f014c6539 --- /dev/null +++ b/test_data/office_structures_table.json @@ -0,0 +1,18 @@ +{ + "table_name": "structures", + "data": [ + { + "id":"C56A4180-65AA-42EC-A945-5FD21DEC0518", + "name":"Epic Tower", + "tag":"Lagos Building", + "location_id":1 + }, + { + "id": "C56A4180-65AA-42EC-A945-5FD21DEC0519", + "name": "Gold Coast", + "tag": "First Floor", + "parent_id":"C56A4180-65AA-42EC-A945-5FD21DEC0518", + "location_id": 1 + } + ] +} diff --git a/test_data/questions_table.json b/test_data/questions_table.json new file mode 100644 index 000000000..009f54495 --- /dev/null +++ b/test_data/questions_table.json @@ -0,0 +1,38 @@ +{ + "table_name": "questions", + "data": [ + { + "question_type":"rate", + "question_title":"Rating Feedback", + "question":"How will you rate the brightness of the room", + "start_date":"20 Nov 2018", + "end_date":"28 Nov 2018", + "is_active":true + }, + { + "question_type": "check", + "question_title": "check Feedback", + "question": "Is there anything missing in the room", + "check_options":["apple tv", "whiteboard", "maker pen"], + "start_date": "20 Nov 2018", + "end_date": "30 Nov 2018", + "is_active": true + }, + { + "question_type":"input", + "question_title":"input Feedback", + "question":"Any other suggestion", + "start_date":"20 Nov 2018", + "end_date":"28 Nov 2018" + }, + { + "question_type":"check", + "question_title":"Missing item", + "question":"Anything missing in the room?", + "check_options":["duster"], + "start_date":"20 Nov 2018", + "end_date":"30 Nov 2018", + "is_active":true + } + ] +} diff --git a/test_data/resources_table.json b/test_data/resources_table.json new file mode 100644 index 000000000..c78c8f0e0 --- /dev/null +++ b/test_data/resources_table.json @@ -0,0 +1,9 @@ +{ + "table_name": "resources", + "data": [ + { + "name":"Markers", + "quantity":3 + } + ] +} diff --git a/test_data/responses_table.json b/test_data/responses_table.json new file mode 100644 index 000000000..a5423815d --- /dev/null +++ b/test_data/responses_table.json @@ -0,0 +1,30 @@ +{ + "table_name": "responses", + "data": [ + { + "question_id":1, + "room_id":1, + "question_type":"rate", + "created_date": "2019-11-30 16:20:24.885363", + "response":"1", + "resolved":false + }, + { + "question_id":2, + "room_id":1, + "question_type":"check", + "created_date": "2019-11-30 16:20:24.885363", + "response":["marker pen", "apple tv"], + "resolved":true + }, + { + "question_id":4, + "room_id":2, + "question_type":"check", + "created_date":"2019-11-30 16:20:24.885363", + "response":["duster"], + "resolved":true, + "state":"archived" + } + ] +} diff --git a/test_data/roles_table.json b/test_data/roles_table.json new file mode 100644 index 000000000..55e5d2137 --- /dev/null +++ b/test_data/roles_table.json @@ -0,0 +1,14 @@ +{ + "table_name": "roles", + "data": [ + { + "role": "Admin" + }, + { + "role": "Test" + }, + { + "role": "Super Admin" + } + ] +} diff --git a/test_data/room_tags_association_table.json b/test_data/room_tags_association_table.json new file mode 100644 index 000000000..db9da093d --- /dev/null +++ b/test_data/room_tags_association_table.json @@ -0,0 +1,13 @@ +{ + "table_name": "room_tags", + "data": [ + { + "tag_id": 1, + "room_id": 1 + }, + { + "tag_id": 1, + "room_id": 2 + } + ] +} diff --git a/test_data/rooms_table.json b/test_data/rooms_table.json new file mode 100644 index 000000000..7312bfc4c --- /dev/null +++ b/test_data/rooms_table.json @@ -0,0 +1,33 @@ +{ + "table_name": "rooms", + "data": [ + { + "name":"Entebbe", + "room_type":"meeting", + "capacity":6, + "location_id": 1, + "structure_id":"851ae8b3-48dd-46b5-89bc-ca3f8111ad87", + "calendar_id":"andela.com_3630363835303531343031@resource.calendar.google.com", + "image_url":"https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg", + "room_labels": + [ + "1st Floor", + "Wing A" + ] + }, + { + "name": "Tana", + "room_type": "meeting", + "capacity": 14, + "location_id":1, + "structure_id": "851ae8b3-48dd-46b5-89bc-ca3f8111ad87", + "calendar_id": "andela.com_3730313534393638323232@resource.calendar.google.com", + "image_url": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg", + "room_labels": [ + "1st Floor", + "Wing B" + ] + } + ] + +} diff --git a/test_data/structures_table.json b/test_data/structures_table.json new file mode 100644 index 000000000..29b7eca3b --- /dev/null +++ b/test_data/structures_table.json @@ -0,0 +1,15 @@ +{ + "table_name": "office_structures", + "data": [ + { + "structure_id":"b05fc5f2-b4aa-4f48-a8fb-30bdcc3fc968", + "level":1, + "name":"Epic tower", + "parent_id":"1", + "parent_title":"parent_title", + "tag":"Building", + "location_id":1, + "position":1 + } + ] +} diff --git a/test_data/tags_table.json b/test_data/tags_table.json new file mode 100644 index 000000000..2feeeeeb4 --- /dev/null +++ b/test_data/tags_table.json @@ -0,0 +1,15 @@ +{ + "table_name": "tags", + "data": [ + { + "name":"Block-B", + "color":"green", + "description":"The description" + }, + { + "name": "Block-C", + "color": "blue", + "description": "The description" + } + ] +} diff --git a/test_data/users_roles_association_table.json b/test_data/users_roles_association_table.json new file mode 100644 index 000000000..67be6cc1f --- /dev/null +++ b/test_data/users_roles_association_table.json @@ -0,0 +1,13 @@ +{ + "table_name": "users_roles", + "data": [ + { + "user_id": 1, + "role_id": 1 + }, + { + "user_id": 2, + "role_id": 1 + } + ] +} diff --git a/test_data/users_table.json b/test_data/users_table.json new file mode 100644 index 000000000..5e458f8f8 --- /dev/null +++ b/test_data/users_table.json @@ -0,0 +1,17 @@ +{ + "table_name": "users", + "data": [ + { + "email": "peter.walugembe@andela.com", + "name": "Peter Walugembe", + "picture": "https://www.andela.com/walugembe", + "location": "Kampala" + }, + { + "email":"peter.adeoye@andela.com", + "name":"Peter Adeoye", + "picture":"https://www.andela.com/adeoye", + "location":"Lagos" + } + ] +} diff --git a/tests/base.py b/tests/base.py index d44febaff..2a9d1fc77 100644 --- a/tests/base.py +++ b/tests/base.py @@ -5,7 +5,6 @@ from flask_testing import TestCase from graphene.test import Client -from datetime import datetime from alembic import command, config from unittest.mock import patch @@ -13,19 +12,14 @@ from schema import schema from helpers.database import engine, db_session, Base from api.location.models import Location -from api.room.models import Room -from api.room_resource.models import Resource from api.user.models import User from api.role.models import Role -from api.events.models import Events -from api.devices.models import Devices -from api.question.models import Question -from api.response.models import Response -from api.tag.models import Tag -from api.structure.models import Structure -from api.office_structure.models import OfficeStructure from fixtures.token.token_fixture import ( ADMIN_TOKEN, USER_TOKEN, ADMIN_NIGERIA_TOKEN) +from tests.save_test_data import ( + insert_association_tables_data, insert_data_in_database, + files, association_tables_files + ) sys.path.append(os.getcwd()) @@ -51,185 +45,8 @@ def setUp(self, mock_verify_calendar_id): command.downgrade(self.alembic_configuration, '-1') command.upgrade(self.alembic_configuration, 'head') - admin_user = User(email="peter.walugembe@andela.com", - name="Peter Walugembe", - picture="https://www.andela.com/walugembe") - admin_user.location = "Kampala" - admin_user.save() - lagos_admin = User(email="peter.adeoye@andela.com", - location="Lagos", name="Peter Adeoye", - picture="https://www.andela.com/adeoye") - lagos_admin.save() - global role - role = Role(role="Admin") - role.save() - role_2 = Role(role="Test") - role_2.save() - role_3 = Role(role="Super Admin") - role_3.save() - admin_user.roles.append(role) - lagos_admin.roles.append(role) - tag = Tag(name='Block-B', - color='green', - description='The description') - tag.save() - - location = Location(name='Kampala', - abbreviation='KLA') - location.save() - location_two = Location(name='Nairobi', - abbreviation='NBO') - location_two.save() - location_three = Location(name='Lagos', - abbreviation='LOS') - location_three.save() - tag_two = Tag(name='Block-C', - color='blue', - description='The description') - tag_two.save() - room = Room(name='Entebbe', - room_type='meeting', - capacity=6, - location_id=location.id, - structure_id='851ae8b3-48dd-46b5-89bc-ca3f8111ad87', - calendar_id='andela.com_3630363835303531343031@resource.calendar.google.com', # noqa: E501 - image_url="https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg", # noqa: E501 - room_labels=["1st Floor", "Wing A"]) - room.save() - room.room_tags.append(tag) - room_2 = Room(name='Tana', - room_type='meeting', - capacity=14, - location_id=location.id, - structure_id='851ae8b3-48dd-46b5-89bc-ca3f8111ad87', - calendar_id='andela.com_3730313534393638323232@resource.calendar.google.com', # noqa: E501 - image_url="https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg", # noqa: E501 - room_labels=["1st Floor", "Wing B"]) - room_2.save() - room_2.room_tags.append(tag) - resource = Resource(name='Markers', - quantity=3) - resource.save() - device = Devices( - last_seen="2018-06-08T11:17:58.785136", - date_added="2018-06-08T11:17:58.785136", - name="Samsung", - location="Kampala", - device_type="External Display", - room_id=1, - state="active" - ) - device.save() - question_1 = Question( - question_type="rate", - question_title="Rating Feedback", - question="How will you rate the brightness of the room", - start_date="20 Nov 2018", - end_date="28 Nov 2018", - is_active=True - ) - question_1.save() - question_2 = Question( - question_type="check", - question_title="check Feedback", - question="Is there anything missing in the room", - check_options=['apple tv', 'whiteboard', 'maker pen'], - start_date="20 Nov 2018", - end_date="30 Nov 2018", - is_active=True - ) - event = Events( - event_id="test_id5", - room_id=1, - event_title="Onboarding", - start_time="2018-07-11T09:00:00Z", - end_time="2018-07-11T09:45:00Z", - number_of_participants=4, - checked_in=False, - cancelled=False) - event.save() - question_2.save() - question_3 = Question( - question_type="input", - question_title="input Feedback", - question="Any other suggestion", - start_date="20 Nov 2018", - end_date="28 Nov 2018" - ) - question_3.save() - question_4 = Question( - question_type="check", - question_title="Missing item", - question="Anything missing in the room?", - check_options=['duster'], - start_date="20 Nov 2018", - end_date="30 Nov 2018", - is_active=True - ) - question_4.save() - response_1 = Response( - question_id=1, - room_id=1, - question_type="rate", - created_date=datetime.now(), - response="1", - resolved=False, - ) - response_1.save() - - response_2 = Response( - question_id=question_2.id, - room_id=room.id, - question_type="check", - created_date=datetime.now(), - response=['marker pen', 'apple tv'], - resolved=True, - ) - response_2.save() - response_2.missing_resources.append(resource) - - response_3 = Response( - question_id=question_4.id, - room_id=room_2.id, - question_type="check", - created_date=datetime.now(), - response=['duster'], - resolved=True, - state="archived" - ) - response_3.save() - - structure = Structure( - structure_id='b05fc5f2-b4aa-4f48-a8fb-30bdcc3fc968', - level=1, - name='Epic tower', - parent_id="1", - parent_title="parent_title", - tag='Building', - location_id=1, - position=1, - ) - structure.save() - parent_node = OfficeStructure( - id='C56A4180-65AA-42EC-A945-5FD21DEC0518', - name='Epic Tower', - tag='Lagos Building', - location_id=1 - ) - parent_node.save() - child_node = OfficeStructure( - id='C56A4180-65AA-42EC-A945-5FD21DEC0519', - name='Gold Coast', - tag='First Floor', - parent_id='C56A4180-65AA-42EC-A945-5FD21DEC0518', - location_id=1 - ) - child_node.save() - db_session.commit() - f = open('mrm.err.log', 'a+') - f.write('[2019-08-06 13:22:32 +0000] [1574] [ERROR] Error /logs\r') - f.write('Traceback (most recent call last):\r') - f.write('if pattern.search(line):\r') + insert_data_in_database(db_session, files) + insert_association_tables_data(db_session, association_tables_files) def get_admin_location_id(self): payload = jwt.decode(ADMIN_TOKEN, verify=False) @@ -366,7 +183,8 @@ def func_wrapper(self): user = User(email='mrm@andela.com', name='this user', location="Nairobi", picture='www.andela.com/user') user.save() - user.roles.append(role) + admin_role = Role.query.filter_by(role='Admin').first() + user.roles.append(admin_role) db_session().commit() return headers return func_wrapper diff --git a/tests/save_test_data.py b/tests/save_test_data.py new file mode 100644 index 000000000..b2fcf5c7e --- /dev/null +++ b/tests/save_test_data.py @@ -0,0 +1,76 @@ +import json +from api.user.models import User, users_roles +from api.location.models import Location +from api.room.models import Room, tags +from api.room_resource.models import Resource +from api.role.models import Role +from api.events.models import Events +from api.devices.models import Devices +from api.question.models import Question +from api.response.models import Response +from api.tag.models import Tag +from api.structure.models import Structure +from api.office_structure.models import OfficeStructure +from utilities.insert_data_in_table import insert_records_in_table + + +models = [ + Role, User, Tag, Location, Room, Resource, + Devices, Question, Events, Response, + Structure, OfficeStructure + ] + +association_tables = [users_roles, tags] + +files = [ + 'roles_table.json', 'users_table.json', + 'tags_table.json', 'locations_table.json', + 'rooms_table.json', 'resources_table.json', + 'devices_table.json', 'questions_table.json', + 'events_table.json', 'responses_table.json', + 'structures_table.json', 'office_structures_table.json' +] + +association_tables_files = [ + 'users_roles_association_table.json', + 'room_tags_association_table.json' +] + + +def insert_association_tables_data(session, files): + """ + Opens association tables test files + and saves its data in the test database + """ + for file in files: + with open('test_data/' + file, 'r') as outfile: + data = json.load(outfile) + records = data['data'] + model = data['table_name'] + condition = ( + table for table in association_tables if table.name == model + ) + for table in condition: + session.execute(table.insert(None), params=records) + session.commit() + f = open('mrm.err.log', 'a+') + f.write('[2019-08-06 13:22:32 +0000] [1574] [ERROR] Error /logs\r') # noqa E501 + f.write('Traceback (most recent call last):\r') + f.write('if pattern.search(line):\r') + + +def insert_data_in_database(session, files): + """ + Opens test files and saves its data + in the test database + """ + for file in files: + with open('test_data/' + file, 'r') as outfile: + data = json.load(outfile) + records = data['data'] + model_name = data['table_name'] + condition = ( + model for model in models if model.__tablename__ == model_name + ) + for model in condition: + insert_records_in_table(session, model, records) diff --git a/tests/test_events/test_sync_events_data.py b/tests/test_events/test_sync_events_data.py index a07074d15..a1d1c4a30 100644 --- a/tests/test_events/test_sync_events_data.py +++ b/tests/test_events/test_sync_events_data.py @@ -17,7 +17,7 @@ def test_sync_all_events(self, mocked_method): response = self.client.execute(sync_data_mutation) self.assertEqual(sync_data_response, response) - def test_syncs_after_notification_is_recieved(self, mocked_method): + def test_syncs_after_notification_is_received(self, mocked_method): mocked_method.return_value = get_events_mock_data() response = self.client.execute(notification_mutation) self.assertEqual(notification_response, response) diff --git a/tests/test_location/test_create_location.py b/tests/test_location/test_create_location.py index 50a980393..f4d9fb895 100644 --- a/tests/test_location/test_create_location.py +++ b/tests/test_location/test_create_location.py @@ -68,6 +68,7 @@ def test_create_location_without_locations_model(self): db_session.remove() with engine.begin() as conn: conn.execute("DROP TABLE locations CASCADE") + conn.execute("ALTER SEQUENCE locations_id_seq RESTART WITH 1") CommonTestCases.admin_token_assert_in( self, create_location_query, diff --git a/tests/test_user/test_delete_user.py b/tests/test_user/test_delete_user.py index 4ff860ade..8bda1ba38 100644 --- a/tests/test_user/test_delete_user.py +++ b/tests/test_user/test_delete_user.py @@ -10,7 +10,6 @@ ) from api.user.models import User from api.role.models import Role -import tests.base as base sys.path.append(os.getcwd()) @@ -55,7 +54,7 @@ def test_user_delete_admin(self): location="Kampala", name="test test", picture="www.andela.com/test") admin_user.save() - role = base.role + role = Role.query.filter_by(role='Admin').first() admin_user.roles.append(role) CommonTestCases.user_token_assert_in( diff --git a/tests/test_user_roles/test_query_user_role.py b/tests/test_user_roles/test_query_user_role.py index 48f09b3a1..c41ee00ab 100644 --- a/tests/test_user_roles/test_query_user_role.py +++ b/tests/test_user_roles/test_query_user_role.py @@ -19,7 +19,6 @@ from api.role.models import Role import sys import os -import tests.base as base sys.path.append(os.getcwd()) user_role = Role(role="Default") @@ -29,7 +28,7 @@ def create_user(): name="test test", picture="www.andela.com/testuser") user.save() - role = base.role + role = Role.query.filter_by(role='Admin').first() user.roles.append(role) db_session().commit() return user diff --git a/utilities/insert_data_in_table.py b/utilities/insert_data_in_table.py new file mode 100644 index 000000000..2a6dd9365 --- /dev/null +++ b/utilities/insert_data_in_table.py @@ -0,0 +1,12 @@ +def insert_records_in_table(session, model, records): + """ + inserts multiple records in a table + """ + for record in records: + send_record = model(**record) + send_record.save() + session.commit() + f = open('mrm.err.log', 'a+') + f.write('[2019-08-06 13:22:32 +0000] [1574] [ERROR] Error /logs\r') # noqa E501 + f.write('Traceback (most recent call last):\r') + f.write('if pattern.search(line):\r')