0

I have a peculiar issue involving saving time using data_mapper and sqlite3 and rspec

if I run the spec below I get this failure report

    # expected: 2013-01-04 07:30:00 +1100
    # got: 2013-01-04 00:00:00 +1100

if I try to save the file manually in irb I get

    # irb(main):003:0> Sleep.create(:id => 1, :wake_time => Time.new(2013, 01, 04, 07, 30, 00))
    # => #<Sleep @id=1 @wake_time=2013-01-04 07:30:00 +1100>

In summary, if I run the rspec test which takes a string, breaks it up and inserts it into the new Time object, the Time part saves incorrectly as 00:00:00

I can also confirm that, after the test has run, if I check the db for the record that had been tested, the time is incorrectly saved as 00:00:00 (just in case you were suspecting something with rspec's comaprison or db retrieval was off :) )

I would love to get to the bottom of this - its is really frustrating me. Especially considering I don't really care about the date (but I would like to solve this because it has piqued my interest now ;) )

Also, am open to suggestions if there is a better way to implement saving a time to the db. Thanks!

life_dm.rb

require "rubygems"
require "json"
require "sqlite3"
require "data_mapper"
require "bigdecimal"


DataMapper::setup(:default, "sqlite3://#{Dir.pwd}../spec/resources/test.db")

class Sleep

    include DataMapper::Resource
    property :id, Integer, :key => true #easier to get methods when key is id vs week
    property :wake_time, Time

end

DataMapper.finalize.auto_migrate!

sleep.rb

def update_sleep_data (id,wake_time)

    #I have also tried saving the time as an sql date/time string
    #i.e :wake_time => "2013-01-04 #{wake_time[0..1]}:#{wake_time[2..3]}"
    #but get the same result with rspec getting 
    Sleep.create(:id => id, :wake_time => Time.new(2013,01,04,wake_time[0..1].to_i,wake_time[2..3].to_i))

end

sleep_spec.rb

require 'data_mapper'
require_relative '../../data/life_dm'
require_relative '../../methods/sleep'

describe "sleep table" do

    it "saves info about sleep to the db" do

        @id = 1
        @wake_time = "0730"
        update_sleep_data(@id, @wake_time)

        sleep_1 = Sleep.get 1

        sleep_1[:id].should eql 1
        sleep_1[:wake_time].should eql Time.new(2013,01,04,07,30,00)




    end

end
JoeyC
  • 764
  • 11
  • 19

1 Answers1

0

Looks like Time::new doesn't like your strings. Pass in integers instead, which appear to work fine for the other parameters.

In sleep.rb use wake_time[0..1].to_i and wake_time[2..3].to_i.

Substantial
  • 6,684
  • 2
  • 31
  • 40
  • I tried this out and get the same result. When in rspec saves the time as 00:00 Running the same method in irb saves the time as 07:30 as expected I will update the question to rule this out. Thx for trying – JoeyC Jan 10 '13 at 22:51