def test_WHEN_given_validator_fails_post_setup_THEN_main_SHOULD_report_hard_error(self): # ARRANGE # environment = fake_post_sds_environment() the_error_message = 'the error message' assertion_part = PartForValidation(SdvValidatorThat( post_setup_return_value=asrt_text_doc.new_single_string_text_for_test(the_error_message)) ) instruction = sut.AssertionInstructionFromAssertionPart(assertion_part, lambda env: 'argument to assertion_part') is_validation_success = asrt_svh.is_success() main_result_is_hard_error = asrt_pfh.is_hard_error( asrt_text_doc.is_single_pre_formatted_text_that_equals(the_error_message) ) # ACT & ASSERT # pre_sds_result = instruction.validate_pre_sds(environment) is_validation_success.apply_with_message(self, pre_sds_result, 'pre sds validation should succeed') post_sds_result = instruction.validate_post_setup(environment) is_validation_success.apply_with_message(self, post_sds_result, 'post setup validation should succeed') main_result = instruction.main(environment, optionally_from_proc_exe_settings(None, environment.proc_exe_settings), self.the_os_services) main_result_is_hard_error.apply_with_message(self, main_result, 'main should fail')
def _check_cases_with_non_empty_file( self, get_assertion_part_function: Callable[[ExpectationType, LineMatcherResolver], FileContentsAssertionPart], actual_file_contents: str, matcher_cases: list): environment = fake_post_sds_environment() checked_file_describer = FilePropertyDescriptorConstructorTestImpl() os_services = new_default() with destination_file_path_getter_that_gives_seq_of_unique_paths() as dst_file_path_getter: # This test is expected to not create files using the above object, # but to be sure, one is used that creates and destroys temporary files. with tmp_file_containing(actual_file_contents) as actual_file_path: for case in matcher_cases: for expectation_type in ExpectationType: with self.subTest(case=case.name, expectation_type=expectation_type): ftc = FileToCheck(actual_file_path, checked_file_describer, environment.phase_logging.space_for_instruction(), IdentityStringTransformer(), dst_file_path_getter) matcher_resolver = LineMatcherConstantResolver(case.matcher) assertion_part = get_assertion_part_function(expectation_type, matcher_resolver) # ACT # actual = assertion_part.check_and_return_pfh(environment, os_services, 'custom env', ftc) # ASSERT # pfh_assertion = pfh_expectation_type_config( expectation_type).main_result(case.expected_result_for_positive_expectation) pfh_assertion.apply_without_message(self, actual)
def _check_cases_for_no_lines( self, get_assertion_part_function: Callable[ [ExpectationType, LineMatcherSdv], AssertionPart[StringSource, pfh.PassOrFailOrHardError]], expected_result_when_positive_expectation: PassOrFail): empty_file_contents = '' environment = fake_post_sds_environment() os_services = new_for_current_os() matchers = [ ('unconditionally true', MatcherWithConstantResult(True)), ('unconditionally false', MatcherWithConstantResult(False)), ] with string_source_factory() as source_factory: # This test is expected to not create files using the above object, # but to be sure, one is used that creates and destroys temporary files. with tmp_file_containing(empty_file_contents) as actual_file_path: for expectation_type in ExpectationType: for matcher_name, matcher in matchers: with self.subTest(expectation_type=expectation_type, matcher_name=matcher_name): model = source_factory.of_file__poorly_described( actual_file_path) matcher_sdv = sdv_components.matcher_sdv_from_constant_primitive( matcher) assertion_part = get_assertion_part_function( expectation_type, matcher_sdv) # ACT # actual = assertion_part.check_and_return_pfh( environment, os_services, model) # ASSERT # pfh_assertion = pfh_expectation_type_config( expectation_type).main_result( expected_result_when_positive_expectation) pfh_assertion.apply_without_message(self, actual)
class TestIsExistingRegularFileAssertionPart(unittest.TestCase): the_os_services = oss.new_for_current_os() environment = fake_post_sds_environment() def test_model_is_returned_WHEN_file_is_existing_regular_file(self): # ARRANGE # assertion_part = sut.IsExistingRegularFileAssertionPart() existing_regular_file = File.empty('regular.txt') with tmp_dir(DirContents([existing_regular_file])) as path_of_existing_directory: path_of_existing_regular_file = path_of_existing_directory / existing_regular_file.name path_ddv = path_ddvs.absolute_path(path_of_existing_regular_file) path = path_ddv.value_of_any_dependency__d(fake_tcds()) model = sut.ComparisonActualFile(path, True) # ACT # actual = assertion_part.check(self.environment, self.the_os_services, model) # ASSERT # self.assertIs(model, actual) def test_PfhHardError_SHOULD_be_raised_WHEN_file_does_not_exist(self): # ARRANGE # assertion_part = sut.IsExistingRegularFileAssertionPart() # ACT & ASSERT # with self.assertRaises(PfhHardErrorException): path = pathlib.Path('a file that does not exist') assertion_part.check(self.environment, self.the_os_services, sut.ComparisonActualFile( described_path.new_primitive(path), True, )) def test_PfhHardError_SHOULD_be_raised_WHEN_file_does_exist_but_is_not_a_regular_file(self): # ARRANGE # assertion_part = sut.IsExistingRegularFileAssertionPart() # ACT & ASSERT # with tmp_dir() as path_of_existing_directory: with self.assertRaises(PfhHardErrorException): assertion_part.check(self.environment, self.the_os_services, sut.ComparisonActualFile( described_path.new_primitive(path_of_existing_directory), True, ) ) def test_no_exception_SHOULD_be_not_raised_WHEN_file_does_not_exist_but_file_does_not_need_to_be_verified(self): # ARRANGE # assertion_part = sut.IsExistingRegularFileAssertionPart() # ACT & ASSERT # path = pathlib.Path('a file that does not exist') assertion_part.check(self.environment, self.the_os_services, sut.ComparisonActualFile( described_path.new_primitive(path), False, ))
def _check_cases_with_non_empty_file(self, get_assertion_part_function: Callable[ [ExpectationType, LineMatcherSdv], FileContentsAssertionPart], actual_file_contents: str, matcher_cases: Sequence[Case]): environment = fake_post_sds_environment() os_services = new_for_current_os() with string_source_factory() as source_factory: # This test is expected to not create files using the above object, # but to be sure, one is used that creates and destroys temporary files. with tmp_file_containing(actual_file_contents) as actual_file_path: for case in matcher_cases: for expectation_type in ExpectationType: with self.subTest(case=case.name, expectation_type=expectation_type): model = source_factory.of_file__poorly_described( actual_file_path) matcher_sdv = sdv_components.matcher_sdv_from_constant_primitive( case.matcher) assertion_part = get_assertion_part_function( expectation_type, matcher_sdv) # ACT # actual = assertion_part.check_and_return_pfh( environment, os_services, model) # ASSERT # pfh_assertion = pfh_expectation_type_config( expectation_type).main_result( case. expected_result_for_positive_expectation) pfh_assertion.apply_without_message(self, actual)
def test_hard_error(self): the_instruction_environment = instruction_environment.fake_post_sds_environment() the_os_services = os_services.new_default() cases = [ ( 'hard error exception in left operand', asrt_pfh.is_hard_error(asrt.equals('error error in left op')), operand_resolver_that( resolve_return_value_action=actions.do_raise(PfhHardErrorException('error error in left op'))), operand_resolver_that(), ), ( 'hard error exception in right operand', asrt_pfh.is_hard_error(asrt.equals('error error in right op')), operand_resolver_that(), operand_resolver_that( resolve_return_value_action=actions.do_raise(PfhHardErrorException('error error in right op'))), ), ] for name, result_assertion, l_op, r_op in cases: instruction_to_check = sut.Instruction(cmp_setup(l_op, r_op)) with self.subTest(name=name): # ACT # actual = instruction_to_check.main(the_instruction_environment, the_os_services) # ASSERT # result_assertion.apply_without_message(self, actual)
class TestAssertionPart(unittest.TestCase): the_os_services = oss.new_for_current_os() environment = fake_post_sds_environment() def test_return_pfh_pass_WHEN_no_exception_is_raised(self): # ARRANGE # assertion_part_that_not_raises = SuccessfulPartThatReturnsConstructorArgPlusOne() # ACT # actual = assertion_part_that_not_raises.check_and_return_pfh(self.environment, self.the_os_services, 1) # ASSERT # assertion = asrt_pfh.is_pass() assertion.apply_without_message(self, actual) def test_return_pfh_fail_WHEN_PfhFailException_is_raised(self): # ARRANGE # assertion_part_that_raises = PartThatRaisesFailureExceptionIfArgumentIsEqualToOne() # ACT # actual = assertion_part_that_raises.check_and_return_pfh(self.environment, self.the_os_services, 1) # ASSERT # assertion = asrt_pfh.is_fail( is_string_for_test( asrt.equals(PartThatRaisesFailureExceptionIfArgumentIsEqualToOne.ERROR_MESSAGE)) ) assertion.apply_without_message(self, actual)
def _check_cases_for_no_lines(self, get_assertion_part_function, expected_result_when_positive_expectation: PassOrFail): empty_file_contents = '' environment = fake_post_sds_environment() checked_file_describer = FilePropertyDescriptorConstructorTestImpl() os_services = new_default() matchers = [ ('unconditionally true', LineMatcherConstant(True)), ('unconditionally false', LineMatcherConstant(False)), ] with destination_file_path_getter_that_gives_seq_of_unique_paths() as dst_file_path_getter: # This test is expected to not create files using the above object, # but to be sure, one is used that creates and destroys temporary files. with tmp_file_containing(empty_file_contents) as actual_file_path: for expectation_type in ExpectationType: for matcher_name, matcher in matchers: with self.subTest(expectation_type=expectation_type, matcher_name=matcher_name): ftc = FileToCheck(actual_file_path, checked_file_describer, environment.phase_logging.space_for_instruction(), IdentityStringTransformer(), dst_file_path_getter) matcher_resolver = LineMatcherConstantResolver(matcher) assertion_part = get_assertion_part_function(expectation_type, matcher_resolver) # ACT # actual = assertion_part.check_and_return_pfh(environment, os_services, 'custom env', ftc) # ASSERT # pfh_assertion = pfh_expectation_type_config( expectation_type).main_result(expected_result_when_positive_expectation) pfh_assertion.apply_without_message(self, actual)
def test_evaluation_of_comparison(self): the_instruction_environment = instruction_environment.fake_post_sds_environment() the_os_services = os_services.new_default() cases = [ ( 'expectation type is positive: pass WHEN comparison succeeds', asrt_pfh.is_pass(), ExpectationType.POSITIVE, 1, comparators.LT, 2, ), ( 'expectation type is positive: fail WHEN comparison does not succeeds', asrt_pfh.is_fail(ASSERT_ERROR_MESSAGE_IS_A_STRING), ExpectationType.POSITIVE, 1, comparators.LT, 1, ), ( 'expectation type is negative: fail WHEN comparison succeeds', asrt_pfh.is_fail(ASSERT_ERROR_MESSAGE_IS_A_STRING), ExpectationType.NEGATIVE, 1, comparators.LT, 2, ), ( 'expectation type is negative: pass WHEN comparison does not succeeds', asrt_pfh.is_pass(), ExpectationType.NEGATIVE, 1, comparators.LT, 1, ), ] for name, result_assertion, expectation_type, l_op, op, r_op in cases: # ARRANGE # instruction_to_check = sut.Instruction( ComparisonHandler(THE_PROPERTY_DESCRIPTOR, expectation_type, operand_resolver_that( resolve_return_value_action=do_return(l_op)), op, operand_resolver_that( resolve_return_value_action=do_return(r_op)))) with self.subTest(name=name): # ACT # actual = instruction_to_check.main(the_instruction_environment, the_os_services) # ASSERT # result_assertion.apply_without_message(self, actual)
def test(self): # ARRANGE # cases = [ Case('empty input should give empty list of models', file_contents='', expected=[] ), Case('single line without trailing newline', file_contents='a', expected=['a'] ), Case('single line with trailing newline', file_contents='a\n', expected=['a\n'] ), Case('multiple lines', file_contents='a\nb\n', expected=['a\n', 'b\n', ] ), ] line_trans_cases = [ ('identity', IdentityStringTransformer(), lambda x: x), ('to-upper', ToUppercaseStringTransformer(), str.upper), ] environment = fake_post_sds_environment() checked_file_describer = FilePropertyDescriptorConstructorTestImpl() with destination_file_path_getter_that_gives_seq_of_unique_paths() as dst_file_path_getter: # This test is expected to not create files using the above object, # but to be sure, one is used that creates and destroys temporary files. for case in cases: with tmp_file_containing(case.file_contents) as actual_file_path: for trans_name, lines_trans, corresponding_expected_trans in line_trans_cases: with self.subTest(case=case.name, trans=trans_name): ftc = FileToCheck(actual_file_path, checked_file_describer, environment.phase_logging.space_for_instruction(), lines_trans, dst_file_path_getter) # ACT # with ftc.lines() as lines: # ASSERT # actual_list = list(lines) expected_adapted_to_transformer = list(map(corresponding_expected_trans, case.expected)) self.assertEqual(expected_adapted_to_transformer, actual_list)
def test_return_pfh_pass_WHEN_no_exception_is_raised(self): # ARRANGE # environment = fake_post_sds_environment() assertion_part_that_not_raises = SuccessfulPartThatReturnsConstructorArgPlusOne() instruction = sut.AssertionInstructionFromAssertionPart(assertion_part_that_not_raises, lambda env: 0) # ACT # actual = instruction.main(environment, optionally_from_proc_exe_settings(None, environment.proc_exe_settings), self.the_os_services) # ASSERT # assertion = asrt_pfh.is_pass() assertion.apply_without_message(self, actual)
def test_WHEN_given_validator_fails_pre_sds_THEN_validation_SHOULD_fail_pre_sds(self): # ARRANGE # environment = fake_post_sds_environment() the_error_message = 'the error message' assertion_part = PartForValidation(SdvValidatorThat( pre_sds_return_value=asrt_text_doc.new_single_string_text_for_test(the_error_message)) ) instruction = sut.AssertionInstructionFromAssertionPart(assertion_part, lambda env: 'argument to assertion_part') # ACT # actual = instruction.validate_pre_sds(environment) # ASSERT # assertion = asrt_svh.is_validation_error( asrt_text_doc.is_single_pre_formatted_text_that_equals(the_error_message) ) assertion.apply_without_message(self, actual)
def test_return_pfh_fail_WHEN_PfhFailException_is_raised(self): # ARRANGE # environment = fake_post_sds_environment() assertion_part_that_raises = PartThatRaisesFailureExceptionIfArgumentIsEqualToOne() instruction = sut.AssertionInstructionFromAssertionPart(assertion_part_that_raises, lambda env: 1) # ACT # actual = instruction.main(environment, optionally_from_proc_exe_settings(None, environment.proc_exe_settings), self.the_os_services) # ASSERT # assertion = asrt_pfh.is_fail( asrt_text_doc.is_string_for_test( asrt.equals(PartThatRaisesFailureExceptionIfArgumentIsEqualToOne.ERROR_MESSAGE)) ) assertion.apply_without_message(self, actual)
def test_WHEN_a_successful_validator_is_given_THEN_validation_SHOULD_succeed(self): # ARRANGE # environment = fake_post_sds_environment() assertion_part_without_validation = PartForValidation(ConstantSuccessSdvValidator()) instruction = sut.AssertionInstructionFromAssertionPart(assertion_part_without_validation, lambda env: 'argument to assertion_part') with self.subTest(name='pre sds validation'): # ACT # actual = instruction.validate_pre_sds(environment) # ASSERT # asrt_svh.is_success().apply_without_message(self, actual) with self.subTest(name='post setup validation'): # ACT # actual = instruction.validate_post_setup(environment) # ASSERT # asrt_svh.is_success().apply_without_message(self, actual)
class _Assertion(AssertionBase[SetupSettingsHandler]): FAKE_TCDS = fake_tcds() ENVIRONMENT = instruction_environment.fake_post_sds_environment() OS_SERVICES = OsServicesThatRaises() def __init__(self, settings_builder: Assertion[SettingsBuilderAssertionModel], atc_exe_input: Assertion[AdvWvAssertionModel[AtcExecutionInput]], ): self._settings_builder = settings_builder self._atc_exe_input = atc_exe_input def _apply(self, put: unittest.TestCase, value: SetupSettingsHandler, message_builder: MessageBuilder, ): settings_builder_model = asrt_settings_builder.SettingsBuilderAssertionModel( value.builder, self.ENVIRONMENT, self.OS_SERVICES, ) self._settings_builder.apply( put, settings_builder_model, message_builder.for_sub_component('settings builder'), ) aei_model = AdvWvAssertionModel( ApplicationEnvironment(self.OS_SERVICES, self.ENVIRONMENT.proc_exe_settings, self.ENVIRONMENT.tmp_dir__path_access.paths_access, self.ENVIRONMENT.mem_buff_size, ), value.as_atc_execution_input(), ) self._atc_exe_input.apply( put, aei_model, message_builder.for_sub_component('act execution info'), )
def test_argument_getter_SHOULD_be_given_environment_as_argument(self): # ARRANGE # environment = fake_post_sds_environment() assertion_part = PartThatRaisesFailureExceptionIfArgumentIsEqualToOne() def argument_getter_that_depends_on_environment(env: InstructionEnvironmentForPostSdsStep) -> int: return 1 if env is environment else 0 instruction = sut.AssertionInstructionFromAssertionPart(assertion_part, argument_getter_that_depends_on_environment) # ACT # actual = instruction.main(environment, optionally_from_proc_exe_settings(None, environment.proc_exe_settings), self.the_os_services) # ASSERT # assertion = asrt_pfh.is_fail( asrt_text_doc.is_string_for_test( asrt.equals(PartThatRaisesFailureExceptionIfArgumentIsEqualToOne.ERROR_MESSAGE) ) ) assertion.apply_without_message(self, actual)
class TestSequence(unittest.TestCase): the_os_services = oss.new_for_current_os() environment = fake_post_sds_environment() def test_WHEN_list_of_assertion_parts_is_empty_THEN_no_exception_SHOULD_be_raised(self): # ARRANGE # assertion_part = sut.SequenceOfCooperativeAssertionParts([]) # ACT & ASSERT # assertion_part.check(self.environment, self.the_os_services, 'arg that is not used') def test_WHEN_single_successful_assertion_part_THEN_that_assertion_part_should_have_been_executed(self): # ARRANGE # assertion_part = sut.SequenceOfCooperativeAssertionParts([SuccessfulPartThatReturnsConstructorArgPlusOne()]) # ACT # actual = assertion_part.check(self.environment, self.the_os_services, 0) # ASSERT # self.assertEqual(1, actual, 'one assertion_part should have been executed') def test_WHEN_multiple_successful_assertion_parts_THEN_that_all_assertion_parts_should_have_been_executed(self): # ARRANGE # assertion_part = sut.SequenceOfCooperativeAssertionParts([SuccessfulPartThatReturnsConstructorArgPlusOne(), SuccessfulPartThatReturnsConstructorArgPlusOne()]) # ACT # actual = assertion_part.check(self.environment, self.the_os_services, 0) # ASSERT # self.assertEqual(2, actual, 'two assertion_part should have been executed') def test_WHEN_a_failing_assertion_part_SHOULD_stop_execution_and_report_the_failure(self): # ARRANGE # assertion_part = sut.SequenceOfCooperativeAssertionParts([ SuccessfulPartThatReturnsConstructorArgPlusOne(), PartThatRaisesFailureExceptionIfArgumentIsEqualToOne(), SuccessfulPartThatReturnsConstructorArgPlusOne(), ]) # ACT & ASSERT # with self.assertRaises(pfh_exception.PfhFailException) as cm: assertion_part.check(self.environment, self.the_os_services, 0) assertion = asrt_text_doc.is_single_pre_formatted_text_that_equals( PartThatRaisesFailureExceptionIfArgumentIsEqualToOne.ERROR_MESSAGE ) assertion.apply_with_message( self, cm.exception.err_msg, 'error message from failing assertion_part should appear in PFH exception' ) def test_references_from_assertion_part_SHOULD_be_reported(self): # ARRANGE # ref_1_info = NameAndValue('ref 1', ValueType.FILE_MATCHER) ref_1 = SymbolReference(ref_1_info.name, ValueTypeRestriction.of_single(ref_1_info.value)) expected_references = asrt.matches_sequence([ asrt_sym_usage.matches_reference(asrt.equals(ref_1_info.name), is_reference_restrictions__value_type__single(ref_1_info.value)), ]) assertion_part_with_references = PartWithReference([ref_1]) instruction = sut.AssertionInstructionFromAssertionPart(assertion_part_with_references, lambda env: 'not used in this test') # ACT # actual = instruction.symbol_usages() # ASSERT # expected_references.apply_without_message(self, actual) def test_WHEN_validation_of_every_assertion_part_is_successful_THEN_validation_SHOULD_succeed(self): # ARRANGE # cases = [ NameAndValue( 'empty list of assertion_parts', sut.SequenceOfCooperativeAssertionParts([]) ), NameAndValue( 'validation of every assertion_part is successful', sut.SequenceOfCooperativeAssertionParts([PartForValidation(SdvValidatorThat()), PartForValidation(SdvValidatorThat())]) ), ] validation_environment = self.environment.path_resolving_environment_pre_or_post_sds for case in cases: with self.subTest(case=case.name): sequence_part = case.value with self.subTest(name='pre sds validation'): # ACT # actual = sequence_part.validator.validate_pre_sds_if_applicable(validation_environment) # ASSERT # self.assertIsNone(actual) with self.subTest(name='post setup validation'): # ACT # actual = sequence_part.validator.validate_post_sds_if_applicable(validation_environment) # ASSERT # self.assertIsNone(actual)