Commit b1bbef2c authored by Hueser, Christian (FWCC) - 138593's avatar Hueser, Christian (FWCC) - 138593 Committed by Erxleben, Fredo
Browse files

Resolve "Method constructing a series from a given question ID should set the...

Resolve "Method constructing a series from a given question ID should set the name of the index column"
parent 2ff6bf69
Pipeline #91563 passed with stage
in 1 minute and 14 seconds
......@@ -159,6 +159,10 @@ class FileSettings(BaseSettings):
# if the output format is not screen
OUTPUT_FOLDER: Path = Path("output")
# The key that is used by LimeSurvey to identify the participants' ID
# column
ID_COLUMN_NAME: str = "id"
class Config:
"""
Subclass for specification.
......
......@@ -34,6 +34,7 @@ from typing import Dict, List, Set, Union
import pandas
from pandas import DataFrame
from hifis_surveyval.core.settings import Settings
from hifis_surveyval.models.mixins.identifiable import Identifiable
from hifis_surveyval.models.mixins.yaml_constructable import YamlDict, YamlList
from hifis_surveyval.models.question import Question
......@@ -51,14 +52,18 @@ class DataContainer(object):
answer being given despite being mandatory.
"""
#: Name of the ID column in the Limesurvey CSV data
ID_COLUMN_NAME: str = "id"
def __init__(self, settings: Settings):
"""
Set up an empty data container.
def __init__(self):
"""Set up an empty data container."""
Args:
settings:
An object representing the current application settings.
"""
self._survey_questions: Dict[str, QuestionCollection] = {}
self._invalid_answer_sets: Set[str] = set()
# Track participant IDs with invalid answer sets.
self._settings = settings
@property
def survey_questions(self) -> List[QuestionCollection]:
......@@ -119,7 +124,7 @@ class DataContainer(object):
A YAML mapping containing the data for one question collection.
"""
new_collection = QuestionCollection.from_yaml_dictionary(
new_collection_yaml
new_collection_yaml, settings=self._settings
)
if new_collection.full_id in self._survey_questions:
raise ValueError(
......@@ -146,7 +151,7 @@ class DataContainer(object):
# questions are identical.
# Step 1: Find the column for the participant IDs
id_column_index = header.index(DataContainer.ID_COLUMN_NAME)
id_column_index = header.index(self._settings.ID_COLUMN_NAME)
# Step 2: Find the Question for each of the headings
for index in range(0, len(header)):
......
......@@ -30,6 +30,7 @@ from typing import Dict, List, Optional, Set
import schema
from pandas import Series
from hifis_surveyval.core.settings import Settings
from hifis_surveyval.models.answer_option import AnswerOption
from hifis_surveyval.models.answer_types import VALID_ANSWER_TYPES
from hifis_surveyval.models.mixins.identifiable import Identifiable
......@@ -80,6 +81,7 @@ class Question(YamlConstructable, Identifiable):
answer_type: type,
mandatory: bool,
answer_options: List[AnswerOption],
settings: Settings,
):
"""
Initialize a question object with metadata.
......@@ -110,8 +112,11 @@ class Question(YamlConstructable, Identifiable):
An optional list of predefined answers. If there are none
given, the question can have any answer, otherwise the answer
must be the short ID of the selected answer option.
settings:
An object reflecting the application settings.
"""
super().__init__(question_id, parent_id)
self._settings = settings
self._text = text
self._label = label
self._answer_type = answer_type
......@@ -221,6 +226,7 @@ class Question(YamlConstructable, Identifiable):
"""
series = Series(self._answers)
series.name = self.full_id
series.index.name = self._settings.ID_COLUMN_NAME
return series
@staticmethod
......@@ -231,10 +237,14 @@ class Question(YamlConstructable, Identifiable):
Args:
yaml:
A YAML dictionary describing the Question
**kwargs:
Must contain the ID of the QuestionCollection-instance to which
the newly generated Question belongs as the parameter
"parent_id".
Keyword Args:
parent_id:
(Required) The full ID of the QuestionCollection this Question
belongs to.
settings:
(Required) An object reflecting the applications settings.
Returns:
A new Question containing the provided data
"""
......@@ -250,6 +260,8 @@ class Question(YamlConstructable, Identifiable):
for answer_yaml in yaml[Question.token_ANSWER_OPTIONS]
]
settings: Settings = kwargs["settings"]
return Question(
question_id=question_id,
parent_id=parent_id,
......@@ -258,4 +270,5 @@ class Question(YamlConstructable, Identifiable):
answer_type=answer_type,
answer_options=answer_options,
mandatory=yaml[Question.token_MANDATORY],
settings=settings
)
......@@ -167,16 +167,20 @@ class QuestionCollection(YamlConstructable, Identifiable):
Args:
yaml:
A YAML dictionary describing the Question
**kwargs:
Only here to satisfy the inherited method signature.
Keyword Args:
settings:
(Required) An object representing the application settings.
Returns:
A new Question containing the provided data
"""
collection_id = yaml[QuestionCollection.token_ID]
settings = kwargs["settings"]
questions = [
Question.from_yaml_dictionary(
yaml=question_yaml, parent_id=collection_id
yaml=question_yaml, parent_id=collection_id, settings=settings
)
for question_yaml in yaml[QuestionCollection.token_QUESTIONS]
]
......
#!/usr/bin/env python
# hifis-surveyval
# Framework to help developing analysis scripts for the HIFIS Software survey.
#
# SPDX-FileCopyrightText: 2021 HIFIS Software <support@hifis.net>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
"""This package contains all test cases of package core."""
#!/usr/bin/env python
# hifis-surveyval
# Framework to help developing analysis scripts for the HIFIS Software survey.
#
# SPDX-FileCopyrightText: 2021 HIFIS Software <support@hifis.net>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
"""This package contains all test cases of module settings."""
......@@ -28,6 +28,7 @@ from typing import List, Union
import pytest
from hifis_surveyval.core.settings import Settings
from hifis_surveyval.data_container import DataContainer
from hifis_surveyval.models.mixins.yaml_constructable import YamlDict, YamlList
from tests.helper.csv_helper.csv_reader import CsvReader
......@@ -43,7 +44,7 @@ def data_container_fixture() -> DataContainer:
DataContainer:
New DataContainer object containing an empty DataFrame object.
"""
return DataContainer()
return DataContainer(Settings())
@pytest.fixture(scope="function")
......@@ -99,7 +100,7 @@ def load_metadata(metadata_yaml_file_path: str) -> DataContainer:
DataContainer:
DataContainer containing metadata from YAML file.
"""
data_container: DataContainer = DataContainer()
data_container: DataContainer = DataContainer(Settings())
metadata: Union[YamlList, YamlDict] = YamlReader.read_in_yaml_file(
metadata_yaml_file_path
)
......@@ -125,7 +126,7 @@ def load_metadata_and_data(
DataContainer containing metadata from YAML file and data from
CSV file.
"""
data_container: DataContainer = DataContainer()
data_container: DataContainer = DataContainer(Settings())
metadata: Union[YamlList, YamlDict] = YamlReader.read_in_yaml_file(
metadata_yaml_file_path
)
......
......@@ -26,6 +26,7 @@
import pytest
from hifis_surveyval.core.settings import Settings
from hifis_surveyval.models.mixins.yaml_constructable import YamlDict
from hifis_surveyval.models.question import Question
from tests.helper.yaml_helper.yaml_reader import YamlReader
......@@ -46,7 +47,9 @@ def question_from_metadata(yaml_file_path: str) -> Question:
target_question_collection_id: str = "Q001"
metadata_yaml: YamlDict = YamlReader.read_in_yaml_file(yaml_file_path)
question: Question = Question.from_yaml_dictionary(
metadata_yaml[0], parent_id=target_question_collection_id
metadata_yaml[0],
parent_id=target_question_collection_id,
settings=Settings()
)
return question
......
......@@ -26,6 +26,7 @@
import pytest
from hifis_surveyval.core.settings import Settings
from hifis_surveyval.models.mixins.yaml_constructable import YamlDict
from hifis_surveyval.models.question_collection import Question
from hifis_surveyval.models.translated import Translated
......@@ -55,7 +56,9 @@ class TestQuestion(object):
)
metadata_yaml: YamlDict = YamlReader.read_in_yaml_file(yaml_file_path)
question: Question = Question.from_yaml_dictionary(
metadata_yaml[0], parent_id=TestQuestion.collection_id
metadata_yaml[0],
parent_id=TestQuestion.collection_id,
settings=Settings()
)
# Make sure that object retrieved from metadata YAML is of type
# Question.
......@@ -72,7 +75,9 @@ class TestQuestion(object):
)
metadata_yaml: YamlDict = YamlReader.read_in_yaml_file(yaml_file_path)
question: Question = Question.from_yaml_dictionary(
metadata_yaml[0], parent_id=TestQuestion.collection_id
metadata_yaml[0],
parent_id=TestQuestion.collection_id,
settings=Settings()
)
# Make sure that Question object retrieved from metadata YAML has
# correct question ID.
......@@ -89,7 +94,9 @@ class TestQuestion(object):
)
metadata_yaml: YamlDict = YamlReader.read_in_yaml_file(yaml_file_path)
question: Question = Question.from_yaml_dictionary(
metadata_yaml[0], parent_id=TestQuestion.collection_id
metadata_yaml[0],
parent_id=TestQuestion.collection_id,
settings=Settings()
)
answer_option_text: Translated = question._answer_options[
TestQuestion.answer_option_id
......
......@@ -26,6 +26,7 @@
import pytest
from hifis_surveyval.core.settings import Settings
from hifis_surveyval.models.mixins.yaml_constructable import YamlDict
from hifis_surveyval.models.question import Question
from hifis_surveyval.models.question_collection import QuestionCollection
......@@ -56,7 +57,8 @@ class TestQuestionCollection(object):
"""
metadata_yaml: YamlDict = metadata_fixture
question_collection: QuestionCollection = (
QuestionCollection.from_yaml_dictionary(metadata_yaml[0])
QuestionCollection.from_yaml_dictionary(metadata_yaml[0],
settings=Settings())
)
# Make sure that QuestionCollection object can be retrieved by
# metadata YAML file.
......@@ -76,7 +78,8 @@ class TestQuestionCollection(object):
expected_translated_answer_option_text: str = "No"
metadata_yaml: YamlDict = metadata_fixture
question_collection: QuestionCollection = (
QuestionCollection.from_yaml_dictionary(metadata_yaml[0])
QuestionCollection.from_yaml_dictionary(metadata_yaml[0],
settings=Settings())
)
answer_option_text: Translated = (
question_collection.question_for_id(
......@@ -107,7 +110,8 @@ class TestQuestionCollection(object):
"""
metadata_yaml: YamlDict = metadata_fixture
question_collection: QuestionCollection = (
QuestionCollection.from_yaml_dictionary(metadata_yaml[0])
QuestionCollection.from_yaml_dictionary(metadata_yaml[0],
settings=Settings())
)
question: Question = question_collection.question_for_id(
TestQuestionCollection.question_id
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment