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

Feature/adding questions #6

Merged
merged 9 commits into from
Feb 19, 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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ setup-db:
| tr '\n' ' ' \
| sed "s|value='\(.*\)'|sqlite3 '\1' < data/setup.sql|")

populate-db:
$(shell yq -o='shell' '.env_variables.SQLITE_FILE' config/local.toml \
| tr '\n' ' ' \
| sed "s|value='\(.*\)'|sqlite3 '\1' < data/initial_data.sql|")

.PHONY: all test venv run clean coverage


10 changes: 8 additions & 2 deletions 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
from app.models import Survey, TextQuestion


class DataService:
Expand All @@ -21,4 +21,10 @@ def get_survey_if_open(self, survey_uid: UUID) -> Survey | None:
return None

def insert_survey(self, survey: Survey) -> None:
return self._driver.insert_survey(survey)
return self._driver.insert_survey(survey=survey)

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

def get_text_questions_from_survey(self, survey_uid: UUID) -> list[TextQuestion]:
return self._driver.get_text_questions_from_survey(survey_uid=survey_uid)
22 changes: 21 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
from app.models import Survey, TextQuestion


class Sqlite3Driver:
Expand Down Expand Up @@ -46,3 +46,23 @@ def insert_survey(self, survey: Survey) -> None:

with self._get_cursor() as cursor:
cursor.execute(query, (str(survey.uid), survey.name, survey.is_open))

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

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

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

def get_text_questions_from_survey(self, survey_uid: UUID) -> list[TextQuestion]:
query = "SELECT * FROM text_question WHERE survey_uid = ?;"

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

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

from .survey import Survey
from .text_question import TextQuestion
13 changes: 13 additions & 0 deletions app/models/base_data_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pydantic import BaseModel


class BaseDataModel(BaseModel):
""" """

def __eq__(self, other: object) -> bool:
if not isinstance(other, self.__class__):
return NotImplemented
return all(
self.__getattribute__(field) == other.__getattribute__(field)
for field in self.__annotations__.keys()
)
Empty file removed app/models/question.py
Empty file.
18 changes: 5 additions & 13 deletions app/models/survey.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from pydantic import BaseModel, Field
from uuid import UUID, uuid4
from pydantic import Field


class Survey(BaseModel):
from .base_data_model import BaseDataModel


class Survey(BaseDataModel):
uid: UUID = Field(default_factory=uuid4)
name: str
is_open: bool

def __eq__(self, other: object) -> bool:
if not isinstance(other, Survey):
return NotImplemented
return all(
(
self.uid == other.uid,
self.name == other.name,
self.is_open == other.is_open,
)
)
10 changes: 10 additions & 0 deletions app/models/text_question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from uuid import UUID, uuid4
from pydantic import Field

from .base_data_model import BaseDataModel


class TextQuestion(BaseDataModel):
uid: UUID = Field(default_factory=uuid4)
survey_uid: UUID
question: str
5 changes: 3 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ def get_open_surveys() -> Dict[str, Any]:
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 = 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)
return render_template("survey.html", survey=survey, questions=questions)


@app.route("/surveys/new")
Expand Down
10 changes: 8 additions & 2 deletions app/templates/survey.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{% extends '_layout.html' %}

{% block content %}
<h3>{{survey.name}}</h3>
<h1>{{survey.name}}</h3>

Not Implemented
<div>
{% for question in questions %}
<h2>{{question.question}}</h2>
{% endfor %}
<div>

Answering Questions is not yet implemented.

{% endblock %}
23 changes: 23 additions & 0 deletions data/initial_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
INSERT INTO survey (
uid
, name
, is_open
) VALUES (
'4b5bfb06-2060-4abf-b5fd-3bae5dcf72b9'
, 'Example Survey #1'
, TRUE
);

INSERT INTO text_question (
uid
, survey_uid
, 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?'
);
11 changes: 1 addition & 10 deletions data/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@ CREATE TABLE IF NOT EXISTS survey (
, name TEXT
);

CREATE TABLE IF NOT EXISTS question (
CREATE TABLE IF NOT EXISTS text_question (
uid TEXT PRIMARY KEY
, survey_uid TEXT
, question TEXT
, FOREIGN KEY(survey_uid) REFERENCES survey(uid)
);

CREATE TABLE IF NOT EXISTS ranking (
uid TEXT PRIMARY KEY
, question_uid TEXT
, first_dimension TEXT
, second_dimension TEXT
, third_dimension TEXT
, FOREIGN KEY(question_uid) REFERENCES question(uid)
);

COMMIT;
17 changes: 13 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest
from pathlib import Path
from uuid import uuid4
from uuid import uuid4, UUID

from app.routes import app
from app.data_service.sqlite3 import Sqlite3Driver
from app.models import Survey
from app.models import Survey, TextQuestion


@pytest.fixture
Expand Down Expand Up @@ -47,9 +47,18 @@ def app_client():


@pytest.fixture
def new_survey_open():
# survey with a random UUID
def open_survey() -> Survey:
return Survey(
uid=UUID("f21ccd82-83d6-40bc-8e60-703382f73860"),
name="Test Survey",
is_open=True,
)


@pytest.fixture
def text_question() -> TextQuestion:
return TextQuestion(
uid=UUID("8c7d6885-0ebb-4870-a8e1-a4630497a089"),
survey_uid=UUID("f21ccd82-83d6-40bc-8e60-703382f73860"),
question="When is it time?",
)
32 changes: 30 additions & 2 deletions tests/test_assets/test_db_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,37 @@ INSERT INTO survey (
(
"00000000-a087-4fb6-a123-24ff30263530"
, true
, "Open Test Survey"
, "Open Test Survey - 1Q"
),
(
"00000000-b37a-32b3-19d9-72ec921021e3"
, true
, "Open Test Survey - 2Qs"
);


INSERT INTO text_question (
uid
, survey_uid
, question
) VALUES (
"11111111-9c88-4b81-9de4-bac7444fbb0a"
, "00000000-9c88-4b81-9de4-bac7444fbb0a"
, "What story would you tell your best friend about this company?"
),
(
"11111111-a087-4fb6-a123-24ff30263530"
, "00000000-a087-4fb6-a123-24ff30263530"
, "What stands out to you about current quarterly plan?"
),
(
"11111111-b37a-32b3-19d9-72ec921021e3"
, "00000000-b37a-32b3-19d9-72ec921021e3"
, "What story?"
),
(
"11111111-b37a-44a1-19d9-72ec921021e3"
, "00000000-b37a-32b3-19d9-72ec921021e3"
, "What story?"
);

COMMIT;
Loading
Loading