def test_cdm_trait_collection_remove_only_from_property(self): manifest = generate_manifest('C:\\Root\\Path') trait = CdmTraitReference(manifest.ctx, 'TraitName', None) other_trait = CdmTraitReference(manifest.ctx, 'Name of other Trait', None) manifest.exhibits_traits.append(trait) manifest.exhibits_traits.append(other_trait) self.assertFalse(trait.is_from_property) self.assertFalse(other_trait.is_from_property) self.assertEqual(2, len(manifest.exhibits_traits)) manifest.exhibits_traits.remove(trait, True) self.assertTrue(trait in manifest.exhibits_traits) self.assertEqual(2, len(manifest.exhibits_traits)) other_trait.is_from_property = True manifest.exhibits_traits.remove(other_trait, True) self.assertTrue(other_trait not in manifest.exhibits_traits) self.assertEqual(1, len(manifest.exhibits_traits)) self.assertEqual(trait, manifest.exhibits_traits[0])
def test_cdm_trait_collection_insert(self): manifest = generate_manifest('C:\\Root\\Path') trait = CdmTraitReference(manifest.ctx, 'TraitName', None) other_trait = CdmTraitReference(manifest.ctx, 'Name of other Trait', None) manifest._trait_cache = dict() manifest.exhibits_traits.insert(0, trait) manifest.exhibits_traits.insert(0, other_trait) self.assertEqual(None, manifest._trait_cache) self.assertEqual(2, len(manifest.exhibits_traits)) self.assertEqual(other_trait, manifest.exhibits_traits[0]) self.assertEqual(trait, manifest.exhibits_traits[1]) self.assertEqual(manifest, manifest.exhibits_traits[0].owner)
def test_extends_trait_property_optional(self): corpus = CdmCorpusDefinition() extend_trait_ref1 = CdmTraitReference(corpus.ctx, 'testExtendTraitName1', True) extend_trait_ref2 = CdmTraitReference(corpus.ctx, 'testExtendTraitName2', True) trait_definition = CdmTraitDefinition(corpus.ctx, 'testTraitName', extend_trait_ref1) self.assertEqual(extend_trait_ref1, trait_definition.extends_trait) trait_definition.extends_trait = None self.assertIsNone(trait_definition.extends_trait) trait_definition.extends_trait = extend_trait_ref2 self.assertEqual(extend_trait_ref2, trait_definition.extends_trait) trait_definition.extends_trait = None self.assertIsNone(trait_definition.extends_trait)
def _create_new_resolved_attribute( proj_ctx: 'ProjectionContext', attr_ctx_under: 'CdmAttributeContext', target_attr_or_resolved_attr: Union['CdmAttribute', 'ResolvedAttribute'], override_default_name: Optional[str] = None, added_simple_ref_traits: Optional[List[str]] = None ) -> 'ResolvedAttribute': """ Projections require a new resolved attribute to be created multiple times This function allows us to create new resolved attributes based on a input attribute """ from cdm.objectmodel import CdmAttribute target_attr = target_attr_or_resolved_attr.copy() \ if isinstance(target_attr_or_resolved_attr, CdmAttribute) else target_attr_or_resolved_attr.target.copy() new_res_attr = ResolvedAttribute( proj_ctx._projection_directive._res_opt, target_attr, override_default_name if override_default_name else target_attr.get_name(), attr_ctx_under) target_attr.in_document = proj_ctx._projection_directive._owner.in_document if isinstance(target_attr_or_resolved_attr, CdmAttribute): if added_simple_ref_traits is not None: for trait in added_simple_ref_traits: if target_attr.applied_traits.item(trait) == None: target_attr.applied_traits.append(trait, True) res_trait_set = target_attr._fetch_resolved_traits( proj_ctx._projection_directive._res_opt) # Create deep a copy of traits to avoid conflicts in case of parameters if res_trait_set is not None: new_res_attr.resolved_traits = res_trait_set.deep_copy() else: new_res_attr.resolved_traits = target_attr_or_resolved_attr.resolved_traits.deep_copy( ) if added_simple_ref_traits is not None: for trait in added_simple_ref_traits: tr = CdmTraitReference(target_attr.ctx, trait, True, False) new_res_attr.resolved_traits = new_res_attr.resolved_traits.merge_set( tr._fetch_resolved_traits()) return new_res_attr
def test_cdm_trait_collection_remove_prioritize_from_property(self): manifest = generate_manifest("C:\\Root\\Path") trait = CdmTraitReference(manifest.ctx, "TraitName", None) other_trait = CdmTraitReference(manifest.ctx, "Name of other Trait", None) manifest.exhibits_traits.append(trait) manifest.exhibits_traits.append(other_trait) trait_copy_from_property = CdmTraitReference(manifest.ctx, "TraitName", None) trait_copy_from_property.is_from_property = True manifest.exhibits_traits.append(trait_copy_from_property) self.assertEqual(3, len(manifest.exhibits_traits)) manifest.exhibits_traits.remove("TraitName") self.assertTrue( trait_copy_from_property not in manifest.exhibits_traits) self.assertEqual(2, len(manifest.exhibits_traits)) self.assertEqual(trait, manifest.exhibits_traits[0]) self.assertEqual(other_trait, manifest.exhibits_traits[1])
def _generate_trait(self) -> 'CdmTraitReference': manifest = generate_manifest('C:\\Nothing') return CdmTraitReference(manifest.ctx, 'traitName', False)