def test_no_bills_in_period(self, fake_billed_org): """Before any bills""" assert not list( fake_billed_org.bills_covering_period( weeks_ago(11)(), weeks_ago(10)(), ))
def test_no_bills_after_period(self, fake_billed_org): """Only the second one, 'start' is too big to include first one""" assert list( fake_billed_org.bills_covering_period( weeks_ago(4)(), weeks_ago(3)(), )) == []
def test_both_bills_outside_range_below(self, fake_billed_org, fake_bills): """both bills, starting before the first one""" bill_1, bill_2 = fake_bills assert list( fake_billed_org.bills_covering_period( weeks_ago(11)(), weeks_ago(6)(), )) == [bill_2, bill_1]
def test_both_bills_outside_range_above(self, fake_billed_org, fake_bills): """both bills, ending after the second one""" bill_1, bill_2 = fake_bills assert list( fake_billed_org.bills_covering_period( weeks_ago(7)(), weeks_ago(3)(), )) == [bill_2, bill_1]
def test_second_bill(self, fake_billed_org, fake_bills): """Only the second one, 'start' is too big to include first one""" bill_1, bill_2 = fake_bills assert list( fake_billed_org.bills_covering_period( weeks_ago(6)(), weeks_ago(3)(), )) == [bill_2]
def test_both_bills_inside_range(self, fake_billed_org, fake_bills): """Inside the range of the bills""" bill_1, bill_2 = fake_bills assert list( fake_billed_org.bills_covering_period( weeks_ago(7)(), weeks_ago(6)(), )) == [bill_2, bill_1]
def test_first_bill(self, fake_billed_org, fake_bills): """Includes the tail end of the first bill""" bill_1, bill_2 = fake_bills assert list( fake_billed_org.bills_covering_period( weeks_ago(11)(), weeks_ago(7)(), )) == [bill_1]
def test_none_future(self, fake_billed_org): """It should not return the organization if the last bill was in the future""" BillFactory( generated_by=fake_billed_org.billed_by, period_start=weeks_ago(7)(), period_end=weeks_ago(6)(), ) assert not list( BilledOrganization.pending_for_current_period(weeks_ago(10)()))
def fix_create_bills(self, fake_billed_org): """Create a couple of bills between 7 and 5 weeks ago""" bill_1 = BillFactory( generated_by=fake_billed_org.billed_by, period_start=weeks_ago(7)(), period_end=weeks_ago(6)(), ) bill_2 = BillFactory( generated_by=fake_billed_org.billed_by, period_start=weeks_ago(6)(), period_end=weeks_ago(5)(), ) return bill_1, bill_2
def _expect_active_devices(self, fake_billed_org, expected): start = weeks_ago(3)() end = weeks_ago(2)() active_1 = fake_billed_org.devices_active_between(start, end) bill = BillFactory( generated_by=fake_billed_org.billed_by, period_start=start, period_end=end, ) active_2 = fake_billed_org.devices_active_for_bill(bill) assert list(active_1) == list(active_2) == expected
def test_pending_at_date(self, fake_billed_org): """None pending before date was created, but there is one now""" BillFactory( generated_by=fake_billed_org.billed_by, period_start=weeks_ago(7)(), period_end=weeks_ago(6)(), ) # There is already a bill for this period assert not list( BilledOrganization.pending_for_current_period(weeks_ago(6)())) # It has been 6 weeks since the last bill here pending = [ org for org in BilledOrganization.pending_for_current_period() ] assert fake_billed_org in pending
def test_get_last_bill(self, fake_billed_org): assert not fake_billed_org.last_bill() # Generate an old bill bill_old = BillFactory(generated_by=fake_billed_org.billed_by, period_end=weeks_ago(3)()) last = fake_billed_org.last_bill() assert last == bill_old assert last.generated_by == fake_billed_org.billed_by # Generate a newer bill - this one should be returned instead bill_new = BillFactory(generated_by=fake_billed_org.billed_by, period_end=weeks_ago(1)()) last = fake_billed_org.last_bill() assert last == bill_new assert last.generated_by == fake_billed_org.billed_by
def test_next_bill(self, fake_billed_org): """Test period of next bill is 1 day after billing period""" assert fake_billed_org.next_bill_on( ) == fake_billed_org.billed_by.active_from_date + period_to_delta( fake_billed_org.billed_by.period) # Generate a bill bill = BillFactory(generated_by=fake_billed_org.billed_by, period_end=weeks_ago(2)()) # The last bill period, plus a week, plus one day (bills are issued one day after the period) expected_next_end = bill.period_end + period_to_delta( fake_billed_org.billed_by.period) + one_day assert fake_billed_org.next_bill_on( weeks_ago(2)()) == expected_next_end # Same if there is no 'next_from' passed, because the last bill will # still be the one we generated above assert fake_billed_org.next_bill_on() == expected_next_end
def test_bills_covering_date(self, fake_billed_org): two_days_ago = datetime.datetime.utcnow() - datetime.timedelta(days=3) # No bill at this date assert not fake_billed_org.bill_covering_date(two_days_ago) bill = BillFactory(generated_by=fake_billed_org.billed_by, period_start=weeks_ago(1)(), period_end=datetime.datetime.utcnow()) assert fake_billed_org.bill_covering_date(two_days_ago) == bill
def test_billed_active_with_data(self, fake_billed_org): """Device with data""" device = DeviceFactory() device.orgs.add(fake_billed_org) device.online = True device.save() sensor = DeviceSensorFactory(device=device, ) TimeSeriesDataFactory(sensor=sensor, ts=weeks_ago(3)() + datetime.timedelta(days=2)) self._expect_active_devices(fake_billed_org, [device])
def test_billed_active_multiple_data_no_repeats(self, fake_billed_org): """If there's multiple sensors/ts readings, it should still only return 1 device""" device = DeviceFactory() device.orgs.add(fake_billed_org) device.online = True device.save() for i in range(3): sensor = DeviceSensorFactory( device=device, sensor_type__sensor_name="Sensor type {}".format(i), ) TimeSeriesDataFactory(sensor=sensor, ts=weeks_ago(3)() + datetime.timedelta(days=i + 1)) self._expect_active_devices(fake_billed_org, [device])
def fix_fake_bill_old(db, fake_bill_generator, fakedevice): bill = BillFactory(generated_by=fake_bill_generator, period_start = factory.LazyFunction(weeks_ago(3)), period_end = factory.LazyFunction(weeks_ago(2))) bill.devices.add(fakedevice) return bill
def test_none_pending(self, fake_billed_org): """No bills pending at all""" assert not list(BilledOrganization.pending_for_current_period()) assert not list( BilledOrganization.pending_for_current_period(weeks_ago(6)()))