Skip to content

Commit 7f1126e

Browse files
committedApr 28, 2021
Test to upload a file to a item
1 parent 2882d29 commit 7f1126e

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
 

‎src/inventory_project/tests/test_admin_item.py

+59
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import io
2+
13
from bx_py_utils.test_utils.html_assertion import HtmlAssertionMixin
24
from django.contrib.auth.models import User
35
from django.test import TestCase
@@ -6,6 +8,7 @@
68

79
from inventory import __version__
810
from inventory.models import ItemImageModel, ItemModel
11+
from inventory.models.item import ItemFileModel
912
from inventory.permissions import get_or_create_normal_user_group
1013

1114

@@ -136,3 +139,59 @@ def test_new_item_with_image(self):
136139
assert image.name == 'test.png'
137140
assert image.item == item
138141
assert image.user_id == self.normaluser.pk
142+
143+
def test_new_item_with_file(self):
144+
self.client.force_login(self.normaluser)
145+
146+
in_memory_file = io.BytesIO(b'FooBar')
147+
in_memory_file.seek(0)
148+
in_memory_file.name = 'a-file-upload.dat'
149+
150+
response = self.client.post(
151+
path='/admin/inventory/itemmodel/add/',
152+
data={
153+
'kind': 'with-file',
154+
'name': 'With File!',
155+
156+
'itemimagemodel_set-TOTAL_FORMS': '0',
157+
'itemimagemodel_set-INITIAL_FORMS': '0',
158+
'itemimagemodel_set-MIN_NUM_FORMS': '0',
159+
'itemimagemodel_set-MAX_NUM_FORMS': '1000',
160+
'itemimagemodel_set-__prefix__-position': '0',
161+
162+
'itemfilemodel_set-TOTAL_FORMS': '1',
163+
'itemfilemodel_set-INITIAL_FORMS': '0',
164+
'itemfilemodel_set-MIN_NUM_FORMS': '0',
165+
'itemfilemodel_set-MAX_NUM_FORMS': '1000',
166+
'itemfilemodel_set-0-position': '0',
167+
'itemfilemodel_set-0-file': in_memory_file,
168+
'itemfilemodel_set-__prefix__-position': '0',
169+
170+
'itemlinkmodel_set-TOTAL_FORMS': '0',
171+
'itemlinkmodel_set-INITIAL_FORMS': '0',
172+
'itemlinkmodel_set-MIN_NUM_FORMS': '0',
173+
'itemlinkmodel_set-MAX_NUM_FORMS': '1000',
174+
'itemlinkmodel_set-__prefix__-position': '0',
175+
176+
'_save': 'Save',
177+
},
178+
)
179+
self.assertRedirects(response, expected_url='/admin/inventory/itemmodel/')
180+
181+
data = list(ItemModel.objects.values_list('kind__name', 'name'))
182+
assert data == [('with-file', 'With File!')]
183+
184+
item = ItemModel.objects.first()
185+
186+
self.assert_messages(response, expected_messages=[
187+
f'The Item "<a href="/admin/inventory/itemmodel/{item.pk}/change/"> - With File!</a>"'
188+
f' was added successfully.'
189+
])
190+
191+
assert item.user_id == self.normaluser.pk
192+
193+
assert ItemFileModel.objects.count() == 1
194+
instance = ItemFileModel.objects.first()
195+
assert instance.name == 'test.png'
196+
assert instance.item == item
197+
assert instance.user_id == self.normaluser.pk

0 commit comments

Comments
 (0)
Please sign in to comment.