Skip to content

Commit

Permalink
Merge pull request #150 from powerapi-ng/fix/issue149/json_representa…
Browse files Browse the repository at this point in the history
…tion_fix

Fix/issue149/json representation fix
  • Loading branch information
roda82 committed Mar 28, 2023
2 parents 516ab34 + 013d418 commit d577f15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
7 changes: 6 additions & 1 deletion powerapi/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ 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 and dispatcher_report_id are not used
json.pop('sender_name')
json.pop('dispatcher_report_id')

return json

@staticmethod
def _extract_timestamp(ts):
Expand Down
36 changes: 32 additions & 4 deletions tests/unit/report/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -52,3 +63,20 @@ 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

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

0 comments on commit d577f15

Please sign in to comment.