keep-it-secret/tests/fields/test_SecretsField.py

65 lines
1.4 KiB
Python
Raw Normal View History

2024-01-04 19:30:54 +00:00
# -*- coding: utf-8 -*-
# type: ignore
from __future__ import annotations
from unittest import mock
from pytest_mock import MockerFixture
from keep_it_secret import fields
from keep_it_secret.secrets import Secrets
def test_init():
# When
result = fields.SecretsField(Secrets)
# Then
assert result.as_type is None
assert result.klass == Secrets
def test_init_with_field_options():
# When
result = fields.SecretsField(
Secrets, as_type=int, required=False, description='eggs',
)
# Then
assert result.as_type is None
assert result.required is False
assert result.description == 'eggs'
def test_new(mocker: MockerFixture):
# Given
mock_init = mocker.patch.object(
fields.SecretsField, '__init__', return_value=None,
)
# When
_ = fields.SecretsField.new(
Secrets, as_type=int, required=False, description='eggs',
)
# Then
mock_init.assert_called_once_with(
Secrets, as_type=int, required=False, description='eggs',
)
def test_get_value(testing_secrets: Secrets):
# Given
wrapped_secrets = mock.Mock(spec=Secrets)
mock_secrets_class = mock.Mock(return_value=wrapped_secrets)
field = fields.SecretsField(mock_secrets_class)
# When
result = field.get_value(testing_secrets)
# Then
assert result == wrapped_secrets
mock_secrets_class.assert_called_once_with(parent=testing_secrets)