Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 266f7f2

Browse files
committed
Update README.md and tests
1 parent 176c175 commit 266f7f2

File tree

3 files changed

+68
-73
lines changed

3 files changed

+68
-73
lines changed

‎README.md‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ driver_standings.json
103103
```
104104

105105
```
106-
json2models -f pydantic -l DriverStandings - driver_standings.json
106+
json2models -f pydantic -m DriverStandings driver_standings.json
107107
```
108108

109109
```python
110110
r"""
111111
generated by json2python-models v0.2.0 at Mon May 4 17:46:30 2020
112-
command: /opt/projects/json2python-models/venv/bin/json2models -f pydantic -s flat -l DriverStandings - driver_standings.json
112+
command: /opt/projects/json2python-models/venv/bin/json2models -f pydantic -s flat -m DriverStandings driver_standings.json
113113
"""
114114
from pydantic import BaseModel, Field
115115
from typing import List
@@ -469,27 +469,25 @@ json2models -m Car car_*.json -f attrs > car.py
469469
```
470470

471471
Arguments:
472-
* `-h`, `--help` - Show help message and exit
473472

474-
* `-m`, `--model` - Model name and its JSON data as path or unix-like path pattern. `*`, `**` or `?` patterns symbols
475-
are supported.
476-
* **Format**: `-m <Model name> [<JSON files> ...]`
477-
* **Example**: `-m Car audi.json reno.json` or `-m Car audi.json -m Car reno.json` (results will be the same)
473+
* `-h`, `--help` - Show help message and exit
478474

479-
* `-l`, `--list` - Like `-m` but given json file should contain list of model data (dataset). If this file contains dict
480-
with nested list than you can pass `<JSON key>` to lookup. Deep lookups are supported by dot-separated path. If no
481-
lookup needed pass `-` as `<JSON key>`.
482-
* **Format**: `-l <Model name> <JSON key> <JSON file>`
483-
* **Example**: `-l Car - cars.json -l Person fetch_results.items.persons result.json`
484-
* **Note**: Models names under these arguments should be unique.
475+
* `-m`, `--model` - Model name and its JSON data as path or unix-like path pattern.
476+
`*`, `**` or `?` patterns symbols are supported.
477+
JSON data could be an array of models or single model.
478+
If this file contains dict with nested list than you can pass
479+
<JSON lookup>. Deep lookups are supported by dot-separated path.
480+
If no lookup needed pass '-' as <JSON lookup> (default)
481+
* **Format**: `-m <Model name> [<JSON lookup>] <File path or pattern>`
482+
* **Example**: `-m Car audi.json -m Car results reno.json`
485483

486484
* `-i`, `--input-format` - Input file format (parser). Default is JSON parser. Yaml parser requires PyYaml or
487485
ruamel.yaml to be installed. Ini parser uses
488486
builtin [configparser](https://docs.python.org/3/library/configparser.html). To implement new one - add new method
489487
to `cli.FileLoaders` (and create pull request :) )
490-
* **Format**: `-i {json, yaml, ini}`
491-
* **Example**: `-i yaml`
492-
* **Default**: `-i json`
488+
* **Format**: `-i {json, yaml, ini}`
489+
* **Example**: `-i yaml`
490+
* **Default**: `-i json`
493491

494492
* `-o`, `--output` - Output file
495493
* **Format**: `-o <FILE>`

‎test/test_cli/test_script.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def test_help():
5757
pytest.param(f"""{executable} -m User "{test_data_path / 'users.json'}" """, id="list2"),
5858
pytest.param(f"""{executable} -l User - "{test_data_path / 'users.json'}" """, id="list2_legacy"),
5959
pytest.param(f"""{executable} -m Photos "{test_data_path / 'photos.json'}" """, id="model1"),
60+
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" \
61+
-m Model - "{test_data_path / 'users.json'}" """, id="duplicate_name"),
6062

6163
pytest.param(f"""{executable} -m Photo items "{test_data_path / 'photos.json'}" \
6264
-m Photos "{test_data_path / 'photos.json'}" """,
@@ -218,8 +220,6 @@ def trim_header(line_string):
218220

219221

220222
wrong_arguments_commands = [
221-
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" \
222-
-m Model - "{test_data_path / 'users.json'}" """, id="duplicate_name"),
223223
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" --merge unknown""",
224224
id="wrong_merge_policy"),
225225
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" --merge unknown_10""",
@@ -231,7 +231,7 @@ def trim_header(line_string):
231231
]
232232

233233

234-
@pytest.mark.xfail
234+
@pytest.mark.xfail(strict=True)
235235
@pytest.mark.parametrize("command", wrong_arguments_commands)
236236
def test_wrong_arguments(command):
237237
print("Command:", command)

‎test/test_code_generation/test_models_composition.py‎

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from typing import Dict, List, Set, Tuple, Union, Any
2-
fromcopyimportdeepcopy
1+
from typing import Dict, List, Set, Tuple, Union
2+
33
import pytest
44

55
from json_to_models.dynamic_typing import ModelMeta
@@ -21,62 +21,59 @@ def test_list_ex():
2121
assert l == [0, 'a', *range(1, 6), 'b', *range(6, 10)]
2222

2323

24-
def generate_list_input(input_dict: Dict[str, Any]) -> Dict[str, Any]:
25-
"""
26-
Convert input into a list format.
27-
28-
Mimics the case where the JSON in a file has a list
29-
at the top level, rather than a dictionary.
30-
31-
:param input_dict: dict with keys 'value', 'expected', and 'id'
32-
:type input_dict: dict
33-
:return: duplicate of the input structure but with the 'value' value as a list
34-
:rtype: dict
35-
"""
36-
outputs = {
37-
"expected": deepcopy(input_dict["expected"]),
38-
"id": input_dict["id"] + "_list",
39-
"value": []
40-
}
41-
42-
for item in input_dict["value"]:
43-
# item is a tuple of model name and model data
44-
model = [{key: deepcopy(value)} for key, value in item[1].items()]
45-
outputs["value"].append((item[0], model))
46-
47-
return outputs
48-
49-
5024
# This test relies on model names as a some sort of models ids
5125
# and may fail if some logic of their generation will be changed
5226
# List of Tuple[root_model_name, JSON data] | Dict[model_name, Set[root_model_names]]
53-
extract_root_data_input = [
54-
{
55-
"value": [
56-
("TestModelA", {"count": 1, "items": [{"x": 0.5, "y": 0.1}]}),
57-
(
58-
"TestModelB",
27+
test_extract_root_data = [
28+
pytest.param([
29+
("TestModelA", {
30+
"count": 1,
31+
"items": [
5932
{
60-
"next": "some_url",
61-
"prev": None,
62-
"count": 2000,
63-
"items": [{"x": 0.5, "y": 0.1}],
64-
},
65-
),
66-
],
67-
"expected": {"Item": {"TestModelA", "TestModelB"}, "TestModelA": set()},
68-
"id": "separate_roots"
69-
},{
70-
"value": [
71-
("TestModelA", {"count": 1, "items": [{"x": 0.5, "y": 0.1}]}),
72-
("TestModelB", {"count": 1, "items": [{"x": 0.5, "y": 0.1}]}),
73-
],
74-
"expected": {"Item": {"TestModelA_TestModelB"}, "TestModelA_TestModelB": set()},
75-
"id": "merge_root",
76-
}]
77-
78-
extract_root_data_input_list = [generate_list_input(i) for i in extract_root_data_input]
79-
test_extract_root_data = [pytest.param(inpt["value"], inpt["expected"], id=inpt["id"]) for inpt in extract_root_data_input + extract_root_data_input_list]
33+
"x": .5,
34+
"y": .1
35+
}
36+
]
37+
}),
38+
("TestModelB", {
39+
"next": "some_url",
40+
"prev": None,
41+
"count": 2000,
42+
"items": [
43+
{
44+
"x": .5,
45+
"y": .1
46+
}
47+
]
48+
}),
49+
], {
50+
'Item': {'TestModelA', 'TestModelB'},
51+
'TestModelA': set()
52+
}),
53+
pytest.param([
54+
("TestModelA", {
55+
"count": 1,
56+
"items": [
57+
{
58+
"x": .5,
59+
"y": .1
60+
}
61+
]
62+
}),
63+
("TestModelB", {
64+
"count": 1,
65+
"items": [
66+
{
67+
"x": .5,
68+
"y": .1
69+
}
70+
]
71+
}),
72+
], {
73+
'Item': {'TestModelA_TestModelB'},
74+
'TestModelA_TestModelB': set()
75+
}, id="merge_root")
76+
]
8077

8178
@pytest.mark.parametrize("value,expected", test_extract_root_data)
8279
def test_extract_root(

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /