DEV Community

Lucas Castejon
Lucas Castejon

Posted on • Edited on

9 2

Create a easy test using Model Baker

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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs