Coverage for src/kwai/api/v1/schemas.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module for defining common JSON:API schemas.""" 

2 

3from typing import Self 

4 

5from pydantic import BaseModel 

6 

7from kwai.api.v1.resources import CountryResourceIdentifier 

8from kwai.core.json_api import Document, ResourceData 

9from kwai.modules.club.domain.country import CountryEntity 

10 

11 

12class CountryAttributes(BaseModel): 

13 """Attributes for the country JSON:API resource.""" 

14 

15 iso_2: str 

16 iso_3: str 

17 name: str 

18 

19 

20class CountryResource(CountryResourceIdentifier, ResourceData[CountryAttributes, None]): 

21 """A JSON:API resource for a country.""" 

22 

23 

24class CountryDocument(Document[CountryResource, None]): 

25 """A JSON:API document for one or more countries.""" 

26 

27 @classmethod 

28 def create(cls, country: CountryEntity) -> Self: 

29 """Create a country document from a country value object.""" 

30 country_resource = CountryResource( 

31 id=str(country.id), 

32 attributes=CountryAttributes( 

33 iso_2=country.iso_2, iso_3=country.iso_3, name=country.name 

34 ), 

35 ) 

36 return cls(data=country_resource)