From 0ea6750891a1e4e59f64f2ce28d85b09f9b69816 Mon Sep 17 00:00:00 2001 From: TheBurchLog <5104941+TheBurchLog@users.noreply.github.com> Date: Wed, 10 Jul 2024 15:19:06 +0000 Subject: [PATCH] code cleanup --- brewtils/models.py | 3 +- brewtils/schema_parser.py | 13 +++--- brewtils/test/comparable.py | 12 +++--- brewtils/test/fixtures.py | 27 +++++++----- test/schema_parser_test.py | 83 +++++++++++++++++++++++++++++++++++-- 5 files changed, 112 insertions(+), 26 deletions(-) diff --git a/brewtils/models.py b/brewtils/models.py index 2107847c..7a74afd4 100644 --- a/brewtils/models.py +++ b/brewtils/models.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- +from datetime import datetime from enum import Enum import pytz # noqa # not in requirements file import six # noqa # not in requirements file -from datetime import datetime from brewtils.errors import ModelError, _deprecate @@ -465,6 +465,7 @@ def __repr__(self): self.history, ) + class RequestFile(BaseModel): schema = "RequestFileSchema" diff --git a/brewtils/schema_parser.py b/brewtils/schema_parser.py index 3041e94e..f023e97c 100644 --- a/brewtils/schema_parser.py +++ b/brewtils/schema_parser.py @@ -438,7 +438,7 @@ def parse_topic(cls, topic, from_string=False, **kwargs): return cls.parse( topic, brewtils.models.Topic, from_string=from_string, **kwargs ) - + @classmethod def parse_status_info(cls, status_info, from_string=False, **kwargs): """Convert raw JSON string or dictionary to a status info model object @@ -454,7 +454,7 @@ def parse_status_info(cls, status_info, from_string=False, **kwargs): return cls.parse( status_info, brewtils.models.StatusInfo, from_string=from_string, **kwargs ) - + @classmethod def parse_status_history(cls, status_history, from_string=False, **kwargs): """Convert raw JSON string or dictionary to a status history model object @@ -468,7 +468,10 @@ def parse_status_history(cls, status_history, from_string=False, **kwargs): A StatusHistory object """ return cls.parse( - status_history, brewtils.models.StatusHistory, from_string=from_string, **kwargs + status_history, + brewtils.models.StatusHistory, + from_string=from_string, + **kwargs ) @classmethod @@ -976,7 +979,7 @@ def serialize_topic(cls, topic, to_string=True, **kwargs): schema_name=brewtils.models.Topic.schema, **kwargs ) - + @classmethod def serialize_status_info(cls, status_info, to_string=True, **kwargs): """Convert a status info model into serialized form @@ -996,7 +999,7 @@ def serialize_status_info(cls, status_info, to_string=True, **kwargs): schema_name=brewtils.models.StatusInfo.schema, **kwargs ) - + @classmethod def serialize_status_history(cls, status_history, to_string=True, **kwargs): """Convert a status history model into serialized form diff --git a/brewtils/test/comparable.py b/brewtils/test/comparable.py index 07dbd19c..dd22e7b3 100644 --- a/brewtils/test/comparable.py +++ b/brewtils/test/comparable.py @@ -36,11 +36,11 @@ RequestTemplate, Resolvable, Runner, - System, + StatusHistory, + StatusInfo, Subscriber, + System, Topic, - StatusHistory, - StatusInfo ) __all__ = [ @@ -202,6 +202,7 @@ def _assert_wrapper(obj1, obj2, expected_type=None, do_raise=False, **kwargs): assert_subscriber_equal = partial(_assert_wrapper, expected_type=Subscriber) assert_status_history_equal = partial(_assert_wrapper, expected_type=StatusHistory) + def assert_status_info_equal(obj1, obj2, do_raise=False): return _assert_wrapper( obj1, @@ -213,6 +214,7 @@ def assert_status_info_equal(obj1, obj2, do_raise=False): do_raise=do_raise, ) + def assert_instance_equal(obj1, obj2, do_raise=False): return _assert_wrapper( obj1, @@ -222,6 +224,7 @@ def assert_instance_equal(obj1, obj2, do_raise=False): do_raise=do_raise, ) + def assert_connection_equal(obj1, obj2, do_raise=False): return _assert_wrapper( obj1, @@ -438,6 +441,3 @@ def assert_topic_equal(obj1, obj2, do_raise=False): }, do_raise=do_raise, ) - - - diff --git a/brewtils/test/fixtures.py b/brewtils/test/fixtures.py index ac0cc861..7946b193 100644 --- a/brewtils/test/fixtures.py +++ b/brewtils/test/fixtures.py @@ -29,11 +29,11 @@ RequestTemplate, Resolvable, Runner, + StatusHistory, + StatusInfo, Subscriber, System, Topic, - StatusHistory, - StatusInfo, ) @@ -798,12 +798,13 @@ def bg_connection(connection_dict, bg_status_info): @pytest.fixture -def bg_connection_publishing(connection_publishing_dict): +def bg_connection_publishing(connection_publishing_dict, bg_status_info): """An connection as a model.""" - dict_copy = copy.deepcopy(connection_publishing_dict, bg_status_info) + dict_copy = copy.deepcopy(connection_publishing_dict) dict_copy["status_info"] = bg_status_info return Connection(**dict_copy) + @pytest.fixture def status_history_dict(ts_epoch): """A status history as a dictionary""" @@ -813,6 +814,7 @@ def status_history_dict(ts_epoch): "heartbeat": ts_epoch, } + @pytest.fixture def bg_status_history(status_history_dict, ts_dt): dict_copy = copy.deepcopy(status_history_dict) @@ -824,10 +826,8 @@ def bg_status_history(status_history_dict, ts_dt): def status_info_dict(ts_epoch, status_history_dict): """A status info as a dictionary""" - return { - "heartbeat": ts_epoch, - "history": [status_history_dict] - } + return {"heartbeat": ts_epoch, "history": [status_history_dict]} + @pytest.fixture def bg_status_info(status_info_dict, ts_dt, bg_status_history): @@ -836,8 +836,11 @@ def bg_status_info(status_info_dict, ts_dt, bg_status_history): dict_copy["heartbeat"] = ts_dt return StatusInfo(**dict_copy) + @pytest.fixture -def garden_dict(ts_epoch, system_dict, connection_dict, connection_publishing_dict, status_info_dict): +def garden_dict( + ts_epoch, system_dict, connection_dict, connection_publishing_dict, status_info_dict +): """A garden as a dictionary.""" return { @@ -858,13 +861,15 @@ def garden_dict(ts_epoch, system_dict, connection_dict, connection_publishing_di @pytest.fixture -def bg_garden(garden_dict, bg_system, bg_connection, bg_connection_publishing, bg_status_info): +def bg_garden( + garden_dict, bg_system, bg_connection, bg_connection_publishing, bg_status_info +): """An operation as a model.""" dict_copy = copy.deepcopy(garden_dict) dict_copy["systems"] = [bg_system] dict_copy["receiving_connections"] = [bg_connection] dict_copy["publishing_connections"] = [bg_connection_publishing] - dict_copy["status_info"] = [bg_status_info] + dict_copy["status_info"] = bg_status_info return Garden(**dict_copy) diff --git a/test/schema_parser_test.py b/test/schema_parser_test.py index 9246dba2..da2205d5 100644 --- a/test/schema_parser_test.py +++ b/test/schema_parser_test.py @@ -5,8 +5,11 @@ import copy -import brewtils.models import pytest +from marshmallow.exceptions import MarshmallowError +from pytest_lazyfixture import lazy_fixture + +import brewtils.models from brewtils.models import System from brewtils.schema_parser import SchemaParser from brewtils.test.comparable import ( @@ -28,12 +31,12 @@ assert_resolvable_equal, assert_role_equal, assert_runner_equal, + assert_status_history_equal, + assert_status_info_equal, assert_subscriber_equal, assert_system_equal, assert_topic_equal, ) -from marshmallow.exceptions import MarshmallowError -from pytest_lazyfixture import lazy_fixture class TestParse(object): @@ -194,6 +197,18 @@ def test_no_modify(self, system_dict): assert_topic_equal, lazy_fixture("bg_topic"), ), + ( + brewtils.models.StatusInfo, + lazy_fixture("status_info_dict"), + assert_status_info_equal, + lazy_fixture("bg_status_info"), + ), + ( + brewtils.models.StatusHistory, + lazy_fixture("status_history_dict"), + assert_status_history_equal, + lazy_fixture("bg_status_history"), + ), ], ) def test_single(self, model, data, assertion, expected): @@ -340,6 +355,18 @@ def test_single_from_string(self): assert_topic_equal, lazy_fixture("bg_topic"), ), + ( + "parse_status_info", + lazy_fixture("status_info_dict"), + assert_status_info_equal, + lazy_fixture("bg_status_info"), + ), + ( + "parse_status_history", + lazy_fixture("status_history_dict"), + assert_status_history_equal, + lazy_fixture("bg_status_history"), + ), ], ) def test_single_specific(self, method, data, assertion, expected): @@ -479,6 +506,18 @@ def test_single_specific_from_string(self): assert_topic_equal, lazy_fixture("bg_topic"), ), + ( + brewtils.models.StatusInfo, + lazy_fixture("status_info_dict"), + assert_status_info_equal, + lazy_fixture("bg_status_info"), + ), + ( + brewtils.models.StatusHistory, + lazy_fixture("status_history_dict"), + assert_status_history_equal, + lazy_fixture("bg_status_history"), + ), ], ) def test_many(self, model, data, assertion, expected): @@ -611,6 +650,18 @@ def test_many(self, model, data, assertion, expected): assert_topic_equal, lazy_fixture("bg_topic"), ), + ( + "parse_status_info", + lazy_fixture("status_info_dict"), + assert_status_info_equal, + lazy_fixture("bg_status_info"), + ), + ( + "parse_status_history", + lazy_fixture("status_history_dict"), + assert_status_history_equal, + lazy_fixture("bg_status_history"), + ), ], ) def test_many_specific(self, method, data, assertion, expected): @@ -666,6 +717,8 @@ class TestSerialize(object): (lazy_fixture("bg_resolvable"), lazy_fixture("resolvable_dict")), (lazy_fixture("bg_subscriber"), lazy_fixture("subscriber_dict")), (lazy_fixture("bg_topic"), lazy_fixture("topic_dict")), + (lazy_fixture("bg_status_info"), lazy_fixture("status_info_dict")), + (lazy_fixture("bg_status_history"), lazy_fixture("status_history_dict")), ], ) def test_single(self, model, expected): @@ -777,6 +830,16 @@ def test_single(self, model, expected): lazy_fixture("bg_topic"), lazy_fixture("topic_dict"), ), + ( + "serialize_status_info", + lazy_fixture("bg_status_info"), + lazy_fixture("status_info_dict"), + ), + ( + "serialize_status_history", + lazy_fixture("bg_status_history"), + lazy_fixture("status_history_dict"), + ), ], ) def test_single_specific(self, method, data, expected): @@ -807,6 +870,8 @@ def test_single_specific(self, method, data, expected): (lazy_fixture("bg_resolvable"), lazy_fixture("resolvable_dict")), (lazy_fixture("bg_subscriber"), lazy_fixture("subscriber_dict")), (lazy_fixture("bg_topic"), lazy_fixture("topic_dict")), + (lazy_fixture("bg_status_info"), lazy_fixture("status_info_dict")), + (lazy_fixture("bg_status_history"), lazy_fixture("status_history_dict")), ], ) def test_many(self, model, expected): @@ -901,6 +966,16 @@ class TestRoundTrip(object): lazy_fixture("bg_subscriber"), ), (brewtils.models.Topic, assert_topic_equal, lazy_fixture("bg_topic")), + ( + brewtils.models.StatusInfo, + assert_status_info_equal, + lazy_fixture("bg_status_info"), + ), + ( + brewtils.models.StatusHistory, + assert_status_history_equal, + lazy_fixture("bg_status_history"), + ), ], ) def test_parsed_start(self, model, assertion, data): @@ -932,6 +1007,8 @@ def test_parsed_start(self, model, assertion, data): (brewtils.models.Operation, lazy_fixture("operation_dict")), (brewtils.models.Runner, lazy_fixture("runner_dict")), (brewtils.models.Resolvable, lazy_fixture("resolvable_dict")), + (brewtils.models.StatusInfo, lazy_fixture("status_info_dict")), + (brewtils.models.StatusHistory, lazy_fixture("status_history_dict")), ], ) def test_serialized_start(self, model, data):