From 31a4d73f119e3ac928b1db175835e84dbac2b36f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Rami=CC=81rez=20Mondrago=CC=81n?= Date: Tue, 3 Jan 2023 16:07:52 -0600 Subject: [PATCH] Use `caplog` fixture --- tests/core/test_typing.py | 128 ++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 67 deletions(-) diff --git a/tests/core/test_typing.py b/tests/core/test_typing.py index 726d1fc89..99a306b29 100644 --- a/tests/core/test_typing.py +++ b/tests/core/test_typing.py @@ -1,7 +1,9 @@ """Test _typing - specifically conform_record_data_types().""" + import datetime import logging -import unittest + +import pytest from singer_sdk.helpers._typing import ( ConformanceLevel, @@ -156,72 +158,64 @@ def test_nested_objects_are_conformed(): assert actual_output == expected_output -class TestSimpleEval(unittest.TestCase): - def test_simple_schema_removes_types(self): - schema = PropertiesList( - Property("keep", StringType), - ).to_dict() - - record = {"keep": "hello", "remove": "goodbye"} - - expected_output = {"keep": "hello"} - - with self.assertLogs("log", level="WARN") as logs: - actual_output = conform_record_data_types( - "test_stream", record, schema, ConformanceLevel.RECURSIVE, logger - ) - assert actual_output == expected_output - self.assertEqual( - logs.output, - [ - "WARNING:log:Properties ('remove',) were present in the 'test_stream' stream but not found in catalog " - "schema. Ignoring." - ], - ) - - def test_nested_objects_remove_types(self): - schema = PropertiesList( - Property("object", PropertiesList(Property("keep", StringType))), - ).to_dict() - - record = {"object": {"keep": "hello", "remove": "goodbye"}} - - expected_output = {"object": {"keep": "hello"}} - - with self.assertLogs("log", level="WARN") as logs: - actual_output = conform_record_data_types( - "test_stream", record, schema, ConformanceLevel.RECURSIVE, logger - ) - assert actual_output == expected_output - self.assertEqual( - logs.output, - [ - "WARNING:log:Properties ('object.remove',) were present in the 'test_stream' stream but not found in " - "catalog schema. Ignoring." - ], - ) - - def test_object_arrays_remove_types(self): - schema = PropertiesList( - Property("list", ArrayType(PropertiesList(Property("keep", StringType)))), - ).to_dict() - - record = {"list": [{"keep": "hello", "remove": "goodbye"}]} - - expected_output = {"list": [{"keep": "hello"}]} - - with self.assertLogs("log", level="WARN") as logs: - actual_output = conform_record_data_types( - "test_stream", record, schema, ConformanceLevel.RECURSIVE, logger - ) - assert actual_output == expected_output - self.assertEqual( - logs.output, - [ - "WARNING:log:Properties ('list.remove',) were present in the 'test_stream' stream but not found in " - "catalog schema. Ignoring." - ], - ) +def test_simple_schema_removes_types(caplog: pytest.LogCaptureFixture): + schema = PropertiesList( + Property("keep", StringType), + ).to_dict() + + record = {"keep": "hello", "remove": "goodbye"} + + expected_output = {"keep": "hello"} + + with caplog.at_level(logging.WARNING): + actual_output = conform_record_data_types( + "test_stream", record, schema, ConformanceLevel.RECURSIVE, logger + ) + assert actual_output == expected_output + assert caplog.records[0].message == ( + "Properties ('remove',) were present in the 'test_stream' stream but not " + "found in catalog schema. Ignoring." + ) + + +def test_nested_objects_remove_types(caplog: pytest.LogCaptureFixture): + schema = PropertiesList( + Property("object", PropertiesList(Property("keep", StringType))), + ).to_dict() + + record = {"object": {"keep": "hello", "remove": "goodbye"}} + + expected_output = {"object": {"keep": "hello"}} + + with caplog.at_level(logging.WARNING): + actual_output = conform_record_data_types( + "test_stream", record, schema, ConformanceLevel.RECURSIVE, logger + ) + assert actual_output == expected_output + assert caplog.records[0].message == ( + "Properties ('object.remove',) were present in the 'test_stream' stream " + "but not found in catalog schema. Ignoring." + ) + + +def test_object_arrays_remove_types(caplog: pytest.LogCaptureFixture): + schema = PropertiesList( + Property("list", ArrayType(PropertiesList(Property("keep", StringType)))), + ).to_dict() + + record = {"list": [{"keep": "hello", "remove": "goodbye"}]} + + expected_output = {"list": [{"keep": "hello"}]} + + with caplog.at_level(logging.WARNING): + actual_output = conform_record_data_types( + "test_stream", record, schema, ConformanceLevel.RECURSIVE, logger + ) + assert actual_output == expected_output + assert caplog.records[0].message == ( + "Properties ('list.remove',) were present in the 'test_stream' stream but " + "not found in catalog schema. Ignoring." + ) def test_conform_primitives():