Scalar Types
Scalar types represent a single value and cannot contain other types. They are used to define simple data types.
None
schema.none
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.none
from d42 import schema, fake
sch = schema.none
assert fake(sch) is None
from d42 import schema
sch = schema.none
assert sch == None
assert sch != False # incorrect type
from d42 import schema
sch = schema.none
assert sch % None == schema.none
with assertRaises(SubstitutionError):
sch % 0 # incorrect type
Bool
schema.bool
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.bool
from d42 import schema, fake
sch = schema.bool
assert fake(sch) in (True, False)
from d42 import schema
sch = schema.bool
assert sch == True
assert sch == False
assert sch != None # incorrect type
from d42 import schema
sch = schema.bool
assert sch % True == schema.bool(True)
with assertRaises(SubstitutionError):
sch % 1 # incorrect type
schema.bool(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.bool(True)
from d42 import schema, fake
sch = schema.bool(True)
assert fake(sch) is True
from d42 import schema
sch = schema.bool(True)
assert sch == True
assert sch != False # incorrect value
from d42 import schema
sch = schema.bool(True)
assert sch % True == schema.bool(True)
with assertRaises(SubstitutionError):
sch % False # incorrect value
Int
schema.int
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.int
from d42 import schema, fake
INT_MIN = -(2 ** 63)
INT_MAX = 2 ** 63 - 1
sch = schema.int
assert INT_MIN <= fake(sch) <= INT_MAX
from d42 import schema
sch = schema.int
assert sch == 42
assert sch != 3.14 # incorrect type
assert sch != "42" # incorrect type
from d42 import schema
sch = schema.int
assert sch % 42 == schema.int(42)
with assertRaises(SubstitutionError):
sch % "42" # incorrect type
schema.int(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.int(42)
from d42 import schema, fake
sch = schema.int(42)
assert fake(sch) == 42
from d42 import schema
sch = schema.int(42)
assert sch == 42
assert sch != 43 # incorrect value
from d42 import schema
sch = schema.int(42)
assert sch % 42 == schema.int(42)
with assertRaises(SubstitutionError):
sch % 43 # incorrect value
schema.int.min(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.int.min(0)
from d42 import schema, fake
INT_MAX = 2 ** 63 - 1
sch = schema.int.min(0)
assert 0 <= fake(sch) <= INT_MAX
from d42 import schema
sch = schema.int.min(0)
assert sch == 0
assert sch == 1
assert sch != -1 # < min
from d42 import schema
sch = schema.int.min(0)
assert sch % 0 == schema.int(0).min(0)
assert sch % 1 == schema.int(1).min(0)
with assertRaises(SubstitutionError):
sch % -1 # < min
schema.int.max(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.int.max(0)
from d42 import schema, fake
INT_MIN = -(2 ** 63)
sch = schema.int.max(0)
assert INT_MIN <= fake(sch) <= 0
from d42 import schema
sch = schema.int.max(0)
assert sch == 0
assert sch == -1
assert sch != 1 # > max
from d42 import schema
sch = schema.int.max(0)
assert sch % 0 == schema.int(0).max(0)
assert sch % -1 == schema.int(-1).max(0)
with assertRaises(SubstitutionError):
sch % 1 # > max
Float
schema.float
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.float
from d42 import schema, fake
sch = schema.float
assert isinstance(fake(sch), float)
from d42 import schema
sch = schema.float
assert sch == 3.14
assert sch != 3 # incorrect type
assert sch != "3.14" # incorrect type
from d42 import schema
sch = schema.float
assert sch % 3.14 == schema.float(3.14)
with assertRaises(SubstitutionError):
sch % 3 # incorrect type
schema.float(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.float(3.14)
from d42 import schema, fake
sch = schema.float(3.14)
assert fake(sch) == 3.14
from d42 import schema
sch = schema.float(3.14)
assert sch == 3.14
assert sch != 3.15 # incorrect value
from d42 import schema
sch = schema.float(3.14)
assert sch % 3.14 == schema.int(3.14)
with assertRaises(SubstitutionError):
sch % 3.15 # incorrect value
schema.float.min(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.float.min(0.0)
from d42 import schema, fake
sch = schema.float.min(0.0)
assert fake(sch) >= 0.0
from d42 import schema
sch = schema.float.min(0.0)
assert sch == 0.0
assert sch == 0.1
assert sch != -0.1 # < min
from d42 import schema
sch = schema.float.min(0.0)
assert sch % 0.0 == schema.float(0.0).min(0.0)
assert sch % 0.1 == schema.float(0.1).min(0.0)
with assertRaises(SubstitutionError):
sch % -0.1 # < min
schema.float.max(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.float.max(0.0)
from d42 import schema, fake
sch = schema.float.max(0.0)
assert fake(sch) <= 0.0
from d42 import schema
sch = schema.float.max(0.0)
assert sch == 0.0
assert sch == -0.1
assert sch != 0.1 # > max
from d42 import schema
sch = schema.float.max(0.0)
assert sch % 0.0 == schema.float(0.0).max(0.0)
assert sch % -0.1 == schema.float(-0.1).max(0.0)
with assertRaises(SubstitutionError):
sch % 0.1 # > max
Str
schema.str
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str
from d42 import schema, fake
sch = schema.str
assert isinstance(fake(sch), str)
from d42 import schema
sch = schema.str
assert sch == "banana"
assert sch != ["b", "a", "n", "a", "n", "a"] # incorrect type
from d42 import schema
sch = schema.str
assert sch % "banana" == schema.str("banana")
with assertRaises(SubstitutionError):
sch % ["b", "a", "n", "a", "n", "a"] # incorrect type
schema.str(value
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str("banana")
from d42 import schema, fake
sch = schema.str("banana")
assert fake(sch) == "banana"
from d42 import schema
sch = schema.str("banana")
assert sch == "banana"
assert sch != "cucumber" # incorrect value
from d42 import schema
sch = schema.str("banana")
assert sch % "banana" == schema.str("banana")
with assertRaises(SubstitutionError):
sch % "cucumber" # incorrect value
schema.str.len(length
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str.len(3)
from d42 import schema, fake
sch = schema.str.len(3)
assert len(fake(sch)) == 3
from d42 import schema
sch = schema.str.len(3)
assert sch == "abc"
assert sch != "ab" # incorrect length
assert sch != "abcd" # incorrect length
from d42 import schema
sch = schema.str.len(3)
assert sch % "abc" == schema.str("abc").len(3)
with assertRaises(SubstitutionError):
sch % "ab" # incorrect length
schema.str.len(min_length
, ...)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str.len(3, ...)
from d42 import schema, fake
sch = schema.str.len(3, ...)
assert len(fake(sch)) >= 3
from d42 import schema
sch = schema.str.len(3, ...)
assert sch == "abc"
assert sch == "abcd"
assert sch != "ab" # incorrect length
from d42 import schema
sch = schema.str.len(3, ...)
assert sch % "abc" == schema.str("abc").len(3, ...)
assert sch % "abcd" == schema.str("abcd").len(3, ...)
with assertRaises(SubstitutionError):
sch % "ab" # incorrect length
schema.str.len(..., max_length
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str.len(..., 3)
from d42 import schema, fake
sch = schema.str.len(..., 3)
assert len(fake(sch)) <= 3
from d42 import schema
sch = schema.str.len(..., 3)
assert sch == "abc"
assert sch == "ab"
assert sch != "abcd" # incorrect length
from d42 import schema
sch = schema.str.len(..., 3)
assert sch % "abc" == schema.str("abc").len(..., 3)
assert sch % "ab" == sschema.str("ab").len(..., 3)
with assertRaises(SubstitutionError):
sch % "abcd" # incorrect length
schema.str.len(min_length
, max_length
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str.len(2, 3)
from d42 import schema, fake
sch = schema.str.len(2, 3)
assert 2 <= len(fake(sch)) <= 3
from d42 import schema
sch = schema.str.len(2, 3)
assert sch == "ab"
assert sch == "abc"
assert sch != "a" # incorrect length
assert sch != "abcd" # incorrect length
from d42 import schema
sch = schema.str.len(2, 3)
assert sch % "ab" == schema.str("ab").len(2, 3)
assert sch % "abc" == schema.str("abc").len(2, 3)
with assertRaises(SubstitutionError):
sch % "a" # incorrect length
schema.str.alphabet(letters
)
- declare
- generate
- validate
- substitute
from d42 import schema
from string import ascii_letters
sch = schema.str.alphabet(ascii_letters)
from d42 import schema, fake
from string import ascii_letters
sch = schema.str.alphabet(ascii_letters)
assert all(x in ascii_letters for x in fake(sch))
from d42 import schema
from string import ascii_letters
sch = schema.str.alphabet(ascii_letters)
assert sch == "banana"
assert sch != "42" # incorrect alphabet
from d42 import schema
from string import ascii_letters
sch = schema.str.alphabet(ascii_letters)
assert sch % "banana" == schema.str("banana").alphabet(ascii_letters)
with assertRaises(SubstitutionError):
sch % "42" # incorrect alphabet
schema.str.contains(substr
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str.contains("banana")
from d42 import schema, fake
sch = schema.str.contains("banana")
assert "banana" in fake(sch)
from d42 import schema
sch = schema.str.contains("banana")
assert sch == "banana"
assert sch == "yellow banana"
assert sch != "" # must contain "banana"
from d42 import schema
sch = schema.str.contains("banana")
assert sch % "yellow banana" == schema.str("yellow banana").contains("banana")
with assertRaises(SubstitutionError):
sch % "" # must contain "banana"
schema.str.regex(pattern
)
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.str.regex(r"^[0-9]{4}$")
from d42 import schema, fake
import re
sch = schema.str.regex(r"^[0-9]{4}$")
assert re.match(r"^[0-9]{4}$", fake(sch))
from d42 import schema
sch = schema.str.regex(r"^[0-9]{4}$")
assert sch == "1234"
assert sch != "abc" # must match regex
from d42 import schema
ssch = schema.str.regex(r"^[0-9]{4}$")
assert sch % "1234" == schema.str("1234").regex("^[0-9]{4}$")
with assertRaises(SubstitutionError):
sch % "abc" # must match regex
Bytes
note
New in version 1.4
schema.bytes
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.bytes
from d42 import schema, fake
sch = schema.bytes
assert isinstance(fake(sch), bytes)
from d42 import schema
sch = schema.bytes
assert sch == b"banana"
assert sch != "banana" # incorrect type
from d42 import schema
sch = schema.bytes
assert sch % b"banana" == schema.bytes(b"banana")
with assertRaises(SubstitutionError):
sch % "banana" # incorrect type
Datetime
note
New in version 1.6
schema.datetime
- declare
- generate
- validate
- substitute
from d42 import schema
sch = schema.datetime
from datetime import datetime
from d42 import schema, fake
sch = schema.datetime
assert isinstance(fake(sch), datetime)
from datetime import datetime, time
from d42 import schema
sch = schema.datetime
assert sch == datetime.now()
assert sch != time() # incorrect type
from datetime import datetime, time
from d42 import schema
sch = schema.datetime
now = datetime.now()
assert sch % now == schema.datetime(now)
with assertRaises(SubstitutionError):
sch % time() # incorrect type
schema.datetime(value
)
- declare
- generate
- validate
- substitute
from datetime import datetime
from d42 import schema
now = datetime.now()
sch = schema.datetime(now)
from datetime import datetime
from d42 import schema, fake
now = datetime.now()
sch = schema.datetime(now)
assert fake(sch) == now
from datetime import datetime, timedelta
from d42 import schema, fake
now = datetime.now()
sch = schema.datetime(now)
assert sch == now
assert sch != now + timedelta(seconds=+1) # incorrect value
from datetime import datetime, timedelta
from d42 import schema
now = datetime.now()
sch = schema.datetime(now)
assert sch % now == schema.datetime(now)
with assertRaises(SubstitutionError):
sch % (now + timedelta(seconds=+1)) # incorrect value