|
| 1 | +from unittest import TestCase |
| 2 | +from mock import patch, Mock, call |
| 3 | + |
| 4 | +from samtranslator.parser.parser import Parser |
| 5 | +from samtranslator.plugins import LifeCycleEvents |
| 6 | +from samtranslator.model.exceptions import InvalidDocumentException, InvalidTemplateException, InvalidResourceException |
| 7 | + |
| 8 | + |
| 9 | +class TestParser(TestCase): |
| 10 | + def test_parse(self): |
| 11 | + parser = Parser() |
| 12 | + parser._validate = Mock() |
| 13 | + sam_plugins_mock = Mock() |
| 14 | + sam_plugins_mock.act = Mock() |
| 15 | + sam_template = {} |
| 16 | + parameter_values = {} |
| 17 | + |
| 18 | + parser.parse(sam_template, parameter_values, sam_plugins_mock) |
| 19 | + parser._validate.assert_has_calls([call(sam_template, parameter_values)]) |
| 20 | + sam_plugins_mock.act.assert_has_calls([call(LifeCycleEvents.before_transform_template, sam_template)]) |
| 21 | + |
| 22 | + @patch("samtranslator.parser.parser.SamTemplateValidator") |
| 23 | + @patch("samtranslator.parser.parser.LOG") |
| 24 | + def test_validate_validator_failure(self, log_mock, sam_template_validator_class_mock): |
| 25 | + exception = Exception() |
| 26 | + sam_template_validator_class_mock.side_effect = exception |
| 27 | + log_mock.exception = Mock() |
| 28 | + |
| 29 | + sam_template = { |
| 30 | + "Resources": { |
| 31 | + "Function": {}, |
| 32 | + "Api": {}, |
| 33 | + } |
| 34 | + } |
| 35 | + paramerter_values = {"Param": "value"} |
| 36 | + parser = Parser() |
| 37 | + parser._validate(sam_template, paramerter_values) |
| 38 | + log_mock.exception.assert_has_calls([call("Exception from SamTemplateValidator: %s", exception)]) |
| 39 | + |
| 40 | + def test_validate_parameter_values_is_required(self): |
| 41 | + parser = Parser() |
| 42 | + with self.assertRaises(ValueError): |
| 43 | + parser._validate({}, None) |
| 44 | + |
| 45 | + def test_validate_template_with_no_resource(self): |
| 46 | + parser = Parser() |
| 47 | + with self.assertRaises(InvalidDocumentException): |
| 48 | + parser._validate({}, {}) |
| 49 | + |
| 50 | + def test_validate_template_with_non_dict_resources(self): |
| 51 | + parser = Parser() |
| 52 | + with self.assertRaises(InvalidDocumentException): |
| 53 | + parser._validate({"Resources": "string"}, {}) |
| 54 | + |
| 55 | + def test_validate_template_with_empty_resources(self): |
| 56 | + parser = Parser() |
| 57 | + with self.assertRaises(InvalidDocumentException): |
| 58 | + parser._validate({"Resources": {}}, {}) |
0 commit comments