From 537573d0e3b6019d051fc03dee89033747e5f1fa Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 12 Aug 2021 23:40:07 +1000 Subject: [PATCH] Add extra unit testing for BOM export --- .gitignore | 1 + InvenTree/part/test_bom_export.py | 99 ++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5610fc4304..f3fa0ac8c1 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ local_settings.py # Files used for testing dummy_image.* +_tmp.csv # Sphinx files docs/_build diff --git a/InvenTree/part/test_bom_export.py b/InvenTree/part/test_bom_export.py index 13ec3a179e..a8f949ac5e 100644 --- a/InvenTree/part/test_bom_export.py +++ b/InvenTree/part/test_bom_export.py @@ -2,6 +2,10 @@ Unit testing for BOM export functionality """ +import csv +from os import read +from django.http import response + from django.test import TestCase from django.urls import reverse @@ -47,13 +51,63 @@ class BomExportTest(TestCase): 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): """ Test BOM download in CSV format """ - print("URL", self.url) - params = { 'file_format': 'csv', 'cascade': True, @@ -70,6 +124,47 @@ class BomExportTest(TestCase): content = response.headers['Content-Disposition'] 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): """ Test BOM download in XLS format