def test_no_template(self): user = User.objects.create(username="******", email="*****@*****.**") events = [ { "group": "stuff_mail", "key": "stuff_mail_1", "context": { "user": user, "spark_mail": { "to": [user.email] } }, }, { "group": "stuff_stuff", "key": "stuff_stuff_1", "context": {} }, ] api.process_mail_events(only_new_events(events)) api.process_mail_events(only_new_events(events)) # Twice. self.assertEqual(len(mail.outbox), 1) sent = mail.outbox[0] self.assertEqual(sent.to, [user.email])
def test_crashing_template(self): def crash(): raise ValueError("Crash me please") Mail.objects.create(event_group="stuff_mail", template_source="{{ hello }}") events = [{ "group": "stuff_mail", "key": "stuff_mail_3", "context": { "hello": crash, "spark_mail": { "to": ["*****@*****.**"], "subject": "Would be overridden", "body": "Yes.", }, }, }] api.process_mail_events(only_new_events(events)) api.process_mail_events(only_new_events(events)) # Twice. self.assertEqual(len(mail.outbox), 1) sent = mail.outbox[0] self.assertEqual(sent.to, ["*****@*****.**"]) self.assertEqual(sent.subject, "Would be overridden") self.assertEqual(sent.body, "Yes.")
def test_empty_template(self): Mail.objects.create(event_group="stuff_mail", template_source="") user = User.objects.create(username="******", email="*****@*****.**") stuff = Stuff.objects.create(key="asdf") events = [ { "group": "stuff_mail", "key": "stuff_mail_2", "context": { "key": stuff.key, "key_length": len(stuff.key), "user": user, "spark_mail": { "to": [user.email], "subject": "Would be overridden if template had content", }, }, }, { "group": "stuff_stuff", "key": "stuff_stuff_1", "context": {} }, ] api.process_mail_events(only_new_events(events)) api.process_mail_events(only_new_events(events)) # Twice. self.assertEqual(len(mail.outbox), 1) sent = mail.outbox[0] self.assertEqual(sent.to, [user.email]) self.assertEqual(sent.subject, "Would be overridden if template had content")
def test_spark(self): global events self.assertEqual(events, []) stuff = Stuff.objects.create(key="first") event = Event.objects.get() # Exactly one self.assertEqual(event.key, "stuff_created_{}".format(stuff.pk)) self.assertEqual(event.group, "stuff_created") self.assertEqual(event.key, "{}".format(event)) self.assertEqual( event.context, "{{'stuff': <Stuff: Stuff object ({})>}}".format(stuff.pk)) self.assertEqual(len(events), 1) self.assertEqual(events[0]["context"]["stuff"], stuff) # Create some duplicates for stuff in Stuff.objects.all(): events_to_process = list(events_from_stuff(stuff)) self.assertTrue(len(events_to_process), 1) api.process_events(api.only_new_events(events_to_process)) # But still only one event self.assertEqual(Event.objects.count(), 1) self.assertEqual(len(events), 1)
def test_mails(self): m = Mail.objects.create(event_group="stuff_mail", template_source=MAIL) m.full_clean() # no exceptions user = User.objects.create(username="******", email="*****@*****.**") stuff = Stuff.objects.create(key="asdf") kwargs = m.render({ "user": user, "key": stuff.key, "key_length": len(stuff.key) }) self.assertEqual(kwargs["subject"], "Dear test") self.assertEqual(kwargs["body"], "This is the key: asdf (4)\n\nBest regards") events = [ { "group": "stuff_mail", "key": "stuff_mail_1", "context": { "key": stuff.key, "key_length": len(stuff.key), "user": user, "spark_mail": { "to": [user.email] }, }, }, { "group": "stuff_stuff", "key": "stuff_stuff_1", "context": {} }, ] api.process_mail_events(only_new_events(events)) api.process_mail_events(only_new_events(events)) # Twice. self.assertEqual(len(mail.outbox), 1) sent = mail.outbox[0] self.assertEqual(sent.to, [user.email]) self.assertEqual(sent.subject, "Dear test")
def test_generators_without_model(self): generator = { "candidates": lambda: Stuff.objects.all(), "conditions": [{ "variable": "key_length", "test": lambda v: v < 3 }], "context": stuff_context, "group": "stuff_no_model_test", } [Stuff.objects.create(key="".join(["x"] * x)) for x in range(1, 11)] # Select stuffs, savepoint/insert/release of two events with self.assertNumQueries(1 + 3 * 2): list(only_new_events(api.events_from_generators([generator]))) with self.assertNumQueries(1 + 4 * 2): list(only_new_events(api.events_from_generators([generator])))
def test_lazyness(self): # mails, two events user = User.objects.create(username="******", email="*****@*****.**") with self.assertNumQueries(4): api.process_mail_events( only_new_events([{ "group": "stuff_mail", "key": "stuff_mail_1", "context": { "user": user, "spark_mail": { "to": [user.email] }, }, }])) with self.assertNumQueries(3): # No mails, but an event api.process_mail_events( only_new_events([{ "group": "stuff_mail", "key": "stuff_mail_2", "context": { "user": user }, }]))
def handle(self, **options): for model, sources in api.MODEL_SOURCES.items(): for instance in model.objects.all(): # FIXME allow restrictions for source in sources: api.process_events(api.only_new_events(source(instance)))
def new_events_from_generators(): return only_new_events(api.events_from_generators())