def test_get_on_non_existent_property(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") self.assertEquals(None, resource_refs.get("logicalId1", "SomeProperty")) self.assertEquals(None, resource_refs.get("SomeLogicalId", "property1"))
def test_get_must_return_correct_value(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId1", "property2", "value2") resource_refs.add("newLogicalId", "newProperty", "newValue") self.assertEquals("value1", resource_refs.get("logicalId1", "property1")) self.assertEquals("value2", resource_refs.get("logicalId1", "property2")) self.assertEquals("newValue", resource_refs.get("newLogicalId", "newProperty"))
def test_get_must_return_correct_value(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId1", "property2", "value2") resource_refs.add("newLogicalId", "newProperty", "newValue") self.assertEqual("value1", resource_refs.get("logicalId1", "property1")) self.assertEqual("value2", resource_refs.get("logicalId1", "property2")) self.assertEqual("newValue", resource_refs.get("newLogicalId", "newProperty"))
class TestSamResourceReferableProperties(TestCase): class ResourceType1(Resource): resource_type = "resource_type1" property_types = {} class ResourceType2(Resource): resource_type = "resource_type2" property_types = {} class ResourceType3(Resource): resource_type = "resource_type3" property_types = {} def setUp(self): self.supported_resource_refs = SupportedResourceReferences() def test_must_get_property_for_available_resources(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = { "prop1": "resource_type1", "prop2": "resource_type2", "prop3": "resource_type3" } sam_resource = NewSamResource("SamLogicalId") cfn_resources = [ self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2") ] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEquals( "logicalId1", self.supported_resource_refs.get("SamLogicalId", "prop1")) self.assertEquals( "logicalId2", self.supported_resource_refs.get("SamLogicalId", "prop2")) # there is no cfn resource of for "prop3" in the cfn_resources list self.assertEquals( None, self.supported_resource_refs.get("SamLogicalId", "prop3")) # Must add only for the given SAM resource self.assertEquals(1, len(self.supported_resource_refs)) def test_must_work_with_two_resources_of_same_type(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = { "prop1": "resource_type1", "prop2": "resource_type2", "prop3": "resource_type3" } sam_resource1 = NewSamResource("SamLogicalId1") sam_resource2 = NewSamResource("SamLogicalId2") cfn_resources = [ self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2") ] self.supported_resource_refs = \ sam_resource1.get_resource_references(cfn_resources, self.supported_resource_refs) self.supported_resource_refs = \ sam_resource2.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEquals( "logicalId1", self.supported_resource_refs.get("SamLogicalId1", "prop1")) self.assertEquals( "logicalId2", self.supported_resource_refs.get("SamLogicalId1", "prop2")) self.assertEquals( "logicalId1", self.supported_resource_refs.get("SamLogicalId2", "prop1")) self.assertEquals( "logicalId2", self.supported_resource_refs.get("SamLogicalId2", "prop2")) self.assertEquals(2, len(self.supported_resource_refs)) def test_must_skip_unknown_resource_types(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = { "prop1": "foo", "prop2": "bar", } sam_resource = NewSamResource("SamLogicalId") # None of the CFN resource types are in the referable list cfn_resources = [ self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2") ] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEquals(0, len(self.supported_resource_refs)) def test_must_skip_if_no_supported_properties(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = {} sam_resource = NewSamResource("SamLogicalId") cfn_resources = [ self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2") ] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEquals(0, len(self.supported_resource_refs)) def test_must_skip_if_no_resources(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = {"prop1": "resource_type1"} sam_resource = NewSamResource("SamLogicalId") cfn_resources = [] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEquals(0, len(self.supported_resource_refs)) def test_must_raise_if_input_is_absent(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = {"prop1": "resource_type1"} sam_resource = NewSamResource("SamLogicalId") cfn_resources = [self.ResourceType1("logicalId1")] with self.assertRaises(ValueError): sam_resource.get_resource_references(cfn_resources, None)
class TestSamResourceReferableProperties(TestCase): class ResourceType1(Resource): resource_type = "resource_type1" property_types = {} class ResourceType2(Resource): resource_type = "resource_type2" property_types = {} class ResourceType3(Resource): resource_type = "resource_type3" property_types = {} def setUp(self): self.supported_resource_refs = SupportedResourceReferences() def test_must_get_property_for_available_resources(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = { "prop1": "resource_type1", "prop2": "resource_type2", "prop3": "resource_type3" } sam_resource = NewSamResource("SamLogicalId") cfn_resources = [self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2")] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEqual("logicalId1", self.supported_resource_refs.get("SamLogicalId", "prop1")) self.assertEqual("logicalId2", self.supported_resource_refs.get("SamLogicalId", "prop2")) # there is no cfn resource of for "prop3" in the cfn_resources list self.assertEqual(None, self.supported_resource_refs.get("SamLogicalId", "prop3")) # Must add only for the given SAM resource self.assertEqual(1, len(self.supported_resource_refs)) def test_must_work_with_two_resources_of_same_type(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = { "prop1": "resource_type1", "prop2": "resource_type2", "prop3": "resource_type3" } sam_resource1 = NewSamResource("SamLogicalId1") sam_resource2 = NewSamResource("SamLogicalId2") cfn_resources = [self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2") ] self.supported_resource_refs = \ sam_resource1.get_resource_references(cfn_resources, self.supported_resource_refs) self.supported_resource_refs = \ sam_resource2.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEqual("logicalId1", self.supported_resource_refs.get("SamLogicalId1", "prop1")) self.assertEqual("logicalId2", self.supported_resource_refs.get("SamLogicalId1", "prop2")) self.assertEqual("logicalId1", self.supported_resource_refs.get("SamLogicalId2", "prop1")) self.assertEqual("logicalId2", self.supported_resource_refs.get("SamLogicalId2", "prop2")) self.assertEqual(2, len(self.supported_resource_refs)) def test_must_skip_unknown_resource_types(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = { "prop1": "foo", "prop2": "bar", } sam_resource = NewSamResource("SamLogicalId") # None of the CFN resource types are in the referable list cfn_resources = [self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2")] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEqual(0, len(self.supported_resource_refs)) def test_must_skip_if_no_supported_properties(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = {} sam_resource = NewSamResource("SamLogicalId") cfn_resources = [self.ResourceType1("logicalId1"), self.ResourceType2("logicalId2")] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEqual(0, len(self.supported_resource_refs)) def test_must_skip_if_no_resources(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = {"prop1": "resource_type1"} sam_resource = NewSamResource("SamLogicalId") cfn_resources = [] self.supported_resource_refs = \ sam_resource.get_resource_references(cfn_resources, self.supported_resource_refs) self.assertEqual(0, len(self.supported_resource_refs)) def test_must_raise_if_input_is_absent(self): class NewSamResource(SamResourceMacro): resource_type = "foo" property_types = {} referable_properties = {"prop1": "resource_type1"} sam_resource = NewSamResource("SamLogicalId") cfn_resources = [self.ResourceType1("logicalId1")] with self.assertRaises(ValueError): sam_resource.get_resource_references(cfn_resources, None)