def test_gzopen_json(): with tmpfile('.json.gz') as filename: with gzip.open(filename, 'w') as f: f.write('[[1, 1], [2, 2]]') # Not a valid JSON file assert raises(Exception, lambda: list(JSON(filename, schema='2 * int'))) dd = JSON(filename, schema='2 * int', open=gzip.open) assert list(dd) == [[1, 1], [2, 2]]
def test_gzopen_json(): with tmpfile('.json.gz') as filename: f = gzip.open(filename, 'wt') f.write('[[1, 1], [2, 2]]') f.close() # Not a valid JSON file assert raises(Exception, lambda: list(JSON(filename, schema='2 * int'))) dd = JSON(filename, schema='2 * int', open=gzip.open) assert tuplify(list(dd)) == ((1, 1), (2, 2))
def test_iter(self): dd = JSON(self.json_file, dshape=json_dshape) # This equality does not work yet # self.assertEqual(dd.dshape, datashape.dshape( # 'Var, %s' % json_schema)) print(list(dd)) self.assertEqual(list(dd), [1, 2, 3, 4, 5])
def test_gzip_json_files(self): with filetexts(texts, open=gzip.open) as filenames: descriptors = [JSON(fn, dshape=schema, open=gzip.open) for fn in sorted(filenames)] dd = Stack(descriptors) self.assertEqual(sorted(dd), sorted(tuples)) self.assertEqual(dd.schema, dshape(schema))
def test_jsonarray_into_mongodb(empty_collec): filename = tempfile.mktemp(".json") with open(filename, "w") as f: json.dump(data, f) dd = JSON(filename, schema="3 * { id : string, name : string, " "posts : var * { content : string, title : string }," " tv_show : string }") coll = empty_collec into(coll, dd, json_array=True) mongo_data = list(coll.find({}, {'_id': 0})) assert mongo_data[0] == data[0]
def test_json_into_mongodb(empty_collec): with filetext(json.dumps(les_mis_data)) as filename: dd = JSON(filename) coll = empty_collec into(coll, dd) mongo_data = list(coll.find()) last = mongo_data[0]['nodes'][-1] first = mongo_data[0]['nodes'][0] first = (first['group'], first['name']) last = (last['group'], last['name']) assert dd.as_py()[1][-1] == last assert dd.as_py()[1][0] == first
def test_basic_object_type(self): dd = JSON(self.json_file, dshape=json_dshape) self.assertEqual(list(dd), [1, 2, 3, 4, 5])
def test_raise_error_on_non_existent_file(self): self.assertRaises(ValueError, lambda: JSON('does-not-exist23424.josn', 'r'))
def test_as_py(self): dd = JSON(self.filename, 'r', dshape=self.dshape) self.assertEqual(dd.as_py(), self.data)
def test_basic(self): dd = JSON(self.filename, 'r', dshape=self.dshape) self.assertEqual(list(dd), [nd.as_py(nd.parse_json(self.dshape, json.dumps(self.data)))])
def test_discovery(self): dd = JSON(self.filename, 'r') s = str(dd.dshape) for word in ['Thumbnail', 'string', 'int', 'images', 'type']: assert word in s
def test_as_py(self): dd = JSON(self.filename, 'r', dshape=self.dshape) self.assertEqual(tuplify(dd.as_py()), self.ordered)
def test_basic(self): dd = JSON(self.filename, 'r', dshape=self.dshape) self.assertRaises(Exception, lambda: tuple(dd))