def test_cast_get_method(self): """Ensure that the get method can cast""" envdict = EnvDict({'DEBUG': '1'}) self.assertEqual(True, envdict.get('DEBUG', cast=bool)) self.assertEqual(True, envdict.get('MISSING', True, cast=bool)) # If the key is missing the default should not be cast self.assertEqual('True', envdict.get('MISSING', 'True', cast=bool))
def test_boolean_cast(self): self.assertTrue( EnvDict({'DEBUG': 'True'}, schema={'DEBUG': bool})['DEBUG']) self.assertTrue( EnvDict({'DEBUG': 'true'}, schema={'DEBUG': bool})['DEBUG']) self.assertTrue( EnvDict({'DEBUG': 'on'}, schema={'DEBUG': bool})['DEBUG']) self.assertTrue( EnvDict({'DEBUG': '1'}, schema={'DEBUG': bool})['DEBUG']) self.assertFalse( EnvDict({'DEBUG': 'False'}, schema={'DEBUG': bool})['DEBUG']) self.assertFalse( EnvDict({'DEBUG': 'off'}, schema={'DEBUG': bool})['DEBUG']) self.assertFalse( EnvDict({'DEBUG': 'debug'}, schema={'DEBUG': bool})['DEBUG'])
def test_recasting(self): envdict = EnvDict({'DEBUG': '1'}) self.assertEqual('1', envdict['DEBUG']) envdict.recast(schema={'DEBUG': int}) self.assertEqual(1, envdict['DEBUG'])
def test_update_casts(self): """Update the cast functions on init""" envdict = EnvDict({'DEBUG': '1'}, casts={'mycast': Decimal}) self.assertEqual(envdict.get('DEBUG', cast='mycast'), Decimal('1'))
def test_cast_pop_method(self): """Ensure that the pop method can cast""" envdict = EnvDict({'DEBUG': '1'}) self.assertEqual(True, envdict.pop('DEBUG', cast=bool)) self.assertEqual(True, envdict.pop('MISSING', True, cast=bool)) self.assertEqual('True', envdict.pop('MISSING', 'True', cast=bool))
def test_missing_key(self): """Ensure the defaults work as expected""" self.assertTrue(EnvDict({}).get('DEBUG', True)) self.assertFalse(EnvDict({}).get('DEBUG', False))
def test_missing_cast_cast(self): """Missing cast should default to a string""" self.assertTrue( isinstance( EnvDict({'DEBUG': '1'}, schema={'DEBUG': 'kjkjdjk'})['DEBUG'], str))
def test_dict_cast(self): self.assertTrue( isinstance( EnvDict({'SOMEDICT': 'a=a,name=tester'}, schema={'SOMEDICT': dict})['SOMEDICT'], dict))
def test_list_cast(self): self.assertTrue( isinstance( EnvDict({'MYLIST': '1'}, schema={'MYLIST': list})['MYLIST'], list))
def test_float_cast(self): self.assertTrue( isinstance( EnvDict({'FLOAT': '1'}, schema={'FLOAT': float})['FLOAT'], float))
def test_decimal_cast(self): self.assertTrue( isinstance( EnvDict({'DECIMAL': '1'}, schema={'DECIMAL': 'decimal'})['DECIMAL'], Decimal))