From f32832ad8e461b1f1b11f3d95111c650740e005e Mon Sep 17 00:00:00 2001 From: Alvaro Leonel Date: Thu, 25 Feb 2021 16:51:47 -0300 Subject: [PATCH] Adds benchmark tests for Schematics tool (#2381) * Adds benchmark tests for Schematics tool * Removes the "to_json" method from the schematics benchmark tests * Fixs exception check in the Schematics benchmark * tweaks and cleanup Co-authored-by: Alvaro Mariano Co-authored-by: Samuel Colvin --- benchmarks/requirements.txt | 1 + benchmarks/run.py | 17 +++++++++++- benchmarks/test_schematics.py | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 benchmarks/test_schematics.py diff --git a/benchmarks/requirements.txt b/benchmarks/requirements.txt index 6f3bf9a098..5c8184bc7f 100644 --- a/benchmarks/requirements.txt +++ b/benchmarks/requirements.txt @@ -11,3 +11,4 @@ attr cattrs cerberus voluptuous +schematics diff --git a/benchmarks/run.py b/benchmarks/run.py index 0f74d9582c..b948e4989a 100644 --- a/benchmarks/run.py +++ b/benchmarks/run.py @@ -58,6 +58,12 @@ print('WARNING: unable to import TestVoluptuous') TestVoluptuous = None +try: + from test_schematics import TestSchematics +except Exception as e: + print('WARNING: unable to import TestSchematics') + TestSchematics = None + PUNCTUATION = ' \t\n!"#$%&\'()*+,-./' LETTERS = string.ascii_letters UNICODE = '\xa0\xad¡¢£¤¥¦§¨©ª«¬ ®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' @@ -65,7 +71,16 @@ random = random.SystemRandom() # in order of performance for csv -other_tests = [TestCAttrs, TestValideer, TestMarshmallow, TestVoluptuous, TestTrafaret, TestDRF, TestCerberus] +other_tests = [ + TestCAttrs, + TestValideer, + TestMarshmallow, + TestVoluptuous, + TestTrafaret, + TestSchematics, + TestDRF, + TestCerberus, +] active_other_tests = [t for t in other_tests if t is not None] diff --git a/benchmarks/test_schematics.py b/benchmarks/test_schematics.py new file mode 100644 index 0000000000..94af84637d --- /dev/null +++ b/benchmarks/test_schematics.py @@ -0,0 +1,50 @@ +from schematics import __version__ +from schematics.exceptions import DataError, ValidationError +from schematics.models import Model as PModel +from schematics.types import IntType, StringType +from schematics.types.base import DateType, FloatType +from schematics.types.compound import ListType, ModelType + + +class TestSchematics: + package = 'schematics' + version = __version__ + + def __init__(self, allow_extra): + class Model(PModel): + id = IntType(required=True) + client_name = StringType(max_length=255, required=True) + sort_index = FloatType(required=True) + client_phone = StringType(max_length=255, default=None) + + class Location(PModel): + latitude = FloatType(default=None) + longitude = FloatType(default=None) + + location = ModelType(model_spec=Location, default=None) + + contractor = IntType(min_value=1, default=None) + upstream_http_referrer = StringType(max_length=1023, default=None) + grecaptcha_response = StringType(min_length=20, max_length=1000, required=True) + last_updated = DateType(formats='%Y-%m-%dT%H:%M:%S') + + class Skill(PModel): + subject = StringType(required=True) + subject_id = IntType(required=True) + category = StringType(required=True) + qual_level = StringType(required=True) + qual_level_id = IntType(required=True) + qual_level_ranking = FloatType(default=0, required=True) + + skills = ListType(ModelType(Skill), default=[]) + + self.model = Model + + def validate(self, data): + try: + obj = self.model(data) + return True, obj.validate() + except DataError as e: + return False, e + except ValidationError as e: + return False, e