From 0f6ae9621aacb2bc38a56993e1a9d832042d26d7 Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Fri, 6 Dec 2013 16:58:57 +0100
Subject: [PATCH] Update unit tests for JSON services

---
 tests/test_services_json.py | 104 +++++++++++-------------------------
 1 file changed, 32 insertions(+), 72 deletions(-)

diff --git a/tests/test_services_json.py b/tests/test_services_json.py
index 67c82785..5be1ba16 100644
--- a/tests/test_services_json.py
+++ b/tests/test_services_json.py
@@ -1,110 +1,70 @@
 """
-Tests for the SOAP interface to Mutalyzer.
+Tests for the JSON interface to Mutalyzer.
 """
 
 
-from urllib import urlencode
-import simplejson as json
-import requests
 from nose.tools import *
+import simplejson as json
+from spyne.server.null import NullServer
 import mutalyzer
-
-
-SERVICE_ROOT = 'http://localhost/mutalyzer/json/'
-
-
-def call(method, **kwargs):
-    """
-    Do a HTTP/RPC request and decode the JSON response.
-    """
-    r = requests.get(SERVICE_ROOT + method + '?' + urlencode(kwargs))
-    return json.loads(r.content)
+from mutalyzer.services.json import application
 
 
 class TestServicesJson():
     """
     Test the Mutalyzer HTTP/RPC+JSON interface.
     """
+    def setUp(self):
+        """
+        Initialize test server.
+        """
+        self.server = NullServer(application, ostr=True)
+
+    def _call(self, method, *args, **kwargs):
+        r = getattr(self.server.service, method)(*args, **kwargs)
+        return json.loads(''.join(r))
+
     def test_checksyntax_valid(self):
         """
         Running checkSyntax with a valid variant name should return True.
         """
-        r = call('checkSyntax', variant='AB026906.1:c.274G>T')
-        assert_equal(r['checkSyntaxResponse']['checkSyntaxResult']['valid'], True)
+        r = self._call('checkSyntax', variant='AB026906.1:c.274G>T')
+        assert_equal(r, {'valid': True, 'messages': []})
 
     def test_checksyntax_invalid(self):
         """
         Running checkSyntax with an invalid variant name should return False
         and give at least one error message.
         """
-        r = call('checkSyntax', variant='0:abcd')
-        assert_equal(r['checkSyntaxResponse']['checkSyntaxResult']['valid'], False)
-        assert len(r['checkSyntaxResponse']['checkSyntaxResult']['messages']['SoapMessage']) >= 1
+        r = self._call('checkSyntax', variant='0:abcd')
+        assert_equal(r['valid'], False)
+        assert len(r['messages']) >= 1
 
     def test_checksyntax_empty(self):
         """
         Running checkSyntax with no variant name should raise exception.
         """
-        r = call('checkSyntax')
-        assert r['Fault']
+        # The validator doesn't work with NullServer, so we cannot do this
+        # test. See https://github.com/arskom/spyne/issues/318
+        #r = self._call('checkSyntax')
+        #assert_equal(r['faultcode'], 'Client.ValidationError')
+        pass
 
     def test_transcriptinfo_valid(self):
         """
         Running transcriptInfo with valid arguments should get us a Transcript
         object.
         """
-        r = call('transcriptInfo', LOVD_ver='123', build='hg19',
-                 accNo='NM_002001.2')
-        assert_equal(r['transcriptInfoResponse']['transcriptInfoResult']['trans_start'], -99)
-        assert_equal(r['transcriptInfoResponse']['transcriptInfoResult']['trans_stop'], 1066)
-        assert_equal(r['transcriptInfoResponse']['transcriptInfoResult']['CDS_stop'], 774)
+        r = self._call('transcriptInfo', LOVD_ver='123', build='hg19',
+                       accNo='NM_002001.2')
+        assert_equal(r['trans_start'], -99)
+        assert_equal(r['trans_stop'], 1066)
+        assert_equal(r['CDS_stop'], 774)
 
     def test_info(self):
         """
         Running the info method should give us some version information.
         """
-        r = call('info')
-        assert_equal(type(r['infoResponse']['infoResult']['versionParts']['string']), list)
-        assert_equal(r['infoResponse']['infoResult']['version'], mutalyzer.__version__)
-
-    def test_method_get(self):
-        """
-        Using a GET request.
-        """
-        r = requests.get(SERVICE_ROOT + 'checkSyntax', params={'variant': 'AB026906.1:c.274G>T'})
-        assert_equal(json.loads(r.content)['checkSyntaxResponse']['checkSyntaxResult']['valid'], True)
-
-    def test_method_get_large(self):
-        """
-        Using a GET request with too large data.
-        """
-        r = requests.get(SERVICE_ROOT + 'checkSyntax', params={'variant': 'AB026906.1:c.274G>T', 'dummy': 'a' * 1000000})
-        assert '414 Request-URI Too Large' in r.content
-
-    def test_method_post_query_string(self):
-        """
-        Using a POST request with query string parameters.
-        """
-        r = requests.post(SERVICE_ROOT + 'checkSyntax', params={'variant': 'AB026906.1:c.274G>T'})
-        assert_equal(json.loads(r.content)['checkSyntaxResponse']['checkSyntaxResult']['valid'], True)
-
-    def test_method_post_query_string_large(self):
-        """
-        Using a POST request with too large data in body.
-        """
-        r = requests.post(SERVICE_ROOT + 'checkSyntax', params={'variant': 'AB026906.1:c.274G>T', 'dummy': 'a' * 1000000})
-        assert '414 Request-URI Too Large' in r.content
-
-    def test_method_post_body(self):
-        """
-        Using a POST request with data in body.
-        """
-        r = requests.post(SERVICE_ROOT + 'checkSyntax', data={'variant': 'AB026906.1:c.274G>T'})
-        assert_equal(json.loads(r.content)['checkSyntaxResponse']['checkSyntaxResult']['valid'], True)
-
-    def test_method_post_body_large(self):
-        """
-        Using a POST request with data in body.
-        """
-        r = requests.post(SERVICE_ROOT + 'checkSyntax', data={'variant': 'AB026906.1:c.274G>T', 'dummy': 'a' * 1000000})
-        assert_equal(json.loads(r.content)['checkSyntaxResponse']['checkSyntaxResult']['valid'], True)
+        r = self._call('info')
+        assert_equal(type(r['versionParts']), list)
+        assert_equal(r['version'], mutalyzer.__version__)
-- 
GitLab