test/test_timestamps.rb in swift-0.4.3 vs test/test_timestamps.rb in swift-0.5.0

- old
+ new

@@ -1,27 +1,40 @@ require_relative 'helper' require 'date' describe 'Adapter' do - supported_by Swift::DB::Postgres, Swift::DB::Mysql do + supported_by Swift::DB::Postgres do describe 'time parsing and time zones' do - it 'should set timezone' do - assert Swift.db.timezone(8, 0) # +08:00 + + before do + @db = Swift.db.class.new Swift.db.options.merge(timezone: 'Asia/Kabul') end it 'should parse timestamps and do conversion accordingly' do - assert Swift.db.timezone(8, 30) # +08:30 + time = DateTime.parse('2010-01-01 15:00:00+04:30') + assert_timestamp_like time, fetch_timestamp_at(time), 'parses correctly' + end - time = DateTime.parse('2010-01-01 15:00:00+08:30') - match = Regexp.new time.to_time.strftime("%F %H:%M") - sql = if Swift.db.kind_of?(Swift::DB::Postgres) - "select '#{time.strftime('%F %H:%M:%S')}'::timestamp as now" - else - "select timestamp('#{time.strftime('%F %H:%M:%S')}') as now" - end - Swift.db.execute(sql) do |r| - assert_match match, r[:now].to_s, "parses time and does zone conversion" - end + it 'should parse correctly when DST is on' do + time = DateTime.parse('2010-10-02 20:31:00+04:30') + assert_timestamp_like time, fetch_timestamp_at(time), 'DST on' + end + + it 'should parse correctly when DST is off' do + time = DateTime.parse('2010-04-04 20:31:00+04:30') + assert_timestamp_like time, fetch_timestamp_at(time), 'DST off' + end + + def fetch_timestamp_at value + sql = "select '%s'::timestamp with time zone as now" % value.strftime('%F %T%z') + @db.execute(sql) + @db.results.first.fetch(:now) + end + + def assert_timestamp_like expect, given, comment + match = Regexp.new expect.to_time.strftime('%F %T') + assert_kind_of DateTime, given + assert_match match, given.strftime('%F %T'), comment end end end end