-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from jamescooke/unittest-support
Initial Unittest support
- Loading branch information
Showing
15 changed files
with
213 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django.test import TestCase | ||
from django.contrib.auth import get_user_model | ||
from django.core.urlresolvers import reverse | ||
|
||
|
||
class TestAccountEditView(TestCase): | ||
|
||
def setUp(self): | ||
User = get_user_model() | ||
self.user = User( | ||
email='test@example.com', | ||
is_active=1 | ||
) | ||
self.user.set_password('blah') | ||
self.user.save() | ||
|
||
def test_url_resolves(self): | ||
result = reverse('account.edit') | ||
|
||
self.assertEqual(result, '/account/') | ||
|
||
def test_not_logged_in(self): | ||
url = reverse('account.edit') | ||
|
||
result = self.client.get(url) | ||
|
||
self.assertEqual(result.status_code, 302) | ||
|
||
def test_loads_template(self): | ||
url = reverse('account.edit') | ||
self.client.login(email='test@example.com', password='blah') | ||
|
||
result = self.client.get(url) | ||
|
||
self.assertEqual(result.status_code, 200) | ||
self.assertTemplateUsed(result, 'account/account.html') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.core.exceptions import ValidationError | ||
from django.db import transaction | ||
from django.db.utils import IntegrityError | ||
from django.test import TestCase | ||
|
||
from ..django_fakery_factories import ItemFactory, UserFactory | ||
from ..models import Item | ||
|
||
|
||
class TestItemFactory(TestCase): | ||
|
||
def test_default(self): | ||
""" | ||
Django Fakery: Plant.Item: RED (creates invalid items) | ||
""" | ||
result = ItemFactory() | ||
|
||
self.assertEqual(Item.objects.count(), 1) | ||
with self.assertRaises(ValidationError) as cm: | ||
result.full_clean() | ||
self.assertIn('has at most 1 character', str(cm.exception)) | ||
|
||
|
||
class TestUserFactory(TestCase): | ||
|
||
user_model = get_user_model() | ||
|
||
def test_default(self): | ||
""" | ||
Django Fakery: User Model: YELLOW (raises IntegrityError) | ||
We have to push the number of Users created, but since Django Fakery | ||
has no collision protection and uses a small number of latin words it | ||
will always fail sooner or later. Sometimes on the second user. | ||
However, instances created are valid if they are able to enter the | ||
database. | ||
""" | ||
with self.assertRaises(IntegrityError) as cm: | ||
for expected_num_created in range(1, 100): | ||
with transaction.atomic(): | ||
UserFactory() | ||
|
||
self.assertEqual(self.user_model.objects.count(), expected_num_created - 1) | ||
self.assertIn('unique', str(cm.exception).lower()) | ||
for u in self.user_model.objects.all(): | ||
u.full_clean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from enum import Enum | ||
|
||
ActBlockType = Enum('ActBlockType', 'marked_act pytest_raises result_assignment') # pylint: disable=invalid-name | ||
ActBlockType = Enum( # pylint: disable=invalid-name | ||
'ActBlockType', | ||
'marked_act pytest_raises result_assignment unittest_raises', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import ast | ||
|
||
import asttokens | ||
import pytest | ||
|
||
from flake8_aaa.helpers import node_is_unittest_raises | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'code_str', [ | ||
''' | ||
def test(self): | ||
with self.assertRaises(IntegrityError) as cm: | ||
do_thing() | ||
''', | ||
''' | ||
def test_other(self): | ||
with self.assertRaises(ValueError) as cm: | ||
do_thing() | ||
''', | ||
] | ||
) | ||
def test(first_node_with_tokens): | ||
with_node = first_node_with_tokens.body[0] | ||
|
||
result = node_is_unittest_raises(with_node) | ||
|
||
assert result is True | ||
|
||
|
||
@pytest.mark.parametrize('code_str', [ | ||
'''with open('test.txt') as f: | ||
f.read()''', | ||
]) | ||
def test_no(code_str): | ||
tree = ast.parse(code_str) | ||
asttokens.ASTTokens(code_str, tree=tree) | ||
node = tree.body[0] | ||
|
||
result = node_is_unittest_raises(node) | ||
|
||
assert result is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters