Skip to content
This repository has been archived by the owner on Feb 28, 2018. It is now read-only.

Refactor tests #228

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8bcb58c
Adds firt and second shared tests
jtemporal Jul 31, 2017
4cb1a8d
Using shared_tests on Suspicions Command test suite
jtemporal Jul 31, 2017
0fbff0b
Adds missing import
jtemporal Jul 31, 2017
84ae334
uses content on assert
jtemporal Jul 31, 2017
94b5716
Using shared test: test_schedule_update_non_existing_record
jtemporal Aug 1, 2017
1f02cae
fix typo
jtemporal Aug 1, 2017
5242f99
Adds missing assert
jtemporal Aug 1, 2017
dea1f26
Starting migrating shared testing methods to `__init__`
jtemporal Aug 2, 2017
8abce0b
Moves TestCase class upwards
jtemporal Aug 3, 2017
34fe8bc
Moving remaining tests to `__init__`
jtemporal Aug 3, 2017
eff52b8
Updating receipts text command test suite
jtemporal Aug 3, 2017
15b9411
improve order of method arguments
jtemporal Aug 3, 2017
15f1b2f
using serializer method for testing
jtemporal Aug 3, 2017
2cc3c37
Better naming for variables
jtemporal Aug 3, 2017
2d8b854
Improving posicional arguments ordering
jtemporal Aug 3, 2017
f441327
updates main testign on receipts text command test suite
jtemporal Aug 3, 2017
8d451ae
update now in __init__
jtemporal Aug 3, 2017
bca0039
handler_with_options() now in __init__
jtemporal Aug 3, 2017
b4836f8
handler_without_options() now in __init__
jtemporal Aug 3, 2017
66a2dca
handler_with_non_existing_file() now in __init__
jtemporal Aug 3, 2017
6d16652
new_command() now in __init__
jtemporal Aug 4, 2017
33da61f
add_arguments now in __init__
jtemporal Aug 4, 2017
18701a9
removing repeated line
jtemporal Aug 5, 2017
19d4d9e
Using command defined on setup
jtemporal Aug 5, 2017
132545c
Using command defined on __init__
jtemporal Aug 5, 2017
2bf6780
using serializer defined on __init__
jtemporal Aug 5, 2017
77ac4f9
Reordering tests on companies command test suite
jtemporal Aug 5, 2017
de09fb1
Adds file_name to setUp()
jtemporal Aug 5, 2017
ff1bed4
Using add_arguments() and moved setup and naming
jtemporal Aug 5, 2017
0f5e3bc
using serializer
jtemporal Aug 5, 2017
fa0553a
Fixing imports on tests
jtemporal Aug 5, 2017
d4c4593
Typo
cuducos Sep 28, 2017
7f9f108
Merge branch 'master' into refactor-tests
cuducos Sep 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion jarbas/core/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
from io import StringIO
from datetime import date
from random import randrange

from unittest.mock import Mock, call

from django.utils import timezone
from django.test import TestCase as DjangoTestCase

from jarbas.core.models import Reimbursement, Tweet


class TestCase(DjangoTestCase):

def serializer(self, command, input, expected):
serialized = command.serialize(input)
self.assertEqual(serialized, expected)

def main(self, command, update, schedule_update, costum_method):
costum_method.return_value = (range(21), range(21, 43))
command.main()
update.assert_has_calls([call()] * 2)
schedule_update.assert_has_calls(call(i) for i in range(42))

def schedule_update_non_existing_record(self, command, content, get):
get.side_effect = Reimbursement.DoesNotExist
command.queue = []
command.schedule_update(content)
get.assert_called_once_with(document_id=42)
self.assertEqual([], command.queue)

def update(self, command, fields, print_, bulk_update):
command.count = 40
command.queue = list(range(2))
command.update()
bulk_update.assert_called_with([0, 1], update_fields=fields)
print_.assert_called_with('42 reimbursements updated.', end='\r')
self.assertEqual(42, command.count)

def handler_with_options(self, command, print_, exits, main, costum_command):
command.handle(dataset=self.file_name, batch_size=42)
main.assert_called_once_with()
print_.assert_called_once_with('0 reimbursements updated.')
self.assertEqual(command.path, self.file_name)
self.assertEqual(command.batch_size, 42)

