Module: QuoteSql::Test

Defined in:
lib/quote_sql/test.rb

Defined Under Namespace

Classes: PseudoActiveRecord

Class Method Summary collapse

Class Method Details

.allObject



2
3
4
5
6
7
8
# File 'lib/quote_sql/test.rb', line 2

def self.all
  methods(false).grep(/^test_/).each do |name|
    run(name)
    puts
  end

end

.expected(v = nil) ⇒ Object



21
22
23
# File 'lib/quote_sql/test.rb', line 21

def self.expected(v = nil)
  @expected ||= v
end

.run(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/quote_sql/test.rb', line 10

def self.run(name)
  name = name.to_s.sub(/^test_/, "")
  @expected = nil
  @test = send("test_#{name}")
  if sql.gsub(/\s+/, "") == expected&.gsub(/\s+/, "")
    STDOUT.puts name, @test.original, @test.quotes.inspect, "#{expected}"
  else
    STDOUT.puts name, @test.inspect, sql, "#{expected}"
  end
end

.sqlObject



25
26
27
# File 'lib/quote_sql/test.rb', line 25

def self.sql
  @test.to_sql
end

.test_bindsObject



82
83
84
85
86
87
# File 'lib/quote_sql/test.rb', line 82

def test_binds
  expected Arel.sql(%(SELECT $1, $2, $1 AS get_bind_1_again FROM "my_table"))
  QuoteSql.new("SELECT %bind, %bind__uuid, %bind1 AS get_bind_1_again FROM %table_name").quote(
    table_name: "my_table"
  )
end

.test_columns_and_table_name_complexObject



52
53
54
55
56
57
58
# File 'lib/quote_sql/test.rb', line 52

def test_columns_and_table_name_complex
  expected Arel.sql(%(SELECT "a","b"."c" FROM "table1","table2"))
  QuoteSql.new("SELECT %columns FROM %table_names").quote(
    columns: [:a, b: :c],
    table_names: ["table1", "table2"]
  )
end

.test_columns_and_table_name_simpleObject



44
45
46
47
48
49
50
# File 'lib/quote_sql/test.rb', line 44

def test_columns_and_table_name_simple
  expected Arel.sql(%(SELECT "a","b"."c" FROM "my_table"))
  QuoteSql.new("SELECT %columns FROM %table_name").quote(
    columns: [:a, b: :c],
    table_name: "my_table"
  )
end

.test_q3Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/quote_sql/test.rb', line 89

def test_q3
  expected Arel.sql(<<-SQL)
        INSERT INTO "responses" ("id","type","task_id","index","data","parts","value","created_at","updated_at") 
        VALUES (NULL,TRUE,'A','[5,5]','{"a":1}'),
               (1,FALSE,'B','[]','{"a":2}'),
               (2,NULL,'c','[1,2,3]','{"a":3}')
        ON CONFLICT (responses_task_id_index_unique) DO NOTHING;
    SQL

  QuoteSql.new(<<-SQL).
      INSERT INTO %table (%columns) VALUES %values
        ON CONFLICT (responses_task_id_index_unique) DO NOTHING;
    SQL
    quote(
      table: Response,
      values: [
        [nil, true, "A", [5, 5], { a: 1 }],
        [1, false, "B", [], { a: 2 }],
        [2, nil, "c", [1, 2, 3], { a: 3 }]
      ]
    )
end

.test_recursive_injectsObject



60
61
62
63
64
65
66
67
68
# File 'lib/quote_sql/test.rb', line 60

def test_recursive_injects
  expected Arel.sql(%(SELECT TRUE FROM "table1"))
  QuoteSql.new("SELECT %raw FROM %table_names").quote(
    raw: "%recurse1_raw",
    recurse1_raw: "%recurse2",
    recurse2: true,
    table_names: "table1"
  )
end

.test_valuesObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/quote_sql/test.rb', line 70

def test_values
  expected Arel.sql(%(SELECT 'a text', 123, 'text' AS abc FROM "my_table"))
  QuoteSql.new("SELECT %text, %{number}, %aliased_with_hash FROM %table_name").quote(
    text: "a text",
    number: 123,
    aliased_with_hash: {
      abc: "text"
    },
    table_name: "my_table"
  )
end