Skip to content

Commit

Permalink
fixed how to factory generate dates so they are relative to a fixed d…
Browse files Browse the repository at this point in the history
…ate 2020, 1, 1 instead of 'today'
  • Loading branch information
mmmavis committed Jan 25, 2024
1 parent 2b615ea commit d079754
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
14 changes: 4 additions & 10 deletions network-api/networkapi/news/factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import timezone
from datetime import datetime, timezone
from random import shuffle

from django.conf import settings
Expand Down Expand Up @@ -43,23 +43,17 @@ class Params:
else Faker("future_datetime", end_date="+30d", tzinfo=timezone.utc)
)
expired = Trait(
expires=Faker("date_time", tzinfo=timezone.utc)
if RANDOM_SEED and not TESTING
else Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc)
Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
)
video = Trait(is_video=True)

headline = LazyAttribute(lambda o: o.headline_sentence.rstrip("."))
outlet = Faker("company")
date = Faker("date") if RANDOM_SEED and not TESTING else Faker("past_date", start_date="-30d")
date = Faker("past_date", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
link = Faker("url")
excerpt = Faker("paragraph", nb_sentences=3, variable_nb_sentences=True)
author = Faker("name")
publish_after = (
Faker("date_time", tzinfo=timezone.utc)
if RANDOM_SEED and not TESTING
else Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc)
)
publish_after = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)

# LazyAttribute helper value
headline_sentence = Faker("sentence", nb_words=4)
Expand Down
7 changes: 2 additions & 5 deletions network-api/networkapi/wagtailpages/factory/blog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import timezone
from datetime import datetime, timezone
from random import choice

from django.conf import settings
Expand Down Expand Up @@ -88,10 +88,7 @@ class Meta:

title = LazyAttribute(lambda o: o.title_text.rstrip("."))
body = Faker("streamfield", fields=blog_body_streamfield_fields)
first_published_at = (
Faker("date_time", tzinfo=timezone.utc)
if RANDOM_SEED and not TESTING
else Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc)
first_published_at = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
)
search_description = Faker("paragraph", nb_sentences=5, variable_nb_sentences=True)
live = True
Expand Down
10 changes: 5 additions & 5 deletions network-api/networkapi/wagtailpages/factory/buyersguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class Meta:
blurb = Faker("sentence")
product_url = Faker("url")
worst_case = Faker("sentence")
first_published_at = Faker("past_datetime", start_date="-2d", tzinfo=timezone.utc)
last_published_at = Faker("past_datetime", start_date="-1d", tzinfo=timezone.utc)
first_published_at = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
last_published_at = Faker("date_time_between", start_date=first_published_at, end_date="+6m", tzinfo=timezone.utc)
evaluation = SubFactory("networkapi.wagtailpages.factory.buyersguide.ProductPageEvaluationFactory")
locale = LazyFunction(lambda: Locale.get_default())

Expand Down Expand Up @@ -246,7 +246,7 @@ class Meta:

title = Faker("sentence")
hero_image = SubFactory(ImageFactory)
first_published_at = Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc)
first_published_at = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
search_image = SubFactory(ImageFactory)
search_description = Faker("paragraph", nb_sentences=5, variable_nb_sentences=True)
body = Faker(
Expand All @@ -271,7 +271,7 @@ class Meta:
header = Faker("sentence")
title = Faker("sentence")
cta = SubFactory(PetitionFactory)
first_published_at = Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc)
first_published_at = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
narrowed_page_content = Faker("boolean", chance_of_getting_true=50)
body = Faker(
provider="streamfield",
Expand All @@ -292,7 +292,7 @@ class Meta:

year = Faker("random_element", elements=(("2023", "2023"))) # Add extra years here once available
title = LazyAttribute(lambda o: f"Annual Consumer Creepometer {o.year}")
first_published_at = Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc)
first_published_at = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
search_image = SubFactory(ImageFactory)
search_description = Faker("paragraph", nb_sentences=5, variable_nb_sentences=True)

Expand Down
9 changes: 3 additions & 6 deletions network-api/networkapi/wagtailpages/factory/publication.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import timezone
from datetime import datetime, timezone
from random import randint, random, shuffle

from django.conf import settings
Expand Down Expand Up @@ -27,7 +27,7 @@ class PublicationPageFactory(PageFactory):
title = Faker("text", max_nb_chars=120)
subtitle = Faker("text", max_nb_chars=250)
secondary_subtitle = Faker("text", max_nb_chars=250)
publication_date = Faker("date_object")
first_published_at = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
hero_image = SubFactory(ImageFactory)
publication_file = SubFactory(DocumentFactory)
intro_notes = Faker("sentence")
Expand All @@ -53,10 +53,7 @@ class Meta:
publication_date = Faker("date_object")
article_file = SubFactory(DocumentFactory)
body = Faker("streamfield", fields=article_body_streamfield_fields)
first_published_at = (
Faker("date_time", tzinfo=timezone.utc)
if RANDOM_SEED and not TESTING
else Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc)
first_published_at = Faker("past_datetime", start_date=datetime(2020, 1, 1), tzinfo=timezone.utc)
)
search_description = Faker("paragraph", nb_sentences=5, variable_nb_sentences=True)
live = True
Expand Down

0 comments on commit d079754

Please sign in to comment.