Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #19 (as discussed) and added generate_xml method #21

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -12,7 +12,7 @@
long_description_content_type="text/markdown",
package_dir={"": "src"},
packages=setuptools.find_packages("src", exclude=["tests"]),
install_requires=["rstr", "faker", "smart_open", "jsonschema", "typer", "pydantic"],
install_requires=["rstr", "faker", "smart_open", "jsonschema", "typer", "pydantic", "defusedxml", "requests", "json2xml"],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since XML wont be added in the package as noted below, could you remove this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not work for me for some reason.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am on ubuntu.
command used: pytest

pwd: jsf/src

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above comments are supposed to be for pytest failure I mentioned above.

url="https://github.com/ghandic/jsf",
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
31 changes: 26 additions & 5 deletions src/jsf/parser.py
Expand Up @@ -5,9 +5,10 @@
from datetime import datetime
from itertools import count
from typing import Any, Dict, List, Optional, Tuple, Union
from json2xml import json2xml

from faker import Faker
from jsonschema import validate
from jsonschema import validate as val
from pydantic import conlist
from smart_open import open as s_open

Expand Down Expand Up @@ -126,22 +127,42 @@ def _parse(self, schema: Dict[str, Any]) -> AllTypes:
def context(self):
return {**self.base_context, "state": deepcopy(self.base_state)}

def generate(self, n: Optional[int] = None) -> Any:
def generate(self, n: Optional[int] = None, validate:Optional[bool] = False) -> Any:
ayushbindlish marked this conversation as resolved.
Show resolved Hide resolved
if n is None or n == 1:
if validate:
data = self.root.generate(context=self.context)
val(instance=data,schema=self.root_schema)
return data
return self.root.generate(context=self.context)
return [self.root.generate(context=self.context) for _ in range(n)]
else:
data_arr = []
for _ in range(n):
if validate:
data = self.root.generate(context=self.context)
val(instance=data,schema=self.root_schema)
data_arr.append(data)
else:
data = self.root.generate(context=self.context)
data_arr.append(data)
return data_arr


def pydantic(self):
return self.root.model(context=self.context)[0]

def generate_and_validate(self) -> None:
def validate(self) -> None:
fake = self.root.generate(context=self.context)
validate(instance=fake, schema=self.root_schema)
val(instance=fake, schema=self.root_schema)

def to_json(self, path: str) -> None:
with open(path, "w") as f:
json.dump(self.generate(), f, indent=2)

def generate_xml(self):
ayushbindlish marked this conversation as resolved.
Show resolved Hide resolved
data = self.generate()
data = json2xml.Json2xml(data, pretty=True).to_xml()
return data

@staticmethod
def from_json(path: str) -> "JSF":
with open(path, "r") as f:
Expand Down
38 changes: 38 additions & 0 deletions src/tests/data/validate.json
@@ -0,0 +1,38 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "Employee Json Template Spec",
"type": "object",
"properties": {
"EmpID": {
"type": "string",
"maximum": 255
},
"UserID": {
"type": "number",
"maximum": 255
},
"Name": {
"type": "string",
"minimum": 2
},
"Email": {
"type": "string",
"minLength": 6,
"maxLength": 255
},
"Phone": {
"type": "string",
"maxLength": 10
},
"Salary": {
"type": "number"
},
"Hiredate": {
"type": "string",
"format": "date"
},
"RemainingHolidays": {
"type": "number"
}
}
}
40 changes: 35 additions & 5 deletions src/tests/test_default_fake.py
@@ -1,7 +1,9 @@
import json
import re

import json2xml
import pytest
from ..jsf.parser import JSF
import re


def test_fake_boolean(TestData):
Expand Down Expand Up @@ -229,7 +231,7 @@ def test_const(TestData):
assert isinstance(f["country"], str)
assert f["country"] == "United States of America"


#@pytest.mark.xfail(reason="Always failing with "no such file or dir" error!!")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should exist if youre running the tests from a unix based machine and from the same directory as the src folder

def test_external_ref(TestData):
with open(TestData / f"external-ref.json", "r") as file:
schema = json.load(file)
Expand All @@ -245,8 +247,36 @@ def test_external_ref(TestData):
assert all(isinstance(t, str) for t in f["ReferenceToExternalSchema"]["src"])


def test_gen_and_validate(TestData):
with open(TestData / f"custom.json", "r") as file:
def test_validate(TestData):
with open(TestData / f"validate.json", "r") as file:
schema = json.load(file)
p = JSF(schema)
[p.generate_and_validate() for _ in range(50)]
[p.validate() for _ in range(50)]

def test_validate_and_generate_single_record(TestData):
"""validate and generate data from schema"""
with open(TestData / f"validate.json", "r") as file:
schema = json.load(file)
p = JSF(schema)
fake_data = p.generate(validate=True)
assert(isinstance(fake_data,dict))

def test_validate_and_generate__multiple_records(TestData):
"""validate and generate data from schema"""
with open(TestData / f"validate.json", "r") as file:
schema = json.load(file)
p = JSF(schema)
fake_data = p.generate(validate=True,n=100)
assert(isinstance(fake_data,list))
assert(len(fake_data)==100)
for x in fake_data:
assert(isinstance(x,dict))

def test_generate_xml(TestData):
ayushbindlish marked this conversation as resolved.
Show resolved Hide resolved
"""validate and generate data from schema"""
with open(TestData / f"validate.json", "r") as file:
schema = json.load(file)
p = JSF(schema)
p.generate_xml()