Class: QuoteSql::Test

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

Defined Under Namespace

Classes: PseudoActiveRecordKlass

Constant Summary collapse

DATATYPES =
<<-DATATYPES
bigint
int8
bigserial
serial8
bit
bit (1)
bit varying
varbit
bit varying (2)
varbit (2)
boolean
bool
box
bytea
character
char
character (1)
char (1)
character varying
varchar
character varying (1)
varchar (1)
cidr
circle
date
double precision
float8
inet
integer
int
int4
interval
interval (1)
json
jsonb
line
lseg
macaddr
macaddr8
money
numeric
numeric(10,3)
decimal
decimal(10,3)
path
pg_lsn
pg_snapshot
point
polygon
real
float4
smallint
int2
smallserial
serial
serial2
serial4
text
time
time(1)
time without time zone
time(1) without time zone
time with time zone
time(2) with time zone
timetz
timestamp
timestamp(1)
timestamp without time zone
timestamp(1) without time zone
timestamp with time zone
timestamp(1) with time zone
timestamptz
tsquery
tsvector
txid_snapshot
uuid
xml
DATATYPES

Instance Method Summary collapse

Instance Method Details

#allObject



258
259
260
261
262
263
264
265
# File 'lib/quote_sql/test.rb', line 258

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

#create_active_record_class(table_name, **options, &block) ⇒ Object



355
356
357
# File 'lib/quote_sql/test.rb', line 355

def create_active_record_class(table_name, **options, &block)
   PseudoActiveRecordKlass.new(table_name, **options, &block)
end

#datatypeObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/quote_sql/test.rb', line 360

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



293
294
295
# File 'lib/quote_sql/test.rb', line 293

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

#run(name, all = false) ⇒ Object



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

def run(name, all = false)
  name = name.to_s.sub(/^test_/, "")
  rv = ["๐Ÿงช #{name}"]
  puts(*rv)
  @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] }
    #"QuoteSql.new(\"#{@test.original}\").quote(#{{ **tables, **columns, **@test.quotes }.inspect}).to_sql",
      rv += ["๐ŸŽฏ #{expected}", "โœ… #{sql}"]

    @success << rv if @success
  else
    rv += [@test.inspect, "๐ŸŽฏ #{expected}", "โŒ #{sql}"]
    rv << "๐ŸŽฏ " + expected&.gsub(/\s+/, "")&.downcase&.strip
    rv << "โŒ " + sql.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



297
298
299
# File 'lib/quote_sql/test.rb', line 297

def sql
  @test.to_sql
end