Skip to content

Commit

Permalink
Increase test coverage (#39)
Browse files Browse the repository at this point in the history
* Add .github/CODEOWNERS

* Remove locales from coverage report

* Increase test coverage of the package
  • Loading branch information
ericof authored Nov 24, 2023
1 parent cd27cb5 commit 5fcafa4
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* @jonaspiterek @ericof @sneridagh @tisto
/.github @ericof
6 changes: 6 additions & 0 deletions .meta.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ dependencies_ignores = "['plone.volto', 'zestreleaser.towncrier', 'zest.releaser
dependencies_mappings = [
"Plone = ['Products.CMFPlone', 'Products.CMFCore', 'Products.GenericSetup', 'Products.ZCatalog', 'z3c.form']",
]
extra_lines = """
[tool.coverage.run]
omit = [
"src/collective/blog/locales/*",
]
"""

[tox]
use_mxdev = true
Expand Down
1 change: 1 addition & 0 deletions news/38.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Increase test coverage of the package [@ericof]
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ ignore = [
# """
##

[tool.coverage.run]
omit = [
"src/collective/blog/locales/*",
]

##
# Add extra configuration options in .meta.toml:
Expand Down
7 changes: 2 additions & 5 deletions src/collective/blog/indexers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ def _find_blog_uid(obj: DexterityContent) -> str:
"""Find a blog."""
if IBlogInfo.providedBy(obj):
return api.content.get_uuid(obj)
elif IPloneSiteRoot.providedBy(obj):
# No blog was found
return "No blog was found."
else:
# Up one level (get parent)
return _find_blog_uid(aq_parent(obj))
# Up one level (get parent) if not reached the root of the site
return "" if IPloneSiteRoot.providedBy(obj) else _find_blog_uid(aq_parent(obj))
10 changes: 8 additions & 2 deletions src/collective/blog/profiles.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@

<genericsetup:registerProfile
name="default"
title="Blog: Install"
title="Blog Support for Plone"
description="Package to configure Blog content types"
provides="Products.GenericSetup.interfaces.EXTENSION"
directory="profiles/default"
/>

<genericsetup:registerProfile
name="uninstall"
title="Blog: Uninstall"
title="Blog Support for Plone: Uninstall"
description="Uninstall Blog setup."
provides="Products.GenericSetup.interfaces.EXTENSION"
directory="profiles/uninstall"
/>

<include package=".upgrades" />

<!-- Hide Uninstall Profile-->
<utility
factory=".setuphandlers.HiddenProfiles"
name="collective.blog"
/>

</configure>
10 changes: 7 additions & 3 deletions src/collective/blog/services/authors/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ def __call__(self, expand=True):
"fullname": brain.Title,
}
)
if not data:
data = [
data = (
data
if data
else [
{
"@id": portal_url,
"fullname": DEFAULT_USER,
}
]
return {"authors": data}
)
result["authors"]["items"] = data
return result


class AuthorsGet(Service):
Expand Down
98 changes: 82 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from collective.blog.testing import ACCEPTANCE_TESTING
from collective.blog.testing import FUNCTIONAL_TESTING
from collective.blog.testing import INTEGRATION_TESTING
from copy import deepcopy
from plone import api
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.restapi.testing import RelativeSession
from pytest_plone import fixtures_factory

import pytest
Expand All @@ -22,54 +26,74 @@


@pytest.fixture
def blogs_payload() -> list:
"""Payload to create two blogs items."""
def filter_items():
def func(content: list, portal_type: str, only_public: bool = False) -> list:
response = []
for raw_item in content:
if raw_item["type"] != portal_type:
continue
if only_public:
item = {k: v for k, v in raw_item.items() if not k.startswith("_")}
else:
item = deepcopy(raw_item)
response.append(item)
return response

return func


@pytest.fixture
def all_content() -> list:
"""Payload to create all content items."""
return [
{
"_container": "/",
"type": "Image",
"id": "an-image",
"title": "A Random Image",
"description": "With some details",
"_image": b"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdjCDO+/R8ABKsCZD++CcMAAAAASUVORK5CYII=", # noQA
},
{
"_container": "/",
"type": "Blog",
"id": "tech-blog",
"title": "Awesome Tech Blog",
"description": "My awesome technical blog",
},
{
"_container": "/",
"type": "Blog",
"id": "dumpster",
"title": "General",
"description": "Posts about life, universe and everything",
},
]


@pytest.fixture
def authors_payload() -> list:
"""Payload to create two author items."""
return [
{
"_container": "/tech-blog/authors",
"type": "Author",
"id": "douglas-adams",
"title": "Douglas Adams",
"description": "A very good writer",
},
{
"_container": "/tech-blog/authors",
"_preview_image_link": "/an-image",
"type": "Author",
"id": "marvin",
"title": "Marvin",
"description": "A very good nice robot",
},
]


@pytest.fixture
def posts_payload() -> list:
"""Payload to create two posts items."""
return [
{
"_container": "/tech-blog",
"_preview_image_link": "/an-image",
"type": "Post",
"id": "initial-commit",
"title": "Initial Blog Post",
"description": "Let's start my blog post with something cool",
},
{
"_container": "/tech-blog",
"_preview_image_link": "/an-image",
"type": "Post",
"id": "collective-blog",
"title": "New package: collective.blog",
Expand All @@ -78,6 +102,24 @@ def posts_payload() -> list:
]


@pytest.fixture
def blogs_payload(all_content, filter_items) -> list:
"""Payload to create two blogs items."""
return filter_items(all_content, "Blog", True)


@pytest.fixture
def authors_payload(all_content, filter_items) -> list:
"""Payload to create two author items."""
return filter_items(all_content, "Author", True)


@pytest.fixture
def posts_payload(all_content, filter_items) -> list:
"""Payload to create two posts items."""
return filter_items(all_content, "Post", True)


@pytest.fixture
def blogs(portal, blogs_payload) -> dict:
"""Create Blogs content items."""
Expand Down Expand Up @@ -117,3 +159,27 @@ def posts(portal, authors, blogs, posts_payload) -> dict:
content = api.content.create(container=blog, **data)
response[content.UID()] = content.title
return response


@pytest.fixture()
def request_factory(portal):
def factory():
url = portal.absolute_url()
api_session = RelativeSession(url)
api_session.headers.update({"Accept": "application/json"})
return api_session

return factory


@pytest.fixture()
def anon_request(request_factory):
return request_factory()


@pytest.fixture()
def manager_request(request_factory):
request = request_factory()
request.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
yield request
request.auth = ()
33 changes: 0 additions & 33 deletions tests/controlpanel/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.restapi.testing import RelativeSession

import pytest


@pytest.fixture()
def app(functional):
return functional["app"]


@pytest.fixture()
def portal(functional):
portal = functional["portal"]
Expand All @@ -19,27 +10,3 @@ def portal(functional):
@pytest.fixture()
def http_request(functional):
return functional["request"]


@pytest.fixture()
def request_factory(portal):
def factory():
url = portal.absolute_url()
api_session = RelativeSession(url)
api_session.headers.update({"Accept": "application/json"})
return api_session

return factory


@pytest.fixture()
def anon_request(request_factory):
return request_factory()


@pytest.fixture()
def manager_request(request_factory):
request = request_factory()
request.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
yield request
request.auth = ()
59 changes: 59 additions & 0 deletions tests/restapi/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from base64 import b64decode
from copy import deepcopy
from plone import api
from plone.namedfile.file import NamedBlobImage

import pytest
import transaction


@pytest.fixture()
def create_content_tree(all_content):
def func():
contents = []
author_uid = ""
with api.env.adopt_roles(["Manager"]):
# Create image to be used as preview
for data in all_content:
item = deepcopy(data)
target_info = None
parent = item.pop("_container")
container = api.content.get(path=parent)
if "_image" in item:
item["image"] = NamedBlobImage(b64decode(item.pop("_image")))
if "_preview_image_link" in item:
target_info = (
api.content.get(item.pop("_preview_image_link")),
"preview_image_link",
)
if item["type"] == "Post":
item["creators"] = [author_uid]
content = api.content.create(container=container, **item)
if target_info:
target, relationship = target_info
api.relation.create(content, target, relationship)
content_uid = api.content.get_uuid(content)
if item["type"] == "Author" and not author_uid:
author_uid = content_uid
contents.append(content_uid)
return contents

return func


@pytest.fixture()
def portal(functional, create_content_tree):
portal = functional["portal"]
with transaction.manager:
contents = create_content_tree()
yield portal
with transaction.manager:
for uid in contents[::-1]:
obj = api.content.get(UID=uid)
if obj:
api.content.delete(obj)


@pytest.fixture()
def http_request(functional):
return functional["request"]
Loading

0 comments on commit 5fcafa4

Please sign in to comment.