Skip to content

Commit

Permalink
Adds benchmark tests for Schematics tool (#2381)
Browse files Browse the repository at this point in the history
* 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 <alvaro.mariano@br.experian.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
3 people committed Feb 25, 2021
1 parent ededd3e commit f32832a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions benchmarks/requirements.txt
Expand Up @@ -11,3 +11,4 @@ attr
cattrs
cerberus
voluptuous
schematics
17 changes: 16 additions & 1 deletion benchmarks/run.py
Expand Up @@ -58,14 +58,29 @@
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¡¢£¤¥¦§¨©ª«¬ ®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
ALL = PUNCTUATION * 5 + LETTERS * 20 + UNICODE
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]


Expand Down
50 changes: 50 additions & 0 deletions 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

0 comments on commit f32832a

Please sign in to comment.