Coverage for tests/test_schema.py: 100%

79 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-26 09:12 +0100

1from __future__ import annotations 

2 

3import json 

4from datetime import datetime 

5from datetime import timezone 

6from pathlib import Path 

7 

8from harborapi.ext import ArtifactInfo 

9from harborapi.models.models import Artifact 

10from harborapi.models.models import ScheduleObj 

11 

12from harbor_cli.output.schema import Schema 

13 

14 

15def test_schema_init() -> None: 

16 """Test deserialization of Schema.""" 

17 schedule = ScheduleObj( 

18 # these are likely mutually exclusive in practice, but 

19 # just test them all in one go 

20 type="Hourly", 

21 cron="0 0 * * * *", 

22 next_scheduled_time="2021-08-31T14:00:00Z", 

23 ) 

24 

25 # Using __init__ 

26 schema = Schema(data=schedule) # type: Schema[ScheduleObj] 

27 assert schema.data == schedule 

28 assert isinstance(schema.data, ScheduleObj) 

29 assert isinstance(schema.data.next_scheduled_time, datetime) 

30 

31 # Using from_data classmethod (preferred) 

32 schema2 = Schema.from_data(schedule) 

33 assert schema2 == schema 

34 

35 

36def test_schema_serialization() -> None: 

37 """Test serialization of Schema.""" 

38 schedule = ScheduleObj( 

39 type="Hourly", 

40 cron="0 0 * * * *", 

41 next_scheduled_time="2021-08-31T14:00:00+00:00", 

42 ) 

43 

44 schema = Schema.from_data(schedule) 

45 # Serialize to JSON, so we can test the string representation 

46 # of the different values. If we just use .dict(), we still 

47 # have to deal with Python objects (e.g. datetime) 

48 schema_dict = json.loads(schema.json()) 

49 assert schema_dict["data"]["type"] == "Hourly" 

50 assert schema_dict["data"]["cron"] == "0 0 * * * *" 

51 assert schema_dict["data"]["next_scheduled_time"] == "2021-08-31T14:00:00+00:00" 

52 assert schema_dict["version"] == "1.0.0" 

53 assert schema_dict["type"] == "ScheduleObj" 

54 assert schema_dict["module"] == "harborapi.models.models" 

55 

56 

57def test_schema_deserialization() -> None: 

58 """Test deserialization of Schema.""" 

59 schedule = ScheduleObj( 

60 type="Hourly", 

61 cron="0 0 * * * *", 

62 next_scheduled_time="2021-08-31T14:00:00+00:00", 

63 ) 

64 schema = Schema.from_data(schedule) 

65 # Serialize to JSON and then deserialize back to Schema 

66 schema_dict = json.loads(schema.json()) 

67 schema2 = Schema(**schema_dict) 

68 assert schema2 == schema 

69 assert schema2.data == schedule 

70 assert isinstance(schema2.data, ScheduleObj) 

71 assert isinstance(schema2.data.next_scheduled_time, datetime) 

72 

73 

74def test_schema_from_file() -> None: 

75 p = Path(__file__).parent / "data/schema_artifact.json" 

76 schema = Schema.from_file(p) 

77 assert isinstance(schema.data, Artifact) 

78 d = schema.data 

79 assert d.id == 1234 

80 assert d.project_id == 123 

81 assert d.repository_id == 456 

82 assert d.digest == "sha256:1234567890abcdef" 

83 assert d.size == 1234567890 

84 assert d.extra_attrs.architecture == "amd64" 

85 assert d.extra_attrs.config["ExposedPorts"] == {"8080/tcp": {}} 

86 

87 d_dict = json.loads(p.read_text()) 

88 assert schema.data == Artifact(**d_dict["data"]) 

89 

90 

91def test_schema_from_file_ext() -> None: 

92 p = Path(__file__).parent / "data/schema_artifactinfo.json" 

93 schema = Schema.from_file(p) 

94 assert isinstance(schema.data, ArtifactInfo) 

95 

96 d = schema.data 

97 assert d.artifact.id == 1234 

98 assert d.artifact.project_id == 123 

99 assert d.artifact.repository_id == 456 

100 assert d.artifact.digest == "sha256:1234567890abcdef" 

101 assert d.artifact.size == 1234567890 

102 assert d.artifact.extra_attrs.architecture == "amd64" 

103 assert d.artifact.extra_attrs.config["ExposedPorts"] == {"8080/tcp": {}} 

104 

105 assert d.repository.project_id == 123 

106 assert d.repository.id == 456 

107 assert d.repository.name == "test-project/test-repo" 

108 assert d.repository.description is None 

109 assert d.repository.artifact_count == 100 

110 assert d.repository.pull_count == 999 

111 assert d.repository.creation_time == datetime( 

112 2022, 1, 1, 1, 2, 3, tzinfo=timezone.utc 

113 ) 

114 assert d.repository.update_time == datetime( 

115 2022, 2, 2, 1, 2, 3, tzinfo=timezone.utc 

116 ) 

117 

118 d_dict = json.loads(p.read_text()) 

119 assert schema.data == ArtifactInfo(**d_dict["data"]) 

120 

121 

122def test_schema_non_basemodel() -> None: 

123 """Test Schema with non-BaseModel data.""" 

124 s = Schema.from_data("hello, world") 

125 assert s.data == "hello, world" 

126 assert s.type == "str" 

127 assert s.module == "builtins" 

128 

129 s2 = Schema( 

130 data="hello, world", 

131 ) # type: Schema[str] 

132 assert s == s2