diff --git a/dcicutils/contribution_utils.py b/dcicutils/contribution_utils.py index 1b0229610..d5fba1162 100644 --- a/dcicutils/contribution_utils.py +++ b/dcicutils/contribution_utils.py @@ -213,23 +213,30 @@ def contributor_values_as_objects(cls, contributor_index: Optional[Dict]) -> Opt if contributor_index is None: return None else: - items = contributor_index.items() - key: str - result = { + return { key: Contributor.from_dict(value) - for key, value in items + for key, value in contributor_index.items() } - return result def checkpoint_state(self): return self.as_dict() def as_dict(self): - data = { - "forked_at": self.forked_at.isoformat() if self.forked_at else None, - "pre_fork_contributors_by_name": self.contributor_values_as_dicts(self.pre_fork_contributors_by_name), - "contributors_by_name": self.contributor_values_as_dicts(self.contributors_by_name), - } + + data = {} + + forked_at = self.forked_at.isoformat() if self.forked_at else None + if forked_at is not None: + data["forked_at"] = forked_at + + pre_fork_contributors_by_name = self.contributor_values_as_dicts(self.pre_fork_contributors_by_name) + if pre_fork_contributors_by_name is not None: + data["pre_fork_contributors_by_name"] = pre_fork_contributors_by_name + + contributors_by_name = self.contributor_values_as_dicts(self.contributors_by_name) + if contributors_by_name is not None: + data["contributors_by_name"] = contributors_by_name + return data def save_contributor_data(self, filename: Optional[str] = None) -> str: diff --git a/test/test_contribution_utils.py b/test/test_contribution_utils.py index 1095480e6..7f8ca2870 100644 --- a/test/test_contribution_utils.py +++ b/test/test_contribution_utils.py @@ -578,24 +578,28 @@ def test_save_contributor_data(): contributions = BasicContributions() cache_file = contributions.save_contributor_data() + assert json.loads(file_contents(cache_file)) == {} - assert file_contents(cache_file) == ( - '{\n' - ' "forked_at": null,\n' - ' "pre_fork_contributors_by_name": null,\n' - ' "contributors_by_name": null\n' - '}\n' - ) + contributions = BasicContributions() + contributions.forked_at = datetime.datetime(2020, 1, 1, 12, 34, 56) + contributions.pre_fork_contributors_by_name = {} + contributions.contributors_by_name = contributions.contributor_values_as_objects({ + "Joe": {"names": ["Joe"], "emails": ["joe@foo"]} + }) + cache_file = contributions.save_contributor_data() + assert json.loads(file_contents(cache_file)) == { + "forked_at": "2020-01-01T12:34:56", + "pre_fork_contributors_by_name": {}, + "contributors_by_name": {"Joe": {"names": ["Joe"], "emails": ["joe@foo"]}} + } cache_file_2 = contributions.save_contributor_data('some.file') assert cache_file_2 == 'some.file' - assert file_contents(cache_file_2) == ( - '{\n' - ' "forked_at": null,\n' - ' "pre_fork_contributors_by_name": null,\n' - ' "contributors_by_name": null\n' - '}\n' - ) + assert json.loads(file_contents(cache_file_2)) == { + "forked_at": "2020-01-01T12:34:56", + "pre_fork_contributors_by_name": {}, + "contributors_by_name": {"Joe": {"names": ["Joe"], "emails": ["joe@foo"]}} + } def test_repo_contributor_names():