def _print_relationship(self, relationship: 'CdmE2ERelationship') -> str: """Print the relationship""" bldr = '' if relationship.name: bldr += ' Name: {}\n'.format(relationship.name) bldr += ' FromEntity: {}\n'.format(relationship.from_entity) bldr += ' FromEntityAttribute: {}\n'.format( relationship.from_entity_attribute) bldr += ' ToEntity: {}\n'.format(relationship.to_entity) bldr += ' ToEntityAttribute: {}\n'.format( relationship.to_entity_attribute) if relationship.exhibits_traits: bldr += ' ExhibitsTraits:\n' order_applied_traits = sorted(relationship.exhibits_traits, key=lambda x: x.named_reference) for trait in order_applied_traits: bldr += ' {}\n'.format(trait.named_reference) for args in trait.arguments: attr_ctx_util = AttributeContextUtil() bldr += ' {}\n'.format( attr_ctx_util.get_argument_values_as_strings(args)) bldr += '\n' print(bldr) return bldr
async def test_all_operations(self): """Tests running all the projections (includes projections that are not implemented).""" test_name = 'TestAllOperations' entity_name = test_name corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name) def callback(level: CdmStatusLevel, message: str): if message.find('Projection operation not implemented yet.') == -1: self.fail('Some unexpected failure - {}!'.format(message)) corpus.set_event_callback(callback, CdmStatusLevel.ERROR) expected_output_path = TestHelper.get_expected_output_folder_path( self.tests_subpath, test_name) ent_test_entity_string_reference = await corpus.fetch_object_async( 'local:/{0}.cdm.json/{0}'.format(entity_name) ) # type: CdmEntityDefinition self.assertIsNotNone(ent_test_entity_string_reference) resolved_test_entity_string_reference = await TestUtils._get_resolved_entity( corpus, ent_test_entity_string_reference, ['referenceOnly']) # type: CdmEntityDefinition self.assertIsNotNone(resolved_test_entity_string_reference) AttributeContextUtil.validate_attribute_context( self, corpus, expected_output_path, entity_name, resolved_test_entity_string_reference)
async def validate_attribute_context(test: 'TestCase', directives: List[str], expected_output_path: str, entity_name: str, resolved_entity: 'CdmEntityDefinition', update_expected_output: Optional[bool] = False): """ Validates if the attribute context of the resolved entity matches the expected output. If update_expected_output is true, will update the expected output txt files for all the tests that are ran. """ if not resolved_entity.attribute_context: raise Exception('ValidateAttributeContext called with not resolved entity.') file_name_prefix = 'AttrCtx_' + entity_name file_name_suffix = ProjectionTestUtils.get_resolution_option_name_suffix(directives) # Get actual text attr_ctx_util = AttributeContextUtil() actual_text = attr_ctx_util.get_attribute_context_strings(resolved_entity) if update_expected_output: expected_string_file_path = os.path.join(expected_output_path, file_name_prefix + file_name_suffix + '.txt') if len(directives) > 0: default_file_name_suffix = ProjectionTestUtils.get_resolution_option_name_suffix([]) default_string_file_path = os.path.join(expected_output_path, file_name_prefix + default_file_name_suffix + '.txt') if os.path.exists(default_string_file_path): with open(default_string_file_path) as default_file: default_text = default_file.read().replace('\r\n', '\n') else: default_text = None if actual_text == default_text: if os.path.exists(expected_string_file_path): os.remove(expected_string_file_path) else: with open(expected_string_file_path, 'w') as expected_file: expected_file.write(actual_text) else: with open(expected_string_file_path, 'w') as expected_file: expected_file.write(actual_text) else: # Actual actual_string_file_path = os.path.join(expected_output_path, '..', TestHelper.get_test_actual_output_folder_name(), file_name_prefix + file_name_suffix + '.txt') # Save Actual AttrCtx_*.txt and Resolved_*.cdm.json with open(actual_string_file_path, 'w') as expected_file: expected_file.write(actual_text) await resolved_entity.in_document.save_as_async(resolved_entity.entity_name + file_name_suffix + '.cdm.json', save_referenced=False) # Expected expected_file_name_suffix = ProjectionTestUtils.get_resolution_option_name_suffix(directives, expected_output_path, entity_name) expected_string_file_path = os.path.join(expected_output_path, file_name_prefix + expected_file_name_suffix + '.txt') with open(expected_string_file_path) as expected_file: expected_text = expected_file.read() # Test if Actual is Equal to Expected test.assertEqual(expected_text.replace('\r\n', '\n'), actual_text.replace('\r\n', '\n'))
async def _load_entity_for_resolution_option_and_save(self, test_name: str, entity_name: str, res_opts: List[str]): corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name) manifest = await corpus.fetch_object_async('local:/default.manifest.cdm.json') expected_output_path = TestHelper.get_expected_output_folder_path(self.tests_subpath, test_name) file_name_suffix = TestUtils.get_resolution_option_name_suffix(res_opts) ent_sales_foreign_key_projection = await corpus.fetch_object_async('local:/{}.cdm.json/{}'.format(entity_name, entity_name), manifest) self.assertIsNotNone(ent_sales_foreign_key_projection) resolved_sales_foreign_key_projection = await self._save_resolved(corpus, manifest, test_name, ent_sales_foreign_key_projection, res_opts) self.assertIsNotNone(resolved_sales_foreign_key_projection) AttributeContextUtil.validate_attribute_context(self, corpus, expected_output_path, '{}{}'.format(entity_name, file_name_suffix), resolved_sales_foreign_key_projection)
def _get_attribute_context_string(self, resolved_entity: 'CdmEntityDefinition', entity_name: str, actual_output_folder: str) -> str: """Check the attribute context for these test scenarios""" return (AttributeContextUtil()).get_attribute_context_strings( resolved_entity, resolved_entity.attribute_context)
async def _load_entity_for_resolution_option_and_save(self, test_name: str, entity_name: str, res_opts: List[str]) -> None: """Loads an entity, resolves it, and then validates the generated attribute contexts""" expected_output_path = TestHelper.get_expected_output_folder_path(self.tests_sub_path, test_name) # type: str file_name_suffix = ProjectionTestUtils.get_resolution_option_name_suffix(res_opts) # type: str corpus = TestHelper.get_local_corpus(self.tests_sub_path, test_name) # type: CdmCorpusDefinition corpus.storage.mount('expected', LocalAdapter(expected_output_path)) # entity = await corpus.fetch_object_async('local:/{}.cdm.json/{}'.format(entity_name, entity_name)) # resolved_entity = await TestUtils._get_resolved_entity(corpus, entity, res_opts, True) entity = await corpus.fetch_object_async('local:/{0}.cdm.json/{0}'.format(entity_name)) resolved_entity = await ProjectionTestUtils.get_resolved_entity(corpus, entity, res_opts, True) await self._validate_resolved_attributes(corpus, resolved_entity, entity_name, file_name_suffix) AttributeContextUtil.validate_attribute_context(self, corpus, expected_output_path, '{}{}'.format(entity_name, file_name_suffix), resolved_entity)
async def _load_entity_for_resolution_option_and_save( self, corpus: 'CdmCorpusDefinition', test_name: str, entity_name: str, res_opts: List[str]) -> None: """Loads an entity, resolves it, and then validates the generated attribute contexts""" expected_output_path = TestHelper.get_expected_output_folder_path( self.tests_subpath, test_name) file_name_suffix = TestUtils.get_resolution_option_name_suffix( res_opts) entity = await corpus.fetch_object_async( 'local:/{}.cdm.json/{}'.format(entity_name, entity_name)) resolved_entity = await TestUtils._get_resolved_entity( corpus, entity, res_opts, True) AttributeContextUtil.validate_attribute_context( self, corpus, expected_output_path, '{}{}'.format(entity_name, file_name_suffix), resolved_entity)
async def test_entity_extends_trait(self): """Entity that extends and exhibits custom traits""" test_name = 'test_entity_extends_trait' entity_name = 'TestEntityExtendsTrait' corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name) expected_output_path = TestHelper.get_expected_output_folder_path( self.tests_subpath, test_name) manifest = await corpus.fetch_object_async( 'local:/default.manifest.cdm.json') ent_test_entity_extends_trait = await corpus.fetch_object_async( 'local:/{}.cdm.json/{}'.format(entity_name, entity_name), manifest) self.assertIsNotNone(ent_test_entity_extends_trait) resolved_test_entity_extends_trait = await TestUtils._get_resolved_entity( corpus, ent_test_entity_extends_trait, []) self.assertIsNotNone(resolved_test_entity_extends_trait) AttributeContextUtil.validate_attribute_context( self, corpus, expected_output_path, entity_name, resolved_test_entity_extends_trait) # Attribute Name self.assertEqual('TestExtendsTraitAttribute', resolved_test_entity_extends_trait.attributes[0].name) # Trait Name self.assertEqual( 'does.haveDefault', resolved_test_entity_extends_trait. attributes[0].applied_traits[3].named_reference) # Trait Name self.assertEqual( 'testTraitDerived', resolved_test_entity_extends_trait. attributes[0].applied_traits[4].named_reference) # Trait Param Name self.assertEqual( 'testTraitParam1', resolved_test_entity_extends_trait.attributes[0].applied_traits[4]. arguments[0]._resolved_parameter.name) # Trait Param Default Value self.assertEqual( 'TestTrait Param 1 DefaultValue', resolved_test_entity_extends_trait.attributes[0].applied_traits[4]. arguments[0].value)
async def test_entity_nested_projection(self): """Extends entity with a nested projection""" test_name = 'test_entity_nested_projection' entity_name = 'TestEntityNestedProjection' corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name) expected_output_path = TestHelper.get_expected_output_folder_path( self.tests_subpath, test_name) manifest = await corpus.fetch_object_async( 'local:/default.manifest.cdm.json') ent_test_entity_nested_projection = await corpus.fetch_object_async( 'local:/{}.cdm.json/{}'.format(entity_name, entity_name), manifest) self.assertIsNotNone(ent_test_entity_nested_projection) resolved_test_entity_nested_projection = await TestUtils._get_resolved_entity( corpus, ent_test_entity_nested_projection, []) self.assertIsNotNone(resolved_test_entity_nested_projection) AttributeContextUtil.validate_attribute_context( self, corpus, expected_output_path, entity_name, resolved_test_entity_nested_projection)
async def test_entity_attribute_entity_reference(self): """Entity attribute referenced with an entity reference""" test_name = 'test_entity_attribute_entity_reference' entity_name = 'TestEntityAttributeEntityReference' corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name) expected_output_path = TestHelper.get_expected_output_folder_path( self.tests_subpath, test_name) manifest = await corpus.fetch_object_async( 'local:/default.manifest.cdm.json') ent_test_entity_attribute_entity_reference = await corpus.fetch_object_async( 'local:/{}.cdm.json/{}'.format(entity_name, entity_name), manifest) self.assertIsNotNone(ent_test_entity_attribute_entity_reference) resolved_test_entity_attribute_entity_reference = await TestUtils._get_resolved_entity( corpus, ent_test_entity_attribute_entity_reference, []) self.assertIsNotNone(resolved_test_entity_attribute_entity_reference) AttributeContextUtil.validate_attribute_context( self, corpus, expected_output_path, entity_name, resolved_test_entity_attribute_entity_reference)