def test_to_dict_must_update_type_and_properties(self): resource_dict = { "Type": "AWS::Serverless::Function", "Properties": { "a": "b" } } resource = SamResource(resource_dict) resource.type = "AWS::Serverless::Api" resource.properties = {"c": "d"} self.assertEqual(resource.to_dict(), { "Type": "AWS::Serverless::Api", "Properties": { "c": "d" } }) # Calling to_dict() has side effect of updating the original dictionary self.assertEqual(resource_dict, { "Type": "AWS::Serverless::Api", "Properties": { "c": "d" } })
def test_valid_must_validate_sam_resources_only(self): self.assertTrue(SamResource({"Type": "AWS::Serverless::Api"}).valid()) self.assertTrue( SamResource({ "Type": "AWS::Serverless::Function" }).valid()) self.assertTrue( SamResource({ "Type": "AWS::Serverless::SimpleTable" }).valid()) self.assertFalse( SamResource({ "Type": "AWS::Lambda::Function" }).valid())
def test_set_must_work_with_sam_resource_input(self): template = SamTemplate(self.template_dict) template.set("NewResource", SamResource({"Type": "something"})) # Set would modify the original template dictionary self.assertEquals(self.template_dict["Resources"].get("NewResource"), {"Type": "something"})
def test_to_dict_must_update_type_and_properties(self): resource_dict = { "Type": "AWS::Serverless::Function", "Properties": { "a": "b" } } resource = SamResource(resource_dict) resource.type = "AWS::Serverless::Api" resource.properties = {"c": "d"} self.assertEquals(resource.to_dict(), {"Type": "AWS::Serverless::Api", "Properties": {"c": "d"}}) # Calling to_dict() has side effect of updating the original dictionary self.assertEquals(resource_dict, {"Type": "AWS::Serverless::Api", "Properties": {"c": "d"}})
def iterate(self, resource_type=None): """ Iterate over all resources within the SAM template, optionally filtering by type :param string resource_type: Optional type to filter the resources by :yields (string, SamResource): Tuple containing LogicalId and the resource """ for logicalId, resource_dict in self.resources.items(): resource = SamResource(resource_dict) needs_filter = resource.valid() if resource_type: needs_filter = needs_filter and resource.type == resource_type if needs_filter: yield logicalId, resource
def test_init_must_default_to_empty_properties_dict(self): resource_dict = { "Type": "foo" # Properties is missing } resource = SamResource(resource_dict) self.assertEqual(resource.properties, {})
def get(self, logicalId): """ Gets the resource at the given logicalId if present :param string logicalId: Id of the resource :return SamResource: Resource, if available at the Id. None, otherwise """ if logicalId not in self.resources: return None return SamResource(self.resources.get(logicalId))
def test_init_must_extract_type_and_properties(self): resource_dict = { "Type": "foo", "Properties": { "a": "b" } } resource = SamResource(resource_dict) self.assertEqual(resource.type, "foo") self.assertEqual(resource.properties, {"a": "b"})
def test_valid_must_not_work_with_resource_without_type(self): self.assertFalse(SamResource({"a": "b"}).valid())