<s> import os <EOL> from pathlib import Path <EOL> import unittest <EOL> from unittest . mock import patch <EOL> from nativeconfig . exceptions import InitializationError , ValidationError , DeserializationError <EOL> from nativeconfig . options import PathOption , StringOption , ArrayOption <EOL> from nativeconfig . options . dict_option import DictOption <EOL> from nativeconfig . options . float_option import FloatOption <EOL> from test import DummyMemoryConfig <EOL> from test . options import TestOptionMixin <EOL> class MyConfig ( DummyMemoryConfig ) : <EOL> test_dict = DictOption ( '<STR_LIT>' , env_name = '<STR_LIT>' , default = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> path_dict = DictOption ( '<STR_LIT>' , value_option = PathOption ( '<STR_LIT>' , path_type = Path ) ) <EOL> float_dict = DictOption ( '<STR_LIT>' , value_option = FloatOption ( '<STR_LIT>' ) ) <EOL> class TestDictOption ( unittest . TestCase , TestOptionMixin ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . OPTION_TYPE = DictOption <EOL> def tearDown ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> del c . test_dict <EOL> del c . path_dict <EOL> del c . float_dict <EOL> os . environ . pop ( '<STR_LIT>' , None ) <EOL> def test_value_option_must_be_instance_of_base_option ( self ) : <EOL> with self . assertRaises ( InitializationError ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . array_option = self . OPTION_TYPE ( '<STR_LIT>' , value_option = str ) <EOL> c = MyConfig . get_instance ( ) <EOL> c . array_option = self . OPTION_TYPE ( '<STR_LIT>' , value_option = StringOption ( "<STR_LIT:test>" ) ) <EOL> def test_value_option_must_not_be_array_or_dict ( self ) : <EOL> with self . assertRaises ( InitializationError ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . array_option = self . OPTION_TYPE ( '<STR_LIT>' , value_option = ArrayOption ( '<STR_LIT:test>' ) ) <EOL> with self . assertRaises ( InitializationError ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . array_option = self . OPTION_TYPE ( '<STR_LIT>' , value_option = DictOption ( '<STR_LIT:test>' ) ) <EOL> def test_option_raises_deserialization_error_if_value_option_invalid ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> with self . assertRaises ( DeserializationError ) : <EOL> c . float_dict = { "<STR_LIT>" : "<STR_LIT>" } <EOL> float_dict = c . float_dict <EOL> def test_json_serialization_deserialization_keeps_value ( self ) : <EOL> o = DictOption ( '<STR_LIT:test>' , value_option = PathOption ( '<STR_LIT:_>' , path_type = Path ) ) <EOL> v = { '<STR_LIT:a>' : Path ( '<STR_LIT:.>' ) , '<STR_LIT:b>' : Path ( '<STR_LIT:..>' ) } <EOL> j = o . serialize_json ( v ) <EOL> self . assertEqual ( o . deserialize_json ( j ) , v ) <EOL> def test_json_serialization_of_None ( self ) : <EOL> o = DictOption ( '<STR_LIT:test>' , value_option = PathOption ( '<STR_LIT:_>' , path_type = Path ) ) <EOL> self . assertEqual ( o . serialize_json ( None ) , '<STR_LIT:null>' ) <EOL> def test_json_deserialization_of_null ( self ) : <EOL> o = DictOption ( '<STR_LIT:test>' , value_option = PathOption ( '<STR_LIT:_>' , path_type = Path ) ) <EOL> self . assertEqual ( o . deserialize_json ( '<STR_LIT:null>' ) , None ) <EOL> def test_snapshot_can_be_restored ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> v = { '<STR_LIT:a>' : Path ( '<STR_LIT:.>' ) , '<STR_LIT:b>' : Path ( '<STR_LIT:..>' ) } <EOL> c . path_dict = v <EOL> s = c . snapshot ( ) <EOL> del c . path_dict <EOL> self . assertNotEqual ( c . path_dict , v ) <EOL> c . restore_snapshot ( s ) <EOL> self . assertEqual ( c . path_dict , v ) <EOL> return c <EOL> def test_default_value_copied ( self ) : <EOL> default = { } <EOL> class TestConfig ( DummyMemoryConfig ) : <EOL> dict_option = DictOption ( '<STR_LIT>' , default = default , env_name = '<STR_LIT>' ) <EOL> c = TestConfig . get_instance ( ) <EOL> self . assertEqual ( c . dict_option , { } ) <EOL> default [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( c . dict_option , { } ) <EOL> def test_choices_copied ( self ) : <EOL> value = { '<STR_LIT:key>' : '<STR_LIT:value>' } <EOL> value2 = { '<STR_LIT>' : '<STR_LIT>' } <EOL> choices = [ value ] <EOL> class TestConfig ( DummyMemoryConfig ) : <EOL> dict_option = DictOption ( '<STR_LIT>' , choices = choices , env_name = '<STR_LIT>' ) <EOL> c = TestConfig . get_instance ( ) <EOL> c . dict_option = { '<STR_LIT:key>' : '<STR_LIT:value>' } <EOL> choices [ <NUM_LIT:0> ] = value2 <EOL> c . dict_option = { '<STR_LIT:key>' : '<STR_LIT:value>' } <EOL> value [ '<STR_LIT:key>' ] = '<STR_LIT>' <EOL> c . dict_option = { '<STR_LIT:key>' : '<STR_LIT:value>' } <EOL> def test_choices_cannot_be_empty ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> with self . assertRaises ( InitializationError ) : <EOL> c . empty_choices = DictOption ( '<STR_LIT>' , choices = [ ] ) <EOL> def test_default_value_must_be_one_of_choices_if_any ( self ) : <EOL> with self . assertRaises ( ValidationError ) : <EOL> class MyConfig ( DummyMemoryConfig ) : <EOL> dict_option = self . OPTION_TYPE ( '<STR_LIT>' , choices = [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ] , default = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_all_choices_must_be_valid ( self ) : <EOL> with self . assertRaises ( ValidationError ) : <EOL> class MyConfig ( DummyMemoryConfig ) : <EOL> dict_option = self . OPTION_TYPE ( '<STR_LIT>' , choices = [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <NUM_LIT:1> ] ) <EOL> def test_default_must_be_valid ( self ) : <EOL> with self . assertRaises ( ValidationError ) : <EOL> class MyConfig ( DummyMemoryConfig ) : <EOL> dict_option = self . OPTION_TYPE ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> def test_value_must_be_one_of_choices_if_any ( self ) : <EOL> class MyConfig ( DummyMemoryConfig ) : <EOL> dict_option = self . OPTION_TYPE ( '<STR_LIT>' , choices = [ { "<STR_LIT>" : "<STR_LIT>" } , { "<STR_LIT>" : "<STR_LIT>" } ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> MyConfig . get_instance ( ) . dict_option = { "<STR_LIT>" : "<STR_LIT>" } <EOL> MyConfig . get_instance ( ) . dict_option = { "<STR_LIT>" : "<STR_LIT>" } <EOL> MyConfig . get_instance ( ) . dict_option = { "<STR_LIT>" : "<STR_LIT>" } <EOL> def test_serialize_json ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . test_dict = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertEqual ( c . get_json_value_for_option_name ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> c . path_dict = { "<STR_LIT>" : Path ( "<STR_LIT:.>" ) } <EOL> self . assertEqual ( c . get_json_value_for_option_name ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_deserialize_json ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . set_json_value_for_option_name ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( c . test_dict , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_value_can_be_overridden_by_env ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> c = MyConfig . get_instance ( ) <EOL> self . assertEqual ( c . test_dict , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_value_can_be_overridden_by_one_shot_value ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . set_one_shot_json_value_for_option_name ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( c . test_dict , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_value_that_cannot_be_deserialized_calls_resolver ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> with self . assertRaises ( DeserializationError ) : <EOL> test_dict = c . test_dict <EOL> with patch . object ( DummyMemoryConfig , '<STR_LIT>' , return_value = '<STR_LIT>' ) : <EOL> test_dict = c . test_dict <EOL> self . assertEqual ( test_dict , '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> test_dict = c . test_dict <EOL> self . assertEqual ( test_dict , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_invalid_deserialized_value_calls_resolver ( self ) : <EOL> class TestConfig ( DummyMemoryConfig ) : <EOL> dict_option = DictOption ( '<STR_LIT>' , choices = [ { "<STR_LIT>" : "<STR_LIT>" } , { "<STR_LIT>" : "<STR_LIT>" } ] , env_name = '<STR_LIT>' ) <EOL> c = TestConfig . get_instance ( ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> with self . assertRaises ( ValidationError ) : <EOL> dict_option = c . dict_option <EOL> with patch . object ( DummyMemoryConfig , '<STR_LIT>' , return_value = '<STR_LIT>' ) : <EOL> dict_option = c . dict_option <EOL> self . assertEqual ( dict_option , '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dict_option = c . dict_option <EOL> self . assertEqual ( dict_option , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_setting_value_resets_one_shot_value ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . set_one_shot_json_value_for_option_name ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> c . test_dict = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertEqual ( c . test_dict , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_setting_invalid_value_raises_exception ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> c . test_dict = "<STR_LIT>" <EOL> def test_setting_none_deletes_value ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> c . test_dict = { "<STR_LIT>" : "<STR_LIT>" } <EOL> c . test_dict = None <EOL> self . assertEqual ( c . test_dict , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_deleting_value ( self ) : <EOL> c = MyConfig . get_instance ( ) <EOL> del c . test_dict <EOL> self . assertEqual ( c . test_dict , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_env_value_must_be_valid_json ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:]>' <EOL> with self . assertRaises ( DeserializationError ) : <EOL> c = MyConfig . get_instance ( ) <EOL> test_dict = c . test_dict <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertEqual ( c . test_dict , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_json_value_is_of_expected_type ( self ) : <EOL> with self . assertRaises ( DeserializationError ) : <EOL> DictOption ( '<STR_LIT:_>' ) . deserialize_json ( "<STR_LIT:1>" ) </s>
