def test_inherit(self): with self.assertRaises(RequiredError): config = Config(self.config) @config('test') class Test(): variable = 1 try: config = Config(self.config) @config('test') class Test(): required_variable = 1 except RequiredError: self.fail('Raised RequiredError')
def test_required_error(self): with self.assertRaises(RequiredError): self.config.test.get_value('variable1', check=True) config = Config(self.config) with self.assertRaises(RequiredError): @config('test') class Test(): variable2 = 1
def test_user_variables(self): config = Config(self.config) @config('test') class Test(): user_variable: int = 2 no_typing_variable = 1 self.assertEqual(config.test.variable, 1) self.assertEqual(config.test.user_variable, 2) self.assertFalse(hasattr(config.test, 'no_typing_variable'))
def test_default(self): self.assertEqual(self.config.test.variable1.data, required) self.assertEqual(self.config.test.variable2.data, 1) config = Config(self.config) @config('test') class Test(): variable1 = 1 self.assertEqual(config.test.variable2, 1)
def test_logic(self): config = Config(self.config) @config('test') class Test(): conditional_variable3 = 1 variable1 = 3 variable2 = 1 self.assertTrue('conditional_variable4' in config.test.to_dict()) self.assertTrue('conditional_variable5' in config.test.to_dict()) self.assertFalse('conditional_variable6' in config.test.to_dict()) config = Config(self.config) @config('test') class Test(): conditional_variable1 = 1 variable1 = 1 variable2 = 1 self.assertFalse('conditional_variable4' in config.test.to_dict()) self.assertTrue('conditional_variable5' in config.test.to_dict()) self.assertTrue('conditional_variable6' in config.test.to_dict()) config = Config(self.config) @config('test') class Test(): conditional_variable1 = 1 variable1 = 1 variable2 = 3 self.assertFalse('conditional_variable4' in config.test.to_dict()) self.assertFalse('conditional_variable5' in config.test.to_dict()) self.assertTrue('conditional_variable6' in config.test.to_dict()) config = Config(self.config) @config('test') class Test(): conditional_variable3 = 1 variable1 = 3 variable2 = 3 self.assertFalse('conditional_variable4' in config.test.to_dict()) self.assertTrue('conditional_variable5' in config.test.to_dict()) self.assertFalse('conditional_variable6' in config.test.to_dict())
def test_required_error(self): with self.assertRaises(RequiredError): self.config.to_dict(check=True) try: self.user_config2.test.to_dict(check=True) except RequiredError: self.fail('Raised RequiredError') with self.assertRaises(RequiredError): config = Config(self.config) @config('test') class Test(): variable1 = 1 try: config = Config(self.config) @config('test') class Test(): variable1 = 2 except RequiredError: self.fail('Raised RequiredError')
def test_update(self): config = Config() @config('test') class Test(): variable: int = 2 choice_variable2: Choices[int] = Choices([1, 2, 3]) config = self.config.update_config(config) self.assertEqual(config.test.variable, 2) self.assertEqual(config.test.choice_variable, 1) # self.assertFalse(isinstance(config.test.choice_variable, Choices)) self.assertTrue(hasattr(config.test, 'choice_variable2')) self.assertTrue(isinstance(config.test.choice_variable2, Choices))
def setUp(self): self.config = Config() @self.config('test') class Test(): variable1: Choices[int] = Choices([1, 2, 3], default=3) variable2: Choices[int] = Choices([1, 2, 3], default=1) conditional_variable1: Condition[Required[int]] = Condition(required, variable1 == 1) conditional_variable2: Condition[int] = Condition(1, variable1 == 2) conditional_variable3: Condition[Required[int]] = Condition(required, variable1 == 3) conditional_variable4: Condition[int] = Condition(1, (variable1 == 3) & (variable2 == 1)) conditional_variable5: Condition[int] = Condition(1, (variable1 == 3) | (variable2 == 1)) conditional_variable6: Condition[int] = Condition(1, ~(variable1 == 3)) self.user_config1 = Config(self.config) @self.user_config1('test') class Test(): variable1 = 1 conditional_variable1 = 2 conditional_variable2 = 2 self.user_config2 = Config(self.config) @self.user_config2('test') class Test(): variable1 = 2
def setUp(self): self.config = Config() @self.config('test') class Test(): variable1: Choices[int] = Choices([1, 2, 3]) variable2: Choices[int] = Choices([1, 2, 3], default=1)
def test_invalid_choices(self): config = Config(self.config) with self.assertRaises(InvalidChoiceError): @config('test') class Test(): variable1 = 4
def test_valid_default(self): config = Config() with self.assertRaises(InvalidChoiceError): @config('test') class Test(): variable: Choices[int] = Choices([1, 2, 3], default=4)
def test_default_choice(self): config = Config(self.config) self.assertTrue(isinstance(config.test.choice_variable, Choices)) @config('test') class Test(): pass
def setUp(self): self.config = Config() @self.config('test') class Test(): variable: int = 2 required_variable: Required[int] = required