def test_do_get_checksum(self): data1 = {"foo": "bar"} os.environ[ENV_VAR_HASHCODE_NAMESPACE] = "bar" os.environ[ENV_VAR_NEXUS_NAMESPACE] = "foo" data2 = {"bar": "bar"} checksum1 = Entity.do_get_checksum(data1) checksum2 = Entity.do_get_checksum(data2) self.assertEqual(checksum1, checksum2)
def create_instance(self, data, schema_data, fail_if_linked_instance_is_missing=True): """Create a new instance for the provided data Arguments: file_path -- path to the location of the file to be uploaded as instance fully_qualify -- if True, prefixes are resolved and the JSON-LD to be uploaded will be interpretable as JSON (but with non-human-friendly, fully qualified keys) """ raw_json = self.resolve_entities( data if not isinstance(data, dict) else json.dumps(data), fail_if_linked_instance_is_missing) raw_json = self._fill_placeholders(raw_json) fully_qualified_json = Entity.fully_qualify(json.loads(raw_json)) if not self._upload_fully_qualified: final_json = json.loads(raw_json) if not isinstance( raw_json, dict) else raw_json else: final_json = fully_qualified_json schema_identifier = "http://schema.org/identifier" hashcode_field = "http://hbp.eu/internal#hashcode" if self._upload_fully_qualified: raw_json = final_json instance = Instance.create_new(schema_data.organization, schema_data.domain, schema_data.name, schema_data.version, raw_json) if hashcode_field not in fully_qualified_json: current_hashcode = Entity.do_get_checksum(fully_qualified_json) fully_qualified_json[hashcode_field] = current_hashcode instance.data[hashcode_field] = current_hashcode else: current_hashcode = fully_qualified_json[hashcode_field] if schema_identifier in fully_qualified_json: identifier = fully_qualified_json.get(schema_identifier) result = self.handle_known_schema_identifier( schema_identifier, instance, hashcode_field, current_hashcode, identifier) if result is not None: return result return self._client.instances.create( Instance.create_new(schema_data.organization, schema_data.domain, schema_data.name, schema_data.version, raw_json))