Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #7 - Adds read ability for dimensional questions. #12

Merged
merged 3 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/data_service/data_service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from uuid import UUID

from .sqlite3 import Sqlite3Driver
from app.models import Survey, TextQuestion
from app.models import Survey, TextQuestion, DimensionalQuestion


class DataService:
Expand Down Expand Up @@ -31,3 +31,13 @@ def get_text_questions_from_survey(self, survey_uid: UUID) -> list[TextQuestion]

def insert_text_question(self, text_question: TextQuestion) -> None:
self._driver.insert_text_question(text_question=text_question)

def get_dimensional_question(
self, question_uid: UUID
) -> DimensionalQuestion | None:
return self._driver.get_dimensional_question(question_uid=question_uid)

def get_dimensional_questions_from_survey(
self, survey_uid: UUID
) -> list[DimensionalQuestion]:
return self._driver.get_dimensional_questions_from_survey(survey_uid=survey_uid)
26 changes: 25 additions & 1 deletion app/data_service/sqlite3/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uuid import UUID

from .model_factory import fetch_query_results_as_model
from app.models import Survey, TextQuestion
from app.models import Survey, TextQuestion, DimensionalQuestion


class Sqlite3Driver:
Expand Down Expand Up @@ -81,3 +81,27 @@ def insert_text_question(self, text_question: TextQuestion) -> None:
text_question.question,
),
)

def get_dimensional_question(
self, question_uid: UUID
) -> DimensionalQuestion | None:
query = "SELECT * FROM dimensional_question WHERE uid = ? LIMIT 1;"

with self._get_cursor() as cursor:
cursor.execute(query, (str(question_uid),))
result = fetch_query_results_as_model(cursor, DimensionalQuestion)

if len(result) > 0:
return result[0]
return None

def get_dimensional_questions_from_survey(
self, survey_uid: UUID
) -> list[DimensionalQuestion]:
query = "SELECT * FROM dimensional_question WHERE survey_uid = ?;"

with self._get_cursor() as cursor:
cursor.execute(query, (str(survey_uid),))
results = fetch_query_results_as_model(cursor, DimensionalQuestion)

return results
3 changes: 2 additions & 1 deletion app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = ["Survey", "TextQuestion"]
__all__ = ["Survey", "TextQuestion", "DimensionalQuestion"]

from .survey import Survey
from .text_question import TextQuestion
from .dimensional_question import DimensionalQuestion
13 changes: 13 additions & 0 deletions app/models/dimensional_question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from uuid import UUID, uuid4
from pydantic import Field

from .base_data_model import BaseDataModel


class DimensionalQuestion(BaseDataModel):
uid: UUID = Field(default_factory=uuid4)
survey_uid: UUID
question: str
dimension_one: str
dimension_two: str
dimension_three: str
15 changes: 11 additions & 4 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
app = Flask(__name__)
jinja_partials.register_extensions(app)

# TODO: test that every endpoint logs the request.


@app.before_request
def create_data_service() -> None:
Expand Down Expand Up @@ -49,12 +47,21 @@ def get_survey(uid: str) -> str:
data_service: DataService = app.data_service # type: ignore[attr-defined]
survey_uid = UUID(uid)
survey = data_service.get_survey_if_open(survey_uid=survey_uid)
questions = data_service.get_text_questions_from_survey(survey_uid=survey_uid)

if survey is None:
abort(404, "Could not find a survey with that UUID.")

return render_template("survey.html", survey=survey, questions=questions)
text_questions = data_service.get_text_questions_from_survey(survey_uid=survey_uid)
dimensional_questions = data_service.get_dimensional_questions_from_survey(
survey_uid=survey_uid
)

return render_template(
"survey.html",
survey=survey,
text_questions=text_questions,
dimensional_questions=dimensional_questions,
)


@app.route("/surveys/new")
Expand Down
19 changes: 16 additions & 3 deletions app/templates/survey.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
{% block content %}
<h1>{{survey.name}}</h3>

<p>Note: Answering Questions is not yet implemented & this page is for testing purposes only.</p>

<div>
{% for question in text_questions %}
<h4>{{question.question}}</h4>
{% endfor %}
<div>
{% for question in questions %}
<h2>{{question.question}}</h2>

<div>
{% for question in dimensional_questions %}
<h4>{{question.question}}</h4>
<ul>
<li>question.dimension_one</li>
<li>question.dimension_two</li>
<li>question.dimension_three</li>
</ul>
{% endfor %}
<div>

Answering Questions is not yet implemented.


{% endblock %}
30 changes: 25 additions & 5 deletions data/initial_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@ INSERT INTO text_question (
) VALUES (
'9c9facb5-f360-4155-852a-8e2ac04607ea'
, '4b5bfb06-2060-4abf-b5fd-3bae5dcf72b9'
, 'What is your name?'
), (
'ee947616-3d16-4095-bc8f-603be72022d3'
, '4b5bfb06-2060-4abf-b5fd-3bae5dcf72b9'
, 'Are you sure?'
, 'What story would you tell your best friend about this company?'
);

