Best Python code snippet using PyHamcrest_python
isdict_containingentries_test.py
Source:isdict_containingentries_test.py
...12class IsDictContainingEntriesTest(MatcherTest):13 def testMatcherCreationRequiresEvenNumberOfPositionalArgs(self):14 self.assertRaises(ValueError, has_entries, 'a', 'b', 'c')15 def testDoesNotMatchNonDictionary(self):16 self.assert_does_not_match('non-dictionary',17 has_entries('a', equal_to(1)), object())18 def testMatchesDictLike(self):19 class DictLike(object):20 def __getitem__(self, key):21 return 'value: ' + str(key)22 def __contains__(self, key):23 return True24 self.assert_matches('matches a dictionary-like object',25 has_entries('a', equal_to('value: a')),26 DictLike())27 def testMatchesUsingSingleDictionaryArgument(self):28 target = {'a': 1, 'b': 2, 'c': 3}29 self.assert_matches('has a & b',30 has_entries({'a':equal_to(1), 'b':equal_to(2)}), target)31 self.assert_matches('has c & a',32 has_entries({'c':equal_to(3), 'a':equal_to(1)}), target)33 self.assert_does_not_match('no d:3',34 has_entries({'b':equal_to(2), 'd':equal_to(3)}), target)35 def testMatcheSingleDictionaryArgumentWithImplicitEqualTo(self):36 target = {'a': 1, 'b': 2, 'c': 3}37 self.assert_matches('has a & b',38 has_entries({'a':1, 'b':2}), target)39 self.assert_matches('has c & a',40 has_entries({'c':3, 'a':1}), target)41 self.assert_does_not_match('no d:3',42 has_entries({'b':2, 'd': 3}), target)43 def testMatchesUsingKwargs(self):44 target = {'a': 1, 'b': 2, 'c': 3}45 self.assert_matches('has a & b',46 has_entries(a=equal_to(1), b=equal_to(2)), target)47 self.assert_matches('has c & a',48 has_entries(c=equal_to(3), a=equal_to(1)), target)49 self.assert_does_not_match('no d:3',50 has_entries(b=equal_to(2), d=equal_to(3)), target)51 def testMatchesKwargsWithImplicitEqualTo(self):52 target = {'a': 1, 'b': 2, 'c': 3}53 self.assert_matches('has a & b',54 has_entries(a=1, b=2), target)55 self.assert_matches('has c & a',56 has_entries(c=3, a=1), target)57 self.assert_does_not_match('no d:3',58 has_entries(b=2, d=3), target)59 def testMatchesDictionaryContainingSingleKeyWithMatchingValue(self):60 target = {'a': 1, 'b': 2}61 self.assert_matches('has a:1', has_entries('a', equal_to(1)), target)62 self.assert_matches('has b:2', has_entries('b', equal_to(2)), target)63 self.assert_does_not_match('no b:3', has_entries('b', equal_to(3)), target)64 self.assert_does_not_match('no c:2', has_entries('c', equal_to(2)), target)65 def testMatchesDictionaryContainingMultipleKeysWithMatchingValues(self):66 target = {'a': 1, 'b': 2, 'c': 3}67 self.assert_matches('has a & b',68 has_entries('a', equal_to(1), 'b', equal_to(2)), target)69 self.assert_matches('has c & a',70 has_entries('c', equal_to(3), 'a', equal_to(1)), target)71 self.assert_does_not_match('no d:3',72 has_entries('b', equal_to(3), 'd', equal_to(3)), target)73 def testProvidesConvenientShortcutForMatchingWithEqualTo(self):74 target = {'a': 1, 'b': 2, 'c': 3}75 self.assert_matches('has a & b', has_entries('a', 1, 'b', 2), target)76 self.assert_matches('has c & a', has_entries('c', 3, 'a', 1), target)77 self.assert_does_not_match('no d:4', has_entries('b', 3, 'd', 4), target)78 def testHasReadableDescription(self):79 self.assert_description("a dictionary containing {'a': <1>, 'b': <2>}",80 has_entries('a', 1, 'b', 2))81 def testSuccessfulMatchDoesNotGenerateMismatchDescription(self):82 self.assert_no_mismatch_description(has_entries('a', 1), {'a': 1})83 def testMismatchDescriptionOfNonDictionaryShowsActualArgument(self):84 self.assert_mismatch_description("'bad' is not a mapping object", has_entries('a', 1), 'bad')85 def testMismatchDescriptionOfDictionaryWithoutKey(self):86 self.assert_mismatch_description("no 'b' key in <{'a': 1, 'c': 3}>",87 has_entries('a', 1, 'b', 2), {'a': 1, 'c': 3})88 def testMismatchDescriptionOfDictionaryWithNonMatchingValue(self):89 self.assert_mismatch_description("value for 'a' was <2>",90 has_entries('a', 1), {'a': 2})91 def testDescribeMismatchOfNonDictionaryShowsActualArgument(self):...
hasproperty_test.py
Source:hasproperty_test.py
...46 has_property('field'), OverridingNewStyleGetAttr())47 self.assert_matches('old-style direct',48 has_property('field'), OverridingNewStyleGetAttribute())49 def testHasPropertyWithoutValueMatcherNegative(self):50 self.assert_does_not_match('old-style direct',51 has_property('not_there'), OnePropertyOldStyle())52 self.assert_does_not_match('old-style direct',53 has_property('not_there'), OnePropertyNewStyle())54 self.assert_does_not_match('old-style direct',55 has_property('not_there'), OverridingOldStyle())56 self.assert_does_not_match('old-style direct',57 has_property('not_there'), OverridingNewStyleGetAttr())58 self.assert_does_not_match('old-style direct',59 has_property('not_there'), OverridingNewStyleGetAttribute())60 def testHasPropertyWithValueMatcher(self):61 self.assert_matches('old-style direct',62 has_property('field', 'value'), OnePropertyOldStyle())63 self.assert_matches('old-style direct',64 has_property('field', 'value'), OnePropertyNewStyle())65 self.assert_matches('old-style direct',66 has_property('field', 'value'), OverridingOldStyle())67 self.assert_matches('old-style direct',68 has_property('field', 'value'), OverridingNewStyleGetAttr())69 self.assert_matches('old-style direct',70 has_property('field', 'value'), OverridingNewStyleGetAttribute())71 def testHasPropertyWithValueMatcherNegative(self):72 self.assert_does_not_match('old-style direct',73 has_property('field', 'not the value'), OnePropertyOldStyle())74 self.assert_does_not_match('old-style direct',75 has_property('field', 'not the value'), OnePropertyNewStyle())76 self.assert_does_not_match('old-style direct',77 has_property('field', 'not the value'), OverridingOldStyle())78 self.assert_does_not_match('old-style direct',79 has_property('field', 'not the value'), OverridingNewStyleGetAttr())80 self.assert_does_not_match('old-style direct',81 has_property('field', 'not the value'), OverridingNewStyleGetAttribute())82 def testDescription(self):83 self.assert_description("an object with a property 'field' matching ANYTHING",84 has_property('field'))85 self.assert_description("an object with a property 'field' matching 'value'",86 has_property('field', 'value'))87 def testDescribeMissingProperty(self):88 self.assert_mismatch_description("<OnePropertyNewStyle> did not have the 'not_there' property",89 has_property('not_there'), OnePropertyNewStyle())90 def testDescribePropertyValueMismatch(self):91 self.assert_mismatch_description("property 'field' was 'value'",92 has_property('field', 'another_value'), OnePropertyNewStyle())93 def testMismatchDescription(self):94 self.assert_describe_mismatch("<OnePropertyNewStyle> did not have the 'not_there' property",...
test_regex_patterns.py
Source:test_regex_patterns.py
...6from cfn_policy_validator.cfn_tools import regex_patterns7class GenericArnPatternTest(unittest.TestCase):8 def assert_matches(self, arn):9 self.assertIsNotNone(regex_patterns.generic_arn_pattern.match(arn), f'{arn} does not match arn pattern.')10 def assert_does_not_match(self, arn):11 self.assertIsNone(regex_patterns.generic_arn_pattern.match(arn), f'{arn} matches arn pattern when it should not.')12 def test_matches_gov_cloud(self):13 arn = "arn:aws-us-gov:lambda:us-gov-west-1:123456789012:function:ProcessKinesisRecords"14 self.assert_matches(arn)15 def test_matches_china(self):16 arn = "arn:aws-cn:rds:cn-north-1:123456789012:db:mysql-db"17 self.assert_matches(arn)18 def test_does_not_match_invalid_partition(self):19 arn = "arn:qws:iam::123456789012:role/MyRole"20 self.assert_does_not_match(arn)21 def test_matches_arbitrary_service(self):22 arn = "arn:aws:iam::123456789012:abcdef/MyRole"23 self.assert_matches(arn)24 def test_matches_no_region(self):25 arn = "arn:aws:iam::123456789012:abcdef/MyRole"26 self.assert_matches(arn)27 def test_matches_region(self):28 arn = "arn:aws:greengrass:us-east-1:123456789012:/greengrass/groups/abc"29 self.assert_matches(arn)30 def test_matches_arbitrary_resource(self):31 arn = "arn:aws:greengrass:us-east-1:123456789012:does_not_matter"32 self.assert_matches(arn)33 def test_does_not_match_missing_arn(self):34 arn = "aws:iam::123456789012:abcdef/MyRole"35 self.assert_does_not_match(arn)36 def test_does_not_match_missing_partition(self):37 arn = "arn:iam::123456789012:abcdef/MyRole"38 self.assert_does_not_match(arn)39 def test_does_not_match_missing_region(self):40 arn = "arn:aws:iam:123456789012:abcdef/MyRole"41 self.assert_does_not_match(arn)42 def test_does_not_match_missing_account_id(self):43 arn = "arn:aws:iam::abcdef/MyRole"44 self.assert_does_not_match(arn)45 def test_does_not_match_missing_resource_path(self):46 arn = "arn:aws:iam::123456789012"47 self.assert_does_not_match(arn)48 def test_captures_group(self):49 arn = "arn:aws:iam::123456789012:abcdef/MyRole"50 match = regex_patterns.generic_arn_pattern.match(arn)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!