Custom Search

Friday, January 10, 2014

Openstack Project Python Keystoneclient Unit Test Tutorial Part2

1)
First go through following Doc
https://github.com/gabrielfalcao/HTTPretty

Try to understand following things
a)
@httpretty.activate

b)
httpretty.register_uri()
c)
requests.get()
requests.post()

d)
response.text
response.json()

e)
httpretty.last_request()

2)
Check the Output of following httpretty example Programs


import requests
import httpretty


a)
def test_one():
    httpretty.enable()
    test_url = "http://192.168.56.101:5000/v3/"
    httpretty.register_uri(httpretty.GET, test_url,
                           body='{"success":"true"}',
                           content_type='text/json')
    response = requests.get(test_url)
    print "\n===response===", response
    print "\n====response.json()========", response.json()
    print "\n====response.text========", response.text
    last_req = httpretty.last_request()
    print "\n===last_req===", last_req
    httpretty.disable()


OutPut
=======

===response===
====response.json()======== {u'success': u'true'}
====response.text======== {"success":"true"}
===last_req===

b)
def test_two():
    httpretty.enable()
    test_url = "http://192.168.56.101:5000/v3/"
    httpretty.register_uri(httpretty.POST, test_url,
                           body='{"success":"true"}',
                           content_type='text/json')
    response = requests.post(test_url, data={1:1}, headers={'content-type': 'text/json'})
    print "\n===response===", response
    print "\n====response.json()========", response.json()
    print "\n====response.text========", response.text
    last_req = httpretty.last_request()
    print "\n===last_req===", last_req.body
    httpretty.disable()


OutPut
=======

===response===
====response.json()======== {u'success': u'true'}
====response.text======== {"success":"true"}
===last_req=== 1=1

3)
Compare example httpretty Programs with keystoneclient test util


a)
Example of httpretty Program


@httpretty.activate
def test_two():
    test_url = "http://192.168.56.101:5000/v3/"
    httpretty.register_uri(httpretty.POST, test_url,
                           body='{"success":"true"}',
                           content_type='text/json')
    response = requests.post(test_url, data={1:1}, headers={'content-type': 'text/json'})
    last_req = httpretty.last_request()


b)
Example from keystoneclient test util

python-keystoneclient/keystoneclient/tests/v3/utils.py
 

@httpretty.activate
    def test_create(self, ref=None, req_ref=None):
    ###Similar to###httpretty.register_uri
    self.stub_entity(httpretty.POST, entity=req_ref, status=201)
    ###Similar to###response = requests.post() OR response = requests.get()
    returned = self.manager.create(**parameterize(manager_ref))
    ###Similar to###last_req = httpretty.last_request()
    self.assertEntityRequestBodyIs(req_ref)


4)
Important Methods from keystoneclient test util


a)

python-keystoneclient/keystoneclient/tests/utils.py

    def assertRequestBodyIs(self, body=None, json=None):
        if json:
            val = jsonutils.loads(httpretty.last_request().body)
            self.assertEqual(json, val)
        elif body:
            self.assertEqual(body, httpretty.last_request().body)


b)
python-keystoneclient/keystoneclient/tests/v3/utils.py

    def stub_entity(self, method, parts=None, entity=None, id=None, **kwargs):
        if entity:
            entity = self.encode(entity)
            kwargs['json'] = entity

        if not parts:
            parts = [self.collection_key]

            if self.path_prefix:
                parts.insert(0, self.path_prefix)

        if id:
            if not parts:
                parts = []

            parts.append(id)

        self.stub_url(method, parts=parts, **kwargs)


c)
/python-keystoneclient/keystoneclient/tests/utils.py

    def stub_url(self, method, parts=None, base_url=None, json=None, **kwargs):
        if not base_url:
            base_url = self.TEST_URL

        if json:
            kwargs['body'] = jsonutils.dumps(json)
            kwargs['content_type'] = 'application/json'

        if parts:
            url = '/'.join([p.strip('/') for p in [base_url] + parts])
        else:
            url = base_url

        httpretty.register_uri(method, url, **kwargs)


d)
python-keystoneclient/keystoneclient/tests/v3/utils.py


@httpretty.activate
    def test_create(self, ref=None, req_ref=None):
    self.stub_entity(httpretty.POST, entity=req_ref, status=201)
    returned = self.manager.create(**parameterize(manager_ref))
    self.assertEntityRequestBodyIs(req_ref)



Part 1 .....



1 comment:

  1. import requests
    from sure import expect
    import httpretty


    def test_one():
    httpretty.enable()
    test_url = "http://192.168.56.101:5000/v3/"
    httpretty.register_uri(httpretty.GET, test_url,
    body='{"success":"true"}',
    content_type='text/json')
    response = requests.get(test_url)
    print "\n===response===", response
    print "\n====response.json()========", response.json()
    print "\n====response.text========", response.text
    last_req = httpretty.last_request()
    print "\n===last_req===", last_req
    httpretty.disable()


    def test_two():
    httpretty.enable()
    test_url = "http://192.168.56.101:5000/v3/"
    httpretty.register_uri(httpretty.POST, test_url,
    body='{"success":"true"}',
    content_type='text/json')
    response = requests.post(test_url, data={1:1}, headers={'content-type': 'text/json'})
    print "\n===response===", response
    print "\n====response.json()========", response.json()
    print "\n====response.text========", response.text
    last_req = httpretty.last_request()
    print "\n===last_req===", last_req.body
    httpretty.disable()

    test_one()
    test_two()

    ReplyDelete