Reputation: 344
My configuration below (django 3.02):
Models:
class Assets(models.Model):
assettag = models.CharField()
...
class Item(models.Model):
asset = models.OneToOneField('Assets')
...
def __str__(self):
return (f"{self.rfid_tag}" + " - " + f"{self.asset.assettag}")
class Comments(models.Model): # I know, it should not be plural.
item = models.ForeignKey('Item',
default=None,
null=True,
on_delete=models.CASCADE,
)
text = models.TextField()
...
Form:
class ItemInsertForm(forms.ModelForm):
rfid_tag = forms.CharField(label='RFID Tag',
widget=forms.TextInput(attrs={"placeholder":"96 bit EPC code",
"pattern":"^[a-fA-F0-9]{24}$",
"size":"25",
"id":"rfid_tag",
}
)
)
asset = forms.CharField(label='AssetTag',
widget=forms.TextInput(attrs={"placeholder":"Item Inventory Tag",
"pattern":"{some regex}",
"size":"25",
"id":"asset",
}
)
)
comments = forms.CharField(label='Comments',
required=False,
widget=forms.Textarea(attrs={"rows":5,
"cols":50,
"id":"comments",
}
)
)
class Meta:
model = Item
fields = [
'rfid_tag',
'asset',
'image',
'date',
]
def clean_rfid_tag(self, *args, **kwargs):
return self.cleaned_data.get("rfid_tag").lower()
def clean_asset(self, *args, **kwargs):
AssetTag = self.cleaned_data.get("asset")
try:
_asset = Assets.objects.get(assettag = AssetTag)
except Assets.DoesNotExist:
raise forms.ValidationError("Asset does not exist !")
self.Asset = _asset
return self.Asset
View:
class InsertView(SuccessMessageMixin, AuditMixin, generic.CreateView):
template_name = '{some template}'
success_message = "Item succesfully created"
form_class = ItemInsertForm
def form_valid(self, form):
item = form.save(commit=False)
_comment = form.cleaned_data.get('comments')
item = form.save()
if _comment:
item.comments_set.create(text =_comment)
self.log_insert(item)
return super().form_valid(form)
Test:
class TestViews(TestCase):
@classmethod
def setUpTestData(cls) -> None:
# create dummy assets
cls.asset = Assets.objects.create(assettag="{some assettag #1}",)
cls.asset2 = Assets.objects.create(assettag="{some assettag #2}")
# create dummy item
cls.item = Item.objects.create(rfid_tag='abcdef012345678900000000', asset=cls.asset)
def setUp(self) -> None:
self.client = Client()
self.insert_url = reverse('inventory:insert')
def test_insert_POST_valid(self):
response = self.client.post(self.insert_url,
data = {'rfid_tag':'abcdef012345678900000001',
'asset':'{some assettag #2}',}
)
new_item = Item.objects.get(rfid_tag="abcdef012345678900000001")
self.assertEqual(response.status_code, 302)
self.assertTrue(isinstance(new_item,Item))
self.assertEqual(Item.objects.filter(rfid_tag="abcdef012345678900000001").count(),1)
self.assertEqual(self.asset2.item, new_item)
self.assertFalse(Comments.objects.all())
def test_insert_POST_comment(self):
response = self.client.post(self.insert_url,
data = {'rfid_tag':'abcdef012345678900000001',
'asset':'{some assettag #2}',
'comments':'Test comment.',
}
)
new_item = Item.objects.get(rfid_tag="abcdef012345678900000001")
self.assertEqual(response.status_code, 302)
self.assertTrue(isinstance(new_item,Item))
self.assertEqual(Item.objects.filter(rfid_tag="abcdef012345678900000001").count(),1)
self.assertEqual(self.asset2.item, new_item)
self.assertTrue(isinstance(Comments.objects.get(text='Test comment.'), Comments))
self.assertEqual(Comments.objects.get(text='Test comment.'),
new_item.comments_set.first())
If I run these tests, test_insert_POST_valid
will fail due to assertion error:
AssertionError: < Item: abcdef012345678900000001 - {some assettag #2} > != < Item: abcdef012345678900000001 - {some assettag #2} >
I have inserted some print statements throughout the code:
# this was placed first in both tests, before the POST request
items = Item.objects.all()
for item in items:
print("Item:",item.id, " - ", item)
# and then again, after each POST request
What I noticed is that the first time the prints run, it will output just one Item
object (this is true for both functions):
Item: 1 - abcdef012345678900000009 - {some assettag #1}
but on the second run (after the POST request is sent and the new item is created):
For test_insert_POST_comment
the result is:
Item: 1 - abcdef012345678900000009 - {some assettag #1}
Item: 2 - abcdef012345678900000001 - {some assettag #2}
While for test_insert_POST_valid
the result is:
Item: 1 - abcdef012345678900000009 - {some assettag #1}
Item: 3 - abcdef012345678900000001 - {some assettag #2}
If I do print("Asset Item:", self.asset2.item.id, " - ", self.asset2.item)
in the test_insert_POST_valid
function (for some reason this test runs second) it will output:
Asset Item: 2 - abcdef012345678900000001 - {some assettag #2}
I don't understand how this happens. Item.objects.all()
will only return one object (abcdef012345678900000000), while the Asset
object still maintains a relationship with a fictive second Item
object, that does not exist.
I was suspecting that test_insert_POST_valid
is not actually creating any object, since self.asset2
already has an Item
associated throug a OneToOne
relationship, but this is not true. If I change the rfid_tag
value to abcdef012345678900000002 (or whatever), it will create the new Item
, but self.asset2.item
will still point to abcdef012345678900000001.
What is the cause of this issue and wow can I solve it? (Putting self.asset2.item = None before POST-ing the data, will not help. A new relationship will not be created.)
Edit:
I could move cls.asset2 = Assets.objects.create(assettag="{some assettag #2}")
in the setUp
method and it will be created before each test, but I have some more logic depending on this I would have to move it too. The prefered way is to leave it in setUpTestdata
so it runs only once.
Upvotes: 1
Views: 351
Reputation: 344
I have found a simple solution: Add self.asset2.refresh_from_db()
in the setUp
method. This will reload every field of this model from the database, before every test is ran.
Upvotes: 1