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 ?

python testing flask sql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.

Python Testing with a Mock Database (SQL)

Python testing with unittest with a mock SQL database. How to patch in a temporary database for testing using the mock package.

Architecting REST API with Python, Flask and Azure SQL

Using Python, Flask and AzureSQL to create a REST API: easy, simple and super powerful. With Dev and Data hand-in-hand amazing things happen! Let's dive into architecting REST API with Python, Flask and Azure SQL