def test_create_range_for_stats_async( self, transactional_db, broker: stub.StubBroker, worker: dramatiq.Worker, hosting_provider_with_sample_user: ac_models.Hostingprovider, green_ip: gc_models.GreencheckIp, client, ): """ Create a collection of daily stats, for a range of dates provided. """ broker.declare_queue("default") generated_dates = self._set_up_dates_for_last_week() for date in generated_dates: gc = gc_factories.GreencheckFactory.create(date=date + relativedelta(hours=2)) # logger.info(f"gc {date}: {gc.__dict__}") logger.info(f"just this date: { generated_dates[0] }") gc_models.DailyStat.create_jobs_for_date_range_async( generated_dates, "total_count") # Wait for all the tasks to be processed broker.join("default") worker.join() green_stats = gc_models.DailyStat.objects.filter( green=gc_choices.BoolChoice.YES) grey_stats = gc_models.DailyStat.objects.filter( green=gc_choices.BoolChoice.NO) mixed_stats = gc_models.DailyStat.objects.exclude( green__in=[gc_choices.BoolChoice.YES, gc_choices.BoolChoice.NO]) # have we generated the expected stats per day? assert green_stats.count() == 7 assert grey_stats.count() == 7 assert mixed_stats.count() == 7 # we should one count showing zero green checks for each day assert [stat.count for stat in green_stats] == [0, 0, 0, 0, 0, 0, 0] # mixed and grey should be the same assert [stat.count for stat in grey_stats] == [1, 1, 1, 1, 1, 1, 1] assert [stat.count for stat in grey_stats] == [1, 1, 1, 1, 1, 1, 1]
def test_create_stat_async( self, transactional_db, broker: stub.StubBroker, worker: dramatiq.Worker, hosting_provider_with_sample_user: ac_models.Hostingprovider, green_ip: gc_models.GreencheckIp, client, ): """ Create a collection of daily stats, for a range of dates provided, but have a worker create the stats asynchronously. """ broker.declare_queue("default") assert gc_models.DailyStat.objects.count() == 0 # set up our date range generated_dates = self._set_up_dates_for_last_week() for date in generated_dates: gc_factories.GreencheckFactory.create(date=date + relativedelta(hours=2)) chosen_date = str(generated_dates[0].date()) # we use the 'send' with the 'transactional_db' fixture here instead of db # because if we use the regular db fixture, the workers can not see what is # happening 'inside' this test. TODO: check that this really is the # explanation for this strange test behaviour gc_tasks.create_stat_async.send(date_string=chosen_date, query_name="total_count") # import ipdb # ipdb.set_trace() # Wait for all the tasks to be processed broker.join(gc_tasks.create_stat_async.queue_name) worker.join() # import ipdb # ipdb.set_trace() # hae we generate the daily stats? assert gc_models.DailyStat.objects.count() == 3 # do that they have the right date? for stat in gc_models.DailyStat.objects.all(): assert str(stat.stat_date) == chosen_date # do the stats count up to what we expect? green_daily_stat = gc_models.DailyStat.objects.filter( green=gc_choices.BoolChoice.YES).first() grey_daily_stat = gc_models.DailyStat.objects.filter( green=gc_choices.BoolChoice.NO).first() mixed_daily_stat = gc_models.DailyStat.objects.exclude( green__in=[gc_choices.BoolChoice.YES, gc_choices.BoolChoice.NO ]).first() assert green_daily_stat.count == 0 assert grey_daily_stat.count == 1 assert mixed_daily_stat.count == 1