Create test files in seperate folder & cleanup before image build (#6571)

* move ignore

* create testfiles in seperate folder

* add cleanup step to docker build

* use pathlib for paths
This commit is contained in:
Matthias Mair 2024-02-26 01:23:49 +01:00 committed by GitHub
parent f441f672d6
commit 85225538e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 41 additions and 26 deletions

View File

@ -103,6 +103,9 @@ jobs:
docker-compose run inventree-dev-server invoke test --disable-pty docker-compose run inventree-dev-server invoke test --disable-pty
docker-compose run inventree-dev-server invoke test --migrations --disable-pty docker-compose run inventree-dev-server invoke test --migrations --disable-pty
docker-compose down docker-compose down
- name: Clean up test folder
run: |
rm -rf InvenTree/_testfolder
- name: Set up QEMU - name: Set up QEMU
if: github.event_name != 'pull_request' if: github.event_name != 'pull_request'
uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # pin@v3.0.0 uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # pin@v3.0.0

10
.gitignore vendored
View File

@ -39,16 +39,6 @@ local_settings.py
# Files used for testing # Files used for testing
inventree-demo-dataset/ inventree-demo-dataset/
inventree-data/ inventree-data/
dummy_image.*
_tmp.csv
InvenTree/label.pdf
InvenTree/label.png
InvenTree/part_image_123abc.png
label.pdf
label.png
InvenTree/my_special*
_tests*.txt
schema.yml
# Local static and media file storage (only when running in development mode) # Local static and media file storage (only when running in development mode)
inventree_media inventree_media

View File

@ -1042,9 +1042,12 @@ class TestSettings(InvenTreeTestCase):
) )
# with env set # with env set
with self.in_env_context({'INVENTREE_CONFIG_FILE': 'my_special_conf.yaml'}): with self.in_env_context({
'INVENTREE_CONFIG_FILE': '_testfolder/my_special_conf.yaml'
}):
self.assertIn( self.assertIn(
'inventree/my_special_conf.yaml', str(config.get_config_file()).lower() 'inventree/_testfolder/my_special_conf.yaml',
str(config.get_config_file()).lower(),
) )
def test_helpers_plugin_file(self): def test_helpers_plugin_file(self):
@ -1058,8 +1061,12 @@ class TestSettings(InvenTreeTestCase):
) )
# with env set # with env set
with self.in_env_context({'INVENTREE_PLUGIN_FILE': 'my_special_plugins.txt'}): with self.in_env_context({
self.assertIn('my_special_plugins.txt', str(config.get_plugin_file())) 'INVENTREE_PLUGIN_FILE': '_testfolder/my_special_plugins.txt'
}):
self.assertIn(
'_testfolder/my_special_plugins.txt', str(config.get_plugin_file())
)
def test_helpers_setting(self): def test_helpers_setting(self):
"""Test get_setting.""" """Test get_setting."""

8
InvenTree/_testfolder/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Files used for testing
dummy_image.*
_tmp.csv
part_image_123abc.png
label.pdf
label.png
my_special*
_tests*.txt

View File

