vendredi 27 septembre 2019

Django deepcopy a model instance to memory

I want to write a test for a Django view that modifies an instance of a model.

In the past there was a problem with the view's code, it was writing on other unconcerned model instances too.

I am looking for a way to "copy" a model's instance in memory, run the view, then compare the memory and database version of the instance.

Pseudo code :

def test_some_view:
    url = reverse('some_view')
    unconcerned_model = MyModel.objecs.get(id=X)
    concerned_model = MyModel.objects.get(id=Y)
    cpy = deepcopy_of(unconcerned_model)  # how can we do that ?
    self.client.post(url, {'mymodel_id':concerned_model.id})
    unconcerned_model.refresh_from_dbs()
    self.assertEqual(cpy, unconcerned_model)

How can the deepcopy be done ? How to compare the 2 versions ?

Aucun commentaire:

Enregistrer un commentaire