50
from webcatalog.management.commands.import_app_install_data import (
51
Command as ImportAppInstallCommand,
53
from webcatalog.management.commands.import_ratings_stats import (
54
Command as ImportRatingsStatsCommand,
56
from webcatalog.tests.factory import TestCaseWithFactory
56
from webcatalog.management.commands import (
57
import_app_install_data,
60
from webcatalog.tests.factory import (
62
WebCatalogObjectFactory,
58
65
__metaclass__ = type
67
'CheckAllLatestTestCase',
69
'CleanupDataTestCase',
60
70
'ImportAppInstallTestCase',
61
71
'ImportExhibitsTestCase',
62
72
'ImportForPurchaseAppsTestCase',
63
73
'ImportRatingsTestCase',
64
'CheckAllLatestTestCase',
788
797
self.assertEqual([True, False], [app.is_latest for app in retrieved])
789
798
self.assertTrue(Application.objects.get(package_name='bar').is_latest)
790
799
self.assertTrue(Application.objects.get(package_name='baz').is_latest)
802
class CleanupTestCase(TestCaseWithFactory):
803
@patch('webcatalog.management.commands.cleanup.connection')
805
def run_and_check_output_and_sql(self, func, output, sql, mock_stdout,
808
actual_output = ''.join(str(call[0][0])
809
for call in mock_stdout.write.call_args_list)
810
self.assertEqual(output, actual_output.split('\n'))
811
call_list = mock_connection.cursor.return_value.execute.call_args_list
812
for expected, call in zip(sql, call_list):
813
actual = (' '.join(call[0][0].split()),) + call[0][1:]
814
self.assertEqual(expected, actual)
816
@skipUnless(connection.vendor == 'postgresql', "Requires postgresql")
817
def test_clean_nonces_no_nonces(self):
819
call_command('cleanup', 'webcatalog_nonce')
820
output = ['Opening cursor',
821
'Removed 0 rows (0 total removed). Batch size 1',
825
('DECLARE _oauth_nonce_clean NO SCROLL CURSOR WITH HOLD FOR '
826
'SELECT id FROM webcatalog_nonce '
827
"WHERE created_at < (CURRENT_TIMESTAMP - INTERVAL '5' HOUR) "
828
'ORDER BY created_at',),
829
('FETCH %s FROM _oauth_nonce_clean', [1])]
830
self.run_and_check_output_and_sql(cleanup, output, sql)
832
@skipUnless(connection.vendor == 'postgresql', "Requires postgresql")
833
def test_clean_session_no_sessions(self):
835
call_command('cleanup', 'django_session')
836
output = ['Opening cursor',
837
'Removed 0 rows (0 total removed). Batch size 1',
841
('DECLARE _django_session_clean NO SCROLL CURSOR WITH HOLD FOR '
842
'SELECT session_key FROM django_session WHERE expire_date < '
843
'CURRENT_TIMESTAMP ORDER BY expire_date',),
844
('FETCH %s FROM _django_session_clean', [1])]
845
self.run_and_check_output_and_sql(cleanup, output, sql)
848
class CleanupDataTestCase(TransactionTestCase):
850
self.factory = WebCatalogObjectFactory()
851
self.patch_stdout = patch('sys.stdout')
852
self.mock_stdout = self.patch_stdout.start()
853
self.addCleanup(self.patch_stdout.stop)
855
def check_expected_output(self):
856
expected_output = '''Opening cursor
857
Removed 1 rows (1 total removed). Batch size 1
858
Removed 2 rows (3 total removed). Batch size 2
859
Removed 1 rows (4 total removed). Batch size 3
860
Removed 0 rows (4 total removed). Batch size 4
863
actual_output = ''.join(str(call[0][0])
864
for call in self.mock_stdout.write.call_args_list)
865
self.assertEqual(expected_output, actual_output)
867
@skipUnless(connection.vendor == 'postgresql', "Requires postgresql")
868
def test_cleanup_nonces(self):
871
created_at = now - timedelta(seconds=5000 * i)
872
self.factory.make_nonce(created_at=created_at)
873
self.assertEqual(8, Nonce.objects.count())
875
call_command('cleanup', 'webcatalog_nonce')
877
self.assertEqual(4, Nonce.objects.count())
878
self.check_expected_output()
880
@skipUnless(connection.vendor == 'postgresql', "Requires postgresql")
881
def test_cleanup_sessions(self):
884
expire_date = now + timedelta(seconds=5000 * (4 - i))
885
self.factory.make_session(expire_date=expire_date)
886
self.assertEqual(8, Session.objects.count())
888
call_command('cleanup', 'django_session')
890
self.assertEqual(4, Session.objects.count())
891
self.check_expected_output()