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

Resolve "Test conversion of questions to DataFramen or Series - testing module question"

parent c66fa881
Pipeline #93334 passed with stages
in 3 minutes and 7 seconds
......@@ -105,6 +105,7 @@ output/
# Survey Data
*.csv
!tests/**/*.csv
# Backups from Meld
*.orig
......@@ -27,8 +27,11 @@
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
from hifis_surveyval.models.question import Question
from tests.helper.data_container_helper.data_container_loader import \
DataContainerLoader
from tests.helper.yaml_helper.yaml_reader import YamlReader
......@@ -102,3 +105,27 @@ def question_no_answer_options_fixture() -> Question:
)
question: Question = question_from_metadata(yaml_file_path)
return question
@pytest.fixture(scope="function")
def data_container_load_metadata_and_data_fixture(
metadata_yaml_file_path: str, test_data_csv_file_path: str
) -> DataContainer:
"""
Read in a YAML file and create a dictionary out of it.
Args:
metadata_yaml_file_path (str):
File name of a metadata YAML file to be read in.
test_data_csv_file_path (str):
File name of a metadata CSV file to be read in.
Returns:
DataContainer:
DataContainer containing metadata from YAML file and data from
CSV file.
"""
data_container: DataContainer = \
DataContainerLoader.prepare_data_container(metadata_yaml_file_path,
test_data_csv_file_path)
return data_container
# 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/>.
---
- id: "Q001"
text:
en: "English question text"
de: "Deutscher Fragentext"
label: "English label text"
questions:
- id: "SQ001"
text:
en: "English question text"
de: "Deutscher Fragentext"
label: "English label text"
datatype: "str"
answers:
- id: "A001"
text:
en: "No"
de: "Nein"
label: "No"
- id: "A002"
text:
en: "Yes"
de: "Ja"
label: "Yes"
- id: "A003"
text:
en: "Maybe"
de: "Vielleicht"
label: "Maybe"
mandatory: True
additional_metadata: "metadata"
additional_metadata: "metadata"
...
"id","Q001/SQ001"
"1","A001"
"2","A002"
"3","A003"
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/>.
......@@ -25,11 +25,15 @@
"""Provide pytest test cases for module question."""
import pytest
from pandas import Series
from hifis_surveyval.core.settings import Settings
from hifis_surveyval.data_container import DataContainer
from hifis_surveyval.models.mixins.yaml_constructable import YamlDict
from hifis_surveyval.models.question_collection import Question
from hifis_surveyval.models.translated import Translated
from tests.helper.data_structure_helper.data_structure_creator import \
DataStructureCreator
from tests.helper.yaml_helper.yaml_reader import YamlReader
......@@ -52,7 +56,7 @@ class TestQuestion(object):
def test_from_yaml_dictionary_works_check_type(self) -> None:
"""Tests that getting a Question object given metadata works."""
yaml_file_path: str = (
"./tests/models/question/fixtures/" "metadata-single-question.yml"
"./tests/models/question/fixtures/metadata-single-question.yml"
)
metadata_yaml: YamlDict = YamlReader.read_in_yaml_file(yaml_file_path)
question: Question = Question.from_yaml_dictionary(
......@@ -71,7 +75,7 @@ class TestQuestion(object):
"""Tests that getting a Question object given metadata works."""
expected_question_id: str = TestQuestion.question_id
yaml_file_path: str = (
"./tests/models/question/fixtures/" "metadata-single-question.yml"
"./tests/models/question/fixtures/metadata-single-question.yml"
)
metadata_yaml: YamlDict = YamlReader.read_in_yaml_file(yaml_file_path)
question: Question = Question.from_yaml_dictionary(
......@@ -90,7 +94,7 @@ class TestQuestion(object):
"""Tests that getting a Question object given metadata works."""
expected_translated_answer_option_text: str = "No"
yaml_file_path: str = (
"./tests/models/question/fixtures/" "metadata-single-question.yml"
"./tests/models/question/fixtures/metadata-single-question.yml"
)
metadata_yaml: YamlDict = YamlReader.read_in_yaml_file(yaml_file_path)
question: Question = Question.from_yaml_dictionary(
......@@ -211,3 +215,38 @@ class TestQuestion(object):
assert (
actual_given_answer_value == expected_given_answer_value
), "Given answer of participant is not casted correctly."
@pytest.mark.ci
@pytest.mark.parametrize(
"metadata_yaml_file_path,test_data_csv_file_path",
[
[
"tests/models/question/fixtures/"
"metadata-single-question-collection.yml",
"tests/models/question/fixtures/"
"test_data_for_module_question.csv",
]
],
)
def test_as_series_works_check_series(
self, data_container_load_metadata_and_data_fixture: DataContainer
) -> None:
"""
Tests that unit returns a Series for given collection ID.
Args:
data_container_load_metadata_and_data_fixture (DataContainer):
Fixture that sets up a DataContainer object with metadata and
data to be used in the test cases.
"""
question_collection_id: str = "Q001/SQ001"
expected_data_dict = {"1": "No", "2": "Yes", "3": "Maybe"}
expected_series: Series = DataStructureCreator. \
create_series_from_dict(expected_data_dict, question_collection_id)
question: Question = \
data_container_load_metadata_and_data_fixture \
.question_for_id(question_collection_id)
actual_series: Series = question.as_series()
# Make sure that expected and actual Series are equal.
assert actual_series.equals(expected_series), \
"Expected and actual Series are not equal."
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