Skip to content

Commit

Permalink
tests: add strategies and data dictionary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhidg committed Nov 6, 2024
1 parent 305505b commit 1aad848
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"Common fixtures"

from pathlib import Path

import pytest

import arcmapper

dictionary_file = str(Path(__file__).parent / "data" / "ccpuk_dictionary.csv")
arc_file = str(Path(__file__).parent / "data" / "ARCH.csv")

@pytest.fixture(scope="session")
def arc_schema():
return arcmapper.read_arc_schema(arc_file)

@pytest.fixture(scope="session")
def data_dictionary():
return arcmapper.read_data_dictionary(dictionary_file, description_field="Field Label",
response_field="Choices, Calculations, OR Slider Labels", response_func="redcap")

39 changes: 39 additions & 0 deletions tests/test_dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pandas as pd

from arcmapper.dictionary import read_from_jsonschema


EXAMPLE_JSON_SCHEMA = """{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"category": {
"type": "string",
"enum": ["option1", "option2", "option3"],
"description": "category of the variable"
},
"value": {
"type": "number",
"description": "default value of variable"
},
"full_name": {
"type": "string",
"description": "full name of variable"
}
},
"required": ["category", "value", "full_name"]
}"""

def test_read_data_dictionary(data_dictionary):
assert data_dictionary.columns.tolist()== ["variable", "description", "responses", "type"]


def test_read_from_jsonschema():
dd = read_from_jsonschema(EXAMPLE_JSON_SCHEMA)
print(dd)
expected = pd.DataFrame({'variable': ["category", "value", "full_name"],
'description': ['category of the variable', 'default value of variable', 'full name of variable'],
'responses': [[('option1', 'option1'),('option2', 'option2'),('option3', 'option3')], None, None],
'type': ['categorical', 'number', 'string']})
assert dd.equals(expected)

19 changes: 19 additions & 0 deletions tests/test_strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pathlib import Path
import numpy as np

from arcmapper.strategies import map, get_categorical_mapping

dictionary_file = Path(__file__).parent / "data" / "ccpuk_dictionary.csv"
arc_file = Path(__file__).parent / "data" / "ARCH.csv"

def test_get_categorical_mapping():
sim = np.array([[0.1, 0.8], [0.9, 0.1]])
target = ["male", "female"]
source = ["femme", "homme"]
assert get_categorical_mapping(source, target, sim) == {"femme": "female", "homme": "male"}


def test_tf_idf(data_dictionary, arc_schema):
map("tf-idf", data_dictionary, arc_schema, num_matches=3)


0 comments on commit 1aad848

Please sign in to comment.