def handler_without_options(self, command, print_, exits, main, costum_command):
command.handle(dataset=self.file_name, batch_size=4096)
main.assert_called_once_with()
print_.assert_called_once_with('0 reimbursements updated.')
self.assertEqual(command.path, self.file_name)
self.assertEqual(command.batch_size, 4096)

def handler_with_non_existing_file(self, command, exists, update, costum_command):
exists.return_value = False
with self.assertRaises(FileNotFoundError):
command.handle(dataset='suspicions.xz', batch_size=4096)
update.assert_not_called()

def new_command(self, command, costum_command, serialize, rows, lzma, print_):
serialize.return_value = '.'
lzma.return_value = StringIO()
rows.return_value = range(42)
command.batch_size = 10
command.path = self.file_name
expected = [['.'] * 10, ['.'] * 10, ['.'] * 10, ['.'] * 10, ['.'] * 2]
self.assertEqual(expected, list(costum_command))
self.assertEqual(42, serialize.call_count)

from jarbas.core.models import Tweet
def add_arguments(self, command):
mock = Mock()
command.add_arguments(mock)
self.assertEqual(2, mock.add_argument.call_count)


suspicions = {
Expand Down
29 changes: 14 additions & 15 deletions jarbas/core/tests/test_companies_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
from io import StringIO
from unittest.mock import patch

from django.test import TestCase

from jarbas.core.management.commands.companies import Command
from jarbas.core.models import Activity, Company
from jarbas.core.tests import sample_company_data
from jarbas.core.tests import TestCase, sample_company_data


class TestCommand(TestCase):

def setUp(self):
self.command = Command()
self.file_name = 'companies.xz'


class TestSerializer(TestCommand):
Expand All @@ -23,7 +22,7 @@ def test_to_email(self):
self.assertEqual(self.command.to_email('[email protected]'), expected)

def test_serializer(self):
company = {
input = {
'email': 'ahoy',
'opening': '31/12/1969',
'situation_date': '31/12/1969',
Expand All @@ -39,7 +38,7 @@ def test_serializer(self):
'latitude': 3.1415,
'longitude': -42.0
}
self.assertEqual(self.command.serialize(company), expected)
self.serializer(self.command, input, expected)


class TestCreate(TestCommand):
Expand Down Expand Up @@ -83,25 +82,25 @@ def test_save_companies(self, create, print_count, serialize, save_activities, r
class TestConventionMethods(TestCommand):

@patch('jarbas.core.management.commands.companies.print')
@patch('jarbas.core.management.commands.companies.LoadCommand.drop_all')
@patch('jarbas.core.management.commands.companies.Command.drop_all')
@patch('jarbas.core.management.commands.companies.Command.save_companies')
@patch('jarbas.core.management.commands.companies.Command.print_count')
def test_handler_without_options(self, print_count, save_companies, drop_all, print_):
def test_handler_with_options(self, print_count, save_companies, drop_all, print_):
print_count.return_value = 0
self.command.handle(dataset='companies.xz')
self.command.handle(dataset=self.file_name, drop=True)
print_.assert_called_with('Starting with 0 companies')
self.assertEqual(2, drop_all.call_count)
self.assertEqual(1, save_companies.call_count)
self.assertEqual(1, print_count.call_count)
self.assertEqual('companies.xz', self.command.path)
drop_all.assert_not_called()

@patch('jarbas.core.management.commands.companies.print')
@patch('jarbas.core.management.commands.companies.Command.drop_all')
@patch('jarbas.core.management.commands.companies.LoadCommand.drop_all')
@patch('jarbas.core.management.commands.companies.Command.save_companies')
@patch('jarbas.core.management.commands.companies.Command.print_count')
def test_handler_with_options(self, print_count, save_companies, drop_all, print_):
def test_handler_without_options(self, print_count, save_companies, drop_all, print_):
print_count.return_value = 0
self.command.handle(dataset='companies.xz', drop=True)
self.command.handle(dataset=self.file_name)
print_.assert_called_with('Starting with 0 companies')
self.assertEqual(2, drop_all.call_count)
self.assertEqual(1, save_companies.call_count)
self.assertEqual(1, print_count.call_count)
self.assertEqual(self.file_name, self.command.path)
drop_all.assert_not_called()
1 change: 1 addition & 0 deletions jarbas/core/tests/test_company_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.test import TestCase

from jarbas.core.models import Activity, Company
from jarbas.core.tests import sample_activity_data, sample_company_data

Expand Down
55 changes: 25 additions & 30 deletions jarbas/core/tests/test_load_command.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,65 @@
from datetime import date
from unittest.mock import Mock, patch

from django.test import TestCase

from jarbas.core.management.commands import LoadCommand
from jarbas.core.models import Activity
from jarbas.core.tests import sample_activity_data
from jarbas.core.tests import TestCase, sample_activity_data


class TestStaticMethods(TestCase):
class TestCommand(TestCase):

def setUp(self):
self.cmd = LoadCommand()
self.command = LoadCommand()

class TestStaticMethods(TestCommand):

def test_get_model_name(self):
self.assertEqual('Activity', self.cmd.get_model_name(Activity))
self.assertEqual('Activity', self.command.get_model_name(Activity))

def test_to_date(self):
expected = date(1991, 7, 22)
self.assertEqual(self.cmd.to_date('22/7/91'), expected)
self.assertEqual(self.cmd.to_date('1991-07-22 03:15:00+0300'), expected)
self.assertEqual(self.cmd.to_date('22/13/91'), None)
self.assertEqual(self.cmd.to_date('aa/7/91'), None)
self.assertEqual(self.cmd.to_date('22/07/16'), date(2016, 7, 22))
self.assertEqual(self.command.to_date('22/7/91'), expected)
self.assertEqual(self.command.to_date('1991-07-22 03:15:00+0300'), expected)
self.assertEqual(self.command.to_date('22/13/91'), None)
self.assertEqual(self.command.to_date('aa/7/91'), None)
self.assertEqual(self.command.to_date('22/07/16'), date(2016, 7, 22))

def test_to_number(self):
self.assertIsNone(self.cmd.to_number(''))
self.assertIsNone(self.cmd.to_number('NaN'))
self.assertIsNone(self.cmd.to_number('nan'))
self.assertEqual(1.0, self.cmd.to_number('1'))
self.assertEqual(1.2, self.cmd.to_number('1.2'))
self.assertEqual(1, self.cmd.to_number('1', int))
self.assertEqual(1, self.cmd.to_number('1.0', int))
self.assertIsNone(self.command.to_number(''))
self.assertIsNone(self.command.to_number('NaN'))
self.assertIsNone(self.command.to_number('nan'))
self.assertEqual(1.0, self.command.to_number('1'))
self.assertEqual(1.2, self.command.to_number('1.2'))
self.assertEqual(1, self.command.to_number('1', int))
self.assertEqual(1, self.command.to_number('1.0', int))


class TestPrintCount(TestCase):

def setUp(self):
self.cmd = LoadCommand()
class TestPrintCount(TestCommand):

@patch('jarbas.core.management.commands.print')
def test_print_no_records(self, mock_print):
self.cmd.print_count(Activity)
self.command.print_count(Activity)
arg = 'Current count: 0 Activitys '
kwargs = {'end': '\r'}
mock_print.assert_called_with(arg, **kwargs)

@patch('jarbas.core.management.commands.print')
def test_print_with_records(self, mock_print):
Activity.objects.create(**sample_activity_data)
self.cmd.print_count(Activity)
self.command.print_count(Activity)
arg = 'Current count: 1 Activitys '
kwargs = {'end': '\r'}
mock_print.assert_called_with(arg, **kwargs)

@patch('jarbas.core.management.commands.print')
def test_print_with_permanent_keyword_arg(self, mock_print):
self.cmd.print_count(Activity, permanent=True)
self.command.print_count(Activity, permanent=True)
arg = 'Current count: 0 Activitys '
kwargs = {'end': '\n'}
mock_print.assert_called_with(arg, **kwargs)


class TestDropAll(TestCase):
class TestDropAll(TestCommand):

@patch('jarbas.core.management.commands.print')
def test_drop_all(self, mock_print):
Expand All @@ -73,12 +70,10 @@ def test_drop_all(self, mock_print):
self.assertEqual(0, Activity.objects.count())


class TestAddArguments(TestCase):
class TestAddArguments(TestCommand):

def test_add_arguments(self):
mock = Mock()
LoadCommand().add_arguments(mock)
self.assertEqual(2, mock.add_argument.call_count)
self.add_arguments(self.command)

def test_add_arguments_without_drop_all(self):
mock = Mock()
Expand Down
Loading