Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
Django 3.0 compat: Avoid pickle of local object
Browse files Browse the repository at this point in the history
If the FireSimpleEvent class is defined in the function, then an error
is raised:

AttributeError: Can't pickle local object 'EventUnionTests.test_fire.<locals>.FireSimpleEvent'

Pickling works when the class is defined at the module level, but the
object attribute .called is set on the deserialized copy, not the
original. Refactor the test class to work on a global variable instead.
  • Loading branch information
jwhitlock committed Mar 29, 2019
1 parent cf27336 commit 4b28463
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ class FilteredContentTypeEvent(ContentTypeEvent):
filters = set(['color', 'flavor'])


fire_simple_event_called = False


class FireSimpleEvent(SimpleEvent):
def _mails(self, users_and_watches):
global fire_simple_event_called
fire_simple_event_called = True
return []


class UsersWatchingTests(TestCase):
"""Unit tests for Event._users_watching_by_filter()"""

Expand Down Expand Up @@ -262,19 +272,13 @@ def _users_watching(self):

def test_fire(self):
"""Assert firing the union gets the mails from the first event."""

class FireSimpleEvent(SimpleEvent):
called = False

def _mails(self, users_and_watches):
self.called = True
return []

global fire_simple_event_called
watch(event_type=TYPE, email='he@llo.com').save()
simple_event = FireSimpleEvent()
fire_simple_event_called = False
another_event = AnotherEvent()
EventUnion(simple_event, another_event).fire()
self.assertTrue(simple_event.called)
self.assertTrue(fire_simple_event_called)

def test_watch_lists(self):
"""Ensure the Union returns every watch a user has."""
Expand Down

0 comments on commit 4b28463

Please sign in to comment.