Add extra unit testing for BOM export

This commit is contained in:
Oliver 2021-08-12 23:40:07 +10:00
parent 1da004d30a
commit 537573d0e3
2 changed files with 98 additions and 2 deletions

1
.gitignore vendored
View File

@ -37,6 +37,7 @@ local_settings.py
# Files used for testing # Files used for testing
dummy_image.* dummy_image.*
_tmp.csv
# Sphinx files # Sphinx files
docs/_build docs/_build

View File

@ -2,6 +2,10 @@
Unit testing for BOM export functionality Unit testing for BOM export functionality
""" """
import csv
from os import read
from django.http import response
from django.test import TestCase from django.test import TestCase
from django.urls import reverse from django.urls import reverse
@ -47,13 +51,63 @@ class BomExportTest(TestCase):
self.url = reverse('bom-download', kwargs={'pk': 100}) self.url = reverse('bom-download', kwargs={'pk': 100})
def test_bom_template(self):
"""
Test that the BOM template can be downloaded from the server
"""
url = reverse('bom-upload-template')
# Download an XLS template
response = self.client.get(url, data={'format': 'xls'})
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.headers['Content-Disposition'],
'attachment; filename="InvenTree_BOM_Template.xls"'
)
# Return a simple CSV template
response = self.client.get(url, data={'format': 'csv'})
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.headers['Content-Disposition'],
'attachment; filename="InvenTree_BOM_Template.csv"'
)
filename = '_tmp.csv'
with open(filename, 'wb') as f:
f.write(response.getvalue())
with open(filename, 'r') as f:
reader = csv.reader(f, delimiter=',')
for line in reader:
headers = line
break
expected = [
'part_id',
'part_ipn',
'part_name',
'quantity',
'optional',
'overage',
'reference',
'note',
'inherited',
'allow_variants',
]
# Ensure all the expected headers are in the provided file
for header in expected:
self.assertTrue(header in headers)
def test_export_csv(self): def test_export_csv(self):
""" """
Test BOM download in CSV format Test BOM download in CSV format
""" """
print("URL", self.url)
params = { params = {
'file_format': 'csv', 'file_format': 'csv',
'cascade': True, 'cascade': True,
@ -70,6 +124,47 @@ class BomExportTest(TestCase):
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'
with open(filename, 'wb') as f:
f.write(response.getvalue())
# Read the file
with open(filename, 'r') as f:
reader = csv.reader(f, delimiter=',')
for line in reader:
headers = line
break
expected = [
'level',
'bom_id',
'parent_part_id',
'parent_part_ipn',
'parent_part_name',
'part_id',
'part_ipn',
'part_name',
'part_description',
'sub_assembly',
'quantity',
'optional',
'overage',
'reference',
'note',
'inherited',
'allow_variants',
'Default Location',
'Available Stock',
]
for header in expected:
self.assertTrue(header in headers)
for header in headers:
self.assertTrue(header in expected)
def test_export_xls(self): def test_export_xls(self):
""" """
Test BOM download in XLS format Test BOM download in XLS format