Coverage for src/tests/core/test_json_api.py: 100%

57 statements  

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

1"""Module for testing the JSON:API models.""" 

2 

3from typing import Literal 

4 

5import pytest 

6 

7from kwai_core.json_api import ( 

8 Error, 

9 ResourceIdentifier, 

10) 

11from pydantic import BaseModel, Field 

12from rich import json 

13 

14 

15class JudokaResourceIdentifier(ResourceIdentifier): 

16 """A JSON:API resource identifier for a judoka.""" 

17 

18 type: Literal["judokas"] = "judokas" 

19 

20 

21def test_judoka_resource_identifier(): 

22 """Test a JSON:API resource identifier.""" 

23 id = JudokaResourceIdentifier(id="1") 

24 assert id.type == "judokas", "Type should be 'judokas'" 

25 

26 

27class JudokaAttributes(BaseModel): 

28 """Attributes for a JSON:API resource of a judoka.""" 

29 

30 name: str 

31 birth_date: str 

32 

33 

34class JudokaResource(JudokaResourceIdentifier): 

35 """A JSON:API resource for a judokas.""" 

36 

37 attributes: JudokaAttributes 

38 

39 

40@pytest.fixture 

41def judoka_resource() -> JudokaResource: 

42 """A fixture for a JSON:API resource of a judoka.""" 

43 return JudokaResource( 

44 id="1", attributes=JudokaAttributes(name="Jigoro Kano", birth_date="18601210") 

45 ) 

46 

47 

48def test_resource_attributes(judoka_resource: JudokaResource): 

49 """Test the attributes of a JSON:API resource.""" 

50 assert judoka_resource.id == "1", "The id of the judoka should be '1'." 

51 assert judoka_resource.attributes.name == "Jigoro Kano", ( 

52 "The judoka should have a name." 

53 ) 

54 

55 

56class JudokaDocument(BaseModel): 

57 """A JSON:API document for a judoka.""" 

58 

59 data: JudokaResource 

60 errors: list[Error] | None = None 

61 

62 

63class JudokasDocument(BaseModel): 

64 """A JSON:API document for multiple judokas.""" 

65 

66 data: list[JudokaResource] = Field(default_factory=list) 

67 errors: list[Error] | None = None 

68 

69 

70def test_dump_json(judoka_resource: JudokaResource): 

71 """Test if the serialized json structure is correct.""" 

72 document = JudokaDocument(data=judoka_resource) 

73 json_doc = json.loads(document.model_dump_json()) 

74 assert "data" in json_doc, "There should be a 'data' key" 

75 assert "id" in json_doc["data"], "There should be an 'id' in the document." 

76 assert json_doc["data"]["id"] == "1", "The id should be '1'." 

77 assert "type" in json_doc["data"], "There should be a 'type' in the document." 

78 assert json_doc["data"]["type"] == "judokas", "The type should be 'judoka'." 

79 assert "attributes" in json_doc["data"], ( 

80 "There should be a 'attributes' in the document." 

81 ) 

82 assert "name" in json_doc["data"]["attributes"], ( 

83 "There should be a 'name' in the attributes." 

84 ) 

85 assert json_doc["data"]["attributes"]["name"] == "Jigoro Kano", ( 

86 "The judoka should have a name." 

87 ) 

88 

89 

90def test_error(judoka_resource): 

91 """Test the error of a JSON:API document.""" 

92 json_doc = JudokaDocument( 

93 data=judoka_resource, 

94 errors=[ 

95 Error( 

96 title="No judoka selected", 

97 detail="There is no judoka selected for this tournament", 

98 ) 

99 ], 

100 ) 

101 json_doc = json.loads(json_doc.model_dump_json()) 

102 assert "errors" in json_doc, "There should be a 'errors' in the document." 

103 assert json_doc["errors"][0]["title"] == "No judoka selected" 

104 

105 

106def test_single_document(judoka_resource): 

107 """Test a single JSON:API document.""" 

108 json_doc = JudokaDocument(data=judoka_resource) 

109 json_doc = json.loads(json_doc.model_dump_json()) 

110 assert "data" in json_doc, "There should be a 'data' key" 

111 assert "id" in json_doc["data"], "There should be an 'id' in the document." 

112 assert json_doc["data"]["id"] == "1", "The id should be '1'." 

113 assert "type" in json_doc["data"], "There should be a 'type' in the document." 

114 assert json_doc["data"]["type"] == "judokas", "The type should be 'judoka'." 

115 assert "attributes" in json_doc["data"], ( 

116 "There should be a 'attributes' in the document." 

117 ) 

118 assert "name" in json_doc["data"]["attributes"], ( 

119 "There should be a 'name' in the attributes." 

120 ) 

121 assert json_doc["data"]["attributes"]["name"] == "Jigoro Kano", ( 

122 "The judoka should have a name." 

123 ) 

124 

125 

126def test_multiple_document(judoka_resource): 

127 """Test a JSON:API document with multiple resources.""" 

128 json_doc = JudokasDocument(data=[judoka_resource]) 

129 json_doc = json.loads(json_doc.model_dump_json()) 

130 assert "data" in json_doc, "There should be a 'data' key" 

131 assert isinstance(json_doc["data"], list), "The 'data' property should be a list" 

132 assert "id" in json_doc["data"][0], "There should be an 'id' in the document." 

133 assert json_doc["data"][0]["id"] == "1", "The id should be '1'."