From fa808da3418a37c52ed2b8c0c10ec5092d79189b Mon Sep 17 00:00:00 2001 From: ktro2828 Date: Thu, 28 Nov 2024 01:25:29 +0900 Subject: [PATCH] feat: add classmethod called new to generate a schema instance with a new token Signed-off-by: ktro2828 --- t4_devkit/schema/tables/base.py | 19 +++++++++++++++++++ tests/schema/tables/test_attribute_table.py | 8 ++++++++ .../tables/test_calibrated_sensor_table.py | 8 ++++++++ tests/schema/tables/test_category_table.py | 8 ++++++++ tests/schema/tables/test_ego_pose_table.py | 8 ++++++++ tests/schema/tables/test_instance_table.py | 8 ++++++++ tests/schema/tables/test_log_table.py | 8 ++++++++ tests/schema/tables/test_map_table.py | 8 ++++++++ tests/schema/tables/test_object_ann_table.py | 8 ++++++++ .../tables/test_sample_annotation_table.py | 8 ++++++++ tests/schema/tables/test_sample_data_table.py | 8 ++++++++ tests/schema/tables/test_sample_table.py | 8 ++++++++ tests/schema/tables/test_sensor_table.py | 8 ++++++++ tests/schema/tables/test_surface_ann_table.py | 8 ++++++++ .../schema/tables/test_vehicle_state_table.py | 11 +++++++++-- tests/schema/tables/test_visibility_table.py | 8 ++++++++ 16 files changed, 140 insertions(+), 2 deletions(-) diff --git a/t4_devkit/schema/tables/base.py b/t4_devkit/schema/tables/base.py index 013049a..3bf8c17 100644 --- a/t4_devkit/schema/tables/base.py +++ b/t4_devkit/schema/tables/base.py @@ -1,6 +1,7 @@ from __future__ import annotations from abc import ABC +from secrets import token_hex from typing import Any, TypeVar from attrs import define @@ -50,5 +51,23 @@ def from_dict(cls, data: dict[str, Any]) -> SchemaTable: """ return cls(**data) + @classmethod + def new(cls, data: dict[str, Any], *, token_nbytes: int = 16) -> SchemaTable: + """Create a new schema instance generating random token. + + Args: + data (dict[str, Any]): Schema field data without token. + token_nbytes (int, optional): The number of bytes of a new token. + + Returns: + Schema instance with a new token. + """ + new_data = data.copy() + + token = token_hex(nbytes=token_nbytes) + new_data["token"] = token + + return cls.from_dict(new_data) + SchemaTable = TypeVar("SchemaTable", bound=SchemaBase) diff --git a/tests/schema/tables/test_attribute_table.py b/tests/schema/tables/test_attribute_table.py index 22816de..795380e 100644 --- a/tests/schema/tables/test_attribute_table.py +++ b/tests/schema/tables/test_attribute_table.py @@ -9,3 +9,11 @@ def test_attribute_json(attribute_json) -> None: def test_attribute(attribute_dict) -> None: """Test loading attribute from a dictionary.""" _ = Attribute.from_dict(attribute_dict) + + +def test_new_attribute(attribute_dict) -> None: + """Test generating attribute with a new token.""" + without_token = {k: v for k, v in attribute_dict.items() if k != "token"} + ret = Attribute.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != attribute_dict["token"] diff --git a/tests/schema/tables/test_calibrated_sensor_table.py b/tests/schema/tables/test_calibrated_sensor_table.py index 5837fb8..ec14a88 100644 --- a/tests/schema/tables/test_calibrated_sensor_table.py +++ b/tests/schema/tables/test_calibrated_sensor_table.py @@ -9,3 +9,11 @@ def test_calibrated_sensor_json(calibrated_sensor_json) -> None: def test_calibrated_sensor(calibrated_sensor_dict) -> None: """Test loading calibrated sensor from a dictionary.""" _ = CalibratedSensor.from_dict(calibrated_sensor_dict) + + +def test_new_calibrated_sensor(calibrated_sensor_dict) -> None: + """Test generating calibrated sensor with a new token.""" + without_token = {k: v for k, v in calibrated_sensor_dict.items() if k != "token"} + ret = CalibratedSensor.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != calibrated_sensor_dict["token"] diff --git a/tests/schema/tables/test_category_table.py b/tests/schema/tables/test_category_table.py index a75c7e1..20d4886 100644 --- a/tests/schema/tables/test_category_table.py +++ b/tests/schema/tables/test_category_table.py @@ -9,3 +9,11 @@ def test_category_json(category_json) -> None: def test_category(category_dict) -> None: """Test loading sample data from a dictionary.""" _ = Category.from_dict(category_dict) + + +def test_new_category(category_dict) -> None: + """Test generating category with a new token.""" + without_token = {k: v for k, v in category_dict.items() if k != "token"} + ret = Category.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != category_dict["token"] diff --git a/tests/schema/tables/test_ego_pose_table.py b/tests/schema/tables/test_ego_pose_table.py index ab1d510..8af38fe 100644 --- a/tests/schema/tables/test_ego_pose_table.py +++ b/tests/schema/tables/test_ego_pose_table.py @@ -9,3 +9,11 @@ def test_ego_pose_json(ego_pose_json) -> None: def test_ego_pose(ego_pose_dict) -> None: """Test loading ego pose from a dictionary.""" _ = EgoPose.from_dict(ego_pose_dict) + + +def test_new_ego_pose(ego_pose_dict) -> None: + """Test generating ego pose with a new token.""" + without_token = {k: v for k, v in ego_pose_dict.items() if k != "token"} + ret = EgoPose.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != ego_pose_dict["token"] diff --git a/tests/schema/tables/test_instance_table.py b/tests/schema/tables/test_instance_table.py index 202c56d..46fa9bf 100644 --- a/tests/schema/tables/test_instance_table.py +++ b/tests/schema/tables/test_instance_table.py @@ -9,3 +9,11 @@ def test_instance_json(instance_json) -> None: def test_instance(instance_dict) -> None: """Test loading instance from a dictionary.""" _ = Instance.from_dict(instance_dict) + + +def test_new_instance(instance_dict) -> None: + """Test generating instance with a new token.""" + without_token = {k: v for k, v in instance_dict.items() if k != "token"} + ret = Instance.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != instance_dict["token"] diff --git a/tests/schema/tables/test_log_table.py b/tests/schema/tables/test_log_table.py index 98351cc..399daa9 100644 --- a/tests/schema/tables/test_log_table.py +++ b/tests/schema/tables/test_log_table.py @@ -9,3 +9,11 @@ def test_log_json(log_json) -> None: def test_log(log_dict) -> None: """Test loading log from a dictionary.""" _ = Log.from_dict(log_dict) + + +def test_new_log(log_dict) -> None: + """Test generating log with a new token.""" + without_token = {k: v for k, v in log_dict.items() if k != "token"} + ret = Log.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != log_dict["token"] diff --git a/tests/schema/tables/test_map_table.py b/tests/schema/tables/test_map_table.py index 1900a53..6dcffe9 100644 --- a/tests/schema/tables/test_map_table.py +++ b/tests/schema/tables/test_map_table.py @@ -9,3 +9,11 @@ def test_map_json(map_json) -> None: def test_map(map_dict) -> None: """Test loading map from a dictionary.""" _ = Map.from_dict(map_dict) + + +def test_new_map(map_dict) -> None: + """Test generating map with a new token.""" + without_token = {k: v for k, v in map_dict.items() if k != "token"} + ret = Map.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != map_dict["token"] diff --git a/tests/schema/tables/test_object_ann_table.py b/tests/schema/tables/test_object_ann_table.py index 2965f35..8391d2c 100644 --- a/tests/schema/tables/test_object_ann_table.py +++ b/tests/schema/tables/test_object_ann_table.py @@ -9,3 +9,11 @@ def test_object_ann_json(object_ann_json) -> None: def test_object_ann(object_ann_dict) -> None: """Test loading object ann from a dictionary.""" _ = ObjectAnn.from_dict(object_ann_dict) + + +def test_new_object_ann(object_ann_dict) -> None: + """Test generating object ann with a new token.""" + without_token = {k: v for k, v in object_ann_dict.items() if k != "token"} + ret = ObjectAnn.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != object_ann_dict["token"] diff --git a/tests/schema/tables/test_sample_annotation_table.py b/tests/schema/tables/test_sample_annotation_table.py index 49ce3cb..6a2c77e 100644 --- a/tests/schema/tables/test_sample_annotation_table.py +++ b/tests/schema/tables/test_sample_annotation_table.py @@ -9,3 +9,11 @@ def test_sample_annotation_json(sample_annotation_json) -> None: def test_sample_annotation(sample_annotation_dict) -> None: """Test loading sample annotation from a dictionary.""" _ = SampleAnnotation.from_dict(sample_annotation_dict) + + +def test_new_sample_annotation(sample_annotation_dict) -> None: + """Test generating sample annotation with a new token.""" + without_token = {k: v for k, v in sample_annotation_dict.items() if k != "token"} + ret = SampleAnnotation.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != sample_annotation_dict["token"] diff --git a/tests/schema/tables/test_sample_data_table.py b/tests/schema/tables/test_sample_data_table.py index b213506..8f4a648 100644 --- a/tests/schema/tables/test_sample_data_table.py +++ b/tests/schema/tables/test_sample_data_table.py @@ -32,3 +32,11 @@ def test_sample_data_json(sample_data_json) -> None: def test_sample_data(sample_data_dict) -> None: """Test loading sample data from a dictionary.""" _ = SampleData.from_dict(sample_data_dict) + + +def test_new_sample_data(sample_data_dict) -> None: + """Test generating sample data with a new token.""" + without_token = {k: v for k, v in sample_data_dict.items() if k != "token"} + ret = SampleData.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != sample_data_dict["token"] diff --git a/tests/schema/tables/test_sample_table.py b/tests/schema/tables/test_sample_table.py index 2db4675..f1f4ed2 100644 --- a/tests/schema/tables/test_sample_table.py +++ b/tests/schema/tables/test_sample_table.py @@ -9,3 +9,11 @@ def test_sample_json(sample_json) -> None: def test_sample(sample_dict) -> None: """Test loading sample from a dictionary.""" _ = Sample.from_dict(sample_dict) + + +def test_new_sample(sample_dict) -> None: + """Test generating sample with a new token.""" + without_token = {k: v for k, v in sample_dict.items() if k != "token"} + ret = Sample.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != sample_dict["token"] diff --git a/tests/schema/tables/test_sensor_table.py b/tests/schema/tables/test_sensor_table.py index 0b6674d..607085e 100644 --- a/tests/schema/tables/test_sensor_table.py +++ b/tests/schema/tables/test_sensor_table.py @@ -23,3 +23,11 @@ def test_sensor_json(sensor_json) -> None: def test_sensor(sensor_dict) -> None: """Test loading sensor from a dictionary.""" _ = Sensor.from_dict(sensor_dict) + + +def test_new_sensor(sensor_dict) -> None: + """Test generating sensor with a new token.""" + without_token = {k: v for k, v in sensor_dict.items() if k != "token"} + ret = Sensor.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != sensor_dict["token"] diff --git a/tests/schema/tables/test_surface_ann_table.py b/tests/schema/tables/test_surface_ann_table.py index 62ddcde..1b8913c 100644 --- a/tests/schema/tables/test_surface_ann_table.py +++ b/tests/schema/tables/test_surface_ann_table.py @@ -9,3 +9,11 @@ def test_surface_ann_json(surface_ann_json) -> None: def test_surface_ann(surface_ann_dict) -> None: """Test loading surface ann from a dictionary.""" _ = SurfaceAnn.from_dict(surface_ann_dict) + + +def test_new_surface_ann(surface_ann_dict) -> None: + """Test generating surface ann with a new token.""" + without_token = {k: v for k, v in surface_ann_dict.items() if k != "token"} + ret = SurfaceAnn.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != surface_ann_dict["token"] diff --git a/tests/schema/tables/test_vehicle_state_table.py b/tests/schema/tables/test_vehicle_state_table.py index 3a60a6a..2ce0ca2 100644 --- a/tests/schema/tables/test_vehicle_state_table.py +++ b/tests/schema/tables/test_vehicle_state_table.py @@ -8,5 +8,12 @@ def test_vehicle_state_json(vehicle_state_json) -> None: def test_vehicle_state(vehicle_state_dict) -> None: """Test loading vehicle state from a dictionary.""" - s = VehicleState.from_dict(vehicle_state_dict) - print(s) + _ = VehicleState.from_dict(vehicle_state_dict) + + +def test_new_vehicle_state(vehicle_state_dict) -> None: + """Test generating vehicle state with a new token.""" + without_token = {k: v for k, v in vehicle_state_dict.items() if k != "token"} + ret = VehicleState.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != vehicle_state_dict["token"] diff --git a/tests/schema/tables/test_visibility_table.py b/tests/schema/tables/test_visibility_table.py index 21aa138..a5a0d96 100644 --- a/tests/schema/tables/test_visibility_table.py +++ b/tests/schema/tables/test_visibility_table.py @@ -34,3 +34,11 @@ def test_visibility_json(visibility_json) -> None: def test_visibility(visibility_dict) -> None: """Test loading visibility from a dictionary.""" _ = Visibility.from_dict(visibility_dict) + + +def test_new_visibility(visibility_dict) -> None: + """Test generating visibility with a new token.""" + without_token = {k: v for k, v in visibility_dict.items() if k != "token"} + ret = Visibility.new(without_token) + # check the new token is not the same with the token in input data + assert ret.token != visibility_dict["token"]