From e3550aab39d3904168c2a416ad564e497833caa3 Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 27 Mar 2023 13:22:25 +0200 Subject: [PATCH 1/3] fix: Correct json representation for Reports. The sensor_sender attribute is never considered as it is always null. The dispatcher_report_id attribute is verified and ignored if it is null. refactor: Improve tests --- powerapi/report/report.py | 10 ++++++++- tests/unit/report/test_report.py | 37 ++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/powerapi/report/report.py b/powerapi/report/report.py index 1f72e5b1..b331b9fd 100644 --- a/powerapi/report/report.py +++ b/powerapi/report/report.py @@ -100,7 +100,15 @@ def to_json(report: Report) -> Dict: """ :return: a json dictionary, that can be converted into json format, from a given Report """ - return report.__dict__ + json = report.__dict__ + # sender_name is not used. It is always None + json.pop('sender_name') + + # dispatcher_report_id is only used by Dispatcher. If empty, it is removed + if json['dispatcher_report_id'] is None: + json.pop('dispatcher_report_id') + + return json @staticmethod def _extract_timestamp(ts): diff --git a/tests/unit/report/test_report.py b/tests/unit/report/test_report.py index fee87fdc..1823ce4c 100644 --- a/tests/unit/report/test_report.py +++ b/tests/unit/report/test_report.py @@ -33,12 +33,23 @@ from datetime import datetime -############ -# METADATA # -############ +@pytest.fixture() +def basic_report(): + return Report(timestamp=datetime.strptime('1970-09-01T09:09:10.543', "%Y-%m-%dT%H:%M:%S.%f"), sensor='toto', + target='all', metadata={"tag": 1}) + + +@pytest.fixture() +def expected_json_report(basic_report): + return {'timestamp': basic_report.timestamp, + 'sensor': basic_report.sensor, + 'target': basic_report.target, + 'metadata': basic_report.metadata} + def test_creating_report_with_metadata(): - report = Report(datetime.strptime('1970-09-01T09:09:10.543', "%Y-%m-%dT%H:%M:%S.%f"), 'toto', 'all', {"tag": 1}) + report = Report(timestamp=datetime.strptime('1970-09-01T09:09:10.543', "%Y-%m-%dT%H:%M:%S.%f"), sensor='toto', + target='all', metadata={"tag": 1}) assert report.metadata["tag"] == 1 @@ -52,3 +63,21 @@ def test_create_two_report_without_metadata_metadata_are_different(): a.metadata["test"] = "value" b = Report(0, 'toto', 'all') assert a.metadata != b.metadata + + +def test_to_json(basic_report, expected_json_report): + + json = Report.to_json(report=basic_report) + assert 'sender_name' not in json + assert 'dispatcher_report_id' not in json + assert json == expected_json_report + + +def test_to_json_with_dispatcher_report_id(basic_report, expected_json_report): + basic_report.dispatcher_report_id = 10 + expected_json_report['dispatcher_report_id'] = basic_report.dispatcher_report_id + + json = Report.to_json(report=basic_report) + assert 'sender_name' not in json + assert 'dispatcher_report_id' in json + assert json == expected_json_report From ef9a1e004275658252af7e737dc07600759822fd Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 27 Mar 2023 15:03:53 +0200 Subject: [PATCH 2/3] fix: Correct usage of dispatcher_report_id. This attribute has also to be ignored in the json representation --- powerapi/report/report.py | 7 ++----- tests/unit/report/test_report.py | 5 ++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/powerapi/report/report.py b/powerapi/report/report.py index b331b9fd..fe5e4acd 100644 --- a/powerapi/report/report.py +++ b/powerapi/report/report.py @@ -101,12 +101,9 @@ def to_json(report: Report) -> Dict: :return: a json dictionary, that can be converted into json format, from a given Report """ json = report.__dict__ - # sender_name is not used. It is always None + # sender_name and dispatcher_report_id are not used json.pop('sender_name') - - # dispatcher_report_id is only used by Dispatcher. If empty, it is removed - if json['dispatcher_report_id'] is None: - json.pop('dispatcher_report_id') + json.pop('dispatcher_report_id') return json diff --git a/tests/unit/report/test_report.py b/tests/unit/report/test_report.py index 1823ce4c..8ab4242a 100644 --- a/tests/unit/report/test_report.py +++ b/tests/unit/report/test_report.py @@ -75,9 +75,8 @@ def test_to_json(basic_report, expected_json_report): def test_to_json_with_dispatcher_report_id(basic_report, expected_json_report): basic_report.dispatcher_report_id = 10 - expected_json_report['dispatcher_report_id'] = basic_report.dispatcher_report_id - + json = Report.to_json(report=basic_report) assert 'sender_name' not in json - assert 'dispatcher_report_id' in json + assert 'dispatcher_report_id' not in json assert json == expected_json_report From 013d41838058e788874cc16f3df195139ec9071d Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 28 Mar 2023 09:04:27 +0200 Subject: [PATCH 3/3] fix: Improve tests for Report according to changes of json representation. --- tests/unit/report/test_report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/report/test_report.py b/tests/unit/report/test_report.py index 8ab4242a..49917eab 100644 --- a/tests/unit/report/test_report.py +++ b/tests/unit/report/test_report.py @@ -75,7 +75,7 @@ def test_to_json(basic_report, expected_json_report): def test_to_json_with_dispatcher_report_id(basic_report, expected_json_report): basic_report.dispatcher_report_id = 10 - + json = Report.to_json(report=basic_report) assert 'sender_name' not in json assert 'dispatcher_report_id' not in json