Skip to content

Commit e4b915e

Browse files
committed
add tests
1 parent e1d0ebe commit e4b915e

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

tests/unit/synapseclient/extensions/unit_test_create_json_schema.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import os
99
from typing import Any, Optional
10+
from unittest import mock
1011
from unittest.mock import Mock
1112

1213
import pytest
@@ -101,6 +102,8 @@ def fixture_test_nodes(
101102
"EnumNotRequired",
102103
"InRange",
103104
"Regex",
105+
"CheckRegexSingle",
106+
"CheckRegexFormat",
104107
"Date",
105108
"URL",
106109
"List",
@@ -327,6 +330,18 @@ def test_update_property(self) -> None:
327330
("InRange", AtomicColumnType.NUMBER, False, 50, 100, None, None),
328331
# Node with "regex search [a-f]" validation rule and columnType "string" - pattern is set, type is STRING
329332
("Regex", AtomicColumnType.STRING, False, None, None, "[a-f]", None),
333+
# Node with "[a-f]" pattern column specification and columnType "string" - pattern is set, type is STRING
334+
("CheckRegexSingle", AtomicColumnType.STRING, False, None, None, "[a-b]", None),
335+
# Node with "regex search [a-f]" validation rule, "^[a-b]" pattern column specification, and columnType "string" - pattern is set, type is STRING
336+
(
337+
"CheckRegexFormat",
338+
AtomicColumnType.STRING,
339+
False,
340+
None,
341+
None,
342+
"^[a-b]",
343+
None,
344+
),
330345
# Node with "date" validation rule and columnType "string" - format is set to DATE, type is STRING
331346
(
332347
"Date",
@@ -350,6 +365,8 @@ def test_update_property(self) -> None:
350365
"List",
351366
"InRange",
352367
"Regex",
368+
"CheckRegexSingle",
369+
"CheckRegexFormat",
353370
"Date",
354371
"URI",
355372
"ListBoolean",
@@ -365,6 +382,7 @@ def test_node_init(
365382
expected_pattern: Optional[str],
366383
expected_format: Optional[JSONSchemaFormat],
367384
test_nodes: dict[str, TraversalNode],
385+
caplog,
368386
) -> None:
369387
"""
370388
Tests for TraversalNode class initialization.
@@ -377,13 +395,17 @@ def test_node_init(
377395
The type property comes from the columnType field, while constraints
378396
come from parsing validation rules like "str", "inRange", "regex", etc.
379397
"""
398+
380399
node = test_nodes[node_name]
381400
assert node.type == expected_type
382401
assert node.format == expected_format
383402
assert node.is_array == expected_is_array
384403
assert node.minimum == expected_min
385404
assert node.maximum == expected_max
386405
assert node.pattern == expected_pattern
406+
if node_name == "Regex":
407+
warning_message = "A regex validation rule is set for property: Regex, but the pattern is not set in the data model."
408+
assert warning_message in test_nodes[node_name].logger.mock_calls[0][1][0]
387409

388410

389411
@pytest.mark.parametrize(
@@ -542,26 +564,29 @@ def test_get_validation_rule_based_fields(
542564
Test for _get_validation_rule_based_fields
543565
Tests that output is expected based on the input validation rules
544566
"""
545-
logger = Mock()
546-
(
547-
is_array,
548-
property_format,
549-
minimum,
550-
maximum,
551-
pattern,
552-
) = _get_validation_rule_based_fields(
553-
validation_rules,
554-
explicit_is_array=explicit_is_array,
555-
explicit_format=explicit_format,
556-
name="name",
557-
column_type=AtomicColumnType.STRING,
558-
logger=logger,
559-
)
560-
assert is_array == expected_is_array
561-
assert property_format == expected_format
562-
assert minimum == expected_min
563-
assert maximum == expected_max
564-
assert pattern == expected_pattern
567+
logger = logging.getLogger("synapseclient.extensions.curator.schema_generation")
568+
with mock.patch.object(logger, "warning") as mock_logger:
569+
(
570+
is_array,
571+
property_format,
572+
minimum,
573+
maximum,
574+
pattern,
575+
) = _get_validation_rule_based_fields(
576+
validation_rules,
577+
explicit_is_array=explicit_is_array,
578+
explicit_format=explicit_format,
579+
name="name",
580+
column_type=AtomicColumnType.STRING,
581+
logger=mock_logger,
582+
)
583+
assert is_array == expected_is_array
584+
assert property_format == expected_format
585+
assert minimum == expected_min
586+
assert maximum == expected_max
587+
assert pattern == expected_pattern
588+
if expected_pattern:
589+
print(expected_pattern)
565590

566591

567592
def test_get_validation_rule_based_fields_inrange_warning(caplog) -> None:

0 commit comments

Comments
 (0)