Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Documenting create_content_instance, and an example Fluent Page test …
Browse files Browse the repository at this point in the history
…creation.
  • Loading branch information
Greg Turner committed Oct 11, 2016
1 parent 2e6a545 commit 1d01aad
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
51 changes: 51 additions & 0 deletions docs/contributing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,54 @@ with migrations applied. It will be restored to the test database in

`pg_dump -O -x -f test_initial_data.sql -d foo`


# To create fluent pages in tests.

We do this by using dynamic fixture:

from django.contrib.auth import get_user_model
from django_webtest import WebTest
from django_dynamic_fixture import G
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from icekit.models import Layout
from icekit.page_types.layout_page.models import LayoutPage

User = get_user_model()


class FluentPageTestCase(WebTest):
def setUp(self):
self.layout_1 = G(
Layout,
template_name='layout_page/layoutpage/layouts/default.html',
)
self.layout_1.content_types.add(ContentType.objects.get_for_model(LayoutPage))
self.layout_1.save()
self.staff_1 = User.objects.create(
email='test@test.com',
is_staff=True,
is_active=True,
is_superuser=True,
)
self.page_1 = LayoutPage.objects.create(
title='Test Page',
slug='test-page',
parent_site=Site.objects.first(),
layout=self.layout_1,
author=self.staff_1,
status='p', # Publish the page
)


There is also a helper function to add content items:

from icekit.utils.fluent_contents import create_content_instance

...
self.child_page_1 = create_content_instance(
models.ContentItem,
page=self.page_1,
placeholder_name="main", # default
**kwargs # arguments for initialising the ContentItem model
)
12 changes: 6 additions & 6 deletions icekit/utils/fluent_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# USEFUL FUNCTIONS FOR FLUENT CONTENTS #############################################################

# Fluent Contents Helper Functions #################################################################
def create_content_instance(content_plugin_class, test_page, placeholder_name='main', **kwargs):
def create_content_instance(content_plugin_class, page, placeholder_name='main', **kwargs):
"""
Creates a content instance from a content plugin class.
:param content_plugin_class: The class of the content plugin.
:param test_page: The fluent_page instance to create the content
:param page: The fluent_page instance to create the content
instance one.
:param placeholder_name: The placeholder name defined in the
template. [DEFAULT: main]
Expand All @@ -18,23 +18,23 @@ def create_content_instance(content_plugin_class, test_page, placeholder_name='m
:return: The content instance created.
"""
# Get the placeholders that are currently available for the slot.
placeholders = test_page.get_placeholder_by_slot(placeholder_name)
placeholders = page.get_placeholder_by_slot(placeholder_name)

# If a placeholder exists for the placeholder_name use the first one provided otherwise create
# a new placeholder instance.
if placeholders.exists():
placeholder = placeholders[0]
else:
placeholder = test_page.create_placeholder(placeholder_name)
placeholder = page.create_placeholder(placeholder_name)

# Obtain the content type for the page instance class.
ct = ContentType.objects.get_for_model(type(test_page))
ct = ContentType.objects.get_for_model(type(page))

# Create the actual plugin instance.
try:
content_instance = content_plugin_class.objects.create(
parent_type=ct,
parent_id=test_page.id,
parent_id=page.id,
placeholder=placeholder,
**kwargs
)
Expand Down

0 comments on commit 1d01aad

Please sign in to comment.