|
3 | 3 | #
|
4 | 4 |
|
5 | 5 | import json
|
| 6 | +from copy import deepcopy |
6 | 7 | from unittest.mock import MagicMock
|
7 | 8 |
|
8 | 9 | import pytest
|
@@ -286,3 +287,93 @@ def test_dynamic_schema_loader_manifest_flow():
|
286 | 287 |
|
287 | 288 | assert len(actual_catalog.streams) == 1
|
288 | 289 | assert actual_catalog.streams[0].json_schema == expected_schema
|
| 290 | + |
| 291 | + |
| 292 | +def test_dynamic_schema_loader_with_type_conditions(): |
| 293 | + _MANIFEST_WITH_TYPE_CONDITIONS = deepcopy(_MANIFEST) |
| 294 | + _MANIFEST_WITH_TYPE_CONDITIONS["definitions"]["party_members_stream"]["schema_loader"][ |
| 295 | + "schema_type_identifier" |
| 296 | + ]["types_mapping"].append( |
| 297 | + { |
| 298 | + "target_type": "number", |
| 299 | + "current_type": "formula", |
| 300 | + "condition": "{{ raw_schema['result']['type'] == 'number' }}", |
| 301 | + } |
| 302 | + ) |
| 303 | + _MANIFEST_WITH_TYPE_CONDITIONS["definitions"]["party_members_stream"]["schema_loader"][ |
| 304 | + "schema_type_identifier" |
| 305 | + ]["types_mapping"].append( |
| 306 | + { |
| 307 | + "target_type": "number", |
| 308 | + "current_type": "formula", |
| 309 | + "condition": "{{ raw_schema['result']['type'] == 'currency' }}", |
| 310 | + } |
| 311 | + ) |
| 312 | + _MANIFEST_WITH_TYPE_CONDITIONS["definitions"]["party_members_stream"]["schema_loader"][ |
| 313 | + "schema_type_identifier" |
| 314 | + ]["types_mapping"].append({"target_type": "array", "current_type": "formula"}) |
| 315 | + |
| 316 | + expected_schema = { |
| 317 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 318 | + "type": "object", |
| 319 | + "properties": { |
| 320 | + "id": {"type": ["null", "integer"]}, |
| 321 | + "first_name": {"type": ["null", "string"]}, |
| 322 | + "description": {"type": ["null", "string"]}, |
| 323 | + "static_field": {"type": ["null", "string"]}, |
| 324 | + "currency": {"type": ["null", "number"]}, |
| 325 | + "salary": {"type": ["null", "number"]}, |
| 326 | + "working_days": {"type": ["null", "array"]}, |
| 327 | + }, |
| 328 | + } |
| 329 | + source = ConcurrentDeclarativeSource( |
| 330 | + source_config=_MANIFEST_WITH_TYPE_CONDITIONS, config=_CONFIG, catalog=None, state=None |
| 331 | + ) |
| 332 | + with HttpMocker() as http_mocker: |
| 333 | + http_mocker.get( |
| 334 | + HttpRequest(url="https://api.test.com/party_members"), |
| 335 | + HttpResponse( |
| 336 | + body=json.dumps( |
| 337 | + [ |
| 338 | + { |
| 339 | + "id": 1, |
| 340 | + "first_name": "member_1", |
| 341 | + "description": "First member", |
| 342 | + "salary": 20000, |
| 343 | + "currency": 10.4, |
| 344 | + "working_days": ["Monday", "Tuesday"], |
| 345 | + }, |
| 346 | + { |
| 347 | + "id": 2, |
| 348 | + "first_name": "member_2", |
| 349 | + "description": "Second member", |
| 350 | + "salary": 22000, |
| 351 | + "currency": 10.4, |
| 352 | + "working_days": ["Tuesday", "Wednesday"], |
| 353 | + }, |
| 354 | + ] |
| 355 | + ) |
| 356 | + ), |
| 357 | + ) |
| 358 | + http_mocker.get( |
| 359 | + HttpRequest(url="https://api.test.com/party_members/schema"), |
| 360 | + HttpResponse( |
| 361 | + body=json.dumps( |
| 362 | + { |
| 363 | + "fields": [ |
| 364 | + {"name": "Id", "type": "integer"}, |
| 365 | + {"name": "FirstName", "type": "string"}, |
| 366 | + {"name": "Description", "type": "singleLineText"}, |
| 367 | + {"name": "Salary", "type": "formula", "result": {"type": "number"}}, |
| 368 | + {"name": "Currency", "type": "formula", "result": {"type": "currency"}}, |
| 369 | + {"name": "WorkingDays", "type": "formula"}, |
| 370 | + ] |
| 371 | + } |
| 372 | + ) |
| 373 | + ), |
| 374 | + ) |
| 375 | + |
| 376 | + actual_catalog = source.discover(logger=source.logger, config=_CONFIG) |
| 377 | + |
| 378 | + assert len(actual_catalog.streams) == 1 |
| 379 | + assert actual_catalog.streams[0].json_schema == expected_schema |
0 commit comments