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



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

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



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

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



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

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



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

def test_q3
  expected Arel.sql("        INSERT INTO \"responses\" (\"id\",\"type\",\"task_id\",\"index\",\"data\",\"parts\",\"value\",\"created_at\",\"updated_at\") \n        VALUES (NULL,TRUE,'A','[5,5]','{\"a\":1}'),\n               (1,FALSE,'B','[]','{\"a\":2}'),\n               (2,NULL,'c','[1,2,3]','{\"a\":3}')\n        ON CONFLICT (responses_task_id_index_unique) DO NOTHING;\n    SQL\n\n  QuoteSql.new(<<-SQL).\n      INSERT INTO %table (%columns) VALUES %values\n        ON CONFLICT (responses_task_id_index_unique) DO NOTHING;\n    SQL\n    quote(\n      table: Response,\n      values: [\n        [nil, true, \"A\", [5, 5], { a: 1 }],\n        [1, false, \"B\", [], { a: 2 }],\n        [2, nil, \"c\", [1, 2, 3], { a: 3 }]\n      ]\n    )\nend\n")

.test_recursive_injectsObject



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

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



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

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