File: pg_row_plugin_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 (81 lines) | stat: -rw-r--r-- 3,528 bytes parent folder | download | duplicates (3)
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
require_relative "spec_helper"

describe "Sequel::Plugins::PgRow" do
  before do
    @db = Sequel.connect('mock://postgres')
    @db.extend_datasets{def quote_identifiers?; false end}
    @db.extension(:pg_array)
    @c = Class.new(Sequel::Model(@db[:address]))
    @c.columns :street, :city
    @c.db_schema[:street][:type] = :string
    @c.db_schema[:city][:type] = :string
    @db.fetch = [[{:oid=>1098, :typrelid=>2, :typarray=>3}], [{:attname=>'street', :atttypid=>1324}, {:attname=>'city', :atttypid=>1324}]]
    @c.plugin :pg_row

    @c2 = Class.new(Sequel::Model(@db[:company]))
    @c2.columns :address
    @c2.db_schema[:address].merge!(:type=>:pg_row_address)
  end

  it "should have schema_type_class include Sequel::Model" do
    @c2.new.send(:schema_type_class, :address).must_equal @c
    @db.conversion_procs[1098].call('(123 Foo St,Bar City)').must_equal @c.load(:street=>'123 Foo St', :city=>'Bar City')
  end

  it "should set up a parser for the type that creates a model class" do
    @db.conversion_procs[1098].call('(123 Foo St,Bar City)').must_equal @c.load(:street=>'123 Foo St', :city=>'Bar City')
  end

  it "should set up type casting for the type" do
    @c2.new(:address=>{'street'=>123, 'city'=>:Bar}).address.must_equal @c.load(:street=>'123', :city=>'Bar')
  end

  it "should return model instances as is when typecasting to rows" do
    o = @c.load(:street=>'123', :city=>'Bar')
    @c2.new(:address=>o).address.must_be_same_as(o)
  end

  it "should handle literalizing model instances" do
    @db.literal(@c.load(:street=>'123 Foo St', :city=>'Bar City')).must_equal "ROW('123 Foo St', 'Bar City')::address"
  end

  it "should handle literalizing model instances when model table is aliased" do
    @c.dataset = @c.dataset.from(Sequel.as(:address, :a))
    @db.literal(@c.load(:street=>'123 Foo St', :city=>'Bar City')).must_equal "ROW('123 Foo St', 'Bar City')::address"
  end

  it "should handle model instances in bound variables" do
    @db.bound_variable_arg(1, nil).must_equal 1
    @db.bound_variable_arg(@c.load(:street=>'123 Foo St', :city=>'Bar City'), nil).must_equal '("123 Foo St","Bar City")'
  end

  it "should handle model instances in arrays of bound variables" do
    @db.bound_variable_arg(1, nil).must_equal 1
    @db.bound_variable_arg(Sequel.pg_array([@c.load(:street=>'123 Foo St', :city=>'Bar City')]), nil).must_equal '{"(\\"123 Foo St\\",\\"Bar City\\")"}'
  end

  it "should allow inserting just this model value" do
    @c2.dataset.insert_sql(@c.load(:street=>'123', :city=>'Bar')).must_equal "INSERT INTO company VALUES (ROW('123', 'Bar')::address)"
  end

  it "should work when loaded into models without a dataset" do
    @db = Sequel.connect('mock://postgres')
    @db.extend_datasets{def quote_identifiers?; false end}
    @db.extension(:pg_array)
    @c = Class.new(Sequel::Model(@db))
    @c.plugin :pg_row
    @c.set_dataset(@db[:address])
    @c.columns :street, :city
    @c.db_schema[:street][:type] = :string
    @c.db_schema[:city][:type] = :string
    @db.fetch = [[{:oid=>1098, :typrelid=>2, :typarray=>3}], [{:attname=>'street', :atttypid=>1324}, {:attname=>'city', :atttypid=>1324}]]
    @c.register_row_type

    @c2 = Class.new(Sequel::Model(@db[:company]))
    @c2.columns :address
    @c2.db_schema[:address].merge!(:type=>:pg_row_address)
    @c2.new.send(:schema_type_class, :address).must_equal @c
    @db.conversion_procs[1098].call('(123 Foo St,Bar City)').must_equal @c.load(:street=>'123 Foo St', :city=>'Bar City')
  end

end