File: pg_extended_date_support_spec.rb

package info (click to toggle)
ruby-sequel 5.41.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,548 kB
  • sloc: ruby: 104,241; makefile: 3
file content (126 lines) | stat: -rw-r--r-- 4,903 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require_relative "spec_helper"

describe "pg_extended_date_support extension" do
  before do
    @db = Sequel.mock(:host=>'postgres', :fetch=>{:v=>1}).extension(:pg_extended_date_support)
    @db.extend_datasets{def quote_identifiers?; false end}
  end
  after do
    Sequel.datetime_class = Time
    Sequel.default_timezone = nil
  end

  it "should convert infinite timestamps and dates as configured" do
    cp = @db.conversion_procs
    d = lambda{|v| cp[1082].call(v)}
    t = lambda{|v| cp[1114].call(v)}
    pi = 'infinity'
    ni = '-infinity'
    today = Date.today
    now = Time.now

    d.(today.to_s).must_equal today
    t.(now.strftime("%Y-%m-%d %H:%M:%S.%N")).must_equal now
    proc{@db.typecast_value(:date, pi)}.must_raise Sequel::InvalidValue
    proc{@db.typecast_value(:datetime, pi)}.must_raise Sequel::InvalidValue

    [:nil, 'nil'].each do |v|
      @db.convert_infinite_timestamps = v
      d.(pi).must_be_nil
      t.(pi).must_be_nil
      d.(ni).must_be_nil
      t.(ni).must_be_nil
      @db.typecast_value(:date, pi).must_equal pi
      @db.typecast_value(:datetime, pi).must_equal pi
      @db.typecast_value(:date, ni).must_equal ni
      @db.typecast_value(:datetime, ni).must_equal ni
    end

    d.(today.to_s).must_equal today
    t.(now.strftime("%Y-%m-%d %H:%M:%S.%N")).must_equal now
    @db.typecast_value(:date, today.to_s).must_equal today
    @db.typecast_value(:datetime, now.strftime("%Y-%m-%d %H:%M:%S.%N")).must_equal now

    [:string, 'string'].each do |v|
      @db.convert_infinite_timestamps = v
      d.(pi).must_equal pi
      t.(pi).must_equal pi
      d.(ni).must_equal ni
      t.(ni).must_equal ni
    end

    [:date, 'date'].each do |v|
      @db.convert_infinite_timestamps = v
      d.(pi).must_equal Date::Infinity.new
      t.(pi).must_equal Date::Infinity.new
      d.(ni).must_equal(-Date::Infinity.new)
      t.(ni).must_equal(-Date::Infinity.new)
    end

    [:float, 'float', 't', true].each do |v|
      @db.convert_infinite_timestamps = v
      d.(pi).must_equal 1.0/0.0
      t.(pi).must_equal 1.0/0.0
      d.(ni).must_equal(-1.0/0.0)
      t.(ni).must_equal(-1.0/0.0)
    end

    ['f', false].each do |v|
      @db.convert_infinite_timestamps = v
      proc{d.(pi)}.must_raise ArgumentError, Sequel::InvalidValue
      proc{t.(pi)}.must_raise ArgumentError, Sequel::InvalidValue
      proc{d.(ni)}.must_raise ArgumentError, Sequel::InvalidValue
      proc{t.(ni)}.must_raise ArgumentError, Sequel::InvalidValue
    end
  end

  it "should handle parsing BC dates" do
    @db.conversion_procs[1082].call("1092-10-20 BC").must_equal Date.new(-1091, 10, 20)
  end

  it "should handle parsing BC timestamps as Time values" do
    @db.conversion_procs[1114].call("1200-02-15 14:13:20-00:00 BC").must_equal Time.at(-100000000000).utc
    @db.conversion_procs[1114].call("1200-02-15 14:13:20-00:00:00 BC").must_equal Time.at(-100000000000).utc
    Sequel.default_timezone = :utc
    @db.conversion_procs[1114].call("1200-02-15 14:13:20 BC").must_equal Time.at(-100000000000).utc
    Sequel.default_timezone = nil
  end

  it "should handle parsing BC timestamps as DateTime values" do
    Sequel.datetime_class = DateTime
    @db.conversion_procs[1114].call("1200-02-15 14:13:20-00:00 BC").must_equal DateTime.new(-1199, 2, 15, 14, 13, 20)
    @db.conversion_procs[1114].call("1200-02-15 14:13:20-00:00:00 BC").must_equal DateTime.new(-1199, 2, 15, 14, 13, 20)
    Sequel.default_timezone = :utc
    @db.conversion_procs[1114].call("1200-02-15 14:13:20 BC").must_equal DateTime.new(-1199, 2, 15, 14, 13, 20)
  end

  it "should handle parsing AD timestamps with offset seconds" do
    @db.conversion_procs[1114].call("1200-02-15 14:13:20-00:00:00").must_equal DateTime.new(1200, 2, 15, 14, 13, 20).to_time
    Sequel.datetime_class = DateTime
    @db.conversion_procs[1114].call("1200-02-15 14:13:20-00:00:00").must_equal DateTime.new(1200, 2, 15, 14, 13, 20)
  end

  it "should format Date::Infinity values" do
    @db.literal(Date::Infinity.new).must_equal "'infinity'"
    @db.literal(-Date::Infinity.new).must_equal "'-infinity'"
  end

  it "should raise errors for literalizing random Objects" do
    proc{@db.literal(Object.new)}.must_raise Sequel::Error
  end

  it "should format BC dates" do
    @db.literal(Date.new(-1091, 10, 20)).must_equal "'1092-10-20 BC'"
    @db.literal(Date.new(1092, 10, 20)).must_equal "'1092-10-20'"
  end

  it "should format BC datetimes" do
    @db.literal(DateTime.new(-1199, 2, 15, 14, 13, 20)).must_equal "'1200-02-15 14:13:20.000000000+0000 BC'"
    @db.literal(DateTime.new(1200, 2, 15, 14, 13, 20)).must_equal "'1200-02-15 14:13:20.000000+0000'"
  end

  it "should format BC times" do
    @db.literal(Time.at(-100000000000).utc).must_equal "'1200-02-15 14:13:20.000000000+0000 BC'"
    @db.literal(Time.at(100000000000).utc).must_equal "'5138-11-16 09:46:40.000000+0000'"
  end
end