3

I build a test suite for a program that runs a lot of SQL statements with now() against a sqlite data base. I would like to mock the sqlite clock --- to test behaviour that is designed to take several days within one second. I do not want to touch the system clock.

Is this possible with sqlite?

Yaakov Belch
  • 4,692
  • 33
  • 39

2 Answers2

4

Example in Python:

import sqlite3
def mock(*_):
    return '1984-01-02 11:22:33'
connection = sqlite3.connect(':memory:')
print(connection.execute('SELECT DATETIME()').fetchone()[0])
connection.create_function('DATETIME', -1, mock)
print(connection.execute('SELECT DATETIME()').fetchone()[0])

Output

2022-03-01 11:23:05
1984-01-02 11:22:33
eternauta3k
  • 103
  • 6
1

SQLite's built-in functions can be redefined with sqlite3_create_function.

[Edit by Yaakov Belch --- additional information:]

Community
  • 1
  • 1
CL.
  • 173,858
  • 17
  • 217
  • 259
  • To make your answer more useful, please consider providing an example of how this can be implemented. – Max Nov 27 '20 at 19:16