@ -4,6 +4,7 @@ import os
from datetime import datetime from datetime import datetime
from decimal import Decimal from decimal import Decimal
from enum import IntEnum from enum import IntEnum
from pathlib import Path
from random import randint from random import randint
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -20,6 +21,7 @@ import company.models
import order.models import order.models
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
from company.models import Company, SupplierPart from company.models import Company, SupplierPart
from InvenTree.settings import BASE_DIR
from InvenTree.status_codes import BuildStatus, PurchaseOrderStatusGroups, StockStatus from InvenTree.status_codes import BuildStatus, PurchaseOrderStatusGroups, StockStatus
from InvenTree.unit_test import InvenTreeAPITestCase from InvenTree.unit_test import InvenTreeAPITestCase
from part.models import ( from part.models import (
@ -1478,10 +1480,11 @@ class PartDetailTests(PartAPITestBase):
print(p.image.file) print(p.image.file)
# Try to upload a non-image file # Try to upload a non-image file
with open('dummy_image.txt', 'w') as dummy_image: test_path = BASE_DIR / '_testfolder' / 'dummy_image'
with open(f'{test_path}.txt', 'w') as dummy_image:
dummy_image.write('hello world') dummy_image.write('hello world')
with open('dummy_image.txt', 'rb') as dummy_image: with open(f'{test_path}.txt', 'rb') as dummy_image:
response = self.upload_client.patch( response = self.upload_client.patch(
url, {'image': dummy_image}, format='multipart' url, {'image': dummy_image}, format='multipart'
) )
@ -1491,7 +1494,7 @@ class PartDetailTests(PartAPITestBase):
# Now try to upload a valid image file, in multiple formats # Now try to upload a valid image file, in multiple formats
for fmt in ['jpg', 'j2k', 'png', 'bmp', 'webp']: for fmt in ['jpg', 'j2k', 'png', 'bmp', 'webp']:
fn = f'dummy_image.{fmt}' fn = f'{test_path}.{fmt}'
img = PIL.Image.new('RGB', (128, 128), color='red') img = PIL.Image.new('RGB', (128, 128), color='red')
img.save(fn) img.save(fn)
@ -1512,7 +1515,7 @@ class PartDetailTests(PartAPITestBase):
# First, upload an image for an existing part # First, upload an image for an existing part
p = Part.objects.first() p = Part.objects.first()
fn = 'part_image_123abc.png' fn = BASE_DIR / '_testfolder' / 'part_image_123abc.png'
img = PIL.Image.new('RGB', (128, 128), color='blue') img = PIL.Image.new('RGB', (128, 128), color='blue')
img.save(fn) img.save(fn)
@ -1557,7 +1560,7 @@ class PartDetailTests(PartAPITestBase):
# First, upload an image for an existing part # First, upload an image for an existing part
p = Part.objects.first() p = Part.objects.first()
fn = 'part_image_123abc.png' fn = BASE_DIR / '_testfolder' / 'part_image_123abc.png'
img = PIL.Image.new('RGB', (128, 128), color='blue') img = PIL.Image.new('RGB', (128, 128), color='blue')
img.save(fn) img.save(fn)

View File

@ -5,6 +5,7 @@ import csv
from django.urls import reverse from django.urls import reverse
import part.models import part.models
from InvenTree.settings import BASE_DIR
from InvenTree.unit_test import InvenTreeTestCase from InvenTree.unit_test import InvenTreeTestCase
@ -43,7 +44,7 @@ class BomExportTest(InvenTreeTestCase):
'attachment; filename="InvenTree_BOM_Template.csv"', 'attachment; filename="InvenTree_BOM_Template.csv"',
) )
filename = '_tmp.csv' filename = BASE_DIR / '_testfolder' / '_tmp.csv'
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
f.write(response.getvalue()) f.write(response.getvalue())
@ -89,7 +90,7 @@ class BomExportTest(InvenTreeTestCase):
content = response.headers['Content-Disposition'] content = response.headers['Content-Disposition']
self.assertEqual(content, 'attachment; filename="BOB | Bob | A2_BOM.csv"') self.assertEqual(content, 'attachment; filename="BOB | Bob | A2_BOM.csv"')
filename = '_tmp.csv' filename = BASE_DIR / '_testfolder' / '_tmp.csv'
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
f.write(response.getvalue()) f.write(response.getvalue())

View File

@ -10,6 +10,7 @@ from django.urls import reverse
from pdfminer.high_level import extract_text from pdfminer.high_level import extract_text
from PIL import Image from PIL import Image
from InvenTree.settings import BASE_DIR
from InvenTree.unit_test import InvenTreeAPITestCase from InvenTree.unit_test import InvenTreeAPITestCase
from label.models import PartLabel, StockItemLabel, StockLocationLabel from label.models import PartLabel, StockItemLabel, StockLocationLabel
from part.models import Part from part.models import Part
@ -165,18 +166,19 @@ class LabelMixinTests(InvenTreeAPITestCase):
# Test that the labels have been printed # Test that the labels have been printed
# The sample labelling plugin simply prints to file # The sample labelling plugin simply prints to file
self.assertTrue(os.path.exists('label.pdf')) test_path = BASE_DIR / '_testfolder' / 'label'
self.assertTrue(os.path.exists(f'{test_path}.pdf'))
# Read the raw .pdf data - ensure it contains some sensible information # Read the raw .pdf data - ensure it contains some sensible information
filetext = extract_text('label.pdf') filetext = extract_text(f'{test_path}.pdf')
matched = [part.name in filetext for part in parts] matched = [part.name in filetext for part in parts]
self.assertIn(True, matched) self.assertIn(True, matched)
# Check that the .png file has already been created # Check that the .png file has already been created
self.assertTrue(os.path.exists('label.png')) self.assertTrue(os.path.exists(f'{test_path}.png'))
# And that it is a valid image file # And that it is a valid image file
Image.open('label.png') Image.open(f'{test_path}.png')
def test_printing_options(self): def test_printing_options(self):
"""Test printing options.""" """Test printing options."""

View File

@ -5,6 +5,7 @@ This does not function in real usage and is more to show the required components
from rest_framework import serializers from rest_framework import serializers
from InvenTree.settings import BASE_DIR
from plugin import InvenTreePlugin from plugin import InvenTreePlugin
from plugin.mixins import LabelPrintingMixin from plugin.mixins import LabelPrintingMixin
@ -35,7 +36,7 @@ class SampleLabelPrinter(LabelPrintingMixin, InvenTreePlugin):
pdf_data = kwargs['pdf_data'] pdf_data = kwargs['pdf_data']
png_file = self.render_to_png(label=None, pdf_data=pdf_data) png_file = self.render_to_png(label=None, pdf_data=pdf_data)
filename = 'label.pdf' filename = str(BASE_DIR / '_testfolder' / 'label.pdf')
# Dump the PDF to a local file # Dump the PDF to a local file
with open(filename, 'wb') as pdf_out: with open(filename, 'wb') as pdf_out: