|
| 1 | +import pytest |
| 2 | +from mat3ra.utils import assertion |
| 3 | +from . import AutoSnakeCaseTestEntity |
| 4 | + |
| 5 | +BASE = { |
| 6 | + "applicationName": "camelCasedValue", |
| 7 | + "applicationVersion": "camelCasedVersion", |
| 8 | + "executableName": "camelCasedExecutable", |
| 9 | + "contextProviders": [], |
| 10 | +} |
| 11 | + |
| 12 | +INSTANTIATION = [ |
| 13 | + {"application_name": BASE["applicationName"], "application_version": BASE["applicationVersion"], |
| 14 | + "executable_name": BASE["executableName"]}, |
| 15 | + {"applicationName": BASE["applicationName"], "applicationVersion": BASE["applicationVersion"], |
| 16 | + "executableName": BASE["executableName"]}, |
| 17 | + {"application_name": BASE["applicationName"], "applicationVersion": BASE["applicationVersion"], |
| 18 | + "executable_name": BASE["executableName"]}, |
| 19 | +] |
| 20 | + |
| 21 | +UPDATES = [ |
| 22 | + ( |
| 23 | + {"application_name": "new_value", "context_providers": ["item_snake"]}, |
| 24 | + {"applicationName": "new_value", "contextProviders": ["item_snake"]}, |
| 25 | + {"application_name": "new_value", "context_providers": ["item_snake"]}, |
| 26 | + ), |
| 27 | + ( |
| 28 | + {"applicationName": "newValueCamel", "contextProviders": ["itemCamel"]}, |
| 29 | + {"applicationName": "newValueCamel", "contextProviders": ["itemCamel"]}, |
| 30 | + {"application_name": "newValueCamel", "context_providers": ["itemCamel"]}, |
| 31 | + ), |
| 32 | + ( |
| 33 | + {"application_name": "new_value_snake", "applicationVersion": "newVersionCamel"}, |
| 34 | + {"applicationName": "new_value_snake", "applicationVersion": "newVersionCamel"}, |
| 35 | + {"application_name": "new_value_snake", "application_version": "newVersionCamel"}, |
| 36 | + ), |
| 37 | + ( |
| 38 | + {"application_name": "new_val", "application_version": "new_version", |
| 39 | + "executable_name": "new_exec", "context_providers": ["a", "b"]}, |
| 40 | + {"applicationName": "new_val", "applicationVersion": "new_version", |
| 41 | + "executableName": "new_exec", "contextProviders": ["a", "b"]}, |
| 42 | + {"application_name": "new_val", "application_version": "new_version", |
| 43 | + "executable_name": "new_exec", "context_providers": ["a", "b"]}, |
| 44 | + ), |
| 45 | +] |
| 46 | + |
| 47 | + |
| 48 | +def camel(entity): |
| 49 | + return dict( |
| 50 | + applicationName=entity.applicationName, |
| 51 | + applicationVersion=entity.applicationVersion, |
| 52 | + executableName=entity.executableName, |
| 53 | + contextProviders=entity.contextProviders, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def snake(entity): |
| 58 | + return dict( |
| 59 | + application_name=entity.application_name, |
| 60 | + application_version=entity.application_version, |
| 61 | + executable_name=entity.executable_name, |
| 62 | + context_providers=entity.context_providers, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("cfg", INSTANTIATION) |
| 67 | +def test_instantiation(cfg): |
| 68 | + entity = AutoSnakeCaseTestEntity(**cfg) |
| 69 | + assertion.assert_deep_almost_equal(BASE, camel(entity)) |
| 70 | + assertion.assert_deep_almost_equal( |
| 71 | + dict(application_name=BASE["applicationName"], |
| 72 | + application_version=BASE["applicationVersion"], |
| 73 | + executable_name=BASE["executableName"], |
| 74 | + context_providers=[]), |
| 75 | + snake(entity), |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize("updates, exp_camel, exp_snake", UPDATES) |
| 80 | +def test_updates(updates, exp_camel, exp_snake): |
| 81 | + entity = AutoSnakeCaseTestEntity(**BASE) |
| 82 | + for k, v in updates.items(): |
| 83 | + setattr(entity, k, v) |
| 84 | + assertion.assert_deep_almost_equal({**BASE, **exp_camel}, camel(entity)) |
| 85 | + assertion.assert_deep_almost_equal( |
| 86 | + {**snake(AutoSnakeCaseTestEntity(**BASE)), **exp_snake}, |
| 87 | + snake(entity), |
| 88 | + ) |
| 89 | + out = entity.to_dict() |
| 90 | + assertion.assert_deep_almost_equal({**BASE, **exp_camel}, out) |
| 91 | + assert "application_name" not in out and "context_providers" not in out |
0 commit comments