Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_edge_validation_with_union_schema_fails(self):
schema_dicts = [self.make_avro_edge_schema("derived_from",
{"aliquot": "analyte"}),
self.make_avro_edge_schema("member_of",
{"file": "archive"})]
schema = make_avsc_object(schema_dicts)
self.driver.edge_validator = AvroEdgeValidator(schema)
self.insert_edge("derived_from", "aliquot", "analyte")
self.insert_edge("member_of", "file", "archive")
with self.assertRaises(ValidationError):
self.insert_edge("random_edge", "file", "archive")
with self.assertRaises(ValidationError):
self.insert_edge("member_of", "file", "analyte")
def test_edge_validation_with_union_schema_succeeds(self):
schema_dicts = [self.make_avro_edge_schema("derived_from",
{"aliquot": "analyte"}),
self.make_avro_edge_schema("member_of",
{"file": "archive"})]
schema = make_avsc_object(schema_dicts)
self.driver.edge_validator = AvroEdgeValidator(schema)
self.insert_edge("derived_from", "aliquot", "analyte")
self.insert_edge("member_of", "file", "archive")
>>> from elasticgit.commands.avro import deserialize
>>> schema = {
... 'name': 'Foo',
... 'type': 'record',
... 'fields': [{
... 'name': 'some_field',
... 'type': 'int',
... }]
... }
>>> deserialize(schema)
>>>
"""
schema_loader = SchemaLoader()
schema = avro.schema.make_avsc_object(data, avro.schema.Names()).to_json()
model_code = schema_loader.generate_model(schema)
model_name = schema['name']
if module_name is not None:
mod = imp.new_module(module_name)
scope = mod.__dict__
else:
scope = {}
exec model_code in scope
return scope.pop(model_name)
def _parse_response(self, response, names):
if isinstance(response, basestring) and names.has_name(response, None):
return names.get_name(response, None)
else:
return schema.make_avsc_object(response, names)
c["type"] = c["type"]
if key == "inputs":
self.inputs_record_schema["fields"].append(c)
elif key == "outputs":
self.outputs_record_schema["fields"].append(c)
try:
self.inputs_record_schema = schema_salad.schema.make_valid_avro(self.inputs_record_schema, {}, set())
avro.schema.make_avsc_object(self.inputs_record_schema, self.names)
except avro.schema.SchemaParseException as e:
raise validate.ValidationException("Got error `%s` while prcoessing inputs of %s:\n%s" % (str(e), self.tool["id"], json.dumps(self.inputs_record_schema, indent=4)))
try:
self.outputs_record_schema = schema_salad.schema.make_valid_avro(self.outputs_record_schema, {}, set())
avro.schema.make_avsc_object(self.outputs_record_schema, self.names)
except avro.schema.SchemaParseException as e:
raise validate.ValidationException("Got error `%s` while prcoessing outputs of %s:\n%s" % (str(e), self.tool["id"], json.dumps(self.outputs_record_schema, indent=4)))
def get_avro_schema_object(schema):
""" Helper function to simplify dealing with the three ways avro schema may
be represented:
- a json string
- a dictionary (parsed json string)
- a parsed `avro.schema.Schema` object
In all cases this returns the `avro.schema.Schema` object form
"""
if isinstance(schema, avro.schema.Schema):
return schema
elif isinstance(schema, basestring):
return avro.schema.parse(schema)
else:
return avro.schema.make_avsc_object(schema)
def _create_schema_elements_from_json(cls, avro_schema_json):
avro_schema_obj = schema.make_avsc_object(avro_schema_json)
schema_elements = []
schema_elements_queue = deque([(avro_schema_obj, None)])
while schema_elements_queue:
schema_obj, parent_key = schema_elements_queue.popleft()
element_cls = _schema_to_element_map.get(schema_obj.__class__)
if not element_cls:
continue
_schema_element = element_cls(schema_obj, parent_key)
schema_elements.append((_schema_element, schema_obj))
parent_key = _schema_element.key
for nested_schema in _schema_element.nested_schema_objects:
schema_elements_queue.append((nested_schema, parent_key))
return schema_elements
def end(self):
if not self._schema_tracker:
# this is the top level schema; do the schema validation
schema_obj = schema.make_avsc_object(self._schema_json)
self._schema_json = None
return schema_obj.to_json()
current_schema_json = self._schema_json
self._restore_current_schema()
return current_schema_json
def schema(j):
names = avro.schema.Names()
j = extend_avro(j)
for t in j:
if not t.get("abstract"):
avro.schema.make_avsc_object(t, names)
return names
if "type" not in c:
raise validate.ValidationException("Missing `type` in parameter `%s`" % c["name"])
if "default" in c and "null" not in aslist(c["type"]):
c["type"] = ["null"] + aslist(c["type"])
else:
c["type"] = c["type"]
if key == "inputs":
self.inputs_record_schema["fields"].append(c)
elif key == "outputs":
self.outputs_record_schema["fields"].append(c)
try:
self.inputs_record_schema = schema_salad.schema.make_valid_avro(self.inputs_record_schema, {}, set())
avro.schema.make_avsc_object(self.inputs_record_schema, self.names)
except avro.schema.SchemaParseException as e:
raise validate.ValidationException("Got error `%s` while prcoessing inputs of %s:\n%s" % (str(e), self.tool["id"], json.dumps(self.inputs_record_schema, indent=4)))
try:
self.outputs_record_schema = schema_salad.schema.make_valid_avro(self.outputs_record_schema, {}, set())
avro.schema.make_avsc_object(self.outputs_record_schema, self.names)
except avro.schema.SchemaParseException as e:
raise validate.ValidationException("Got error `%s` while prcoessing outputs of %s:\n%s" % (str(e), self.tool["id"], json.dumps(self.outputs_record_schema, indent=4)))