コード例 #1
0
def test_tracker_set_status():
    """ Test that the SnapshotTracker.set_status method functions as expected. """
    # Arrange
    statuses = [
        SnapshotStatus.UNCHECKED,
        SnapshotStatus.PASSED,
        SnapshotStatus.WRITTEN,
        SnapshotStatus.FAILED,
    ]
    snapshot_file = Path(__file__)
    function_name = "test_function"
    metadata = SnapshotMetadata(
        caller_info=CallerInfo(snapshot_file, function_name, dict()),
        update_on_next_run=False,
        test_runner_provided_name="",
        user_provided_name="",
    )

    # Act
    tracker = SnapshotTracker()
    tracker.set_status(
        statuses=statuses,
        snapshot_file=snapshot_file,
        function_name=function_name,
        metadata=metadata,
    )

    # Assert
    assert tracker.snapshots == {
        snapshot_file: {
            function_name:
            [dict(metadata=metadata.as_dict(), snapshots=statuses)]
        }
    }
コード例 #2
0
def single_function_with_args(foo: str = "FOO",
                              bar: bool = True,
                              pi: float = 3.14) -> Result_Expected:
    """ Single function call, with arguments. """
    expected = CallerInfo(FILE, "single_function_with_args",
                          dict(foo=foo, bar=bar, pi=pi))
    return get_caller_info(), expected
コード例 #3
0
def nested_function() -> Result_Expected:
    """ Nested function call. """
    def inner():
        """ Inner (nested) function. """
        return get_caller_info(frame_index=3)

    expected = CallerInfo(FILE, "nested_function", {})
    return inner(), expected
コード例 #4
0
def get_caller_info(frame_index: int = 2) -> CallerInfo:
    """ Calls the `snappiershot.inspection.CallerInfo.from_call_stack` method.

    This simulates the expected call stack during normal snappiershot runtime.

    Returns:
        The resultant CallerInfo object.
    """
    return CallerInfo.from_call_stack(frame_index)
コード例 #5
0
ファイル: conftest.py プロジェクト: MORSECorp/snappiershot
def _empty_caller_info(tmp_path: Path, mocker: MockerFixture) -> CallerInfo:
    """ Mocks the call to the "from_call_stack" method of snappiershot.inspection.CallerInfo.

    The method returns an CallerInfo object that contains no arguments.
    """
    empty_caller_info = CallerInfo(tmp_path, "test_function", args=dict())
    mocker.patch(
        "snappiershot.snapshot.snapshot.CallerInfo.from_call_stack",
        return_value=empty_caller_info,
    )
    return empty_caller_info
コード例 #6
0
    def test_encoded_metadata(config: Config, snapshot_file: Path):
        """ Test that SnapshotMetadata gets encoded to eliminate serialization errors. """
        # Arrange
        args = dict(unhandled_type=SimpleNamespace(complex=3 + 4j))
        caller_info = CallerInfo(snapshot_file, "test_function", args)
        metadata = SnapshotMetadata(caller_info, update_on_next_run=False)
        snapshot_file_object = _SnapshotFile(config, metadata)

        # Act
        snapshot_file_object._changed_flag = True
        snapshot_file_object.write()

        # Assert
        assert snapshot_file.exists()
        print(snapshot_file.read_text())
コード例 #7
0
def test_tracker_get_status_report():
    """ Test that the SnapshotTracker.get_status_report method functions as expected. """
    # Arrange
    expected = StatusReport(1, 2, 3, 4, 5)

    tracker = SnapshotTracker()
    snapshot_file = Path(__file__)
    function_name = "test_function"
    metadata = SnapshotMetadata(
        caller_info=CallerInfo(snapshot_file, function_name, dict()),
        update_on_next_run=False,
        test_runner_provided_name="",
        user_provided_name="",
    )
    tracker.snapshots = {
        snapshot_file: {
            function_name: [
                dict(
                    metadata=metadata.as_dict(),
                    snapshots=[SnapshotStatus.UNCHECKED] * expected.unchecked,
                ),
                dict(
                    metadata=metadata.as_dict(),
                    snapshots=[SnapshotStatus.FAILED] * expected.failed,
                ),
                dict(
                    metadata=metadata.as_dict(),
                    snapshots=[SnapshotStatus.PASSED] * expected.passed,
                ),
                dict(
                    metadata=metadata.as_dict(),
                    snapshots=[SnapshotStatus.RECORDED] * expected.recorded,
                ),
                dict(
                    metadata=metadata.as_dict(),
                    snapshots=[SnapshotStatus.WRITTEN] * expected.written,
                ),
            ]
        }
    }

    # Act
    status_report = tracker.get_status_report()

    # Assert
    assert status_report == expected
