def testInitializeWithAnchor(self, proj_value, case_value, shelf_value, book_value, expected): book_fallthrough = deps.Fallthrough(lambda: book_value, 'h', active=True) fallthroughs_map = { 'project': [ deps.FullySpecifiedAnchorFallthrough(book_fallthrough, self.book_collection, 'projectsId'), deps.Fallthrough(lambda: proj_value, 'h', active=True) ], 'case': [ deps.FullySpecifiedAnchorFallthrough(book_fallthrough, self.book_collection, 'casesId'), deps.Fallthrough(lambda: case_value, 'h', active=True) ], 'shelf': [ deps.FullySpecifiedAnchorFallthrough(book_fallthrough, self.book_collection, 'shelvesId'), deps.Fallthrough(lambda: shelf_value, 'h') ], 'book': [book_fallthrough] } self.assertEqual( expected, self.resource.Initialize(fallthroughs_map).result.RelativeName())
def testInitializeNotFullySpecified(self): book_fallthrough = deps.Fallthrough(lambda: 'my-book', 'h', active=True) fallthroughs_map = { 'project': [ deps.FullySpecifiedAnchorFallthrough(book_fallthrough, self.book_collection, 'projectsId'), deps.Fallthrough(lambda: 'my-project', 'h') ], 'organization': [ deps.FullySpecifiedAnchorFallthrough( book_fallthrough, self.org_shelf_book_collection, 'organizationsId'), deps.Fallthrough(lambda: 'my-org', 'h', active=True) ], 'shelf': [ deps.FullySpecifiedAnchorFallthrough( book_fallthrough, self.org_shelf_book_collection, 'shelvesId'), deps.FullySpecifiedAnchorFallthrough(book_fallthrough, self.book_collection, 'shelvesId'), deps.Fallthrough(lambda: None, 'h') ], 'book': [book_fallthrough] } with self.assertRaisesRegexp(concepts.InitializationError, re.escape('[shelf]')): self.resource.Initialize(fallthroughs_map).result.RelativeName()
def testAnchorFallthrough(self, orig_fallthrough, active): """Test the FullySpecifiedAnchorFallthrough gives other parameters.""" proj_fallthrough = deps.FullySpecifiedAnchorFallthrough( orig_fallthrough, self.book_collection, 'projectsId') shelf_fallthrough = deps.FullySpecifiedAnchorFallthrough( orig_fallthrough, self.book_collection, 'shelvesId') parsed_args = self._GetMockNamespace(a='projects/p/shelves/s/books/b') self.assertEqual('p', proj_fallthrough.GetValue(parsed_args)) self.assertEqual('s', shelf_fallthrough.GetValue(parsed_args)) self.assertEqual(active, proj_fallthrough.active) self.assertEqual(active, shelf_fallthrough.active)
def testAnchorFallthroughFails(self, proj_param, anchor_value): """Test failures with FullySpecifiedAnchorFallthrough.""" proj_fallthrough = deps.FullySpecifiedAnchorFallthrough( deps.ArgFallthrough('--a'), self.book_collection, proj_param) parsed_args = self._GetMockNamespace(a=anchor_value) with self.assertRaises(deps.FallthroughNotFoundError): proj_fallthrough.GetValue(parsed_args)
def _GetAttributeAnchorFallthroughs(self, anchor_fallthroughs, attribute): """Helper to get anchor-depednent fallthroughs for a specific attribute.""" parameter_name = self.ParamName(attribute.name) anchor_based_fallthroughs = [ deps_lib.FullySpecifiedAnchorFallthrough( anchor_fallthrough, self.collection_info, parameter_name) for anchor_fallthrough in anchor_fallthroughs ] return anchor_based_fallthroughs
def BuildFullFallthroughsMap(self): """Builds map of all fallthroughs including arg names. Fallthroughs are a list of objects that, when called, try different ways of getting values for attributes (see googlecloudsdk.calliope.concepts.deps. _Fallthrough). This method builds a map from the name of each attribute to its fallthroughs, including the "primary" fallthrough representing its corresponding argument value in parsed_args if any, and any fallthroughs that were configured for the attribute beyond that. Returns: {str: [deps_lib._Fallthrough]}, a map from attribute name to its fallthroughs. """ fallthroughs_map = {} for attribute in self.concept_spec.attributes: # The only args that should be lists are anchor args for plural # resources. attribute_name = attribute.name attribute_fallthroughs = [] plural = (attribute_name == self.concept_spec.anchor.name and self.plural) # Start the fallthroughs list with the primary associated arg for the # attribute. arg_name = self.attribute_to_args_map.get(attribute_name) if arg_name: attribute_fallthroughs.append( deps_lib.ArgFallthrough(arg_name, plural=plural)) given_fallthroughs = self.fallthroughs_map.get(attribute_name, []) for fallthrough in given_fallthroughs: final_fallthrough = copy.deepcopy(fallthrough) final_fallthrough.plural = plural attribute_fallthroughs.append(final_fallthrough) fallthroughs_map[attribute_name] = attribute_fallthroughs anchor_fallthroughs = fallthroughs_map[self.concept_spec.anchor.name] for attribute in self.concept_spec.attributes[:-1]: parameter_name = self.concept_spec.ParamName(attribute.name) anchor_based_fallthroughs = [ deps_lib.FullySpecifiedAnchorFallthrough( anchor_fallthrough, self.concept_spec.collection_info, parameter_name) for anchor_fallthrough in anchor_fallthroughs ] fallthroughs_map[attribute.name] = ( anchor_based_fallthroughs + fallthroughs_map[attribute.name]) return fallthroughs_map
def _GetAttributeAnchorFallthroughs(self, anchor_fallthroughs, attribute): """Helper to get anchor-dependent fallthroughs for a given attribute.""" anchor_based_fallthroughs = [] for spec in self._concept_specs: # Only add fallthroughs for attributes that are not the anchor but do # belong to the relevant concept. if attribute not in spec.attributes or attribute == spec.anchor: continue parameter_name = spec.ParamName(attribute.name) anchor_based_fallthroughs += [ deps_lib.FullySpecifiedAnchorFallthrough( anchor_fallthrough, spec.collection_info, parameter_name) for anchor_fallthrough in anchor_fallthroughs ] return anchor_based_fallthroughs