-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from browniebroke/test-coverage-renderers
Increase test coverage for renderers.py
- Loading branch information
Showing
9 changed files
with
168 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from PIL import Image | ||
from rest_framework import serializers | ||
from rest_framework.generics import GenericAPIView | ||
from rest_framework.response import Response | ||
|
||
from drf_excel.renderers import XLSXRenderer | ||
|
||
|
||
class MySerializer(serializers.Serializer): | ||
title = serializers.CharField() | ||
|
||
|
||
class MyBaseView(GenericAPIView): | ||
serializer_class = MySerializer | ||
|
||
def retrieve(self, request, *args, **kwargs): | ||
return Response({"title": "example"}) | ||
|
||
|
||
class TestXLSXRenderer: | ||
renderer = XLSXRenderer() | ||
|
||
def test_validation_error(self): | ||
assert self.renderer.render({"detail": "invalid"}) == '{"detail": "invalid"}' | ||
|
||
def test_none(self): | ||
assert self.renderer.render(None) == b"" | ||
|
||
def test_with_header_attribute(self, tmp_path, workbook_reader): | ||
image_path = tmp_path / "image.png" | ||
with Image.new(mode="RGB", size=(100, 100), color="blue") as img: | ||
img.save(image_path, format="png") | ||
|
||
class MyView(MyBaseView): | ||
header = { | ||
"use_header": True, | ||
"header_title": "My Header", | ||
"tab_title": "My Tab", | ||
"img": str(image_path), | ||
"style": {"font": {"name": "Arial"}}, | ||
} | ||
|
||
result = self.renderer.render({}, renderer_context={"view": MyView}) | ||
wb = workbook_reader(result) | ||
sheet = wb.worksheets[0] | ||
rows = list(sheet.rows) | ||
assert len(rows) == 1 | ||
row0_col0 = rows[0][0] | ||
assert row0_col0.value == "My Header" | ||
assert row0_col0.font.name == "Arial" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import ExampleModel | ||
from .models import ExampleModel, AllFieldsModel | ||
|
||
|
||
class ExampleSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = ExampleModel | ||
fields = ("title", "description") | ||
|
||
|
||
class AllFieldsSerializer(serializers.ModelSerializer): | ||
tags = serializers.ListField(source="get_tag_names") | ||
|
||
class Meta: | ||
model = AllFieldsModel | ||
fields = ( | ||
"title", | ||
"created_at", | ||
"updated_date", | ||
"updated_time", | ||
"age", | ||
"is_active", | ||
"tags", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from rest_framework import routers | ||
|
||
from .testapp.views import ExampleViewSet | ||
from .testapp.views import ExampleViewSet, AllFieldsViewSet | ||
|
||
router = routers.SimpleRouter() | ||
router.register(r"examples", ExampleViewSet) | ||
router.register(r"all-fields", AllFieldsViewSet) | ||
|
||
urlpatterns = router.urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters