Commit 02fc3258 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_collection"

parent 6fa4dbc0
Pipeline #93345 passed with stages
in 3 minutes and 14 seconds
......@@ -26,7 +26,10 @@
import pytest
from hifis_surveyval.data_container import DataContainer
from hifis_surveyval.models.mixins.yaml_constructable import YamlDict
from tests.helper.data_container_helper.data_container_loader import \
DataContainerLoader
from tests.helper.yaml_helper.yaml_reader import YamlReader
......@@ -44,3 +47,43 @@ def metadata_fixture() -> YamlDict:
"metadata-single-question-collection.yml"
)
return YamlReader.read_in_yaml_file(yaml_file_path)
@pytest.fixture(scope="function")
def metadata_question_collection_three_questions_fixture() -> YamlDict:
"""
Get metadata from YAML file.
Returns:
YamlDict:
YAML containing metadata.
"""
yaml_file_path: str = (
"./tests/models/question_collection/fixtures/"
"metadata-single-question-collection-three-questions.yml"
)
return YamlReader.read_in_yaml_file(yaml_file_path)
@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: "I do not know"
de: "Ich weiß nicht"
label: "I do not know"
mandatory: True
additional_metadata: "metadata"
- id: "SQ002"
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: "I do not know"
de: "Ich weiß nicht"
label: "I do not know"
mandatory: True
additional_metadata: "metadata"
- id: "SQ003"
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: "I do not know"
de: "Ich weiß nicht"
label: "I do not know"
mandatory: True
additional_metadata: "metadata"
additional_metadata: "metadata"
...
"id","Q001/SQ001","Q001/SQ002","Q001/SQ003"
"1","A001","A002","A003"
"2","A002","A003","A001"
"3","A003","A001","A002"
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/>.
......@@ -23,14 +23,19 @@
# -*- coding: utf-8 -*-
"""Provide pytest test cases for module question_collection."""
from typing import List
import pytest
from pandas import DataFrame
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 hifis_surveyval.models.question_collection import QuestionCollection
from hifis_surveyval.models.translated import Translated
from tests.helper.data_structure_helper.data_structure_creator import \
DataStructureCreator
class TestQuestionCollection(object):
......@@ -120,3 +125,80 @@ class TestQuestionCollection(object):
assert isinstance(
question, Question
), "Object is not of type Question."
@pytest.mark.ci
@pytest.mark.parametrize(
"metadata_yaml_file_path,test_data_csv_file_path",
[
[
"tests/models/question_collection/fixtures/"
"metadata-single-question-collection-three-questions.yml",
"tests/models/question_collection/fixtures/"
"test_data_for_module_question_collection.csv"
],
],
)
def test_as_data_frame_works(
self,
data_container_load_metadata_and_data_fixture: DataContainer):
"""
Tests that the DataFrame retrieved from Collection is correct.
Args:
data_container_load_metadata_and_data_fixture (DataContainer):
DataContainer containing metadata from YAML file and data from
CSV file.
"""
expected_data_dict = {"id": ["1", "2", "3"],
"Q001/SQ001": ["No", "Yes", "I do not know"],
"Q001/SQ002": ["Yes", "I do not know", "No"],
"Q001/SQ003": ["I do not know", "No", "Yes"]}
expected_frame: DataFrame = DataStructureCreator. \
create_dataframe_from_dict(expected_data_dict)
question_collection_id: str = "Q001"
question_collection: QuestionCollection = \
data_container_load_metadata_and_data_fixture \
.collection_for_id(question_collection_id)
actual_data_frame: DataFrame = question_collection.as_data_frame()
# Make sure that expected and actual DataFrames are equal.
assert actual_data_frame.equals(expected_frame), \
"Expected and actual DataFrames are not equal."
@pytest.mark.ci
@pytest.mark.parametrize(
"metadata_yaml_file_path,test_data_csv_file_path",
[
[
"tests/models/question_collection/fixtures/"
"metadata-single-question-collection-three-questions.yml",
"tests/models/question_collection/fixtures/"
"test_data_for_module_question_collection.csv"
],
],
)
def test_as_data_frame_works_check_question_subset(
self,
data_container_load_metadata_and_data_fixture: DataContainer):
"""
Tests that the DataFrame subset retrieved from Collection is correct.
Args:
data_container_load_metadata_and_data_fixture (DataContainer):
DataContainer containing metadata from YAML file and data from
CSV file.
"""
expected_data_dict = {"id": ["1", "2", "3"],
"Q001/SQ001": ["No", "Yes", "I do not know"],
"Q001/SQ003": ["I do not know", "No", "Yes"]}
expected_frame: DataFrame = DataStructureCreator. \
create_dataframe_from_dict(expected_data_dict)
question_collection_id: str = "Q001"
exclude_list_question_ids: List[str] = ["SQ002"]
question_collection: QuestionCollection = \
data_container_load_metadata_and_data_fixture \
.collection_for_id(question_collection_id)
actual_data_frame: DataFrame = \
question_collection.as_data_frame(exclude_list_question_ids)
# Make sure that expected and actual DataFrames are equal.
assert actual_data_frame.equals(expected_frame), \
"Expected and actual DataFrames 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