Fix unit test
Also fix factory magic for blocks that have contents defined using the `wagtail_factories` syntax
This commit is contained in:
parent
75ecaedd29
commit
d0fb5c5fe4
|
|
@ -61,8 +61,14 @@ class BookFactory(BasePageFactory):
|
|||
site = wagtail_factories.SiteFactory.create(is_default_site=True)
|
||||
Page.objects.get(title="Root").delete()
|
||||
|
||||
logger.info(
|
||||
"BookFactory::create_default_structure: creating default book structure"
|
||||
)
|
||||
|
||||
book = BookFactory.create(parent=site.root_page, title="A book")
|
||||
logger.info("BookFactory::create_default_structure: created book")
|
||||
topic = TopicFactory.create(parent=book, order=1, title="A topic")
|
||||
logger.info("BookFactory::create_default_structure: created topic")
|
||||
module = ModuleFactory.create(
|
||||
parent=topic,
|
||||
title="A module",
|
||||
|
|
@ -70,7 +76,9 @@ class BookFactory(BasePageFactory):
|
|||
teaser="Whatever",
|
||||
intro="<p>Hello</p>",
|
||||
)
|
||||
logger.info("BookFactory::create_default_structure: created module")
|
||||
chapter = ChapterFactory.create(parent=module, title="A chapter")
|
||||
logger.info("BookFactory::create_default_structure: created chapter")
|
||||
content_block = ContentBlockFactory.create(
|
||||
parent=chapter,
|
||||
module=module,
|
||||
|
|
@ -78,6 +86,7 @@ class BookFactory(BasePageFactory):
|
|||
type="task",
|
||||
contents=[],
|
||||
)
|
||||
logger.info("BookFactory::create_default_structure: created content_block")
|
||||
|
||||
return book, topic, module, chapter, content_block
|
||||
|
||||
|
|
@ -224,7 +233,7 @@ class ContentBlockFactory(BasePageFactory):
|
|||
lambda x: random.choice(
|
||||
[
|
||||
"normal",
|
||||
# "instrument",
|
||||
# "instrument", # todo: re-enable when Factory exists
|
||||
"task",
|
||||
]
|
||||
)
|
||||
|
|
@ -249,15 +258,23 @@ class ContentBlockFactory(BasePageFactory):
|
|||
@classmethod
|
||||
def stream_field_magic(cls, module, kwargs, stream_field_name):
|
||||
user = get_user_model().objects.first()
|
||||
logger.debug(f"stream_field_name {stream_field_name}")
|
||||
logger.debug(kwargs)
|
||||
# if there already is something like `contents__0__text_block__text`, do nothing, as the whole block is already assumed to be defined
|
||||
if any(stream_field_name in s for s in kwargs.keys()):
|
||||
logger.debug("doing nothing")
|
||||
return
|
||||
if stream_field_name in kwargs:
|
||||
"""
|
||||
stream_field_name is most likely 'contents'
|
||||
this means: if there is a property named contents, use the defined ones in this block.
|
||||
otherwise, go into the other block and randomize the contents
|
||||
"""
|
||||
logger.debug(enumerate(kwargs[stream_field_name]))
|
||||
for idx, resource in enumerate(kwargs[stream_field_name]):
|
||||
value = resource["value"]
|
||||
block_type = resource["type"]
|
||||
logger.debug(f"idx {idx} value {value} block_type {block_type}")
|
||||
|
||||
if block_type == "assignment":
|
||||
assignment = Assignment.objects.create(
|
||||
|
|
@ -281,6 +298,7 @@ class ContentBlockFactory(BasePageFactory):
|
|||
)
|
||||
] = survey
|
||||
else:
|
||||
logger.debug("inner else")
|
||||
for jdx, field in enumerate(value):
|
||||
if block_type == "text_block":
|
||||
kwargs[
|
||||
|
|
@ -321,8 +339,10 @@ class ContentBlockFactory(BasePageFactory):
|
|||
)
|
||||
] = value[field]
|
||||
|
||||
logger.debug(f"deleting {stream_field_name}")
|
||||
del kwargs[stream_field_name]
|
||||
else: # random contents from generator
|
||||
logger.debug("outer else")
|
||||
for i in range(0, random.randint(3, 7)):
|
||||
block_type = random.choice(block_types)
|
||||
if block_type == "text_block":
|
||||
|
|
@ -371,5 +391,9 @@ class ContentBlockFactory(BasePageFactory):
|
|||
|
||||
@classmethod
|
||||
def create(cls, module, **kwargs):
|
||||
logger.debug("before magic")
|
||||
logger.debug(kwargs)
|
||||
cls.stream_field_magic(module, kwargs, "contents")
|
||||
logger.debug("after magic")
|
||||
logger.debug(kwargs)
|
||||
return cls._generate(CREATE_STRATEGY, kwargs)
|
||||
|
|
|
|||
|
|
@ -15,11 +15,14 @@ logger = get_logger(__name__)
|
|||
|
||||
class DuplicateContentsTestCase(TestCase):
|
||||
def setUp(self) -> None:
|
||||
logger.debug("start setUp")
|
||||
create_users()
|
||||
logger.debug("after create_users")
|
||||
_, _, self.module, chapter, _ = BookFactory.create_default_structure()
|
||||
self.assignment = AssignmentFactory(assignment="Assignment", title="Hello")
|
||||
survey = SurveyFactory(title="Survey Title")
|
||||
rich_text = RichText("Hallo")
|
||||
logger.debug("before factory")
|
||||
self.content_block = ContentBlockFactory.create(
|
||||
parent=chapter,
|
||||
module=self.module,
|
||||
|
|
@ -31,6 +34,7 @@ class DuplicateContentsTestCase(TestCase):
|
|||
)
|
||||
|
||||
def test_duplicate_entities(self):
|
||||
logger.debug("start test")
|
||||
self.assertEqual(
|
||||
self.content_block.contents[1].value["assignment_id"], self.assignment
|
||||
)
|
||||
|
|
@ -39,7 +43,9 @@ class DuplicateContentsTestCase(TestCase):
|
|||
)
|
||||
self.assertEqual(Assignment.objects.count(), 1)
|
||||
self.assertEqual(Survey.objects.count(), 1)
|
||||
logger.debug("before duplicate")
|
||||
self.content_block.duplicate_attached_entities()
|
||||
logger.debug("after duplicate")
|
||||
self.assertEqual(Assignment.objects.count(), 2)
|
||||
self.assertEqual(Survey.objects.count(), 2)
|
||||
new_assignment = Assignment.objects.latest("id")
|
||||
|
|
|
|||
Loading…
Reference in New Issue