INSERT INTO dimensional_question (
uid
, survey_uid
, question
, dimension_one
, dimension_two
, dimension_three
) VALUES (
"4ccfc2b3-c18a-4c86-b918-1f30efdfeea2"
, "4b5bfb06-2060-4abf-b5fd-3bae5dcf72b9"
, "How strongly does this story demonstrate the following values?"
, "Empathy to Colleagues"
, "Service to Others"
, "Individual Growth"
),
(
"a6938e91-e7cc-4048-9b77-8cbba8d735cd"
, "4b5bfb06-2060-4abf-b5fd-3bae5dcf72b9"
, "How strongly does this story demonstrate the following values?"
, "Accountability"
, "Efficiency"
, "Individual Growth"
);
10 changes: 10 additions & 0 deletions data/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ CREATE TABLE IF NOT EXISTS text_question (
, FOREIGN KEY(survey_uid) REFERENCES survey(uid)
);

CREATE TABLE IF NOT EXISTS dimensional_question (
uid TEXT PRIMARY KEY
, survey_uid TEXT
, question TEXT
, dimension_one TEXT
, dimension_two TEXT
, dimension_three TEXT
, FOREIGN KEY(survey_uid) REFERENCES survey(uid)
);

COMMIT;
21 changes: 20 additions & 1 deletion tests/integration/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from app.data_service import DataService, Sqlite3Driver
from app.models import Survey
from tests.unit.test_routes_helpers import assert_response_is_valid_html


@pytest.fixture
Expand All @@ -19,6 +20,24 @@ def patched_init(self, driver):
monkeypatch.undo()


class TestReadEndpoints:
test_cases = (
"/",
"/admin",
"/surveys/new",
"/surveys/00000000-1f7c-43e9-ab61-3c34fd59a333",
)

@pytest.mark.parametrize("slug", test_cases)
def test_endpoint_returns_html(
self, app_client, setup_data_service, slug: str
) -> None:
response = app_client.get(slug)

assert response.status_code == 200
assert_response_is_valid_html(response)


class TestMutationEndpoints:
test_cases = [
(
Expand All @@ -38,7 +57,7 @@ class TestMutationEndpoints:
"name": "test mutable endpoints 2",
"is_open": "on",
"question-0": "What's my name again?",
"question-1": "What's you name again?",
"question-1": "What's your name again?",
"question-2": "Why are we here again?",
},
"/surveys",
Expand Down
76 changes: 76 additions & 0 deletions tests/test_assets/test_db_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ INSERT INTO survey (
"00000000-b37a-32b3-19d9-72ec921021e3"
, true
, "Open Test Survey - 2Qs"
),
(
"00000000-0762-4fd1-b927-65ddb494e04f"
, true
, "Open Test Survey - 1DQ"
),
(
"00000000-e253-4c39-b32b-eeb4f8e8711d"
, true
, "Open Test Survey - 2DQ"
),
(
"00000000-1f7c-43e9-ab61-3c34fd59a333"
, true
, "Open Test Survey - Mixed Questions"
);

INSERT INTO text_question (
Expand All @@ -43,6 +58,67 @@ INSERT INTO text_question (
"11111111-b37a-44a1-19d9-72ec921021e3"
, "00000000-b37a-32b3-19d9-72ec921021e3"
, "What story?"
),
(
"11111111-8713-4275-9f43-4d127671f0ff"
, "00000000-1f7c-43e9-ab61-3c34fd59a333"
, "What story would you tell your grandchild about working in the military?"
);

INSERT INTO dimensional_question (
uid
, survey_uid
, question
, dimension_one
, dimension_two
, dimension_three
) VALUES (
"11111111-3e01-4b2c-b396-1b20facf99c2"
, "00000000-9c88-4b81-9de4-bac7444fbb0a"
, "How would you describe your manager on the following dimensions?"
, "Empathetic"
, "Motivational"
, "Knowledgable"
),
(
"11111111-2b47-4d02-8c48-0fa65f0da016"
, "00000000-0762-4fd1-b927-65ddb494e04f"
, "What came across in your last interaction with the CEO?"
, "Purpose"
, "Audacity"
, "Clarity of Direction"
),
(
"11111111-041a-490d-a60c-82babc856120"
, "00000000-e253-4c39-b32b-eeb4f8e8711d"
, "How would you describe your manager on the following dimensions?"
, "Empathetic"
, "Motivational"
, "Knowledgable"
),
(
"11111111-b36f-4e80-aba4-9707a10d6acf"
, "00000000-e253-4c39-b32b-eeb4f8e8711d"
, "What came across in your last interaction with the CEO?"
, "Purpose"
, "Audacity"
, "Clarity of Direction"
),
(
"11111111-0fcc-484b-bab4-c33309cebd3c"
, "00000000-1f7c-43e9-ab61-3c34fd59a333"
, "How strongly does this story demonstrate the following values?"
, "Empathy to Colleagues"
, "Service to Others"
, "Individual Growth"
),
(
"11111111-b54a-45ee-86af-05f625f3d239"
, "00000000-1f7c-43e9-ab61-3c34fd59a333"
, "How strongly does this story demonstrate the following values?"
, "Accountability"
, "Efficiency"
, "Individual Growth"
);;

COMMIT;
Loading
Loading