def test_simpleschema_params(schema): @schema.add() class A(Document): a = ArrayField(min_items=1, max_items=2, unique_items=True, items=[NumberField()], additional_items=NumberField()) check_schema( schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'type': 'array', "minItems": 1, "maxItems": 2, "uniqueItems": True, 'items': [{ 'type': 'number' }], 'additionalItems': { 'type': 'number' } } }, } }, '$ref': '#/definitions/A', })
def test_compound_field_doc(schema): @schema.add() class A(Document): a = CompoundField(one_of=[StringField(), DocumentField("B")]) @schema.add() class B(Document): b = IntField() check_schema( schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'oneOf': [{ "type": "string" }, { 'type': 'object', 'properties': { 'b': { "type": "integer" } } }] } }, }, }, '$ref': '#/definitions/A', })
def test_extra(schema): @schema.add() class A(Document): a = IntField() b = 1 @schema.add() class B(A): c = IntField() d = 1 check_schema( schema, "B", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'B': { 'type': 'object', 'properties': { "a": { "type": "integer" }, "c": { "type": "integer" } } } }, '$ref': '#/definitions/B', })
def test_simpleschema_params(schema): @schema.add() class A(Document): a = NumberField(multiple_of=1, minimum=0, maximum=10, exclusive_minimum=True, exclusive_maximum=True, enum=[1, 2, 3]) check_schema(schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'type': 'number', 'multipleOf': 1, 'minimum': 0, 'maximum': 10, 'exclusiveMinimum': True, 'exclusiveMaximum': True, 'enum': [1, 2, 3] } }, } }, '$ref': '#/definitions/A', })
def preprocess_configs(self): for leaf in self.leafs: check_schema(leaf, FRECKLES_INPUT_CONFIG_SCHEMA) if FRECK_META_KEY not in leaf.keys(): continue if leaf[FRECK_META_KEY][TASK_NAME_KEY] in self.freck_plugins.keys( ): leaf[FRECK_META_KEY][FRECK_NAME_KEY] = leaf[FRECK_META_KEY][ TASK_NAME_KEY] continue freck_to_use = False for freck_name, freck in self.freck_plugins.iteritems(): if freck.can_be_used_for(copy.deepcopy(leaf[FRECK_META_KEY])): freck_to_use = freck_name break if not freck_to_use: raise FrecklesConfigError( "Can't find freck that can execute task with name '{}' (full config: {})" .format(leaf[FRECK_META_KEY][TASK_NAME_KEY], leaf[FRECK_META_KEY]), FRECK_META_KEY, leaf[FRECK_META_KEY]) leaf[FRECK_META_KEY][FRECK_NAME_KEY] = freck_to_use
def test_basic_inheritance(schema): @schema.add() class A(Document): a = StringField() @schema.add() class B(A): b = StringField() check_schema( schema, "B", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'B': { 'type': 'object', 'properties': { 'a': { 'type': 'string' }, 'b': { 'type': 'string' } }, }, }, '$ref': '#/definitions/B', })
def test_schema_ref(schema): @schema.add("A") class A(Document): a = NumberField() @schema.add("B") class B(Document): b = DocumentField("A", as_ref=True) check_schema(schema, "B", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': {'type': 'number'} }, }, 'B': { 'type': 'object', 'properties': { 'b': {'$ref': '#/definitions/A'} }, } }, '$ref': '#/definitions/B', })
def test_compound_doc_two_types(schema): @schema.add() class A(Document): a = StringField() @schema.add() class B(Document): b = StringField() @schema.add() class C(CompoundDocument): one_of = ["A", "B"] any_of = ["A", "B"] check_schema( schema, "C", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'type': 'string', } }, }, 'B': { 'type': 'object', 'properties': { 'b': { 'type': 'string', } }, }, 'C': { 'anyOf': [ { '$ref': '#/definitions/A', }, { '$ref': '#/definitions/B', }, ], 'oneOf': [ { '$ref': '#/definitions/A', }, { '$ref': '#/definitions/B', }, ] } }, '$ref': '#/definitions/C', })
def test_roles_list(schema): @schema.add() class A(Document): a = IntField(roles=["test"]) check_schema(schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': {'type': 'object'} }, '$ref': '#/definitions/A', }, ["nottest"])
def test_roles_default_none_available(schema): @schema.add() class A(Document): a = IntField(roles="test") check_schema(schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': {'type': 'object'} }, '$ref': '#/definitions/A', })
def test_get_authority_onids(self, endpoint='/spendingauthority'): for onid in self.test_cases['valid_authority_onids']: params = {'onid': onid} response = utils.make_request(self, endpoint, 200, params=params) # Test case: GET /spendingauthority with valid authority onids spending_schema = utils.get_resource_schema( self, 'SpendingAuthorityResource') utils.check_schema(self, response, spending_schema) response_data = response.json()['data'] actual_onid = response_data['id'] logging.debug(f'Requested onid: {onid}') logging.debug(f'Received onid: {actual_onid}') self.assertEqual(actual_onid.lower(), onid.lower()) self.checking_spending_limits(response_data['attributes'])
def test_formats(schema): @schema.add() class A(Document): date = DateTimeField() email = EmailField() hostname = HostnameField() ipv4 = IPv4Field() ipv6 = IPv6Field() uri = URIField() check_schema( schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'date': { 'type': 'string', 'format': "date-time" }, 'email': { 'type': 'string', 'format': "email" }, 'hostname': { 'type': 'string', 'format': "hostname" }, 'ipv4': { 'type': 'string', 'format': "ipv4" }, 'ipv6': { 'type': 'string', 'format': "ipv6" }, 'uri': { 'type': 'string', 'format': "uri" }, }, } }, '$ref': '#/definitions/A', })
def test_roles(schema): @schema.add() class A(Document): a = IntField(roles="test") check_schema(schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': {'type': 'integer'} }, } }, '$ref': '#/definitions/A', }, roles="test")
def test_required(schema): @schema.add("A") class A(Document): a = NumberField(required=True) check_schema(schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': {'type': 'number'} }, 'required': ['a'] } }, '$ref': '#/definitions/A', })
def test_integer_field(schema): @schema.add() class A(Document): a = IntField(default=0) check_schema(schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'type': 'integer', 'default': 0 } }, } }, '$ref': '#/definitions/A', })
def test_empty_params(schema): @schema.add() class A(Document): a = ArrayField() check_schema( schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'type': 'array' } }, } }, '$ref': '#/definitions/A', })
def test_title_description(schema): @schema.add("A") class A(Document): a = NumberField(required=True, title="title", description="description") check_schema(schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'title': 'title', "description": "description", 'type': 'number'} }, 'required': ['a'] } }, '$ref': '#/definitions/A', })
def test_simpleschema_params(schema): @schema.add() class A(Document): a = DictField(min_properties=1, max_properties=2, properties={"a": NumberField()}, additional_properties=NumberField(), pattern_properties={".*": NumberField()}) check_schema( schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'type': 'object', "properties": { "a": { "type": "number" } }, "additionalProperties": { "type": "number" }, "patternProperties": { ".*": { "type": "number" } }, "minProperties": 1, "maxProperties": 2 } }, } }, '$ref': '#/definitions/A', })
def test_schema_ref(schema): @schema.add("A", title="title") class A(Document): """description""" a = NumberField() check_schema( schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { "title": "title", "description": "description", 'type': 'object', 'properties': { 'a': { 'type': 'number' } }, }, }, '$ref': '#/definitions/A', })
def test_simpleschema(schema): @schema.add() class A(Document): a = StringField(pattern=".", default="a", min_length=1, max_length=1) check_schema( schema, "A", { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'A': { 'type': 'object', 'properties': { 'a': { 'type': 'string', 'pattern': ".", "default": "a", "minLength": 1, "maxLength": 1, } }, } }, '$ref': '#/definitions/A', })
def test_bad_request_response(self, endpoint='/spendingauthority'): bad_params = [{'onid': ''}, {}] for param in bad_params: response = utils.make_request(self, endpoint, 400, params=param) error_schema = utils.get_resource_schema(self, 'Error') utils.check_schema(self, response, error_schema)
def process_leafs(self): frecks = [] for freck_nr, leaf in enumerate(self.leafs): if FRECK_META_KEY not in leaf.keys(): continue freck_name = leaf[FRECK_META_KEY][FRECK_NAME_KEY] runner, processed = self.freck_plugins[freck_name].process_leaf( copy.deepcopy(leaf), self.supported_runners, self.debug_freck) if not processed: log.debug("No frecks created for freck_name '{}'.".format( freck_name)) continue if self.debug_freck: click.echo("Processed leaf '{}'.".format(freck_name)) click.echo("---") click.echo("Input:") click.echo(pprint.pformat(leaf)) click.echo("---") click.echo("Result:") click.echo(pprint.pformat(processed)) click.echo("===============================================") if isinstance(processed, dict): processed = [processed] # apply result on top of original configuration temp = [] for p in processed: t = copy.deepcopy(leaf[FRECK_META_KEY]) dict_merge(t, p) temp.append(t) processed = temp new_run = False for prep in processed: prep[FRECK_RUNNER_KEY] = runner prep[FRECK_INDEX_KEY] = freck_nr prep.setdefault(FRECK_ITEM_NAME_KEY, "{}".format(prep[FRECK_NAME_KEY])) prep.setdefault(FRECK_NEW_RUN_AFTER_THIS_KEY, False) if FRECK_PRIORITY_KEY not in prep.keys(): prep[FRECK_PRIORITY_KEY] = FRECK_DEFAULT_PRIORITY + ( freck_nr * 1000) check_schema(prep, FRECKLES_POST_PREPROCESS_SCHEMA) if prep[FRECK_NEW_RUN_AFTER_THIS_KEY]: new_run = True frecks.extend(processed) if new_run: frecks = self.sort_frecks(frecks) run_frecks = [] for f in frecks: run_frecks.append(f) if f[FRECK_NEW_RUN_AFTER_THIS_KEY]: yield self.sort_frecks(run_frecks) run_frecks = [] frecks = run_frecks yield self.sort_frecks(frecks)