Mock default attribute of SQLAlchemy

Mock default attribute of SQLAlchemy

I have some issues to mock a SQLAlchemy object when using the default and onupdate fields in my models :

I have some issues to mock a SQLAlchemy object when using the default and onupdate fields in my models :

def get_uuid():
    return str(uuid.uuid4())

def get_now(): return db.func.now()

class BaseModel(db.Model): abstract = True

id = db.Column(UUIDType(binary=False), primary_key=True, nullable=False, default=get_uuid)
created_at = db.Column(db.DateTime(timezone=True), default=get_now(), nullable=False, index=True)

The get_now() and get_uuid() behaviour do not change even when I try to mock them in my tests :

def test_create_source(client, mocker):

mock = mocker.MagicMock(return_value='123e4567-e89b-12d3-a456-426655440000')
mocker.patch('myproject.models.get_uuid', mock)
mock = mocker.MagicMock(return_value=datetime.datetime(2019, 1, 1))
mocker.patch('myproject.models.get_now', mock)

resp = client.post('/sources', json={'name': 'My source'})
assert resp.json == {
    'name': 'My source',
    'id': '123e4567-e89b-12d3-a456-426655440000',
    'createdAt': 'Tue, 01 Jan 2019 00:00:00 GMT',
    'updatedAt': 'Tue, 01 Jan 2019 00:00:00 GMT'
}

Results :

> assert resp.json == { 'name': 'My source', 'id': '123e4567-e89b-12d3-a456-426655440000', 'createdAt': 'Tue, 01 Jan 2019 00:00:00 GMT', 'updatedAt': 'Tue, 01 Jan 2019 00:00:00 GMT' } E AssertionError: assert {'createdAt':...17:38:38 GMT'} == {'createdAt': ...00:00:00 GMT'} E Omitting 1 identical items, use -vv to show E Differing items: E {'id': '8eb074c0-41e9-436c-8f71-b4c6842f4809'} != {'id': '123e4567-e89b-12d3-a456-426655440000'} E {'createdAt': 'Fri, 18 Jan 2019 17:38:38 GMT'} != {'createdAt': 'Tue, 01 Jan 2019 00:00:00 GMT'} E {'updatedAt': 'Fri, 18 Jan 2019 17:38:38 GMT'} != {'updatedAt': 'Tue, 01 Jan 2019 00:00:00 GMT'} E Use -v to get the full diff

tests/test_sources.py:17: AssertionError

I think it's because my models and its attributes are already imported and evaluated before doing my test, so the mock is useless here. It's explained in the "Mocking class helpers" part of this post but I still was not able to fix my issue :(

The full runnable code to reproduce the problem is available here : https://github.com/ncrocfer/flaskmock

Do you have some ideas please ?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Flask for Beginners: Build a CRUD Web App with Python and Flask

In this Python Flask tutorial, you'll learn to build CRUD web applications using Python and Flask. Python and Flask can make building a CRUD app super easy.

Aprender Python, Django, Flask, Tkinter, POO, SQLite, MySQL y MÁS 🐍

Aprender Python, Django, Flask, Tkinter, POO, SQLite, MySQL y MÁS 🐍 POO, Programación Orientada a Objetos en Python. Bases de datos SQL, trabajando en conjunto con nuestros desarrollos. Tkinter, para crear aplicaciones de escritorio con interfaz gráfica. Django, el framework de desarrollo web para Python más popular y demandado por las empresas. Flask, el moderno framework para desarrollar aplicaciones web.

System testing with Pytest, Docker, and Flask

System testing with Pytest, Docker, and Flask. The composability of fixtures in pytest is an improvement over traditional xUnit setup/teardown. Learn how to combine the power of pytest fixtures with Docker to build high-level integration tests for microservices or other complex systems with multiple components