Skip to content

Commit

Permalink
Add extra unit cases for test_mapper.py
Browse files Browse the repository at this point in the history
  • Loading branch information
san99tiago committed Sep 2, 2023
1 parent 0a32c03 commit e677720
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 8 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: JSON Experiments with JSONPATH-NG and FastAPI

on:
push:
branches: [ 'main', 'feature/**']
tags: [ 'v*']

jobs:

code-quality:
name: Check coding standards
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Install poetry
run: pipx install poetry

- uses: actions/setup-python@v4
with:
python-version: 3.11
cache: poetry

- name: Install Python dependencies
run: poetry install

- name: Check code formatting
run: poetry run poe black-check


test:
name: Run tests
needs: code-quality
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Install poetry
run: pipx install poetry

- uses: actions/setup-python@v4
with:
python-version: 3.11
cache: poetry

- name: Install dependencies
run: poetry install

- name: Run tests
run: poetry run poe test-unit

- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: code-coverage-report
path: htmlcov
172 changes: 172 additions & 0 deletions tests/unit/mapper/test_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import pytest
from src.mapper.mapper import Mapper
from jsonpath_ng.jsonpath import JSONPath


# Mocking os.path.join to avoid reading a real JSON mapper file
@pytest.fixture
def mock_os_path_join(mocker):
return mocker.patch("os.path.join", return_value="fake_mapper_path")


def test_mapper_init_without_json_mapper(mock_os_path_join, mocker):
mock_os_path_join.return_value = "fake_mapper_path"
mock_open = mocker.patch(
"src.mapper.mapper.open", mocker.mock_open(read_data='{"mapper": "[]"}')
)

mapper = Mapper({})

assert mock_os_path_join.call_count == 2
mock_open.assert_called_once_with("fake_mapper_path", "r")
assert "mapper" in mapper.json_mapper
assert mapper.payload == {}


def test_mapper_init_with_json_mapper(mock_os_path_join):
json_mapper = {"mapper": []}
mapper = Mapper({}, json_mapper=json_mapper)
mock_os_path_join.assert_not_called()
assert mapper.json_mapper == json_mapper
assert mapper.payload == {}


def test_mapper_extract_indexes():
mapper = Mapper({})
assert mapper.extract_indexes("hello.value") == []
assert mapper.extract_indexes("hello.list1[0].value.list2[3].othervalue") == [0, 3]
assert mapper.extract_indexes("hello.list1[2].value") == [2]


def test_mapper_inject_indexes():
mapper = Mapper({})
assert mapper.inject_indexes("value", []) == "value"
assert (
mapper.inject_indexes("list1[*].value.list2[*].result", [1, 2])
== "list1[1].value.list2[2].result"
)
assert mapper.inject_indexes("list1[*].value", [2]) == "list1[2].value"


def test_mapper_apply_transform_logic_with_default():
mapper = Mapper({})
logic = {
"CASE": {
"enumsMapping": [{"inputValue": "input", "outputValue": "output"}],
"default": "defaultValue",
}
}

match_value_in_case = "input"
transformed_value = mapper.apply_transform_logic(match_value_in_case, logic)
assert transformed_value == "output"

match_value_not_in_case = "unknown"
transformed_value = mapper.apply_transform_logic(match_value_not_in_case, logic)
assert transformed_value == "defaultValue"


def test_mapper_apply_transform_logic_without_default():
mapper = Mapper({})
logic = {
"CASE": {
"enumsMapping": [{"inputValue": "input", "outputValue": "output"}],
}
}

match_value_not_in_case = "other"
transformed_value = mapper.apply_transform_logic(match_value_not_in_case, logic)
assert transformed_value == "other"


def test_mapper_convert_payload_direct():
payload = {
"key1": "value1",
"key2": "value2",
"key3": {"inner_key": "inner_value"},
}

json_mapper = {
"mapper": [
{"source": "$.key1", "destination": "new_key1"},
{"source": "$.key2", "destination": "super.cool.new_key2"},
{"source": "$.key3.inner_key", "destination": "another.cool.new_key3"},
{"source": "$.key4", "destination": "new_key4"},
]
}

mapper = Mapper(payload, json_mapper)
transformed_payload = mapper.convert_payload()

assert transformed_payload == {
"another": {"cool": {"new_key3": "inner_value"}},
"new_key1": "value1",
"super": {"cool": {"new_key2": "value2"}},
}


def test_mapper_convert_payload_multiple():
payload = {
"myList": [
{
"key1": "value1A",
"key2": "value2A",
"key3": "value3A",
},
{
"key1": "value1B",
"key2": "value2B",
"key3": "value3B",
},
{
"key1": "value1C",
"key2": "value2C",
"key3": "value3C",
"key4": "value4C",
},
],
}

json_mapper = {
"mapper": [
{"source": "$.myList[*].key1", "destination": "hi.newList[*].id1"},
{
"source": "$.myList[*].key2",
"destination": "hi.newList[*].id2",
"logic": {
"CASE": {
"enumsMapping": [
{
"inputValue": "value2B",
"outputValue": "SUPER_TRANSFORMED_VALUE_2B",
}
],
}
},
},
{"source": "$.myList[*].key3", "destination": "hi.newList[*].id3"},
{"source": "$.myList[*].key4", "destination": "hi.newList[*].id4"},
]
}

mapper = Mapper(payload, json_mapper)
transformed_payload = mapper.convert_payload()

assert transformed_payload == {
"hi": {
"newList": [
{"id1": "value1A", "id2": "value2A", "id3": "value3A"},
{
"id1": "value1B",
"id2": "SUPER_TRANSFORMED_VALUE_2B",
"id3": "value3B",
},
{
"id1": "value1C",
"id2": "value2C",
"id3": "value3C",
"id4": "value4C",
},
]
}
}
8 changes: 0 additions & 8 deletions tests/unit/mapper/test_validator.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import os
import json
import pytest
from jsonschema import ValidationError
from src.mapper.validator import Validator


# Mocking jsonschema.validate to control validation results
@pytest.fixture
def mock_jsonschema_validate(mocker):
return mocker.patch("jsonschema.validate")


def test_validator_init_without_schema(mocker):
# Mocking "open" and "join" to avoid OS files usage
mock_os_path_join = mocker.patch("os.path.join", return_value="fake_schema_path")
Expand Down

0 comments on commit e677720

Please sign in to comment.