Skip to content

Instantly share code, notes, and snippets.

@edgabaldi
Created December 15, 2014 13:35
Show Gist options
  • Save edgabaldi/52b99b94060a507c6dec to your computer and use it in GitHub Desktop.
Save edgabaldi/52b99b94060a507c6dec to your computer and use it in GitHub Desktop.
running tests very fast
COLOR_CHOICES = (
('red', 'Red'),
('green', 'Green'),
('blue', 'Blue'),
)
class Vehicle(models.Model):
year = models.CharField(max_length=4)
color = models.CharField(max_length=30, choices = COLOR_CHOICES)
from models import Vehicle
class ReportManager(object):
def __init__(self, title):
self.error_list = []
self.title = title
def get_queryset(self):
return Vehicle.objects.filter(color='blue')
def change_vehicle_color_to_red(self, queryset):
queryset.update(color='red')
def run(self):
queryset = self.get_queryset()
self.change_vehicle_color_to_red(queryset)
from unittest import TestCase
from mock import patch, Mock
from tasks import ReportManager
class ReportManagerTest(TestCase):
def setUp(self):
self.manager = ReportManager(title='Foo')
def test_instance(self):
self.assertEqual(self.manager.error_list, [])
self.assertEqual(self.manager.title, 'Foo')
@patch('tasks.Vehicle')
def test_get_queryset(self, _model):
self.manager.get_queryset()
_model.objects.filter.assert_called_once_with(color='blue')
def test_change_vehicle_color_to_red(self):
mock = Mock()
self.manager.change_vehicle_color_to_red(mock)
mock.update.assert_called_once_with(color='red')
@patch('tasks.ReportManager.get_queryset')
@patch('tasks.ReportManager.change_vehicle_color_to_red')
def test_run(self, _change_vehicle_color_to_red, _get_queryset):
_get_queryset.return_value = 'foo'
self.manager.run()
_get_queryset.assert_called_once_with()
__change_vehicle_color_to_red.assert_called_once_with('foo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment