Skip to content

Commit

Permalink
changed all assertRaises in tests to use the with statement
Browse files Browse the repository at this point in the history
  • Loading branch information
winstonf88 authored and zupo committed Oct 9, 2013
1 parent cf78ca9 commit 54d8277
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 389 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGES.rst
Expand Up @@ -9,6 +9,9 @@ Changes
- Add plone.recipe.codeanalysis to our buildout.
[flohcim]

- Make all assertRaise() calls use the `with` keyword.
[winstonf88]

- Amend user.get method to accept a userid parameter, refs #112.
[cewing, xiru, winstonf88]

Expand Down
189 changes: 91 additions & 98 deletions src/plone/api/tests/test_content.py
Expand Up @@ -71,28 +71,25 @@ def test_create_constraints(self):
api.content.create()

# Check the contraints for the type container
self.assertRaises(
MissingParameterError,
api.content.create,
type='Document',
id='test-doc',
)
with self.assertRaises(MissingParameterError):
api.content.create(
type='Document',
id='test-doc',
)

# Check the contraints for the type parameter
container = mock.Mock()
self.assertRaises(
MissingParameterError,
api.content.create,
container=container,
id='test-doc',
)
with self.assertRaises(MissingParameterError):
api.content.create(
container=container,
id='test-doc',
)

# Check the contraints for id and title parameters
self.assertRaises(
MissingParameterError,
api.content.create,
container=container, type='Document'
)
with self.assertRaises(MissingParameterError):
api.content.create(
container=container, type='Document'
)

# Check the contraints for allowed types in the container
container = self.events
Expand Down Expand Up @@ -128,13 +125,12 @@ def test_create_constraints(self):
# Constraint the allowed types
folder.setConstrainTypesMode(1)
folder.setLocallyAllowedTypes(('News Item',))
self.assertRaises(
InvalidParameterError,
api.content.create,
container=folder,
type='Document',
id='test-doc'
)
with self.assertRaises(InvalidParameterError):
api.content.create(
container=folder,
type='Document',
id='test-doc'
)

