def test_weekly_business_ordering(default_client): """weekly_business should be ordered by year then week""" oldest = WeeklyBusinessFactory(year=today().year + 1, week=2) middel = WeeklyBusinessFactory(year=today().year + 1, week=10) newest = WeeklyBusinessFactory(year=today().year + 2, week=4) response = default_client.get(_get_weekly_business_url()) response = response.json() assert response["results"][0].get("business_name") == oldest.business_name assert response["results"][1].get("business_name") == middel.business_name assert response["results"][2].get("business_name") == newest.business_name
def get_queryset(self): """ Return all non-expired weekly_business by default. Filter expired events based on url query parameter. """ if "expired" in self.request.query_params: return WeeklyBusiness.objects.all().order_by("year", "week") if "this_week" in self.request.query_params: return WeeklyBusiness.objects.filter(year=today().year, week=week_nr(today())) in_future_this_year_filter = Q(year=today().year) & Q( week__gte=week_nr(today())) next_year_filter = Q(year__gt=today().year) return WeeklyBusiness.objects.filter((in_future_this_year_filter) | next_year_filter).order_by( "year", "week")
def test_create_weekly_business_last_year(): """ Test if possible to create weekly_business object last year """ with pytest.raises(ValueError) as v_err: WeeklyBusinessFactory(year=(today().year) - 1) assert "Ukens bedrift kan ikke opprettes i fortiden" == str(v_err.value)
def weekly_business_post_data(): return { "image": "https://miro.medium.com/max/256/0*kFOfXBVPI_ZJvTa-.", "image_alt": "index_logo", "business_name": "Index", "body": "Lorem ipsum penum rektum benedikt gagge", "year": today().year + 1, "week": randint(1, 52), }
class WeeklyBusinessFactory(DjangoModelFactory): class Meta: model = WeeklyBusiness business_name = factory.Faker("name") body = factory.Faker("sentence", nb_words=100, variable_nb_words=True) year = today().year + 1 week = randint(1, 52)
def save(self, *args, **kwargs): if self.week < 1 or self.week > 52: raise ValueError("Uke må være mellom 1 og 52") if self.year < today().year: raise ValueError("Ukens bedrift kan ikke opprettes i fortiden") try: if WeeklyBusiness.objects.get(week=self.week, year=self.year).id != self.id: raise ValueError( "Finnes allerede en ukens bedrift for denne uken") except WeeklyBusiness.DoesNotExist: pass super().save(*args, **kwargs)
def test_create_weekly_business_next_year(): """ Test if possible to create weekly_business object next year """ weekly_business = WeeklyBusinessFactory(year=(today().year) + 1) assert weekly_business.year == (today().year) + 1
def test_create_weekly_business_this_year(weekly_business): """ Test if possible to create weekly_business object this year """ assert weekly_business.year == today().year + 1