Class: QuoteSql::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/quote_sql/test.rb

Defined Under Namespace

Classes: PseudoActiveRecord

Constant Summary collapse

DATATYPES =
"bigint\nint8\nbigserial\nserial8\nbit\nbit (1)\nbit varying\nvarbit\nbit varying (2)\nvarbit (2)\nboolean\nbool\nbox\nbytea\ncharacter\nchar\ncharacter (1)\nchar (1)\ncharacter varying\nvarchar\ncharacter varying (1)\nvarchar (1)\ncidr\ncircle\ndate\ndouble precision\nfloat8\ninet\ninteger\nint\nint4\ninterval\ninterval (1)\njson\njsonb\nline\nlseg\nmacaddr\nmacaddr8\nmoney\nnumeric\nnumeric(10,3)\ndecimal\ndecimal(10,3)\npath\npg_lsn\npg_snapshot\npoint\npolygon\nreal\nfloat4\nsmallint\nint2\nsmallserial\nserial\nserial2\nserial4\ntext\ntime\ntime(1)\ntime without time zone\ntime(1) without time zone\ntime with time zone\ntime(2) with time zone\ntimetz\ntimestamp\ntimestamp(1)\ntimestamp without time zone\ntimestamp(1) without time zone\ntimestamp with time zone\ntimestamp(1) with time zone\ntimestamptz\ntsquery\ntsvector\ntxid_snapshot\nuuid\nxml\n"

Instance Method Summary collapse

Instance Method Details

#allObject



208
209
210
211
212
213
214
215
# File 'lib/quote_sql/test.rb', line 208

def all
  @success = []
  @fail = []
  private_methods(false).grep(/^test_/).each { run(_1, true) }
  @success.each { STDOUT.puts(*_1, nil) }
  @fail.each { STDOUT.puts(*_1, nil) }
  puts
end

#datatypeObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/quote_sql/test.rb', line 263

def datatype
  errors = {}
  success = []
  spaces = ->(*) { " " * (rand(4) + 1) }

  DATATYPES.each_line(chomp: true) do |line|

    l = line.gsub(/\s+/, &spaces).gsub(/(?<=\()\d+|\d+(?=\))/) { "#{spaces.call}#{rand(10) + 1}#{spaces.call}" }.gsub(/\(/) { "#{spaces.call}(" }

    m = "jgj hsgjhsgfjh ag %field::#{l} asldfalskjdfl".match(QuoteSql::CASTS)
    if m.present? and l == m[1]
        success << line
      else
        errors[line] = m&.to_a
    end
    line = line + "[]"*(rand(3) + 1)
    m = "jgj hsgjhsgfjh ag %field::#{line} asldfalskjdfl".match(QuoteSql::CASTS)
    if m.present? and line == m[1] + m[2]
      success << line
    else
      errors[line] = m&.to_a
    end
  end
  puts success.sort.inspect
  ap errors
end

#expected(v = nil) ⇒ Object



241
242
243
# File 'lib/quote_sql/test.rb', line 241

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

#run(name, all = false) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/quote_sql/test.rb', line 217

def run(name, all = false)
  name = name.to_s.sub(/^test_/, "")
  rv = ["๐Ÿงช #{name}"]
  @expected = nil
  @test = send("test_#{name}")
  if sql.gsub(/\s+/, "")&.downcase&.strip == expected&.gsub(/\s+/, "")&.downcase&.strip
    tables = @test.tables.to_h { [[_1, "table"].compact.join("_"), _2] }
    columns = @test.instance_variable_get(:@columns).to_h { [[_1, "columns"].compact.join("_"), _2] }
    rv += [
      "QuoteSql.new(\"#{@test.original}\").quote(#{{ **tables, **columns, **@test.quotes }.inspect}).to_sql", "๐ŸŽฏ #{expected}", "โœ… #{sql}"]
    @success << rv if @success
  else
    rv += [@test.inspect, "๐ŸŽฏ #{expected}", "โŒ #{sql}"]
    rv << sql.gsub(/\s+/, "")&.downcase&.strip
    rv << expected&.gsub(/\s+/, "")&.downcase&.strip
    @fail << rv if @fail
  end
rescue => exc
  rv += [@test.inspect, "๐ŸŽฏ #{expected}", "โŒ #{sql}", exc.message]
  @fail << rv if @fail
ensure
  STDOUT.puts(*rv) unless @fail or @success
end

#sqlObject



245
246
247
# File 'lib/quote_sql/test.rb', line 245

def sql
  @test.to_sql
end