-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add JSON schema fuzz test strategy (#32)
* test: add JSON schema fuzz test strategy * test: add parametrized tests using examples from ethPM spec * refactor: rename test file to be more accurate * refactor: remove unnecessary tests * refactor: leverage dataclasses more * fix: json serialization must sort keys per EIP; add EIP notes * fix: `name` and `version` in Manifest can be empty, add `manifest` default * docs: add EIP notes to manifest * test: add xfail to schema test because the schema produces erronous data * refactor: fixup unprocessed dicts
- Loading branch information
Showing
6 changed files
with
204 additions
and
531 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import json | ||
from copy import deepcopy | ||
from pathlib import Path | ||
from typing import Dict | ||
|
||
import dataclassy as dc | ||
|
||
|
||
def update_params(params, param_name, param_type): | ||
if param_name in params and params[param_name]: | ||
params[param_name] = param_type.from_dict(params[param_name]) | ||
|
||
|
||
def update_list_params(params, param_name, param_type): | ||
if param_name in params and params[param_name]: | ||
params[param_name] = [param_type.from_dict(p) for p in params[param_name]] | ||
|
||
|
||
def update_dict_params(params, param_name, param_type): | ||
if param_name in params and params[param_name]: | ||
for key in params[param_name]: | ||
params[param_name][key] = param_type.from_dict(params[param_name][key]) | ||
|
||
|
||
def remove_none_fields(data): | ||
if isinstance(data, dict): | ||
return { | ||
k: remove_none_fields(v) | ||
for k, v in data.items() | ||
if v is not None and remove_none_fields(v) is not None | ||
} | ||
|
||
elif isinstance(data, list): | ||
return [ | ||
remove_none_fields(v) | ||
for v in data | ||
if v is not None and remove_none_fields(v) is not None | ||
] | ||
|
||
return data | ||
|
||
|
||
@dc.dataclass(slots=True, kwargs=True) | ||
class SerializableType: | ||
def to_dict(self) -> Dict: | ||
return remove_none_fields({k: v for k, v in dc.asdict(self).items() if v}) | ||
|
||
@classmethod | ||
def from_dict(cls, params: Dict): | ||
params = deepcopy(params) | ||
return cls(**params) # type: ignore | ||
|
||
|
||
class FileMixin(SerializableType): | ||
@classmethod | ||
def from_file(cls, path: Path): | ||
return cls.from_dict(json.load(path.open())) | ||
|
||
def to_file(self, path: Path): | ||
# NOTE: EIP-2678 specifies document *must* be tightly packed | ||
# NOTE: EIP-2678 specifies document *must* have sorted keys | ||
json.dump(self.to_dict(), path.open("w"), indent=4, sort_keys=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.