Module: ActiveRecord::Metal::Postgresql::Conversions::Etest

Includes:
EtestBase
Defined in:
lib/active_record/metal/postgresql/conversions/etest.rb,
lib/active_record/metal/postgresql/conversions/etest.rb

Overview

require “active_record/metal/postgresql/conversions/etest”

Constant Summary

Constants included from EtestBase

EtestBase::SELF

Instance Method Summary collapse

Methods included from EtestBase

#count, load_expectation_assertions, #metal, #setup

Instance Method Details

#test_byteaObject



34
35
36
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 34

def test_bytea
  expect! metal.ask("SELECT '1'::bytea") => '1'
end

#test_bytea_argsObject



96
97
98
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 96

def test_bytea_args
  expect! metal.ask("SELECT '1'::bytea WHERE  '1'::bytea=$1", '1') => '1'
end

#test_column_namesObject



57
58
59
60
61
62
63
64
65
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 57

def test_column_names
  result = metal.exec("SELECT 1 as one, 2 as two WHERE FALSE")
  expect! result.columns == %w(one two)
  expect! result.empty?

  result = metal.exec("SELECT 1 as one, 2 as two")
  expect! result.columns == %w(one two)
  expect! result == [[ 1, 2 ]]
end

#test_datesObject



38
39
40
41
42
43
44
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 38

def test_dates
  expect! metal.ask("SELECT '1/18/1999'::date") => Date.parse("18. 1. 1999")
  expect! metal.ask("SELECT '1999-01-08 04:05:06'::timestamp") => Time.parse("1999-01-08 04:05:06")
  expect! metal.ask("SELECT TIMESTAMP '1999-01-08 04:05:06'") => Time.parse("1999-01-08 04:05:06")
  expect! metal.ask("SELECT CURRENT_TIME") => Time
  expect! metal.ask("SELECT CURRENT_TIMESTAMP") => Time
end

#test_dates_argsObject



100
101
102
103
104
105
106
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 100

def test_dates_args
  date = Date.parse("18. 1. 1999")
  expect! metal.ask("SELECT '1/18/1999'::date WHERE '1/18/1999'::date=$1", date) => date

  ts = Time.parse("1999-01-08 04:05:06")
  expect! metal.ask("SELECT '1999-01-08 04:05:06'::timestamp WHERE '1999-01-08 04:05:06'::timestamp=$1", ts) => ts
end

#test_empty_resultObject



52
53
54
55
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 52

def test_empty_result
  expect! metal.ask("SELECT NULL WHERE FALSE") => nil
  expect! metal.ask("SELECT 1 WHERE FALSE") => nil
end

#test_hstoreObject



67
68
69
70
71
72
73
74
75
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 67

def test_hstore
  result = metal.ask("SELECT 'foo=>foo,bar=>NULL'::hstore")
  assert_equal result, foo: "foo", bar: nil

  # C = PgTypedQueries::Conversions
  #
  # assert_equal C::HStore.escape(a: 1), "'a=>1'::hstore"
  # assert_equal C::HStore.escape(foo: "foo", bar: nil), "'foo=>foo,bar=>NULL'::hstore"
end

#test_hstore_argsObject



113
114
115
116
117
118
119
120
121
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 113

def test_hstore_args
  id = metal.ask "INSERT INTO alloys(hsh) VALUES($1) RETURNING id", foo: "foo", bar: nil
  assert_equal metal.ask("SELECT hsh FROM alloys WHERE id=$1", id), bar: nil, foo: "foo"


  result = metal.ask("SELECT 'foo=>foo,bar=>NULL'::hstore WHERE 'foo=>foo,bar=>NULL'::hstore=$1",
                      foo: "foo", bar: nil)
  assert_equal result, foo: "foo", bar: nil
end

#test_moneyObject



23
24
25
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 23

def test_money
  expect! metal.ask("SELECT 1234::text::money") => 1234.0
end

#test_money_argsObject



87
88
89
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 87

def test_money_args
  expect! metal.ask("SELECT 1234::text::money WHERE 1234::text::money=$1", 1234.0) => 1234.0
end

#test_numeric_typesObject



7
8
9
10
11
12
13
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 7

def test_numeric_types
  expect! metal.ask("SELECT 1") => 1

  [ "smallint", "integer", "bigint", "decimal", "numeric", "real", "double precision" ].each do |type|
    expect! metal.ask("SELECT 1::#{type}") => 1
  end
end

#test_numeric_types_argsObject



81
82
83
84
85
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 81

def test_numeric_types_args
  [ "smallint", "integer", "bigint", "decimal", "numeric", "real", "double precision" ].each do |type|
    expect! metal.ask("SELECT 1::#{type} WHERE 1=$1", 1) => 1
  end
end

#test_special_numbersObject



15
16
17
18
19
20
21
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 15

def test_special_numbers
  expect! metal.ask("SELECT 'Infinity'::real") => Float::INFINITY
  expect! metal.ask("SELECT '-Infinity'::real") => -Float::INFINITY 

  # Can't test for NAN == NAN
  # expect! metal.ask("SELECT 'NaN'::real") => Float::NAN 
end

#test_stringsObject



27
28
29
30
31
32
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 27

def test_strings
  expect! metal.ask("SELECT 'ok'")
  expect! metal.ask("SELECT 'good      '") => 'good      '
  expect! metal.ask("SELECT 'too long'::varchar(5)") => 'too l'
  expect! metal.ask("SELECT 'ä'") => 'ä'
end

#test_strings_argsObject



91
92
93
94
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 91

def test_strings_args
  expect! metal.ask("SELECT 'ok' WHERE 'ok'=$1", 'ok')    => 'ok'
  expect! metal.ask("SELECT 'ä' WHERE 'ä'=$1", 'ä')       => 'ä'
end

#test_true_and_friendsObject



46
47
48
49
50
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 46

def test_true_and_friends
  expect! metal.ask("SELECT TRUE") => true
  expect! metal.ask("SELECT FALSE") => false
  expect! metal.ask("SELECT NULL") => nil
end

#test_true_and_friends_argsObject



108
109
110
111
# File 'lib/active_record/metal/postgresql/conversions/etest.rb', line 108

def test_true_and_friends_args
  expect! metal.ask("SELECT TRUE WHERE TRUE=$1", true) => true
  expect! metal.ask("SELECT FALSE WHERE FALSE=$1", false) => false
end