You've already forked keep-it-secret
Release v1.0.0
This commit is contained in:
0
tests/secrets/__init__.py
Normal file
0
tests/secrets/__init__.py
Normal file
40
tests/secrets/test_Secrets.py
Normal file
40
tests/secrets/test_Secrets.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from __future__ import annotations
|
||||
|
||||
from keep_it_secret import secrets
|
||||
|
||||
from tests.fixtures import TestingSecrets
|
||||
|
||||
|
||||
class ParentSecrets(secrets.Secrets):
|
||||
pass
|
||||
|
||||
|
||||
def test_init():
|
||||
# When
|
||||
result = TestingSecrets()
|
||||
|
||||
# Then
|
||||
assert isinstance(result, TestingSecrets)
|
||||
assert result.__secrets_parent__ is None
|
||||
assert result.__secrets_data__ == {'spam': 'spam', 'eggs': 'eggs'}
|
||||
|
||||
|
||||
def test_init_with_parent():
|
||||
# Given
|
||||
parent_secrets = ParentSecrets()
|
||||
|
||||
# When
|
||||
result = TestingSecrets(parent=parent_secrets)
|
||||
|
||||
# Then
|
||||
assert result.__secrets_parent__ is parent_secrets
|
||||
|
||||
|
||||
def test_field_property(testing_secrets: TestingSecrets):
|
||||
# When
|
||||
result = testing_secrets.spam
|
||||
|
||||
# Then
|
||||
assert result == testing_secrets.__secrets_data__['spam']
|
||||
82
tests/secrets/test_SecretsBase.py
Normal file
82
tests/secrets/test_SecretsBase.py
Normal file
@@ -0,0 +1,82 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from __future__ import annotations
|
||||
|
||||
import types
|
||||
|
||||
from keep_it_secret import secrets
|
||||
from keep_it_secret.fields import EnvField, LiteralField
|
||||
|
||||
|
||||
class TestingSecrets(secrets.Secrets):
|
||||
spam: str = LiteralField.new('spam')
|
||||
|
||||
something = False
|
||||
|
||||
def do_something(self):
|
||||
return True
|
||||
|
||||
|
||||
class SubclassSecrets(TestingSecrets):
|
||||
spam: str = EnvField.new('KEEP_IT_SECRET_TESTS_SPAM')
|
||||
eggs: str = LiteralField.new('eggs')
|
||||
|
||||
|
||||
def test_generic_attribute_initialization():
|
||||
# Then
|
||||
assert TestingSecrets.__class__ is secrets.SecretsBase
|
||||
assert isinstance(TestingSecrets.__init__, types.FunctionType)
|
||||
|
||||
|
||||
def test_secrets_dunder_attribute_initialization():
|
||||
# Then
|
||||
assert hasattr(TestingSecrets, '__secrets_fields__')
|
||||
|
||||
assert 'spam' in TestingSecrets.__secrets_fields__
|
||||
assert isinstance(TestingSecrets.__secrets_fields__['spam'], LiteralField) is True
|
||||
assert TestingSecrets.__secrets_fields__['spam'].name == 'TestingSecrets.spam'
|
||||
|
||||
|
||||
def test_secret_properties_initialization():
|
||||
# Then
|
||||
assert hasattr(TestingSecrets, 'spam')
|
||||
assert isinstance(TestingSecrets.spam, property)
|
||||
|
||||
|
||||
def test_subclass_attribute_initialization():
|
||||
# Then
|
||||
assert hasattr(TestingSecrets, 'something')
|
||||
assert TestingSecrets.something is False
|
||||
|
||||
assert hasattr(TestingSecrets, 'do_something')
|
||||
assert isinstance(TestingSecrets.do_something, types.FunctionType)
|
||||
|
||||
|
||||
def test_inheritance():
|
||||
# Then
|
||||
assert isinstance(SubclassSecrets.__secrets_fields__['spam'], EnvField) is True
|
||||
assert isinstance(SubclassSecrets.__secrets_fields__['eggs'], LiteralField) is True
|
||||
|
||||
assert hasattr(SubclassSecrets, 'do_something')
|
||||
assert isinstance(SubclassSecrets.do_something, types.FunctionType)
|
||||
|
||||
|
||||
def test_multiple_inheritance():
|
||||
# Given
|
||||
class TestingMixin:
|
||||
def do_something_else(self):
|
||||
return False
|
||||
|
||||
class SubclassWithMixin(TestingMixin, SubclassSecrets):
|
||||
spameggs = LiteralField.new('spameggs')
|
||||
|
||||
# Then
|
||||
assert isinstance(SubclassWithMixin.__secrets_fields__['spam'], EnvField) is True
|
||||
assert isinstance(SubclassWithMixin.__secrets_fields__['eggs'], LiteralField) is True
|
||||
assert isinstance(SubclassWithMixin.__secrets_fields__['spameggs'], LiteralField) is True
|
||||
|
||||
assert hasattr(SubclassWithMixin, 'do_something')
|
||||
assert isinstance(SubclassWithMixin.do_something, types.FunctionType)
|
||||
|
||||
assert hasattr(SubclassWithMixin, 'do_something_else')
|
||||
assert isinstance(SubclassWithMixin.do_something_else, types.FunctionType)
|
||||
57
tests/secrets/test_field_property_factory.py
Normal file
57
tests/secrets/test_field_property_factory.py
Normal file
@@ -0,0 +1,57 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockFixture
|
||||
|
||||
from keep_it_secret import secrets
|
||||
from keep_it_secret.secrets import Secrets
|
||||
|
||||
|
||||
class TestingSecrets(Secrets):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_field(mocker: MockFixture) -> mock.Mock:
|
||||
result = mocker.Mock(return_value='spam')
|
||||
result.description = None
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def testing_secrets() -> TestingSecrets:
|
||||
yield TestingSecrets()
|
||||
|
||||
TestingSecrets.__secrets_fields__ = {}
|
||||
|
||||
|
||||
def test_getter_cache_miss(mock_field: mock.Mock, testing_secrets: TestingSecrets):
|
||||
# Given
|
||||
testing_secrets.__secrets_fields__['spam'] = mock_field
|
||||
|
||||
# When
|
||||
result = secrets.field_property_factory('spam', mock_field).fget(testing_secrets)
|
||||
|
||||
# Then
|
||||
assert result == 'spam'
|
||||
|
||||
mock_field.assert_called_once_with(testing_secrets)
|
||||
|
||||
|
||||
def test_getter_cache_hit(mock_field: mock.Mock, testing_secrets: TestingSecrets):
|
||||
# Given
|
||||
testing_secrets.__secrets_fields__['spam'] = mock_field
|
||||
testing_secrets.__secrets_data__['spam'] = 'eggs'
|
||||
|
||||
# When
|
||||
result = secrets.field_property_factory('spam', mock_field).fget(testing_secrets)
|
||||
|
||||
# Then
|
||||
assert result == 'eggs'
|
||||
|
||||
mock_field.assert_not_called()
|
||||
Reference in New Issue
Block a user