Skip to content

Commit

Permalink
Merge pull request #4613 from freelawproject/fix-make-dev-data-command
Browse files Browse the repository at this point in the history
  • Loading branch information
mlissner authored Oct 24, 2024
2 parents de984a3 + a0b18c7 commit 8c77749
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
52 changes: 49 additions & 3 deletions cl/audio/factories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from factory import Faker
from factory import Faker, post_generation
from factory.django import DjangoModelFactory, FileField
from factory.fuzzy import FuzzyChoice

Expand All @@ -15,8 +15,54 @@ class Meta:
case_name = Faker("case_name")
sha1 = Faker("sha1")
download_url = Faker("url")
local_path_mp3 = FileField(upload_to="/tmp/audio")
local_path_original_file = FileField(upload_to="/tmp/audio/")

@classmethod
def _create(cls, model_class, *args, **kwargs):
"""Creates an instance of the model class without indexing."""
obj = model_class(*args, **kwargs)
# explicitly sets `index=False` to prevent it from being indexed in SOLR.
# Once Solr is removed, we can just remove this method completely.
obj.save(index=False)
return obj

"""
These hooks are necessary to make this factory compatible with the
`make_dev_command`. by delegating the file creation to the hooks, we prevent
the model from trying to use our storage settings when the field is not
explicitly requested.
"""

@post_generation
def local_path_mp3(self, create, extracted, **kwargs):
if extracted:
self.local_path_mp3 = extracted
elif kwargs:
# Factory Boy uses the `evaluate` method of each field to calculate
# values for object creation. The FileField class only requires the
# extra dictionary to create the stub django file.
#
# Learn more about FactoryBoy's `FileField` class:
# https://github.com/FactoryBoy/factory_boy/blob/ac49fb40ec424276c3cd3ca0925ba99a626f05f7/factory/django.py#L249
self.local_path_mp3 = FileField().evaluate(None, None, kwargs)

@post_generation
def local_path_original_file(self, create, extracted, **kwargs):
if extracted:
self.local_path_original_file = extracted
elif kwargs:
self.local_path_original_file = FileField().evaluate(
None, None, kwargs
)

@classmethod
def _after_postgeneration(cls, instance, create, results=None):
"""Save again the instance if creating and at least one hook ran."""
if create and results:
# Some post-generation hooks ran, and may have modified the instance.
instance.save(
index=False,
update_fields=["local_path_mp3", "local_path_original_file"],
)


class AudioWithParentsFactory(AudioFactory, DocketParentMixin):
Expand Down
4 changes: 4 additions & 0 deletions cl/audio/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,17 @@ def setUpTestData(cls) -> None:
docket=DocketFactory(
court=cls.court_1, date_argued=datetime.date(2014, 8, 14)
),
local_path_mp3__data=b"\x10" * 10,
local_path_original_file__data=b"\x10" * 10,
duration=2000,
stt_status=Audio.STT_NEEDED,
)
cls.audio_to_be_retried = AudioFactory.create(
docket=DocketFactory(
court=cls.court_1, date_argued=datetime.date(2014, 8, 13)
),
local_path_mp3__data=b"\x10" * 10,
local_path_original_file__data=b"\x10" * 10,
duration=1000,
stt_status=Audio.STT_FAILED,
)
Expand Down
27 changes: 26 additions & 1 deletion cl/search/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
RelatedFactory,
SelfAttribute,
SubFactory,
post_generation,
)
from factory.django import DjangoModelFactory, FileField
from factory.fuzzy import FuzzyChoice, FuzzyText
Expand Down Expand Up @@ -269,6 +270,7 @@ class DocketEntryReuseParentsFactory(
class DocketFactory(DjangoModelFactory):
class Meta:
model = Docket
skip_postgeneration_save = True

source = FuzzyChoice(Docket.SOURCE_CHOICES, getter=lambda c: c[0])
court = SubFactory(CourtFactory)
Expand All @@ -281,9 +283,32 @@ class Meta:
pacer_case_id = Faker("pyint", min_value=100_000, max_value=400_000)
docket_number = Faker("federal_district_docket_number")
slug = Faker("slug")
filepath_local = FileField(filename="docket.xml")
date_argued = Faker("date_object")

"""
This hook is necessary to make this factory compatible with the
`make_dev_command` by delegating the file creation to the hook, we prevent
the model from trying to use our storage settings when the field is not
explicitly requested
"""

@post_generation
def filepath_local(self, create, extracted, **kwargs):
"""Attaches a stub file to an instance of this factory."""
if extracted:
self.filepath_local = extracted
elif kwargs:
# Factory Boy uses the `evaluate` method of each field to calculate
# values for object creation. The FileField class only requires the
# extra dictionary to create the stub django file.
#
# Learn more about FactoryBoy's `FileField` class:
# https://github.com/FactoryBoy/factory_boy/blob/ac49fb40ec424276c3cd3ca0925ba99a626f05f7/factory/django.py#L249
self.filepath_local = FileField().evaluate(None, None, kwargs)

if create:
self.save(update_fields=["filepath_local"])


class DocketWithChildrenFactory(DocketFactory):
clusters = RelatedFactory(
Expand Down

0 comments on commit 8c77749

Please sign in to comment.