Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yaml types #77

Merged
merged 14 commits into from
Mar 28, 2024
Prev Previous commit
Next Next commit
begin tests
avishaihalev committed Mar 27, 2024
commit 61aeaaf130631c749e6b618cedbdb261b22e154a
48 changes: 48 additions & 0 deletions tests/test_config/test_simple.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,38 @@
from unittest import mock
from expfig import Config


class AYamlObject(yaml.YAMLObject):
yaml_dumper = yaml.SafeDumper
yaml_loader = yaml.SafeLoader
yaml_tag = u'!AYamlObject'

def __init__(self, value):
self.value = value

def __eq__(self, other):
if type(self) != type(other):
return NotImplemented

return self.value == other.value


class AnotherYamlObject(yaml.YAMLObject):
yaml_dumper = yaml.SafeDumper
yaml_loader = yaml.SafeLoader
yaml_tag = u'!AnotherYamlObject'

def __init__(self, a=None, b=None):
self.a = a
self.b = b

def __eq__(self, other):
if type(self) != type(other):
return NotImplemented

return self.a == other.a and self.b == other.b


CONTENTS = {
'car': 'vroom',
'wheels': 4,
@@ -32,6 +64,11 @@
'dealer': 'michael-jordan-nissan'
}

YAML_CONTENTS = {
**CONTENTS,
'insurance': AYamlObject(value=10)
}


def mock_sys_argv(*args):
return mock.patch.object(sys, 'argv', [sys.argv[0], *args])
@@ -194,6 +231,17 @@ def test_float_could_be_int_default_float(self):
assert isinstance(config.truck.axles, float)


class TestYamlTypes:
def test_no_argv(self):
config = Config(default=YAML_CONTENTS)
assert config.insurance == AYamlObject(10)

@mock_sys_argv('--car', '!AYamlObject {value: 10}')
def test_same_type_argv(self):
config = Config(default=YAML_CONTENTS)
assert config.insurance == AYamlObject(10)


class TestConfigFile:
def test_config_file(self):
yaml_dump = {'dealer': 'michael-jordan-toyota'}