def get(self, nodeId=None, slug=None): args = parser.parse_args() if nodeId is not None: with get_db().read_transaction: try: survey = Survey.nodes.get(nodeId=nodeId) except DoesNotExist: return { "message": f"The survey with nodeId {nodeId} could not be found" }, 400 except NeomodelException as e: return { "message": f"An internal error has occured: { repr(e) }" }, 500 elif slug is not None: with get_db().read_transaction: try: survey = Survey.nodes.get(slug=slug) except DoesNotExist: return { "message": f"The survey with nodeId {slug} could not be found" }, 400 except NeomodelException as e: return { "message": f"An internal error has occured: { repr(e) }" }, 500 elif nodeId is None and slug is None: with get_db().read_transaction: if args.get('language') is not None: surveys = Survey.nodes.filter( language=args.get('language')).all() else: surveys = Survey.nodes.all() survey_dump = [] try: for survey in surveys: survey_dump.append(SurveyWrapper(survey).dump()) return survey_dump, 200 except Exception as e: return { "message": f"An internal error has occured { repr(e)}" }, 500 try: survey_dump = SurveyWrapper(survey).dump() return survey_dump, 200 except Exception as e: return { "message": ("The survey requested could not be serialized " + f"with the following error: {repr(e)} ") }, 500
def test_delete_node(self): with self.app.app_context(): from src.database.db import get_db from src.models.answers_model import Answer current_transaction = get_db().transaction with current_transaction: test_answer_2 = Answer.nodes.get(answer="this is an answer 2") test_answer_2.delete() node = get_db().cypher_query( "MATCH (a:Answer {answer: 'this is an answer 2'}) " + "RETURN a") assert not node[0]
def test_delete_node(self): with self.app.app_context(): from src.database.db import get_db from src.models.survey_model import Survey current_transaction = get_db().transaction with current_transaction: Survey.nodes.get(slug="test_survey_1").delete() deletedNode = get_db().cypher_query( "MATCH (s:Survey {slug: 'test_survey_1'}) RETURN s" ) assert not deletedNode[0]
def test_delete_node(self): with self.app.app_context(): from src.database.db import get_db from src.models.conducted_survey_model import ConductedSurvey current_transaction = get_db().transaction with current_transaction: test_conducted_survey_1 = ConductedSurvey.nodes.all()[0] test_conducted_survey_1.delete() deletedNode = get_db().cypher_query( "MATCH (s:ConductedSurvey {nodeId: " + f"'{test_conducted_survey_1.nodeId}'" + "}) " + "RETURN s") assert not deletedNode[0]
def test_get_survey_versions_between_datetime_lower_inclusive_higher_inclusive( self): with self.app.app_context(): from src.database.db import get_db current_transaction = get_db().transaction with current_transaction: test_survey_version_4 = pytest.test_survey_version_4 test_survey_version_5 = pytest.test_survey_version_5 test_survey_version_6 = pytest.test_survey_version_6 test_survey_version_7 = pytest.test_survey_version_7 test_survey_version_8 = pytest.test_survey_version_8 test_survey_wrapper_3 = pytest.test_survey_wrapper_3 nodes = test_survey_wrapper_3.get_survey_versions_between_datetime( datetime(2019, 4, 22, 14, 2, 40, 12, pytz.utc), datetime(2019, 4, 23, 18, 2, 40, 12, pytz.utc), True, True) for i in range(0, len(nodes)): nodes[i] = nodes[i].version assert test_survey_version_4 not in nodes assert test_survey_version_5 in nodes assert test_survey_version_6 in nodes assert test_survey_version_7 in nodes assert test_survey_version_8 not in nodes assert len(nodes) == 3
def test_get_survey_versions_gt_datetime(self): with self.app.app_context(): from src.database.db import get_db current_transaction = get_db().transaction with current_transaction: test_survey_version_4 = pytest.test_survey_version_4 test_survey_version_5 = pytest.test_survey_version_5 test_survey_version_6 = pytest.test_survey_version_6 test_survey_version_7 = pytest.test_survey_version_7 test_survey_version_8 = pytest.test_survey_version_8 test_survey_wrapper_3 = pytest.test_survey_wrapper_3 test_survey_wrapper_3 = pytest.test_survey_wrapper_3 nodes = test_survey_wrapper_3.get_survey_versions_gt_datetime( datetime(2019, 4, 22, 14, 2, 40, 12, pytz.utc)) assert test_survey_version_4 not in nodes assert test_survey_version_5 not in nodes assert test_survey_version_6 in nodes assert test_survey_version_7 in nodes assert test_survey_version_8 in nodes assert len(nodes) == 3 nodes = test_survey_wrapper_3.get_survey_versions_gt_datetime( datetime(2019, 4, 22, 14, 2, 40, 12, pytz.utc), True) assert test_survey_version_4 not in nodes assert test_survey_version_5 in nodes assert test_survey_version_6 in nodes assert test_survey_version_7 in nodes assert test_survey_version_8 in nodes assert len(nodes) == 4
def test_parent_wrapper_assignment(self): with self.app.app_context(): from src.database.db import get_db from src.models.survey_model import Survey current_transaction = get_db().transaction test_survey_version_wrapper_1 = pytest.test_survey_version_wrapper_1 with current_transaction: test_survey_1 = Survey( language="en", slug="test_survey_1" ) test_survey_1.save() test_survey_1.versions.connect( test_survey_version_wrapper_1.version ) test_survey_wrapper_1 = SurveyWrapper(test_survey_1) pytest.test_survey_wrapper_1 = test_survey_wrapper_1 test_survey_version_wrapper_1.parent_wrapper = test_survey_wrapper_1 assert test_survey_version_wrapper_1.parent_wrapper == test_survey_wrapper_1
def test_create_relationship(self): with self.app.app_context(): from src.database.db import get_db, init_db, distroy_db from src.models.question_model import PreQuestion from src.models.survey_model import SurveyVersion distroy_db(self.app) init_db(self.app) current_transaction = get_db().transaction with current_transaction: test_prequestion_1 = PreQuestion( slug="test_prequestion_1", text="This is and example PreQuestion 1", language="en") test_prequestion_1.save() test_surveyversion_1 = SurveyVersion( title="Test SurveyVersion 1") test_surveyversion_1.save() rel = test_surveyversion_1.prequestions.connect( test_prequestion_1) pytest.test_prequestion_1 = test_prequestion_1 pytest.test_surveyversion_1 = test_surveyversion_1 pytest.test_surveyversion_prequestion_rel_1 = rel
def test_dump_with_previousVersions(self): with self.app.app_context(): from src.database.db import get_db from src.models.survey_model import SurveyVersion current_transaction = get_db().transaction with current_transaction: test_survey_wrapper_1 = pytest.test_survey_wrapper_1 test_survey_version_2 = SurveyVersion( title = 'Test Survey Version 2' ) test_survey_version_2.save() test_survey_version_3 = SurveyVersion( title = 'Test Survey Version 3' ) test_survey_version_3.save() test_survey_wrapper_1.currentVersion = test_survey_version_2 test_survey_wrapper_1.currentVersion = test_survey_version_3 pytest.test_survey_version_3 = test_survey_version_3 pytest.test_survey_version_2 = test_survey_version_2 pytest.test_output_4 = test_survey_wrapper_1.currentVersion.dump()
def test_update_node(self): with self.app.app_context(): from src.database.db import get_db from src.models.answers_model import Answer current_transaction = get_db().transaction with current_transaction: test_answer_1 = Answer.nodes.get(answer="this is an answer 1") oldUpdatedOn = test_answer_1.updatedOn test_answer_1.answer = "this is an updated answer 1" test_answer_1.save() assert test_answer_1.updatedOn > oldUpdatedOn node = get_db().cypher_query( 'MATCH (a:Answer {answer:"this is an updated answer 1"}) ' + 'RETURN a') assert node[0]
def test_create_relationship(self): with self.app.app_context(): from src.database.db import init_db, get_db, distroy_db from src.models.survey_model import Survey distroy_db(self.app) init_db(self.app) current_transaction = get_db().transaction with current_transaction: test_survey_1 = Survey( language="en", slug = "test_survey_1" ) test_survey_1.save() test_survey_2 = Survey( language="fr", slug = "test_survey_2" ) test_survey_2.save() rel = test_survey_1.related_surveys.connect( test_survey_2, { 'reason': 'language', 'description': "These surveys are related" } ) pytest.test_survey_1 = test_survey_1 pytest.test_survey_2 = test_survey_2 pytest.test_survey_survey_rel_1 = rel
def test_create_relationship(self): with self.app.app_context(): from src.database.db import init_db, distroy_db, get_db from src.models.question_model import Question, PreQuestion distroy_db(self.app) init_db(self.app) current_transaction = get_db().transaction with current_transaction: test_question_1 = Question( question="Test Question 1", slug="test_question_1", language="en" ) test_question_1.save() test_prequestion_1 = PreQuestion( text = "This is an example prequestion", slug = "test_prequestion_1", language = "en" ) test_prequestion_1.save() rel = test_prequestion_1.questions.connect(test_question_1) pytest.test_question_1 = test_question_1 pytest.test_prequestion_1 = test_prequestion_1 pytest.test_prequestion_question_rel_1 = rel
def test_create_relationship(self): with self.app.app_context(): from src.database.db import get_db, init_app, distroy_db from src.models.survey_model import Survey, SurveyVersion distroy_db(self.app) init_app(self.app) current_transaction = get_db().transaction with current_transaction: test_Survey_1 = Survey( slug="test_survey_1", language="en" ) test_SurveyVersion_1 = SurveyVersion( title="Test Survey 1" ) test_Survey_1.save() test_SurveyVersion_1.save() rel = test_Survey_1.versions.connect(test_SurveyVersion_1) assert len(test_SurveyVersion_1.survey.all()) == 1 pytest.test_Survey_SurveyVersion_rel_1 = rel pytest.test_Survey_1 = test_Survey_1 pytest.test_SurveyVersion_1 = test_SurveyVersion_1
def test_add_questions_method(self): with self.app.app_context(): from src.database.db import get_db from src.models.question_model import Question from neomodel import RelationshipManager current_transaction = get_db().transaction test_survey_version_wrapper_1 = pytest.test_survey_version_wrapper_1 with current_transaction: test_question_1 = Question( question = "Test Question 1", slug = "test_question_1", language = "en" ) test_question_1.save() test_survey_version_wrapper_1.add_question(test_question_1) assert isinstance( test_survey_version_wrapper_1._questions, RelationshipManager ) test_question_2 = Question( question = "Test Question 2", slug = "test_question_2", language = "en" ) test_survey_version_wrapper_1.add_question(test_question_2) assert len(test_survey_version_wrapper_1._version.questions) == 2 pytest.test_question_1 = test_question_1 pytest.test_question_2 = test_question_2
def test_create_answer_node(self): with self.app.app_context(): from src.database.db import get_db, init_db, distroy_db distroy_db(self.app) init_db(self.app) from src.models.answers_model import Answer current_transaction = get_db().transaction with current_transaction: test_answer_1 = Answer(answer="this is an answer 1") test_answer_1.save() node = get_db().cypher_query("MATCH (a:Answer) RETURN a") assert test_answer_1.answer == \ node[0][0][0]._properties['answer'] assert test_answer_1.updatedOn is not None assert test_answer_1.addedOn is not None assert test_answer_1.id is not None
def test_slug_unique_constrain(self): with self.app.app_context(): from src.database.db import get_db from src.models.survey_model import Survey from neomodel.exceptions import UniqueProperty current_transaction = get_db().transaction with pytest.raises(UniqueProperty): with current_transaction: test_survey_6 = Survey( slug="test_survey_5", language="en" ) test_survey_6.save() current_transaction = get_db().transaction with current_transaction: test_survey_6.slug = "test_survey_6" test_survey_6.save()
def test_setup_for_datetime_filtering_methods(self): with self.app.app_context(): from src.database.db import get_db from src.models.survey_model import Survey, SurveyVersion current_transaction = get_db().transaction with current_transaction: test_survey_3 = Survey(slug="test_survey_3", language="en") test_survey_3.save() test_survey_version_4 = SurveyVersion( title="Test Survey Version 4", ) test_survey_version_5 = SurveyVersion( title="Test Survey Version 5") test_survey_version_6 = SurveyVersion( title="Test Survey Version 6") test_survey_version_7 = SurveyVersion( title="Test Survey Version 7") test_survey_version_8 = SurveyVersion( title="Test Survey Version 8") test_survey_version_4.save() test_survey_version_5.save() test_survey_version_6.save() test_survey_version_7.save() test_survey_version_8.save() test_survey_3.versions.connect(test_survey_version_4, { 'addedOn': datetime(2019, 4, 21, 14, 2, 40, 12, pytz.utc) }) test_survey_3.versions.connect(test_survey_version_5, { 'addedOn': datetime(2019, 4, 22, 14, 2, 40, 12, pytz.utc) }) test_survey_3.versions.connect(test_survey_version_6, { 'addedOn': datetime(2019, 4, 22, 18, 2, 40, 12, pytz.utc) }) test_survey_3.versions.connect(test_survey_version_7, { 'addedOn': datetime(2019, 4, 23, 18, 2, 40, 12, pytz.utc) }) test_survey_3.versions.connect(test_survey_version_8, { 'addedOn': datetime(2019, 5, 22, 18, 2, 40, 12, pytz.utc) }) test_survey_wrapper_3 = SurveyWrapper(test_survey_3) test_survey_wrapper_3.currentVersion = test_survey_version_8 pytest.test_survey_3 = test_survey_3 pytest.test_survey_version_4 = test_survey_version_4 pytest.test_survey_version_5 = test_survey_version_5 pytest.test_survey_version_6 = test_survey_version_6 pytest.test_survey_version_7 = test_survey_version_7 pytest.test_survey_version_8 = test_survey_version_8 pytest.test_survey_wrapper_3 = test_survey_wrapper_3
def test_dump_with_survey(self): with self.app.app_context(): from src.database.db import get_db current_transaction = get_db().transaction with current_transaction: test_survey_version_wrapper_1 = pytest.test_survey_version_wrapper_1 test_output_2 = test_survey_version_wrapper_1.dump() pytest.test_output_2 = test_output_2
def test_currentVersion_getter_no_versions(self): with self.app.app_context(): from src.database.db import get_db from src.utils.exceptions.wrapper_exceptions import NoCurrentVersionFound current_transction = get_db().transaction with current_transction: test_survey_wrapper_1 = pytest.test_survey_wrapper_1 with pytest.raises(NoCurrentVersionFound): test_survey_wrapper_1.currentVersion
def test_update_node(self): with self.app.app_context(): from src.database.db import get_db from src.models.conducted_survey_model import ConductedSurvey current_transation = get_db().transaction with current_transation: test_conducted_survey_1 = ConductedSurvey.nodes.all()[0] test_conducted_survey_1.googleSentimentScore = 0.4 test_conducted_survey_1.save() assert test_conducted_survey_1.updatedOn >\ pytest.test_conducted_survey_1.updatedOn
def test_dump_method(self): with self.app.app_context(): from src.database.db import get_db current_transaction = get_db().transaction test_survey_wrapper_3 = pytest.test_survey_wrapper_3 with current_transaction: output = test_survey_wrapper_3.dump() pytest.test_output_1 = output
def test_setClosedOn_method(self): with self.app.app_context(): from src.database.db import get_db from src.models.conducted_survey_model import ConductedSurvey current_transaction = get_db().transaction with current_transaction: test_conducted_survey_3 = ConductedSurvey() test_conducted_survey_3.set_closedOn() test_conducted_survey_3.save() assert test_conducted_survey_3.closedOn is not None assert isinstance(test_conducted_survey_3.closedOn, datetime)
def test_answer_required_constraint(self): with self.app.app_context(): from src.database.db import get_db from src.models.answers_model import Answer current_transaction = get_db().transaction from neomodel.exception import RequiredProperty with pytest.raises(RequiredProperty): with current_transaction: test_answer_2 = Answer() test_answer_2.save() with current_transaction: test_answer_2.answer = "this is an answer 2" test_answer_2.save()
def test_dump_excludes_argument(self): with self.app.app_context(): from src.database.db import get_db current_transaction = get_db().transaction with current_transaction: test_survey_wrapper_3 = pytest.test_survey_wrapper_3 test_output_2 = test_survey_wrapper_3.dump(exclude=['slug']) with pytest.raises(KeyError): test_output_2['slug']
def test_reason_field_choices(self): with self.app.app_context(): from src.database.db import get_db current_transaction = get_db().transaction with current_transaction: test_survey_survey_rel_1 = pytest.test_survey_survey_rel_1 test_survey_survey_rel_1.reason = "similar" test_survey_survey_rel_1.save() test_survey_survey_rel_1.reason = "shorter" test_survey_survey_rel_1.save() test_survey_survey_rel_1.reason = "language" test_survey_survey_rel_1.save()
def test_set_up(self): with self.app.app_context(): from src.database.db import get_db, init_db, distroy_db from src.models.survey_model import Survey distroy_db(self.app) init_db(self.app) current_transaction = get_db().transaction with current_transaction: test_survey_1 = Survey(slug="test_survey_1", language="en") test_survey_1.save() pytest.test_survey_1 = test_survey_1 # type: Survey
def test_one_cardinality_for_SurveyVersion(self): with self.app.app_context(): from src.database.db import get_db from src.models.survey_model import SurveyVersion, Survey from neomodel.exceptions import AttemptedCardinalityViolation current_transaction = get_db().transaction with pytest.raises(AttemptedCardinalityViolation): with current_transaction: test_Survey_2 = Survey( slug='test_survey_2', language='en' ) test_Survey_2.save() test_SurveyVersion_1 = pytest.test_SurveyVersion_1 test_SurveyVersion_1.survey.connect(test_Survey_2)
def test_survey_setter(self): with self.app.app_context(): from src.database.db import get_db from src.models.survey_model import Survey current_transaction = get_db().transaction with current_transaction: test_survey_2 = Survey(slug="test_survey_2", language="en") test_survey_2.save() test_survey_wrapper_1 = pytest.test_survey_wrapper_1 # type: SurveyWrapper test_survey_wrapper_1.survey = test_survey_2 assert test_survey_wrapper_1._survey == test_survey_2
def test_create_node(self): with self.app.app_context(): from src.database.db import get_db, distroy_db, init_db distroy_db(self.app) init_db(self.app) from src.models.conducted_survey_model import ConductedSurvey transaction_factory = get_db() current_transaction = transaction_factory.transaction with current_transaction: from src.models.conducted_survey_model import ConductedSurvey test_conducted_survey_1 = ConductedSurvey() test_conducted_survey_1.save() pytest.test_conducted_survey_1 = test_conducted_survey_1 pytest.nodeId = test_conducted_survey_1.nodeId
def test_create_node(self): with self.app.app_context(): from src.database.db import get_db, distroy_db, init_db from src.models.survey_model import SurveyVersion distroy_db(self.app) init_db(self.app) current_transaction = get_db().transaction with current_transaction: test_survey_version_1 = SurveyVersion( title="Survey Version 1" ) test_survey_version_1.save() pytest.test_survey_version_1 = test_survey_version_1