Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

tests.py for Django example #211

Closed
Konjkov opened this Issue · 12 comments

3 participants

Vladimir Burak Arslan rotherfuchs
Vladimir

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
        self.get_out_string(self.ctx)

        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.
        self.get_in_object(self.ctx)

        if not (self.ctx.in_error is None):
            raise self.ctx.in_error
        elif code >= 400:
            raise self.ctx.in_error
        else:
            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)
Burak Arslan
Owner

Hi,

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?

Thanks,

Vladimir

Hi,

Django have the test client
https://docs.djangoproject.com/en/dev/topics/testing/#module-django.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.

Burak Arslan plq referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Burak Arslan plq referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Burak Arslan
Owner

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

Burak Arslan
Owner

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

Burak Arslan
Owner

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

Burak Arslan plq referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Burak Arslan
Owner

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

Burak Arslan plq referenced this issue from a commit in plq/spyne
Burak Arslan plq initial commit for a muted django test. Issue #211 59b6f87
Vladimir

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.

Burak Arslan
Owner

Hi,

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.

Best,

Burak Arslan
Owner

reopen if you're willing to do this.

Burak Arslan plq closed this
rotherfuchs

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

Vladimir

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
Something went wrong with that request. Please try again.