Hello, I have a couple questions regarding the best way to do date math in SQLAlchemy with Postgresql and how I can write fixture-based unit tests for this without requiring an actual database in my test.
There's 2 specific types of dates which I need to be retrieving. Written in SQL these would be: 1) CURRENT_TIMESTAMP 2) CURRENT_TIMESTAMP + interval 'X months' I know that I can use sqlalchemy.func.current_timestamp() to retrieve the first, but this breaks when I try to unit test it and there is no database session present. For example, consider the following code: @property def is_active(self): return ((not self.date_closed) or (self.date_closed >= sa.func.current_timestamp())) If I unit test this with several mock fixtures and there's no database present, is_active fails when it encounters a fixture with a "date_closed": AssertionError: True is not False AssertionError: <sqlalchemy.sql.expression._BinaryExpression object at 0x2a684d0> is not False I would like to be able to use our postgresql database to retrieve the current_timestamp as well as "CURRENT_TIMESTAMP + interval 'X months'" and preserve the ability to unit test this code without requiring an actual database. One idea my boss suggested was to use the Fudge module in my tests to overload sqlalchemy.func.current_timestamp() with a function that uses python's datetime module to return the current timestamp. Thus, when run in production, my code will use the database to get the current time, but when run in a unit test, we avoid a broken test by instead using python's datetime to get the current timestamp. Does anyone have any experience or examples of this type of approach? Also, while this seems like it will work for getting the current time, how would I use the same approach to get "CURRENT_TIMESTAMP + interval 'X months'"? I don't see a pre-written "sqlalchemy.func.current_timestamp_plus_interval('X months') function. Do I need to write my own? If so, am I best off using raw SQL or is there some other mechanism to do date math in SQLAlchemy? Thanks for any help/guidance you can give me. -Dan -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.