Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 3af6c00

Browse files
authored
Implement ApplicationMetadata class and add unit tests (#5)
* Implement ApplicationMetadata class and add unit tests
1 parent 126852e commit 3af6c00

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

serverlessrepo/application_metadata.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from .exceptions import InvalidApplicationMetadataError
2+
3+
14
class ApplicationMetadata(object):
25
"""
36
Class representing SAR metadata
@@ -36,10 +39,17 @@ def __init__(self, app_metadata):
3639
def __eq__(self, other):
3740
return isinstance(other, type(self)) and self.__dict__ == other.__dict__
3841

39-
def is_valid(self):
42+
def validate(self, required_props):
4043
"""
41-
Checks if the required properties for application metadata have been populated
44+
Checks if the required application metadata properties have been populated
4245
46+
:param required_props: List of required properties
47+
:type required_props: list
4348
:return: True, if the metadata is valid
49+
:raises: InvalidApplicationMetadataError
4450
"""
51+
missing_props = [p for p in required_props if not getattr(self, p)]
52+
if missing_props:
53+
missing_props_str = ', '.join(sorted(missing_props))
54+
raise InvalidApplicationMetadataError(properties=missing_props_str)
4555
return True
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from unittest import TestCase
2+
3+
from serverlessrepo.application_metadata import ApplicationMetadata
4+
from serverlessrepo.exceptions import InvalidApplicationMetadataError
5+
6+
7+
class TestApplicationMetadata(TestCase):
8+
9+
def test_init(self):
10+
app_metadata_dict = {
11+
'Name': 'name',
12+
'Description': 'description',
13+
'Author': 'author',
14+
'SpdxLicenseId': '123456',
15+
'LicenseUrl': 's3://bucket/license.txt',
16+
'ReadmeUrl': 's3://bucket/README.md',
17+
'Labels': ['label1', 'label2', 'label3'],
18+
'HomepageUrl': 'https://github.com/my-id/my-repo/',
19+
'SemanticVersion': '1.0.0',
20+
'SourceCodeUrl': 's3://bucket/code.zip'
21+
}
22+
app_metadata = ApplicationMetadata(app_metadata_dict)
23+
self.assertEqual(app_metadata.name, app_metadata_dict['Name'])
24+
self.assertEqual(app_metadata.description, app_metadata_dict['Description'])
25+
self.assertEqual(app_metadata.author, app_metadata_dict['Author'])
26+
self.assertEqual(app_metadata.spdx_license_id, app_metadata_dict['SpdxLicenseId'])
27+
self.assertEqual(app_metadata.license_url, app_metadata_dict['LicenseUrl'])
28+
self.assertEqual(app_metadata.readme_url, app_metadata_dict['ReadmeUrl'])
29+
self.assertEqual(app_metadata.labels, app_metadata_dict['Labels'])
30+
self.assertEqual(app_metadata.home_page_url, app_metadata_dict['HomepageUrl'])
31+
self.assertEqual(app_metadata.semantic_version, app_metadata_dict['SemanticVersion'])
32+
self.assertEqual(app_metadata.source_code_url, app_metadata_dict['SourceCodeUrl'])
33+
34+
def test_invalid_app_metadata(self):
35+
app_metadata_dict = { 'description': 'hello' }
36+
app_metadata = ApplicationMetadata(app_metadata_dict)
37+
required_props = ['author', 'name']
38+
with self.assertRaises(InvalidApplicationMetadataError) as context:
39+
app_metadata.validate(required_props)
40+
41+
message = str(context.exception)
42+
self.assertTrue(', '.join(required_props) in message)
43+
44+
def test_valid_app_metadata(self):
45+
app_metadata = ApplicationMetadata({})
46+
self.assertTrue(app_metadata.validate([]))

0 commit comments

Comments
 (0)