keep-it-secret/tests/secrets/test_Secrets.py
2024-01-18 21:31:48 +01:00

94 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-x
# type: ignore
from __future__ import annotations
from keep_it_secret import secrets
from keep_it_secret.fields import LiteralField
from tests.fixtures import TestingSecrets
class NestedSecrets(secrets.Secrets):
spameggs: str = LiteralField.new('spameggs')
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(testing_secrets: TestingSecrets):
# When
result = NestedSecrets(parent=testing_secrets)
# Then
assert result.__secrets_parent__ is testing_secrets
def test_field_property(testing_secrets: TestingSecrets):
# When
result = testing_secrets.spam
# Then
assert result == testing_secrets.__secrets_data__['spam']
def test_resolve_dependency_from_self():
# Given
secrets = NestedSecrets()
# When
result = secrets.resolve_dependency('spameggs')
# Then
assert result == secrets.spameggs
def test_resolve_dependency_from_parent(testing_secrets: TestingSecrets):
# Given
secrets = NestedSecrets(parent=testing_secrets)
# When
result = secrets.resolve_dependency('spam')
# Then
assert result == testing_secrets.spam
def test_resolve_dependency_unresolved_from_self():
# Given
secrets = NestedSecrets()
# When
result = secrets.resolve_dependency('thisisntright')
# Then
assert result is secrets.UNRESOLVED_DEPENDENCY
def test_resolve_dependency_unresolved_with_parent(testing_secrets: TestingSecrets):
# Given
secrets = NestedSecrets(parent=testing_secrets)
# When
result = secrets.resolve_dependency('thisisntright')
# Then
assert result is secrets.UNRESOLVED_DEPENDENCY
def test_resolve_dependency_unresolved_dont_include_parents(testing_secrets: TestingSecrets):
# Given
secrets = NestedSecrets(parent=testing_secrets)
# When
result = secrets.resolve_dependency('spam', include_parents=False)
# Then
assert result is secrets.UNRESOLVED_DEPENDENCY