def test_nested_matchers(self): matcher = EachLike({ 'username': Term('[a-z]+', 'user'), 'id': SomethingLike(123) }) generate = matcher.generate() self.assertEqual( generate, { 'json_class': 'Pact::ArrayLike', 'contents': { 'username': { 'json_class': 'Pact::Term', 'data': { 'matcher': { 'json_class': 'Regexp', 's': '[a-z]+', 'o': 0 }, 'generate': 'user' } }, 'id': { 'json_class': 'Pact::SomethingLike', 'contents': 123 } }, 'min': 1 })
def test_basic_type(self): generate = SomethingLike(123).generate() self.assertEqual(generate, { 'json_class': 'Pact::SomethingLike', 'contents': 123 })
def test_nested(self): request = [ EachLike({ 'username': Term('[a-zA-Z]+', 'firstlast'), 'id': SomethingLike(123) }) ] self.assertEqual(from_term(request), [{ 'contents': { 'id': { 'contents': 123, 'json_class': 'Pact::SomethingLike' }, 'username': { 'data': { 'generate': 'firstlast', 'matcher': { 'json_class': 'Regexp', 'o': 0, 's': '[a-zA-Z]+' } }, 'json_class': 'Pact::Term' } }, 'json_class': 'Pact::ArrayLike', 'min': 1 }])
def test_nested(self): input = [ EachLike({ 'username': Term('[a-zA-Z]+', 'firstlast'), 'id': SomethingLike(123)})] self.assertEqual( get_generated_values(input), [[{'username': '******', 'id': 123}]])
def test_complex_type(self): generate = SomethingLike({'name': Term('.+', 'admin')}).generate() self.assertEqual( generate, {'json_class': 'Pact::SomethingLike', 'contents': {'name': { 'json_class': 'Pact::Term', 'data': { 'matcher': { 'json_class': 'Regexp', 's': '.+', 'o': 0 }, 'generate': 'admin' } }}})
def test_invalid_types(self): with self.assertRaises(AssertionError) as e: SomethingLike(set()) self.assertIn('matcher must be one of ', str(e.exception))
def test_valid_types(self): types = [None, list(), dict(), 1, 1.0, 'string', u'unicode', Matcher()] for t in types: SomethingLike(t)
def test_something_like(self): self.assertEqual(get_generated_values(SomethingLike(123)), 123)
def test_something_like(self): self.assertEqual(from_term(SomethingLike(123)), { 'json_class': 'Pact::SomethingLike', 'contents': 123 })