File: pg_inet_spec.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (78 lines) | stat: -rw-r--r-- 3,244 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
require_relative "spec_helper"

describe "pg_inet extension" do
  ipv6_broken = (IPAddr.new('::1'); false) rescue true
  before do
    @db = Sequel.connect('mock://postgres')
    @db.extend_datasets{def quote_identifiers?; false end}
    @db.extension(:pg_array, :pg_inet)
  end

  it "should literalize IPAddr v4 instances to strings correctly" do
    @db.literal(IPAddr.new('127.0.0.1')).must_equal "'127.0.0.1/32'"
    @db.literal(IPAddr.new('127.0.0.0/8')).must_equal "'127.0.0.0/8'"
  end

  it "should literalize IPAddr v6 instances to strings correctly" do
    @db.literal(IPAddr.new('2001:4f8:3:ba::/64')).must_equal "'2001:4f8:3:ba::/64'"
    @db.literal(IPAddr.new('2001:4f8:3:ba:2e0:81ff:fe22:d1f1')).must_equal "'2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'"
  end unless ipv6_broken

  it "should set up conversion procs correctly" do
    cp = @db.conversion_procs
    cp[869].call("127.0.0.1").must_equal IPAddr.new('127.0.0.1')
    cp[650].call("127.0.0.1").must_equal IPAddr.new('127.0.0.1')
  end

  it "should set up conversion procs for arrays correctly" do
    cp = @db.conversion_procs
    cp[1041].call("{127.0.0.1}").must_equal [IPAddr.new('127.0.0.1')]
    cp[651].call("{127.0.0.1}").must_equal [IPAddr.new('127.0.0.1')]
    cp[1040].call("{127.0.0.1}").must_equal ['127.0.0.1']
  end

  it "should not affect literalization of custom objects" do
    o = Object.new
    def o.sql_literal(ds) 'v' end
    @db.literal(o).must_equal 'v'
  end

  it "should support using IPAddr as bound variables" do
    @db.bound_variable_arg(1, nil).must_equal 1
    @db.bound_variable_arg(IPAddr.new('127.0.0.1'), nil).must_equal '127.0.0.1/32'
  end

  it "should support using IPAddr instances in array types in bound variables" do
    @db.bound_variable_arg(Sequel.pg_array([IPAddr.new('127.0.0.1')]), nil).must_equal '{"127.0.0.1/32"}'
  end

  it "should parse inet/cidr type from the schema correctly" do
    @db.fetch = [{:name=>'id', :db_type=>'integer'}, {:name=>'i', :db_type=>'inet'}, {:name=>'c', :db_type=>'cidr'}]
    @db.schema(:items).map{|e| e[1][:type]}.must_equal [:integer, :ipaddr, :ipaddr]
  end

  it "should set :ruby_default schema entries if default value is recognized" do
    @db.fetch = [{:name=>'id', :db_type=>'integer', :default=>'1'}, {:name=>'t', :db_type=>'inet', :default=>"'127.0.0.1'::inet"}]
    s = @db.schema(:items)
    s[1][1][:ruby_default].must_equal IPAddr.new('127.0.0.1')
  end

  it "should support typecasting for the ipaddr type" do
    ip = IPAddr.new('127.0.0.1')
    @db.typecast_value(:ipaddr, ip).must_be_same_as(ip)
    @db.typecast_value(:ipaddr, ip.to_s).must_equal ip
    proc{@db.typecast_value(:ipaddr, '')}.must_raise(Sequel::InvalidValue)
    proc{@db.typecast_value(:ipaddr, 1)}.must_raise(Sequel::InvalidValue)

    proc{@db.typecast_value(:ipaddr, '::ffff%'+'a'*100)}.must_raise(Sequel::InvalidValue)
    @db.check_string_typecast_bytesize = false
    if RUBY_VERSION >= '3.1'
      @db.typecast_value(:ipaddr, '::ffff%'+'a'*100).must_equal IPAddr.new('::ffff%'+'a'*100)
    end
  end

  it "should return correct results for Database#schema_type_class" do
    @db.schema_type_class(:ipaddr).must_equal IPAddr
    @db.schema_type_class(:integer).must_equal Integer
  end
end