Exemple #1
0
 def test_setdefault(self):
     py_obj = jsonasobj.JsonObj()
     py_obj._setdefault('test', dict(foo=17))
     py_obj._setdefault('test', 'nada')
     py_obj._setdefault('test2', 'sama')
     self.assertTrue(compare_dicts(dict(test=dict(foo=17), test2="sama"), py_obj._as_dict))
     self.assertTrue(compare_dicts(dict(test=dict(foo=17), test2="sama"), as_dict(py_obj)))
Exemple #2
0
 def test_example(self):
     pyobj = jsonasobj.loads(str(test_json))
     self.assertEqual('Markus Lanthaler', pyobj.name)
     self.assertEqual(pyobj.name, pyobj['name'])
     self.assertEqual('Dave Longley', pyobj.knows[0].name)
     self.assertEqual('http://xmlns.com/foaf/0.1/name', pyobj['@context'].name)
     self.assertEqual(json.loads(test_json), json.loads(pyobj._as_json))
     self.assertEqual(json.loads(pyobj._as_json), json.loads(as_json(pyobj)))
     self.assertTrue(compare_dicts(test_data, pyobj._as_dict))
     self.assertTrue(compare_dicts(test_data, as_dict(pyobj)))
Exemple #3
0
def compare_json(shex_url: str, shex_json: str, log: Logger) -> bool:
    """
    Compare the JSON generated from shex_url to the JSON in the target directory
    :param shex_url: URL where we got the ShExC
    :param shex_json: ShExJ equivalent of ShExC
    :param log: Where comparison errors are recorded
    :return: True if they match, false otherwise.  If no match, the offending string is printed
    """
    json_url = shex_url.rsplit(".", 1)[0] + ".json"
    if ':' in json_url:
        resp = requests.get(json_url)
        if not resp.ok:
            return False
        json_text = resp.text
    else:
        try:
            with open(json_url) as f:
                json_text = f.read()
        except FileNotFoundError:
            print(f"****> {json_url} not found. Comparison not done ***")
            return True
    d1 = jao_loads(json_text)
    d2 = jao_loads(shex_json)
    if not compare_dicts(as_dict(d1),
                         as_dict(d2),
                         d1name="expected",
                         d2name="actual  ",
                         file=log,
                         filtr=json_filtr):
        print(as_json(d2))
        return False
    return True
Exemple #4
0
def compare_json(j1: str, j2: str, log: TextIO) -> bool:
    """
    Compare two JSON representation
    :param j1: first string
    :param j2: second string
    :param log: log file to record differences
    :return: Result of comparison
    """
    d1 = jao_loads(j1)
    d2 = jao_loads(j2)
    return compare_dicts(as_dict(d1), as_dict(d2), file=log)
Exemple #5
0
 def test_facet(self):
     file_loc = os.path.join(os.path.dirname(__file__), 'data', '1bnodeLength.json')
     with open(file_loc)  as f:
         text = f.read()
     facets = loads(text, ShExJ)
     # print(as_json(facets))
     log = Logger()
     val = is_valid(facets, log)
     # print(log.getvalue())
     self.assertTrue(val)
     d1 = jao_loads(text)
     d2 = jao_loads(as_json(facets))
     self.assertTrue(compare_dicts(as_dict(d1), as_dict(d2)))
    def compare_shexj(
            shex: Union[ShExJ.Schema, str],
            shexj: Union[ShExJ.Schema, str]) -> Tuple[bool, StringIO]:
        d1 = loads(as_json(shex) if isinstance(shex, ShExJ.Schema) else shex)
        d2 = loads(
            as_json(shexj) if isinstance(shexj, ShExJ.Schema) else shexj)

        log = StringIO()
        with redirect_stdout(log):
            return compare_dicts(d1._as_dict,
                                 d2._as_dict,
                                 d1name="expected",
                                 d2name="actual  ",
                                 filtr=json_filtr), log
def compare_json(shex_url: str, shex_json: str, log: Logger) -> bool:
    """
    Compare the JSON generated from shex_url to the JSON in the target directory
    :param shex_url: URL where we got the ShExC
    :param shex_json: ShExJ equivalent of ShExC
    :param log: Where comparison errors are recorded
    :return: True if they match, false otherwise.  If no match, the offending string is printed
    """
    json_url = shex_url.rsplit(".", 1)[0] + ".json"
    if ':' in json_url:
        resp = requests.get(json_url)
        if not resp.ok:
            return False
        json_text = resp.text
    else:
        with open(json_url) as f:
            json_text = f.read()
    d1 = jao_loads(json_text)
    d2 = jao_loads(shex_json)
    if not compare_dicts(d1._as_dict, d2._as_dict, d1name="expected", d2name="actual  ", file=log, filtr=json_filtr):
        print(d2._as_json_dumps())
        return False
    return True
Exemple #8
0
def compare_json(j1: str, j2: str, log: TextIO) -> bool:
    """ Compare two JSON strings """
    d1 = jao_loads(j1)
    d2 = jao_loads(j2)
    return compare_dicts(as_dict(d1), as_dict(d2), file=log)
Exemple #9
0
def compare_json(j1, j2, log):
    d1 = jao_loads(j1)
    d2 = jao_loads(j2)
    return compare_dicts(d1._as_dict, d2._as_dict, file=log)