mercredi 14 mars 2018

how can i test my custom model manager in django

following is my custom manager

class DefaultModelManager(models.Manager):
def get_or_none(self, **kwargs):
    try:
        return self.get(**kwargs)
    except self.model.DoesNotExist:
        return None

Model

class TestModel(models.Model):
    objects=DefaultModelManager()

how can i test this code? Following is my code

class ModelManagerTest(TestCase):
    def test_can_get_or_none(self):
        TestModel.create('...')
        test = TestModel.objects.get_or_none('...')
        test2 = TestModel.objects.get('...')
        self.assertEqual(test, test2)
    def test_cant_get_or_none(self):
        test = TestModel.objects.get_or_none('...')
        self.assertEqual(test, None)

is it correct??? or another method... What is the best way to test custom model manager

Aucun commentaire:

Enregistrer un commentaire