DEV Community

loading...

Create a easy test using Model Baker

lucascastejon profile image Lucas Castejon Updated on ・1 min read

Install this guy

pip install model_bakery
Enter fullscreen mode Exit fullscreen mode

models.py

class Customer(models.Model):
    name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    love_python = models.BooleanField()
    last_coding = models.DateTimeField()
Enter fullscreen mode Exit fullscreen mode

test_models.py

from django.test import TestCase
from model_bakery import baker
from datetime import datetime

class TestCustomerModel(TestCase):
    def test_simple(self):
        customer = baker.make('shop.Customer')
        assert customer

    def test_models_fields(self):
        date = datetime.datetime.now()

        person = baker.make(
                'shop.Customer',
                name='Lucas',
                last_name='Castejon',
                love_python=True,
                last_coding=date
            )
        assert person.name = 'Lucas'
        assert person.last_name = 'Castejon'
        assert person.love_python
        assert isinstance(person.last_coding, datetime)
Enter fullscreen mode Exit fullscreen mode

pprint(self.person.__dict__)

{'_state': <django.db.models.base.ModelState object at 0x1129a3240>,
 'age': 3841,
 'bio': 'vUFzMUMyKzlnTyiCxfgODIhrnkjzgQwHtzIbtnVDKflqevczfnaOACkDNqvCHwvtWdLwoiKrCqfppAlogSLECtMmfleeveyqefkGyTGnpbkVQTtviQVDESpXascHAluGHYEotSypSiHvHzFteKIcUebrzUVigiOacfnGdvijEPrZdSCIIBjuXZMaWLrMXyrsUCdKPLRBRYklRdtZhgtxuASXdhNGhDsrnPHrYRClhrSJSVFojMkUHBvSZhoXoCrTfHsAjenCEHvcLeCecsXwXgWJcnJPSFdOmOpiHRnhSgRF',
 'birthday': datetime.date(2019, 12, 3),
 'happy': True,
 'id': 1,
 'last_shopping': datetime.datetime(2019, 12, 3, 21, 42, 34, 77019),
 'name': 'qiayYnESvqcYLLBzxpFOcGBIfnQEPx',
 'wanted_games_qtd': 6016}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide