Skip to content

Commit

Permalink
Add a from_dict constructor for the About class #376
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Nov 30, 2018
1 parent 6e57a07 commit e4ad747
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/attributecode/model.py
Expand Up @@ -987,6 +987,15 @@ def load_dict(self, fields_dict, base_dir, running_inventory=False, reference_di
self.errors = errors
return errors

@classmethod
def from_dict(cls, about_data, base_dir=''):
"""
Return an About object loaded from a python dict.
"""
about = cls()
about.load_dict(about_data, base_dir=base_dir)
return about

def dumps(self):
"""
Return self as a formatted ABOUT string.
Expand Down
25 changes: 25 additions & 0 deletions tests/test_model.py
Expand Up @@ -849,6 +849,31 @@ def test_load_dict_as_dict_is_idempotent_ignoring_special(self):

assert expected == dict(as_dict)

def test_about_model_class_from_dict_constructor(self):
about_data = {
'about_resource': ['.'],
'attribute': 'yes',
'author': ['Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez'],
'copyright': 'Copyright (c) 2013-2014 nexB Inc.',
'description': 'AboutCode is a tool to process ABOUT files. An ABOUT file is a file.',
'homepage_url': 'http://dejacode.org',
'license_expression': 'apache-2.0',
'name': 'AboutCode',
'owner': 'nexB Inc.',
'vcs_repository': 'https://github.com/dejacode/about-code-tool.git',
'vcs_tool': 'git',
'version': '0.11.0',
}

about = model.About.from_dict(about_data)
assert isinstance(about, model.About)

about_data.update({
'about_file_path': None,
'about_resource': OrderedDict([('.', None)]),
})
assert about_data == about.as_dict()

def test_write_output_csv(self):
path = 'test_model/this.ABOUT'
test_file = get_test_loc(path)
Expand Down

0 comments on commit e4ad747

Please sign in to comment.