Module: QuoteSql::Test
- Defined in:
- lib/quote_sql/test.rb
Defined Under Namespace
Classes: PseudoActiveRecord
Class Method Summary collapse
- .all ⇒ Object
- .expected(v = nil) ⇒ Object
- .run(name, all) ⇒ Object
- .sql ⇒ Object
- .test_binds ⇒ Object
- .test_columns_and_table_name_complex ⇒ Object
- .test_columns_and_table_name_simple ⇒ Object
- .test_from_values_array ⇒ Object
- .test_from_values_hash_no_columns ⇒ Object
- .test_from_values_hash_with_columns ⇒ Object
- .test_from_values_hash_with_type_columns ⇒ Object
- .test_insert_values_array ⇒ Object
- .test_insert_values_hash ⇒ Object
- .test_recursive_injects ⇒ Object
- .test_values ⇒ Object
Class Method Details
.all ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/quote_sql/test.rb', line 2 def self.all @success = [] @fail = [] methods(false).grep(/^test_/).each do |name| run(name, true) end @success.each { STDOUT.puts(*_1, nil) } @fail.each { STDOUT.puts(*_1, nil) } puts end |
.expected(v = nil) ⇒ Object
28 29 30 |
# File 'lib/quote_sql/test.rb', line 28 def self.expected(v = nil) @expected ||= v end |
.run(name, all) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/quote_sql/test.rb', line 13 def self.run(name, all) name = name.to_s.sub(/^test_/, "") @expected = nil @test = send("test_#{name}") if sql.gsub(/\s+/, "")&.downcase&.strip == expected&.gsub(/\s+/, "")&.downcase&.strip rv = [name, @test.original, @test.quotes.inspect, "✅ #{expected}"] @success << rv if @success else rv = [name, @test.inspect, sql, "❌ #{expected}"] @fail << rv if @fail end STDOUT.puts rv unless @fail or @success end |
.sql ⇒ Object
32 33 34 |
# File 'lib/quote_sql/test.rb', line 32 def self.sql @test.to_sql end |
.test_binds ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/quote_sql/test.rb', line 91 def test_binds expected <<~SQL SELECT $1, $2, $1 AS get_bind_1_again FROM "my_table" SQL 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_complex ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/quote_sql/test.rb', line 59 def test_columns_and_table_name_complex expected %(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_simple ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/quote_sql/test.rb', line 51 def test_columns_and_table_name_simple expected %(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_from_values_array ⇒ Object
100 101 102 103 104 105 |
# File 'lib/quote_sql/test.rb', line 100 def test_from_values_array expected <<~SQL SELECT * FROM (VALUES ('a',1,TRUE,NULL)) AS "x" ("column1","column2","column3","column4") SQL "SELECT * FROM %x_values".quote_sql(x_values: [['a', 1, true, nil]]) end |
.test_from_values_hash_no_columns ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/quote_sql/test.rb', line 107 def test_from_values_hash_no_columns expected <<~SQL SELECT * FROM (VALUES ('a', 1, true, NULL), ('a', 1, true, NULL), (NULL, 1, NULL, 2)) AS "y" ("a", "b", "c", "d") SQL "SELECT * FROM %y_values".quote_sql(y_values: [ { a: 'a', b: 1, c: true, d: nil }, { d: nil, a: 'a', c: true, b: 1 }, { d: 2, b: 1 } ]) end |
.test_from_values_hash_with_columns ⇒ Object
118 119 120 121 122 123 |
# File 'lib/quote_sql/test.rb', line 118 def test_from_values_hash_with_columns expected <<~SQL SELECT * FROM (VALUES (NULL, true, 1, 'a')) AS "x" ("d","c","b","a") SQL "SELECT * FROM %x_values".quote_sql(x_columns: %i[d c b a], x_values: [{ a: 'a', b: 1, c: true, d: nil }]) end |
.test_from_values_hash_with_type_columns ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/quote_sql/test.rb', line 125 def test_from_values_hash_with_type_columns expected <<~SQL SELECT * FROM (VALUES ('a'::TEXT, 1::INTEGER, true::BOOLEAN, NULL::FLOAT), ('a', 1, true, NULL), (NULL, 1, NULL, 2)) AS "x" ("a", "b", "c", "d") SQL "SELECT * FROM %x_values".quote_sql( x_columns: { a: "text", b: "integer", c: "boolean", d: "float" }, x_values: [ { a: 'a', b: 1, c: true, d: nil }, { d: nil, a: 'a', c: true, b: 1 }, { d: 2, b: 1 } ]) end |
.test_insert_values_array ⇒ Object
143 144 145 146 147 148 |
# File 'lib/quote_sql/test.rb', line 143 def test_insert_values_array expected <<~SQL INSERT INTO x VALUES ('a', 1, true, NULL) SQL "INSERT INTO x %values".quote_sql(values: [['a', 1, true, nil]]) end |
.test_insert_values_hash ⇒ Object
150 151 152 153 154 155 |
# File 'lib/quote_sql/test.rb', line 150 def test_insert_values_hash expected <<~SQL INSERT INTO x ("a", "b", "c", "d") VALUES ('a', 1, true, NULL) SQL "INSERT INTO x %values".quote_sql(values: [{ a: 'a', b: 1, c: true, d: nil }]) end |
.test_recursive_injects ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/quote_sql/test.rb', line 67 def test_recursive_injects expected %(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_values ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/quote_sql/test.rb', line 77 def test_values expected <<~SQL SELECT 'a text', 123, 'text' AS abc FROM "my_table" SQL 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 |