1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for `rfc3339_validator` package."""
import re
import pytest
from rfc3339_validator import validate_rfc3339
import strict_rfc3339
from hypothesis import given, settings, example
import hypothesis.strategies as st
# It is supposed to be used to generate both valid and invalid dates
RFC3339_REGEX = r"""
^
(\d\d\d\d) # Year
-
(\d\d) # Month
-
(\d\d) # Day
[T ]
(\d\d) # Hours
:
(?:\d\d) # Minutes
:
(?:\d\d) # Seconds
(?:\.\d+)? # Secfrac
( [Zz]
|(?:[+\-])(\d\d):(?:\d\d)
)
$
"""
RFC3339_REGEX_ASCII = re.compile(RFC3339_REGEX, re.X | re.A)
RFC3339_REGEX_UNICODE = re.compile(RFC3339_REGEX, re.X)
@given(datetime_str=st.datetimes().filter(lambda d: d.year > 1000).map(lambda d: d.strftime("%Y-%m-%dT%H:%M:%SZ")))
def test_valid_dates(datetime_str):
assert validate_rfc3339(datetime_str)
@settings(max_examples=1500)
@given(datetime_str=st.from_regex(RFC3339_REGEX_ASCII, fullmatch=True))
@example(datetime_str='')
def test_against_legacy(datetime_str):
legacy_result = strict_rfc3339.validate_rfc3339(datetime_str)
new_result = validate_rfc3339(datetime_str)
assert legacy_result == new_result
@settings(max_examples=1)
@given(datetime_str=st.from_regex(RFC3339_REGEX_UNICODE, fullmatch=True))
@example(datetime_str='0001-01-01T00:00:0٠Z')
def test_with_unicode(datetime_str):
assert not validate_rfc3339(datetime_str)
|