def test_get_next_billing_log_entry(self) -> None: second_realm = Realm.objects.create(string_id='second', name='second') entry1 = self.add_log_entry(realm=second_realm) realm_processor = BillingProcessor.objects.create( realm=second_realm, log_row=entry1, state=BillingProcessor.DONE) entry2 = self.add_log_entry() # global processor processor = BillingProcessor.objects.create( log_row=entry2, state=BillingProcessor.STARTED) # Test STARTED, STALLED, and typo'ed state entry self.assertEqual(entry2, get_next_billing_log_entry(processor)) processor.state = BillingProcessor.STALLED processor.save() with self.assertRaises(AssertionError): get_next_billing_log_entry(processor) processor.state = 'typo' processor.save() with self.assertRaisesRegex(BillingError, 'unknown processor state'): get_next_billing_log_entry(processor) # Test global processor is handled correctly processor.state = BillingProcessor.DONE processor.save() # test it ignores entries with requires_billing_update=False entry3 = self.add_log_entry(requires_billing_update=False) # test it ignores entries with realm processors entry4 = self.add_log_entry(realm=second_realm) self.assertIsNone(get_next_billing_log_entry(processor)) # test it does catch entries it should entry5 = self.add_log_entry() self.assertEqual(entry5, get_next_billing_log_entry(processor)) # Test realm processor is handled correctly # test it gets the entry with its realm, and ignores the entry with # requires_billing_update=False, when global processor is up ahead processor.log_row = entry5 processor.save() self.assertEqual(entry4, get_next_billing_log_entry(realm_processor)) # test it doesn't run past the global processor processor.log_row = entry3 processor.save() self.assertIsNone(get_next_billing_log_entry(realm_processor))