tests.py for Django example #211

Konjkov opened this Issue Dec 6, 2012 · 12 comments


None yet

3 participants


I write example test.py for Django example project.
To make test successful You have to create stub models.py file, set django.db.backends to anything non default, put my code into tests.py. Run ./manage.py test core.

"""The Django test client transport."""

from django.test.client import Client

from spyne.client import Service
from spyne.client import ClientBase
from spyne.client import RemoteProcedureBase

class _RemoteProcedure(RemoteProcedureBase):
    def __call__(self, *args, **kwargs):
        # there's no point in having a client making the same request more than
        # once, so if there's more than just one context, it is a bug.
        # the comma-in-assignment trick is a general way of getting the first
        # and the only variable from an iterable. so if there's more than one
        # element in the iterable, it'll fail miserably.
        self.ctx, = self.contexts

        # sets ctx.out_object
        self.get_out_object(self.ctx, args, kwargs)

        # sets ctx.out_string

        out_string = ''.join(self.ctx.out_string)
        # Hack
        client = Client()
        response = client.post(self.url, content_type='text/xml', data=out_string)
        code = response.status_code
        self.ctx.in_string = [response.content]

        # this sets ctx.in_error if there's an error, and ctx.in_object if
        # there's none.

        if not (self.ctx.in_error is None):
            raise self.ctx.in_error
        elif code >= 400:
            raise self.ctx.in_error
            return self.ctx.in_object

class DjangoClient(ClientBase):
    def __init__(self, url, app):
        super(DjangoClient, self).__init__(url, app)

        self.service = Service(_RemoteProcedure, url, app)

from django.test import TransactionTestCase
from views import hello_world_service

class SpyneTestCase(TransactionTestCase):
    def setUp(self):
        self.client = DjangoClient('/hello_world/', hello_world_service.app)

    def test_say_hello(self):
        resp =  self.client.service.say_hello('Joe',5)
        list_resp = list(resp)
        self.assertEqual(len(list_resp), 5)
        self.assertEqual(list_resp,['Hello, Joe']*5)
arskom member


Thanks a lot, but I don't see what you're testing here. Mayber it's me, could you be a little more specific by pointing to the blocks of code that you're actually testing?




Django have the test client
it can make GET, POST, PUT, DELETE... requests to Django test server.
When I use spyne service as DjangoApplication, I want to test it like other Django applications, so I need special client for testing soap request in Django (over http transport).
class DjangoClient(ClientBase) is this client and it tests simple soap django application hello_world in this example.

arskom member

Okay, but you should refactor this test to start its own server. As you may know, Spyne tests run unsupervised in travis-ci.org

arskom member

oh, and the test client should actually go inside client.django module. I'll take care of that.

arskom member

On second thought, I don't think it should. It's from django.test so I'd think it actually belongs in spyne.test.

arskom member

And on third thought, I'll put it back in spyne.client but under the name of DjangoTestClient :)

@plq plq added a commit to plq/spyne that referenced this issue Dec 11, 2012
@plq plq initial commit for a muted django test. Issue #211 59b6f87

Thanks Arslan. This test was writing as example, how to test spyne server in Django project, with Django tools.
In my project I implement spyne server in Django and use the same way to test my application.

arskom member


As you may know, the spyne tests are run by travis-ci.org, so need to be completely autonomous. So you should factor in the logic that creates a django project, starts the server and runs the tests. I can merge this the moment the test runs just fine simply by calling it. You can have a look at the suds test to see how we got it to work.


arskom member

reopen if you're willing to do this.

@plq plq closed this Dec 21, 2012

Hi, what is hello_world_service when it is supposed to have an app attribute? Some guidance doc would be splendid!

arskom member

I thought this project was dead, sorry......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment