~stephen-stewart/ubuntu-webcatalog/unfubar-js

« back to all changes in this revision

Viewing changes to src/webcatalog/tests/test_commands.py

[r=michael.nelson],[bug=1015604] Add admin class for ReviewStatsImport and optional --download-all flag for import_ratings_stats.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
from django.test.testcases import TransactionTestCase
39
39
 
40
40
from mock import (
 
41
    call,
41
42
    patch,
42
43
    MagicMock,
43
44
    Mock,
773
774
        self.mock_review_stats.assert_called_with(
774
775
            origin='any', distroseries='onion', days=3)
775
776
 
 
777
    def test_force_download_all_stats(self):
 
778
        # If the download_all option is used, all stats are
 
779
        # downloaded, rather than the optimised last X days.
 
780
        onion = self.factory.make_distroseries(code_name='onion')
 
781
        orig_timestamp = datetime.utcnow() - timedelta(2)
 
782
        import_record = ReviewStatsImport.objects.create(
 
783
            distroseries=onion, last_import=orig_timestamp)
 
784
 
 
785
        call_command('import_ratings_stats', 'onion', download_all=True)
 
786
 
 
787
        self.mock_review_stats.assert_called_with(origin='any',
 
788
                                                  distroseries='onion')
 
789
 
776
790
    def test_update_app_stats(self):
777
791
        # Any stats provided in the api call will be used to update our
778
792
        # apps in the db.
1106
1120
 
1107
1121
 
1108
1122
class ImportAllRatingsStatsTestCase(TestCaseWithFactory):
1109
 
    @patch('sys.stdout')
1110
 
    def test_handle(self, mock_stdout):
1111
 
        patch_attr = ('webcatalog.management.commands.'
1112
 
                      'import_all_ratings_stats.call_command')
1113
 
        with patch_settings(UBUNTU_SERIES_FOR_VERSIONS=TEST_VERSIONS):
1114
 
            with patch(patch_attr) as mock_call_command:
1115
 
                call_command('import_all_ratings_stats')
1116
 
 
1117
 
            self.assertEqual(mock_call_command.call_count, len(TEST_VERSIONS))
 
1123
 
 
1124
    def setUp(self):
 
1125
        patcher = patch('webcatalog.management.commands.'
 
1126
                        'import_all_ratings_stats.call_command')
 
1127
        self.mock_call_command = patcher.start()
 
1128
        self.addCleanup(patcher.stop)
 
1129
 
 
1130
        patcher = patch('sys.stdout')
 
1131
        patcher.start()
 
1132
        self.addCleanup(patcher.stop)
 
1133
 
 
1134
    def test_handle_download(self):
 
1135
        with patch_settings(UBUNTU_SERIES_FOR_VERSIONS=TEST_VERSIONS):
 
1136
            call_command('import_all_ratings_stats')
 
1137
 
 
1138
        expected_calls = [
 
1139
            call('import_ratings_stats', series, verbosity=1,
 
1140
                 download_all=False) for series in TEST_VERSIONS.values()]
 
1141
        self.mock_call_command.assert_has_calls(expected_calls,
 
1142
                                                any_order=True)
 
1143
 
 
1144
    def test_handle_download_all(self):
 
1145
        with patch_settings(UBUNTU_SERIES_FOR_VERSIONS=TEST_VERSIONS):
 
1146
            call_command('import_all_ratings_stats', download_all=True)
 
1147
 
 
1148
        expected_calls = [
 
1149
            call('import_ratings_stats', series, verbosity=1,
 
1150
                 download_all=True) for series in TEST_VERSIONS.values()]
 
1151
        self.mock_call_command.assert_has_calls(expected_calls,
 
1152
                                                any_order=True)