コード例 #8
0
 def staticmethod_cls_arg(cls=None) -> Result_Expected:
     """ Static method call, but the first argument is "cls". """
     expected = CallerInfo(FILE, "ClassTestObject.staticmethod_cls_arg",
                           dict(cls=None))
     return get_caller_info(), expected
コード例 #9
0
 def classmethod_no_self_arg(notcls) -> Result_Expected:
     """ Class method call, but the "self" argument is mis-named. """
     expected = CallerInfo(FILE, "ClassTestObject.classmethod_no_self_arg",
                           {})
     return get_caller_info(), expected
コード例 #10
0
 def staticmethod_() -> Result_Expected:
     """ Static method call. """
     expected = CallerInfo(FILE, "ClassTestObject.staticmethod_", {})
     return get_caller_info(), expected
コード例 #11
0
 def classmethod_(cls) -> Result_Expected:
     """ Class method call. """
     expected = CallerInfo(FILE, "ClassTestObject.classmethod_", {})
     return get_caller_info(), expected
コード例 #12
0
 def method(self) -> Result_Expected:
     """ Method call. """
     expected = CallerInfo(FILE, "ClassTestObject.method", {})
     return get_caller_info(), expected
コード例 #13
0
def single_function_no_args() -> Result_Expected:
    """ Single function call, with no arguments. """
    expected = CallerInfo(FILE, "single_function_no_args", {})
    return get_caller_info(), expected
コード例 #14
0
 def inner():
     """ Inner function definition. """
     CallerInfo.from_call_stack(1)
コード例 #15
0
 def method(self) -> Result_Expected:
     """ Method call within nested class definition. """
     expected = CallerInfo(FILE, "ClassTestObject.NestedClass.method",
                           {})
     return get_caller_info(), expected
コード例 #16
0
class TestSnapshotMetadata:
    """ Tests for the snappiershot.snapshot.SnapshotMetadata object. """

    FAKE_CALLER_INFO = CallerInfo(
        file=Path("fake/file/path"),
        function="fake_fully_qualified_function_name",
        args={
            "foo": 1,
            "bar": "two"
        },
    )

    DEFAULT_METADATA_KWARGS = dict(
        caller_info=FAKE_CALLER_INFO,
        update_on_next_run=False,
        test_runner_provided_name="",
        user_provided_name="",
    )

    @staticmethod
    @pytest.mark.parametrize(
        "metadata_kwargs, expected_error",
        [
            ({
                **DEFAULT_METADATA_KWARGS, "caller_info": None
            }, TypeError),
            ({
                **DEFAULT_METADATA_KWARGS, "update_on_next_run": "foo"
            }, TypeError),
            ({
                **DEFAULT_METADATA_KWARGS, "test_runner_provided_name": 1.23
            }, TypeError),
            ({
                **DEFAULT_METADATA_KWARGS, "user_provided_name": 1.23
            }, TypeError),
        ],
    )
    def test_metadata_validate(metadata_kwargs: Dict,
                               expected_error: Type[Exception]):
        """ Checks that validation of the metadata values occurs as expected. """
        # Arrange

        # Act & Assert
        with pytest.raises(expected_error):
            SnapshotMetadata(**metadata_kwargs)

    @staticmethod
    @pytest.mark.parametrize(
        "metadata_kwargs, metadata_dict, matches",
        [
            (DEFAULT_METADATA_KWARGS, {
                "arguments": FAKE_CALLER_INFO.args
            }, True),
            (DEFAULT_METADATA_KWARGS, {
                "arguments": {
                    "foo": 1,
                    "bar": 2
                }
            }, False),
        ],
    )
    def test_metadata_matches(metadata_kwargs: Dict, metadata_dict: Dict,
                              matches: bool):
        """ Checks that the SnapshotMetadata.matches method functions as expected. """
        # Arrange
        metadata = SnapshotMetadata(**metadata_kwargs)

        # Act
        result = metadata.matches(metadata_dict)

        # Assert
        assert result == matches