Skip to content

Commit

Permalink
[fix] test fail
Browse files Browse the repository at this point in the history
- webapp2 only support python 2.x (after 2.5)
- python 2.6 is no longer supported by python core team, deprecated it
- pytest would warn about failing collecting class with name prefixed
with “Test”, so rename those classes.
- the unit test actually cover python 3.5, not 3.4, (fix readme)
  • Loading branch information
dev authored and dev committed Dec 9, 2016
1 parent 3d574dd commit 6c5b190
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ branches:
only:
- master
python:
- "2.6"
- "2.7"
- "3.3"
- "3.5"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Read the [Document](http://pyswagger.readthedocs.org/en/latest/), or just go thr

##Features
- **NEW** convert Swagger Document from older version to newer one. (ex. convert from 1.2 to 2.0)
- support Swagger **1.2**, **2.0** on python **2.6**, **2.7**, **3.3**, **3.4**
- support Swagger **1.2**, **2.0** on python ~~2.6~~, **2.7**, **3.3**, **3.5**
- support YAML via [Pretty-YAML](https://github.com/mk-fg/pretty-yaml)
- support $ref to **External Document**, multiple swagger.json will be organized into a group of App. And external document with self-describing resource is also supported (refer to [issue](https://github.com/swagger-api/swagger-spec/issues/219)).
- type safe, input/output are converted to python types according to [Data Type](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#43-data-types) described in Swagger. You don't need to touch any json schema when using pyswagger. Limitations like **minimum/maximum** or **enum** are also checked. **Model inheritance** also supported.
Expand Down
8 changes: 8 additions & 0 deletions pyswagger/contrib/client/webapp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ...core import BaseClient
import webapp2
import six
import sys


class Webapp2TestClient(BaseClient):
Expand All @@ -18,6 +19,13 @@ def __init__(self, app, auth=None, keep_cookie=False, **kw):
:param keep_cookie bool: keep cookie as session or not
"""
super(Webapp2TestClient, self).__init__(auth)

#
# if webapp2 finally support python 3, please submit an issue to remove this check
#
if sys.version_info[0] > 2:
raise Exception('webapp2 only support python 2.x')

self.__app = app
self.__keep_cookie = keep_cookie
self.__cookie = None
Expand Down
44 changes: 23 additions & 21 deletions pyswagger/tests/contrib/client/test_webapp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import webapp2
import json
import six
import sys
import os


Expand Down Expand Up @@ -83,21 +84,21 @@ def uploadFile(self):


class CookieHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)

@webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)

try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)

@webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()

def getCookie(self):
self.session['test_pyswagger'] = 'test 123'
Expand All @@ -114,15 +115,16 @@ def keepCookie(self):
webapp2.Route(r'/api/get_cookie', handler=CookieHandler, handler_method='getCookie', methods=['GET']),
webapp2.Route(r'/api/keep_cookie', handler=CookieHandler, handler_method='keepCookie', methods=['GET']),
], config={
'webapp2_extras.sessions': {
'secret_key': 'key123',
'cookie_args': {
'max_age': 31557600, # one year
'webapp2_extras.sessions': {
'secret_key': 'key123',
'cookie_args': {
'max_age': 31557600, # one year
'secure': False
}
}
}
})

@unittest.skipIf(sys.version_info[0] > 2, 'webapp2 only supports python 2.x')
class Webapp2TestCase(unittest.TestCase):

@classmethod
Expand All @@ -138,7 +140,7 @@ def test_updatePet(self):
resp = Webapp2TestClient(wapp).request(
self.app.op['updatePet'](body=dict(id=1, name='Tom1'))
)
self.assertEqual(resp.status, 200)
self.assertEqual(resp.status, 200)
self.assertEqual(pet_db.read_(1)['name'], 'Tom1')

def test_addPet(self):
Expand Down
56 changes: 28 additions & 28 deletions pyswagger/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class GrandChildObj(six.with_metaclass(base.FieldMeta, base.BaseObj)):
__swagger_fields__ = {
'name': ''
}
}

class GrandChildContext(base.Context):
__swagger_ref_object__ = GrandChildObj
Expand All @@ -24,7 +24,7 @@ class ChildContext(base.Context):
'g': (None, GrandChildContext)
}

class TestObj(six.with_metaclass(base.FieldMeta, base.BaseObj)):
class TObj(six.with_metaclass(base.FieldMeta, base.BaseObj)):
__swagger_fields__ = {
'a': [],
'b': {},
Expand All @@ -33,8 +33,8 @@ class TestObj(six.with_metaclass(base.FieldMeta, base.BaseObj)):
'f': None
}

class TestContext(base.Context):
__swagger_ref_object__ = TestObj
class TContext(base.Context):
__swagger_ref_object__ = TObj
__swagger_child__ = {
'a': (base.ContainerType.list_, ChildContext),
'b': (base.ContainerType.dict_, ChildContext),
Expand All @@ -49,7 +49,7 @@ def test_baseobj_children(self):
""" test _children_ """
tmp = {'t': {}}
obj = {'a': [{}, {}, {}], 'b': {'/a': {}, '~b': {}, 'cc': {}}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(obj)
c = tmp['t']._children_.keys()

Expand All @@ -59,7 +59,7 @@ def test_baseobj_parent(self):
""" test _parent_ """
tmp = {'t': {}}
obj = {'a': [{}], 'b': {'bb': {}}, 'c': {'cc': [{}]}, 'd': {}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(obj)

def _check(o):
Expand All @@ -77,7 +77,7 @@ def test_field_rename(self):
class TestRenameObj(six.with_metaclass(base.FieldMeta, base.BaseObj)):
__swagger_fields__ = {'a': None}
__swagger_rename__ = {'a': 'b'}

class TestRenameContext(base.Context):
__swagger_ref_object__ = TestRenameObj

Expand All @@ -94,8 +94,8 @@ class TestRenameContext(base.Context):
def test_field_default_value(self):
""" field default value, make sure we won't reference to a global declared list_
"""
o1 = TestObj(base.NullContext())
o2 = TestObj(base.NullContext())
o1 = TObj(base.NullContext())
o2 = TObj(base.NullContext())
self.assertTrue(id(o1.a) != id(o2.a))

def test_merge(self):
Expand All @@ -112,9 +112,9 @@ class MergeObj(six.with_metaclass(base.FieldMeta, base.BaseObj)):

class MergeContext(base.Context):
__swagger_child__ = {
'ma': (None, TestContext),
'mb': (None, TestContext),
'mc': (base.ContainerType.dict_, TestContext)
'ma': (None, TContext),
'mb': (None, TContext),
'mc': (base.ContainerType.dict_, TContext)
}
__swagger_ref_object__ = MergeObj

Expand Down Expand Up @@ -173,7 +173,7 @@ def _chk_parent(o_from, o_to):
self.assertEqual(id(o_to.mc['/a'].c['cc'][0]._parent_), id(o_to.mc['/a']))

self.assertEqual(o2.ma, None)
self.assertTrue(isinstance(o2.mb, TestObj))
self.assertTrue(isinstance(o2.mb, TObj))
self.assertTrue(len(o2.mb.a), 3)
self.assertEqual(len(o2.mc), 0)

Expand All @@ -198,13 +198,13 @@ def test_merge_exclude(self):
""" test 'exclude' in merge """
tmp = {'t': {}}
obj = {'a': [{}, {}, {}], 'b': {'/a': {}, '~b': {}, 'cc': {}}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(obj)
o = tmp['t']

o1, o2 = TestObj(base.NullContext()), TestObj(base.NullContext())
o1.merge(o, TestContext)
o2.merge(o, TestContext, exclude=['b'])
o1, o2 = TObj(base.NullContext()), TObj(base.NullContext())
o1.merge(o, TContext)
o2.merge(o, TContext, exclude=['b'])
self.assertEqual(len(o1.a), 3)
self.assertEqual(len(o2.a), 3)
self.assertEqual(len(o1.b), 3)
Expand All @@ -214,9 +214,9 @@ def test_resolve(self):
""" test resolve function """
tmp = {'t': {}}
obj = {'a': [{}, {}, {}], 'b': {'/a': {}, '~b': {}, 'cc': {}}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(obj)

o = tmp['t']
self.assertEqual(id(o.resolve('a')), id(o.a))
self.assertEqual(id(o.resolve(['a'])), id(o.resolve('a')))
Expand All @@ -232,13 +232,13 @@ def is_produced(kls, obj):
return False

class TestOkContext(base.Context):
__swagger_ref_object__ = TestObj
__swagger_ref_object__ = TObj
__swagger_child__ = {
'a': (None, ChildContext)
}

class TestNotOkContext(base.Context):
__swagger_ref_object__ = TestObj
__swagger_ref_object__ = TObj
__swagger_child__ = {
'a': (None, ChildNotOkContext)
}
Expand All @@ -263,7 +263,7 @@ class TestNotOkContext(base.Context):
def test_produce(self):
""" test produce function """
class TestBoolContext(base.Context):
__swagger_ref_object__ = TestObj
__swagger_ref_object__ = TObj
__swagger_child__ = {
'a': (None, ChildContext),
}
Expand Down Expand Up @@ -295,7 +295,7 @@ def test_compare(self):
},
'd': {}
}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(obj)
obj1 = tmp['t']

Expand All @@ -307,7 +307,7 @@ def test_compare(self):
objt['a'][0]['g']['name'] = 'Tom1'

tmp = {'t': {}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(objt)
obj2 = tmp['t']
self.assertEqual((False, 'a/0/g/name'), obj1.compare(obj2))
Expand All @@ -317,7 +317,7 @@ def test_compare(self):
objt['a'][0], objt['a'][1] = objt['a'][1], objt['a'][0]

tmp = {'t': {}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(objt)
obj3 = tmp['t']
self.assertEqual((False, 'a/0/g/name'), obj1.compare(obj3))
Expand All @@ -327,7 +327,7 @@ def test_compare(self):
objt['b']['bbb']['g']['name'] = 'Leo'

tmp = {'t': {}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(objt)
obj4 = tmp['t']
self.assertEqual((False, 'b/bbb/g/name'), obj1.compare(obj4))
Expand All @@ -337,7 +337,7 @@ def test_compare(self):
objt['c']['cc'][0]['g']['name'] = 'Celios'

tmp = {'t': {}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(objt)
obj5 = tmp['t']
self.assertEqual((False, 'c/cc/0/g/name'), obj1.compare(obj5))
Expand All @@ -347,7 +347,7 @@ def test_compare(self):
objt['b']['bbbb'] = {'g': {'name': 'Leo'}}

tmp = {'t': {}}
with TestContext(tmp, 't') as ctx:
with TContext(tmp, 't') as ctx:
ctx.parse(objt)
obj6 = tmp['t']
self.assertEqual((False, 'b/bbbb'), obj1.compare(obj6))
Expand Down

0 comments on commit 6c5b190

Please sign in to comment.