Repository: cdent/gabbi

Issue:
regex matching in JSONResponseHandler doesn't deal with numbers
If the match is a number, the regex matching errors out. We need to cast to a string before doing the regex test.

Comments:

Patch:
diff --git a/gabbi/handlers.py b/gabbi/handlers.py
index e4ed7f5..5302f3e 100644
--- a/gabbi/handlers.py
+++ b/gabbi/handlers.py
@@ -110,6 +110,8 @@ class JSONResponseHandler(ResponseHandler):
         if (hasattr(expected, 'startswith') and expected.startswith('/')
                 and expected.endswith('/')):
             expected = expected.strip('/').rstrip('/')
+            # match may be a number so stringify
+            match = str(match)
             test.assertRegexpMatches(
                 match, expected,
                 'Expect jsonpath %s to match /%s/, got %s' %


Test patch:
diff --git a/gabbi/tests/__init__.py b/gabbi/tests/__init__.py
index e69de29..d17f718 100644
--- a/gabbi/tests/__init__.py
+++ b/gabbi/tests/__init__.py
@@ -0,0 +1,16 @@
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import six
+
+six.add_move(six.MovedModule('mock', 'mock', 'unittest.mock'))
diff --git a/gabbi/tests/gabbits_intercept/regex.yaml b/gabbi/tests/gabbits_intercept/regex.yaml
index 4416be9..9a0c055 100644
--- a/gabbi/tests/gabbits_intercept/regex.yaml
+++ b/gabbi/tests/gabbits_intercept/regex.yaml
@@ -14,6 +14,8 @@ tests:
   data:
     alpha: cow
     beta: pig
+    gamma: 1
   response_json_paths:
     $.alpha: /ow$/
     $.beta: /(?!cow).*/
+    $.gamma: /\d+/
diff --git a/gabbi/tests/test_fixtures.py b/gabbi/tests/test_fixtures.py
index 19a8747..6e75a53 100644
--- a/gabbi/tests/test_fixtures.py
+++ b/gabbi/tests/test_fixtures.py
@@ -13,7 +13,7 @@
 """Use mocks to confirm that fixtures operate as context managers.
 """
 
-import mock
+from six.moves import mock
 import unittest
 
 from gabbi import fixture
diff --git a/gabbi/tests/test_handlers.py b/gabbi/tests/test_handlers.py
index 67bd30c..3f8b72f 100644
--- a/gabbi/tests/test_handlers.py
+++ b/gabbi/tests/test_handlers.py
@@ -128,6 +128,34 @@ class HandlersTest(unittest.TestCase):
         with self.assertRaises(AssertionError):
             self._assert_handler(handler)
 
+    def test_response_json_paths_regex(self):
+        handler = handlers.JSONResponseHandler(self.test_class)
+        self.test.content_type = "application/json"
+        self.test.test_data = {'response_json_paths': {
+            '$.objects[0].name': '/ow/',
+        }}
+        self.test.json_data = {
+            'objects': [{'name': 'cow',
+                         'location': 'barn'},
+                        {'name': 'chris',
+                         'location': 'house'}]
+        }
+        self._assert_handler(handler)
+
+    def test_response_json_paths_regex_number(self):
+        handler = handlers.JSONResponseHandler(self.test_class)
+        self.test.content_type = "application/json"
+        self.test.test_data = {'response_json_paths': {
+            '$.objects[0].name': '/\d+/',
+        }}
+        self.test.json_data = {
+            'objects': [{'name': 99,
+                         'location': 'barn'},
+                        {'name': 'chris',
+                         'location': 'house'}]
+        }
+        self._assert_handler(handler)
+
     def test_response_headers(self):
         handler = handlers.HeadersResponseHandler(self.test_class)
         self.test.response = {'content-type': 'text/plain'}
diff --git a/test-requirements.txt b/test-requirements.txt
index c6439fa..3dd01e6 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -1,4 +1,4 @@
-mock
+mock ; python_version < '3.3'
 testrepository
 coverage
 hacking