Skip to content

Commit d61a766

Browse files
author
Ferran Llamas
authored
Support integers schema type (#2)
1 parent e977130 commit d61a766

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
1.0.6 (unreleased)
22
------------------
33

4-
- Nothing changed yet.
5-
4+
- Support integers schema type [lferran]
65

76
1.0.5 (2019-11-01)
87
------------------
@@ -36,4 +35,4 @@
3635
1.0.0 (2019-09-26)
3736
------------------
3837

39-
- initial
38+
- initial

enviral/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ def serialize(
114114
# we attempt but ignore error
115115
# allow validation to catch properly
116116
pass
117+
if schema_type == "integer":
118+
try:
119+
value = int(value)
120+
except ValueError:
121+
# we attempt but ignore error
122+
# allow validation to catch properly
123+
pass
117124
if schema_type in ("object", "array"):
118125
if value[0] in ("[", "{"):
119126
try:

tests/test_serialize.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ def test_get_setting_from_env_with_prefix(env):
2222
assert settings["foo"] == "bar"
2323

2424

25+
26+
def test_convert_number_setting_from_env(env):
27+
for number, conversion in (("2", int), ("9.85", float)):
28+
os.environ["FOO"] = number
29+
settings = enviral.serialize(
30+
{"type": "object", "properties": {"foo": {"type": "number"}}}
31+
)
32+
assert settings["foo"] == conversion(number)
33+
34+
2535
def test_convert_int_setting_from_env(env):
26-
os.environ["FOO"] = "2"
36+
os.environ["FOO"] = "250"
2737
settings = enviral.serialize(
28-
{"type": "object", "properties": {"foo": {"type": "number"}}}
38+
{"type": "object", "properties": {"foo": {"type": "integer"}}}
2939
)
30-
assert settings["foo"] == 2
40+
assert settings["foo"] == 250
3141

3242

3343
def test_convert_object_setting_from_env(env):

tests/test_validate.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,35 @@ def test_validate_from_env(env):
2020
enviral.validate_env(schema)
2121

2222

23-
def test_validate_int_from_env(env):
23+
def test_validate_number_from_env(env):
2424
os.environ["FOO"] = "bar"
2525
schema = {"type": "object", "properties": {"foo": {"type": "number"}}}
2626
with pytest.raises(jsonschema.exceptions.ValidationError):
2727
enviral.validate_env(schema)
2828

29+
# Check int
2930
os.environ["FOO"] = "2"
3031
settings = enviral.validate_env(schema)
3132
assert settings["foo"] == 2
33+
34+
# Check float
35+
os.environ["FOO"] = "2.25"
36+
settings = enviral.validate_env(schema)
37+
assert settings["foo"] == 2.25
38+
39+
40+
def test_validate_integer_from_env(env):
41+
os.environ["FOO"] = "bar"
42+
schema = {"type": "object", "properties": {"foo": {"type": "integer"}}}
43+
with pytest.raises(jsonschema.exceptions.ValidationError):
44+
enviral.validate_env(schema)
45+
46+
# Check int
47+
os.environ["FOO"] = "50"
48+
settings = enviral.validate_env(schema)
49+
assert settings["foo"] == 50
50+
51+
# Float should fail
52+
os.environ["FOO"] = "2.25"
53+
with pytest.raises(jsonschema.exceptions.ValidationError):
54+
enviral.validate_env(schema)

0 commit comments

Comments
 (0)