<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Taras Mankovski Blog &#187; Testing</title>
	<atom:link href="http://taras.cc/index.php/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://taras.cc</link>
	<description>Building Beecoop and generaly making developer&#039;s lives easier and more productive.</description>
	<lastBuildDate>Wed, 25 Nov 2009 03:41:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Testing REST API with restclient</title>
		<link>http://taras.cc/index.php/2009/10/07/testing-rest-api-with-restclient/</link>
		<comments>http://taras.cc/index.php/2009/10/07/testing-rest-api-with-restclient/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 20:58:34 +0000</pubDate>
		<dc:creator>Taras</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://taras.cc/?p=166</guid>
		<description><![CDATA[Here are some code snippets from the test that I wrote for testing the REST API for one of our projects.
I am using appengine-rest-server as API provider on the server.
Do not copy these example verbatim, I&#8217;m only showing them as examples.
Each of this methods is defined inside of a class
Imports

from nose.tools import assert_equals, assert_true, assert_false
from [...]]]></description>
			<content:encoded><![CDATA[<p>Here are some code snippets from the test that I wrote for testing the REST API for one of our projects.<span id="more-166"></span><br />
I am using <a href="http://code.google.com/p/appengine-rest-server/">appengine-rest-server</a> as API provider on the server.</p>
<p>Do not copy these example verbatim, I&#8217;m only showing them as examples.<br />
Each of this methods is defined inside of a class</p>
<h4>Imports</h4>
<pre><code>
from nose.tools import assert_equals, assert_true, assert_false
from restclient import GET, POST, PUT, DELETE
from lxml import etree, objectify
from StringIO import StringIO
</pre>
<p></code></p>
<h4>Verifying XML against XML Schema</h4>
<pre><code>
    def verify_against_schema(self, xml, schema):
        '''
        Check if data is valid for provided schema

        xml data to verify
        schema is url to the xml schema
        '''
        schema = etree.XMLSchema(etree.parse(StringIO(schema)))
        parser = etree.XMLParser(schema=schema)
        return etree.parse(StringIO(xml), parser=parser)

    def test_review_schema(self):
        'Vacation :: Verifying review schema'

        xml = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <service>4</service>
            <cleanliness>4</cleanliness>
            <reason>Wedding</reason>
            <value>4</value>
            <resort>34r3423f3rf32f3f</resort>
            <ta_id>334323443</ta_id>
            <rooms>300</rooms>
            <recommend>true</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''

        verified = self.verify_against_schema(xml, GET(self.url('/api/rest/metadata/Review')))
        assert_true(verified)

</code></pre>
<h4>Creating an item</h4>
<pre><code>

    def create_review(self, resort, full=False):
        'Create review for testing purposes'

        xml = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <service>4</service>
            <cleanliness>4</cleanliness>
            <reason>Wedding</reason>
            <value>4</value>
            <resort>%s</resort>
            <ta_id>334323443</ta_id>
            <rooms>300</rooms>
            <recommend>1</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''%resort

        if full:
            url = '/api/rest/Review?type=full'
        else:
            url = '/api/rest/Review'     

        return POST(self.url(url), body=xml, async=False, resp=True)
</code></pre>
<h4>Updating an item</h4>
<pre><code>
   def test_updating_review(self):
        'Vacation :: Updating review'

        response, resort = self.create_resort()
        response, review_id = self.create_review(resort)

        xml = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <service>5</service>
            <cleanliness>5</cleanliness>
            <reason>Wedding</reason>
            <value>5</value>
            <resort>%s</resort>
            <ta_id>334323443</ta_id>
            <rooms>520</rooms>
            <recommend>true</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''%resort
        response, content = PUT(self.url('/api/rest/Review/%s?type=full'%review_id), body=xml, resp=True, async=False )

        expected = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <key>%s</key>
            <service>5</service>
            <cleanliness>5</cleanliness>
            <reason>Wedding</reason>
            <value>5</value>
            <resort>%s</resort>
            <ta_id>334323443</ta_id>
            <rooms>520</rooms>
            <recommend>true</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''%(review_id,resort)

        expected = objectify.fromstring(expected)
        expected = etree.tostring(expected)

        result = objectify.fromstring(content)
        result = etree.tostring(result)

        assert_equals(expected, result)
</code></pre>
<p>I hope you find these helpful</p>
]]></content:encoded>
			<wfw:commentRss>http://taras.cc/index.php/2009/10/07/testing-rest-api-with-restclient/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
