def test_repr(self): datetime_repr, to_datetime_repr = t.DateTime(), t.ToDateTime() assert repr(datetime_repr) == '<DateTime %Y-%m-%d %H:%M:%S>' assert repr(to_datetime_repr) == '<ToDateTime %Y-%m-%d %H:%M:%S>' datetime_repr, to_datetime_repr = t.DateTime('%Y-%m-%d %H:%M'), t.ToDateTime('%Y-%m-%d %H:%M') assert repr(datetime_repr) == '<DateTime %Y-%m-%d %H:%M>' assert repr(to_datetime_repr) == '<ToDateTime %Y-%m-%d %H:%M>'
def test_to_datetime(self): res = t.ToDateTime('%Y-%m-%d %H:%M').check("2019-07-25 21:45") assert res == datetime(year=2019, month=7, day=25, hour=21, minute=45)
import trafaret as t from citizens_dwh_api.constants import DATE_FORMAT def _build_trafaret(schema, optional=False): return t.Dict( {t.Key(key, optional=optional): item for key, item in schema.items()}) OptionalCitizenSchema = { "town": t.String(min_length=1), "street": t.String(min_length=1), "building": t.String(min_length=1), "apartment": t.ToInt(gte=0), "name": t.String(min_length=1), "birth_date": t.ToDateTime( format=DATE_FORMAT # datetime, because pymongo cannot insert date ), "gender": t.Enum("male", "female"), "relatives": t.List(t.ToInt(gte=0)), } CitizenSchema = {**OptionalCitizenSchema, **{"citizen_id": t.ToInt(gte=0)}} OptionalCitizen = _build_trafaret(OptionalCitizenSchema, optional=True) Citizen = _build_trafaret(CitizenSchema, optional=False)
import json from dataclasses import dataclass from json import JSONDecodeError from typing import Optional, Any import trafaret as t from loguru import logger from core.models import Response REQUEST_SCHEMA = t.Dict( { t.Key("url"): t.URL, t.Key("load_time"): t.ToFloat, t.Key("request_time"): t.ToDateTime("%Y-%m-%dT%H:%M:%S.%f%z"), t.Key("status_code"): t.ToInt, t.Key("error"): t.String | t.Null, } ).ignore_extra("*") @dataclass class KafkaDeserializer: """ Kafka deserializer """ document_schema: t.Trafaret def __call__(