Time Test::Unit Question

F

Frew Schmidt

I am trying to do a Test that has to do with the date of something
relative to the current time. Should I just have a predefined but
dynamic dataset or what?

Thoughts?
 
D

Dan Zwell

Frew said:
I am trying to do a Test that has to do with the date of something
relative to the current time. Should I just have a predefined but
dynamic dataset or what?

Thoughts?

I would use a set of predefined "random" dates for tests. The reasoning
(why not just use randomly generated dates?) is that if a test fails, it
should be reproducible. It would be harder to debug a test that
sometimes fails.

Dan
 
F

Frew Schmidt

I would use a set of predefined "random" dates for tests. The reasoning
(why not just use randomly generated dates?) is that if a test fails, it
should be reproducible. It would be harder to debug a test that
sometimes fails.

Dan

Well the problem is that they will all fail eventually. This is the
deal, we have date X, let's say that it's yesterday. My program uses
some SQL statements to check if date X (which is obviously in a
database) was within Y amount of days. But date X will no longer be in
that range after so many days, so I either have to make it dynamically
generated (no fun and possible error prone) or somehow make the computer
think that the current date and time is a certain value.

-fREW
 
J

John Wilger

Well the problem is that they will all fail eventually. This is the
deal, we have date X, let's say that it's yesterday. My program uses
some SQL statements to check if date X (which is obviously in a
database) was within Y amount of days. But date X will no longer be in
that range after so many days, so I either have to make it dynamically
generated (no fun and possible error prone) or somehow make the computer
think that the current date and time is a certain value.

I've been using the foloowing code for a while to do exactly that:

## BEGIN time_warp.rb ##
class Time #:nodoc:
class <<self
attr_accessor :warp
alias_method :real_now, :now
def now
warp
end
alias_method :new, :now
end
end
Time.warp = Time.real_now

def pretend_now_is(time)
begin
Time.warp = time
yield
ensure
Time.warp = Time.real_now
end
end
## END time_warp.rb ##

I'm not the original author. I grabbed the code of one of the numerous
code pasting sites quite a while ago, and I no longer remember exactly
where it came from.

At any rate, you can use that in your tests like so:

## Begin test_relative_time.rb ##
require 'time_warp'
require 'test/unit'

class TestRelativeTime < Test::Unit::TestCase
DAY = 86400
def test_should_not_really_be_within_1_day_of_2005_07_04
assert( ( Time.now - Time.mktime( 2005, 7, 4 ) ).abs > DAY )
end

def test_should_pretend_to_be_within_1_day_of_2005_07_04
pretend_now_is( Time.mktime( 2005, 7, 5 ) ) do
assert( ( Time.now - Time.mktime( 2005, 7, 4 ) ).abs <= DAY )
end
end
end
## END test_relative_time.rb ##

Hope that helps.
 
E

Eric Hodel

Well the problem is that they will all fail eventually. This is the
deal, we have date X, let's say that it's yesterday. My program uses
some SQL statements to check if date X (which is obviously in a
database) was within Y amount of days. But date X will no longer
be in
that range after so many days, so I either have to make it dynamically
generated (no fun and possible error prone) or somehow make the
computer
think that the current date and time is a certain value.

You should test just the SQL statement, not its results. Let
somebody else worry about making sure the DB returns the right stuff.

This may mean you need to stub the DB interface to record your
queries and return your bogus results, but that should only take a
handful of extra lines.
 
J

James Mead

Do you need to get the current time in SQL? Can you not move the logic
into Ruby where it is easier to test? Of course sometimes it needs to
be in SQL for performance reasons, but often this is just premature
optimization.

Perhaps you could share the actual code and SQL with us...?
 
A

Aureliano Calvo

James said:
I dig that idea, but I get the current time in the actual DB code. I am
trying to test my DB code just as much as I am testing my Ruby code
here.

Why don't you calculate the dates in the tests using Time.now as a
base? Like: yesterday = Time.now - /*1 day*/ (not actual ruby code).
This should make your tests "time independant" while leaving the data
base in its actual state and it only adds a little complexity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top