@unittest.skipUnless(
HAS_DEXTERITY,
Expand Down Expand Up @@ -170,13 +166,12 @@ def test_create_dexterity(self):
self.assertEqual(page.portal_type, 'Dexterity Item')

# Try to create another item with same id, this should fail
self.assertRaises(
BadRequest,
api.content.create,
container=folder,
type='Dexterity Item',
id='test-item',
)
with self.assertRaises(BadRequest):
api.content.create(
container=folder,
type='Dexterity Item',
id='test-item',
)

def test_create_archetypes(self):
"""Test creating content based on Archetypes."""
Expand Down Expand Up @@ -206,13 +201,12 @@ def test_create_archetypes(self):
self.assertEqual(page.portal_type, 'Document')

# Try to create another page with same id, this should fail
self.assertRaises(
BadRequest,
api.content.create,
container=folder,
type='Document',
id='test-document',
)
with self.assertRaises(BadRequest):
api.content.create(
container=folder,
type='Document',
id='test-document',
)

def test_create_with_safe_id(self):
"""Test the content creating with safe_id mode."""
Expand Down Expand Up @@ -244,12 +238,16 @@ def test_get_constraints(self):

# Path and UID parameter can not be given together
from plone.api.exc import InvalidParameterError
self.assertRaises(
InvalidParameterError, api.content.get, path='/', UID='dummy')
with self.assertRaises(InvalidParameterError):
api.content.get(
path='/',
UID='dummy'
)

# Either a path or UID must be given
from plone.api.exc import MissingParameterError
self.assertRaises(MissingParameterError, api.content.get)
with self.assertRaises(MissingParameterError):
api.content.get()

def test_get(self):
"""Test the getting of content in varios ways."""
Expand Down Expand Up @@ -280,17 +278,18 @@ def test_move_constraints(self):
from plone.api.exc import MissingParameterError

# When no parameters are given an error is raised
self.assertRaises(MissingParameterError, api.content.move)
with self.assertRaises(MissingParameterError):
api.content.move()

container = mock.Mock()

# Source is missing an should raise an error
self.assertRaises(
MissingParameterError, api.content.move, source=container)
with self.assertRaises(MissingParameterError):
api.content.move(source=container)

# Target is missing an should raise an error
self.assertRaises(
MissingParameterError, api.content.move, target=container)
with self.assertRaises(MissingParameterError):
api.content.move(target=container)

def test_move(self):
"""Test moving of content."""
Expand Down Expand Up @@ -337,15 +336,13 @@ def test_rename_constraints(self):
from plone.api.exc import MissingParameterError

# When no parameters are given an error is raised
self.assertRaises(MissingParameterError, api.content.rename)
with self.assertRaises(MissingParameterError):
api.content.rename()

container = mock.Mock()
# Source is missing an should raise an error
self.assertRaises(
MissingParameterError,
api.content.rename,
obj=container,
)
with self.assertRaises(MissingParameterError):
api.content.rename(obj=container)

def test_rename(self):
"""Test renaming of content."""
Expand All @@ -371,12 +368,11 @@ def test_rename(self):
# Rename to existing id
api.content.create(
container=self.about, type='Link', id='link-to-blog')
self.assertRaises(
CopyError,
api.content.rename,
obj=container['about']['link-to-blog'],
new_id='link-to-blog-1',
)
with self.assertRaises(CopyError):
api.content.rename(
obj=container['about']['link-to-blog'],
new_id='link-to-blog-1',
)
api.content.rename(
obj=container['about']['link-to-blog'],
new_id='link-to-blog-1',
Expand All @@ -390,12 +386,13 @@ def test_copy_constraints(self):
from plone.api.exc import MissingParameterError

# When no parameters are given an error is raised
self.assertRaises(MissingParameterError, api.content.copy)
with self.assertRaises(MissingParameterError):
api.content.copy()

container = mock.Mock()
# Source is missing and should raise an error
self.assertRaises(
MissingParameterError, api.content.copy, source=container)
with self.assertRaises(MissingParameterError):
api.content.copy(source=container)

def test_copy(self):
"""Test the copying of content."""
Expand Down Expand Up @@ -436,7 +433,8 @@ def test_delete_constraints(self):

# When no parameters are given an error is raised
from plone.api.exc import MissingParameterError
self.assertRaises(MissingParameterError, api.content.delete)
with self.assertRaises(MissingParameterError):
api.content.delete()

def test_delete(self):
"""Test deleting a content item."""
Expand All @@ -445,7 +443,8 @@ def test_delete(self):

# The content item must be given as parameter
from plone.api.exc import MissingParameterError
self.assertRaises(MissingParameterError, api.content.delete)
with self.assertRaises(MissingParameterError):
api.content.delete()

# Delete the contact page
api.content.delete(self.contact)
Expand All @@ -456,7 +455,8 @@ def test_get_state(self):

# This should fail because an content item is mandatory
from plone.api.exc import MissingParameterError
self.assertRaises(MissingParameterError, api.content.get_state)
with self.assertRaises(MissingParameterError):
api.content.get_state()

review_state = api.content.get_state(obj=self.blog)
self.assertEqual(review_state, 'private')
Expand All @@ -466,20 +466,14 @@ def test_transition(self):
from plone.api.exc import InvalidParameterError
from plone.api.exc import MissingParameterError

self.assertRaises(
MissingParameterError,
api.content.transition,
)
self.assertRaises(
MissingParameterError,
api.content.transition,
obj=mock.Mock(),
)
self.assertRaises(
MissingParameterError,
api.content.transition,
transition='publish',
)
with self.assertRaises(MissingParameterError):
api.content.transition()

with self.assertRaises(MissingParameterError):
api.content.transition(obj=mock.Mock())

with self.assertRaises(MissingParameterError):
api.content.transition(transition='publish')

api.content.transition(obj=self.blog, transition='publish')
review_state = api.content.get_state(obj=self.blog)
Expand All @@ -505,31 +499,29 @@ def test_get_view_constraints(self):
request = self.layer['request']

# When no parameters are given an error is raised
self.assertRaises(MissingParameterError, api.content.get_view)
with self.assertRaises(MissingParameterError):
api.content.get_view()

# name is required
self.assertRaises(
MissingParameterError,
api.content.get_view,
context=self.blog,
request=request,
)
with self.assertRaises(MissingParameterError):
api.content.get_view(
context=self.blog,
request=request,
)

# context is required
self.assertRaises(
MissingParameterError,
api.content.get_view,
name='plone',
request=request,
)
with self.assertRaises(MissingParameterError):
api.content.get_view(
name='plone',
request=request,
)

# request is required
self.assertRaises(
MissingParameterError,
api.content.get_view,
name='plone',
context=self.blog,
)
with self.assertRaises(MissingParameterError):
api.content.get_view(
name='plone',
context=self.blog,
)

def test_get_view(self):
"""Test the view."""
Expand Down Expand Up @@ -560,7 +552,8 @@ def test_get_uuid(self):
container = self.portal

# The content item must be given as parameter
self.assertRaises(MissingParameterError, api.content.get_uuid)
with self.assertRaises(MissingParameterError):
api.content.get_uuid()

generator = getUtility(IUUIDGenerator)

Expand Down

0 comments on commit 54d8277

Please sign in to comment.