Skip to content

Commit

Permalink
feat: support not nullable type list (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwab12 authored Jan 19, 2023
1 parent 22fa798 commit 115db61
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion jsf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def __is_field_nullable(self, schema: Dict[str, Any]) -> Tuple[str, bool]:
item_type_deep_copy = deepcopy(item_type)
item_type_deep_copy.remove("null")
return random.choice(item_type_deep_copy), True
raise TypeError # pragma: no cover - not currently supporting other types TODO
if len(set(item_type)) >= 1:
item_type_deep_copy = deepcopy(item_type)
return random.choice(item_type_deep_copy), False
return item_type, False

def __parse_anyOf(self, name: str, path: str, schema: Dict[str, Any]) -> AnyOf:
Expand Down
36 changes: 36 additions & 0 deletions jsf/tests/data/type-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "object",
"properties": {
"randTypeValueNullable": {
"type": [
"null",
"boolean"
]
},
"randTypeValue": {
"type": [
"boolean",
"number",
"integer",
"string"
]
},
"int": {
"type": [
"integer"
]
},
"null": {
"type": [
"null"
]
}
},
"additionalProperties": false,
"required": [
"randTypeValueNullable",
"randTypeValue",
"int",
"null"
]
}
13 changes: 13 additions & 0 deletions jsf/tests/test_default_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,16 @@ def test_gen_and_validate(TestData):
schema = json.load(file)
p = JSF(schema)
[p.generate_and_validate() for _ in range(50)]


def test_list_of_types(TestData):
with open(TestData / "type-list.json", "r") as file:
schema = json.load(file)
fake_data = [JSF(schema).generate() for _ in range(100)]
for f in fake_data:
print(f)
assert all(isinstance(f, dict) for f in fake_data), fake_data
assert all(type(f["randTypeValueNullable"]) in [type(None), bool] for f in fake_data), fake_data
assert all(type(f["randTypeValue"]) in [bool, int, float, str] for f in fake_data), fake_data
assert all(isinstance(f["int"], int) for f in fake_data), fake_data
assert all(isinstance(f["null"], type(None)) for f in fake_data), fake_data

0 comments on commit 115db61

Please sign in to comment.