def test_data_export(self): class SomeEntity(Entity): declared_params = {"some_value": "string"} obj = SomeEntity() self.mapper.register(SomeEntity) self.mapper.save(obj) data_to_save = [("some key", "ABCDE", None), ("some other key", "ABCDEFGHIJ", "text")] for (key, value, mimetype) in data_to_save: obj.data[key].put(value) if mimetype: obj.data[key].mimetype = mimetype # we create a named temporary file and just hope # there is no conflict with the autodetected folder name tmp_name = tempfile.mktemp() jio = JsonIO(self.mapper, add_new_types=True) jio.write_file(self.mapper.find_roots(), tmp_name) # check that file has been written: for (data_key, data_value, data_mimetype) in data_to_save: data_file = os.path.join(tmp_name + ".data", obj.unique_id, data_key) with open(data_file) as df: self.assertEqual(df.read(), data_value) with open(tmp_name) as tmp_file: json_obj = json.load(tmp_file) #print json_obj jio_2 = JsonIO(self.mapper) self.assertRaises(IntegrityError, jio_2.read_file, tmp_name) self.mapper.delete(*self.mapper.find_roots()) jio_2 = JsonIO(self.mapper) jio_2.read_file(tmp_name) obj_in_db = self.mapper.find_first(SomeEntity) for (data_key, data_value, data_mimetype) in data_to_save: self.assertEqual(obj_in_db.data[data_key].get_string(), data_value) self.assertEqual(obj_in_db.data[data_key].mimetype, data_mimetype)
from xdapy import Connection, Mapper, Entity from xdapy.io import JsonIO connection = Connection.profile("demo") # use standard profile connection.create_tables() #connection.engine.echo = True m = Mapper(connection) # We create a new JSON importer. # add_new_types = True so all defined and declared objects are imported jio = JsonIO(m, add_new_types=True) jio.ignore_unknown_attributes = True with open("demo/detection.json") as f: objs = jio.read_file(f) for cls in m.registered_entities: print """class {name}(EntityObject):\n parameter_types = {types!r}\n""".format(name=cls.__original_class_name__, types=cls.declared_params) print len(objs), "objects" print jio.write_string(objs)