<?xml version="1.0" encoding="windows-1252"?>
<node id="879208" title="Mocking time with SQLite" created="2010-12-26 07:10:09" updated="2010-12-26 07:10:09">
<type id="115">
perlquestion</type>
<author id="25823">
jaldhar</author>
<data>
<field name="doctext">
&lt;p&gt;Normally when I test an application that makes use of times, I turn to [cpan://Test::MockTime] which works wonderfully for setting Perls notions of the current time to arbitrary values.  In this app, however it doesn't work because the database has the following fields:&lt;/p&gt;
&lt;code&gt;
created   TIMESTAMP     DEFAULT CURRENT_TIMESTAMP,
&lt;/code&gt;
&lt;p&gt;and...&lt;/p&gt;
&lt;code&gt;
CREATE TRIGGER IF NOT EXISTS update_modification_time 
        AFTER UPDATE OF title, body ON items
        FOR EACH ROW BEGIN
            UPDATE items SET modified = DATETIME('NOW') WHERE id = new.id;
        END;
&lt;/code&gt;
&lt;p&gt;Modules like [cpan://Test::MockTime] work by intercepting  Perls time related functions but here the time is being set by the SQLite library (calling down to libc functions no doubt) and so this approach does not work.  I can think of two possible alternatives:&lt;/p&gt;
&lt;p&gt;There are C preload libraries like [http://www.code-wizards.com/projects/libfaketime/|faketime] which intercept C library time functions.  Can something like that be used from within a Perl script?&lt;/p&gt;
&lt;p&gt;[cpan://DBD::SQlite] lets you use &lt;code&gt;sqlite_create_function&lt;/code&gt;.  Could this be used to override SQLites time related functions?  If so which ones?&lt;/p&gt;
&lt;p&gt;Comments welcome on whether either of these ideas are feasable or if there are better solutions.&lt;p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-25823"&gt;
&lt;p&gt;-- &lt;br&gt;&amp;#2716;&amp;#2738;&amp;#2727;&amp;#2736;&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
