def test_handle_dependents_to_singleton(year_began, key_date): with mock.patch('%s.save' % IREV) as save_mock, \ mock.patch('%s.commit_to_display' % IREV) as commit_mock: # Make the IssueRevision that would be returned by the patched # constructor call. Only patch the methods for this. s = Series() c = Changeset() ir_params = { 'changeset': c, 'series': s, 'after': None, 'number': '[nn]', 'publication_date': year_began, } ir = IssueRevision(**ir_params) with mock.patch(IREV) as ir_class_mock: # Now patch the IssueRevision constructor itself. ir_class_mock.return_value = ir sr = SeriesRevision(changeset=c, series=s, is_singleton=True, year_began=year_began) sr._handle_dependents({'to is_singleton': True}) ir_class_mock.assert_called_once_with(**ir_params) assert ir.key_date == key_date save_mock.assert_called_once_with() assert not commit_mock.called
def test_get_major_changes_added(patched_series_class): new = SeriesRevision(changeset=NO_DB_CHANGESET, series=None, country=COUNTRY_TWO, language=LANGUAGE_TWO, publisher=PUBLISHER_TWO, is_comics_publication=True, is_current=True, is_singleton=False) c = new._get_major_changes() assert c == { 'publisher changed': True, 'country changed': True, 'language changed': True, 'is_comics_publication changed': True, 'is_singleton changed': True, 'is_current changed': True, 'to is_comics_publication': True, 'from is_comics_publication': False, 'to is_singleton': False, 'from is_singleton': False, 'to is_current': True, 'from is_current': False, 'old publisher': None, 'new publisher': PUBLISHER_TWO, 'old country': None, 'new country': COUNTRY_TWO, 'old language': None, 'new language': LANGUAGE_TWO, }
def fix_series(): old_series = Series.objects.filter( created__lte=settings.NEW_SITE_CREATION_DATE) no_prior_revs = old_series.exclude( revisions__created__lte=settings.NEW_SITE_CREATION_DATE) print "%d series to fix..." % no_prior_revs.count() for series in no_prior_revs: print "Fixing series %s" % series old = VieuxBoisSeries.objects.get(pk=series.pk) # Create changeset changeset = Changeset(indexer=ANON_USER, approver=ANON_USER, state=states.APPROVED, change_type=CTYPES['series'], migrated=True, date_inferred=True) changeset.save() # Circumvent automatic field behavior. # all dates and times guaranteed to be valid mc = MigratoryChangeset.objects.get(id=changeset.id) mc.created = old.created mc.modified = old.modified mc.save() changeset = Changeset.objects.get(id=changeset.id) comment_text = COMMENT_TEXT + COMMENT_TEXT_FOR_ADD changeset.comments.create(commenter=ANON_USER, text=comment_text, old_state=states.APPROVED, new_state=states.APPROVED) comment = changeset.comments.all()[0] comment.created = changeset.created comment.save() revision = SeriesRevision(changeset=changeset, series=series, country=old.country, language=old.language, publisher=old.publisher, name=old.name, imprint=old.imprint, format=old.format, year_began=old.year_began, year_ended=old.year_ended, publication_notes=old.publication_notes, tracking_notes=old.tracking_notes, notes=old.notes, date_inferred=True) revision.save() # Circumvent automatic field behavior. mr = MigratorySeriesRevision.objects.get(id=revision.id) mr.created = changeset.created mr.modified = changeset.created mr.save()
def test_handle_prerequisites_deleted_singleton(): ir_mock = mock.MagicMock() with mock.patch('%s.clone' % IREV, return_value=ir_mock, spec=IssueRevision), \ mock.patch('%s.series' % SREV), \ mock.patch('%s.save' % SREV), \ mock.patch('%s.commit_to_display' % SREV): s = SeriesRevision(changeset=Changeset(), previous_revision=SeriesRevision()) s.deleted = True s.series.is_singleton = True
def test_handle_prerequisites_deleted_singleton(): ir_mock = mock.MagicMock() with mock.patch('%s.clone' % IREV, return_value=ir_mock, spec=IssueRevision), \ mock.patch('%s.series' % SREV), \ mock.patch('%s.save' % SREV), \ mock.patch('%s.commit_to_display' % SREV): s = SeriesRevision(changeset=Changeset(), previous_revision=SeriesRevision()) s.deleted = True s.series.is_singleton = True
def test_create_edit_revision(any_added_series, series_add_values, any_editing_changeset, keywords): # Simple version of this for mocking. Real article testing elsewhere. sort_name = any_added_series.name[any_added_series.name.index(' ') + 1:] with mock.patch('apps.oi.models.remove_leading_article') as remover: remover.return_value = sort_name rev = SeriesRevision.clone(data_object=any_added_series, changeset=any_editing_changeset) for k, v in series_add_values.items(): if k == 'keywords': # rev.###.keywords.names() gives wrong result for 'Bar', 'bar' kws = [k.name for k in rev.series.keywords.all()] kws.sort() assert kws == keywords['list'] else: assert getattr(rev, k) == v assert rev.series is any_added_series assert rev.reservation_requested is False assert rev.changeset == any_editing_changeset assert rev.date_inferred is False assert rev.source is any_added_series assert rev.source_name == 'series'
def test_get_major_changes_deleted(patched_series_class): old = Series(country=COUNTRY_ONE, language=LANGUAGE_ONE, publisher=PUBLISHER_ONE, is_comics_publication=True, is_current=True, is_singleton=False) new = SeriesRevision(changeset=NO_DB_CHANGESET, series=old, country=COUNTRY_ONE, language=LANGUAGE_ONE, publisher=PUBLISHER_ONE, is_comics_publication=True, is_current=True, is_singleton=False, previous_revision=SeriesRevision(), deleted=True) c = new._get_major_changes() assert c == { 'publisher changed': True, 'country changed': True, 'language changed': True, 'is_comics_publication changed': True, 'is_singleton changed': True, 'is_current changed': True, 'to is_comics_publication': False, 'from is_comics_publication': True, 'to is_singleton': False, 'from is_singleton': False, 'to is_current': False, 'from is_current': True, 'old publisher': PUBLISHER_ONE, 'new publisher': None, 'old country': COUNTRY_ONE, 'new country': None, 'old language': LANGUAGE_ONE, 'new language': None, }
def test_get_major_changes_deleted(patched_series_class): old = Series(country=COUNTRY_ONE, language=LANGUAGE_ONE, publisher=PUBLISHER_ONE, is_comics_publication=True, is_current=True, is_singleton=False) new = SeriesRevision(changeset=NO_DB_CHANGESET, series=old, country=COUNTRY_ONE, language=LANGUAGE_ONE, publisher=PUBLISHER_ONE, is_comics_publication=True, is_current=True, is_singleton=False, previous_revision=SeriesRevision(), deleted=True) c = new._get_major_changes() assert c == { 'publisher changed': True, 'country changed': True, 'language changed': True, 'is_comics_publication changed': True, 'is_singleton changed': True, 'is_current changed': True, 'to is_comics_publication': False, 'from is_comics_publication': True, 'to is_singleton': False, 'from is_singleton': False, 'to is_current': False, 'from is_current': True, 'old publisher': PUBLISHER_ONE, 'new publisher': None, 'old country': COUNTRY_ONE, 'new country': None, 'old language': LANGUAGE_ONE, 'new language': None, }
def test_create_edit_revision(any_added_series, series_add_values, any_editing_changeset): # Simple version of this for mocking. Real article testing elsewhere. sort_name = any_added_series.name[any_added_series.name.index(' ') + 1:] with mock.patch('apps.oi.models.remove_leading_article') as remover: remover.return_value = sort_name rev = SeriesRevision.clone(data_object=any_added_series, changeset=any_editing_changeset) for k, v in series_add_values.items(): assert getattr(rev, k) == v assert rev.series is any_added_series assert rev.reservation_requested is False assert rev.changeset == any_editing_changeset assert rev.date_inferred is False assert rev.source is any_added_series assert rev.source_name == 'series'
def test_create_edit_revision(any_added_series, series_add_values, any_editing_changeset): # Simple version of this for mocking. Real article testing elsewhere. sort_name = any_added_series.name[any_added_series.name.index(' ') + 1:] with mock.patch('apps.oi.models.remove_leading_article') as remover: remover.return_value = sort_name rev = SeriesRevision.clone(data_object=any_added_series, changeset=any_editing_changeset) for k, v in series_add_values.iteritems(): assert getattr(rev, k) == v assert rev.series is any_added_series assert rev.reservation_requested is False assert rev.changeset == any_editing_changeset assert rev.date_inferred is False assert rev.source is any_added_series assert rev.source_name == 'series'
def series_and_revision(): """ Tuple of series, series revision, and a mock of update_all_counts. For use with _adjust_stats() testing. The series and series revision are connected but not saved, and database access is patched. """ # Don't use the "constant" PUBLISHER_ONE/TWO as we expect # the mock call state to be changed. old_pub = mock.MagicMock() new_pub = mock.MagicMock() with mock.patch('%s.update_cached_counts' % SERIES), \ mock.patch('%s.publisher' % SERIES), \ mock.patch('%s.save' % SERIES), \ mock.patch('apps.gcd.models.publisher.Publisher.save'), \ mock.patch('%s.changeset' % SREV), \ mock.patch('%s.publisher' % SREV), \ mock.patch('%s.objects.update_all_counts' % CSTATS) as uac_mock: s = Series(publisher=old_pub) rev = SeriesRevision(changeset=mock.MagicMock(), series=s, publisher=new_pub) yield s, rev, uac_mock
def any_added_series_rev(any_adding_changeset, series_add_values): sr = SeriesRevision(changeset=any_adding_changeset, leading_article=True, **series_add_values) sr.save() return sr
def any_added_series_rev(any_adding_changeset, series_add_values): sr = SeriesRevision(changeset=any_adding_changeset, leading_article=True, **series_add_values) sr.save() return sr
def test_handle_dependents_no_singleton(): with mock.patch(IREV) as ir_class_mock: sr = SeriesRevision() sr._handle_dependents({'to is_singleton': False}) assert ir_class_mock.called is False
def pre_save_mocks(): with mock.patch('%s.get_ongoing_reservation' % SERIES) as get_ongoing, \ mock.patch('apps.gcd.models.series.Series.scan_count', new_callable=mock.PropertyMock) as scan_count: yield SeriesRevision(series=Series()), get_ongoing, scan_count
def test_post_assign_fields_leading_article(leading_article, name, sort_name): s = Series(name=name) sr = SeriesRevision(leading_article=leading_article, name=name, series=s) sr._post_assign_fields({}) assert s.sort_name == sort_name
def test_excluded_fields(): assert SeriesRevision._get_excluded_field_names() == { 'open_reserve', 'publication_dates', } | Revision._get_excluded_field_names()
def test_handle_dependents_no_singleton(): with mock.patch(IREV) as ir_class_mock: sr = SeriesRevision() sr._handle_dependents({'to is_singleton': False}) assert ir_class_mock.called is False
def test_post_assign_fields_leading_article(leading_article, name, sort_name): s = Series(name=name) sr = SeriesRevision(leading_article=leading_article, name=name, series=s) sr._post_assign_fields({}) assert s.sort_name == sort_name
def test_excluded_fields(): assert SeriesRevision._get_excluded_field_names() == { 'open_reserve', 'publication_dates', } | Revision._get_excluded_field_names()