|
4 | 4 | require 'models/subscriber' |
5 | 5 | require 'models/minimalistic' |
6 | 6 |
|
7 | | -# require 'models/reply' |
8 | | -# require 'models/joke' |
9 | | -# require 'models/post' |
10 | | -# require 'models/sqlserver/fk_test_has_pk' |
11 | | -# require 'models/sqlserver/fk_test_has_fk' |
12 | | -# require 'models/sqlserver/customers_view' |
13 | | -# require 'models/sqlserver/string_defaults_big_view' |
14 | | -# require 'models/sqlserver/string_defaults_view' |
15 | | -# require 'models/sqlserver/topic' |
16 | | -# require 'models/sqlserver/upper_test_default' |
17 | | -# require 'models/sqlserver/upper_test_lowered' |
18 | | - |
19 | 7 | class AdapterTestSQLServer < ActiveRecord::TestCase |
20 | 8 |
|
21 | 9 | fixtures :tasks |
@@ -324,198 +312,179 @@ class AdapterTestSQLServer < ActiveRecord::TestCase |
324 | 312 |
|
325 | 313 | end |
326 | 314 |
|
327 | | - describe 'For SchemaStatements' do |
328 | | - |
329 | | - describe 'returning from #type_to_sql' do |
330 | | - |
331 | | - it 'create integers when no limit supplied' do |
332 | | - assert_equal 'integer', connection.type_to_sql(:integer) |
333 | | - end |
334 | | - |
335 | | - it 'create integers when limit is 4' do |
336 | | - assert_equal 'integer', connection.type_to_sql(:integer, 4) |
337 | | - end |
338 | | - |
339 | | - it 'create integers when limit is 3' do |
340 | | - assert_equal 'integer', connection.type_to_sql(:integer, 3) |
341 | | - end |
342 | | - |
343 | | - it 'create smallints when limit is less than 3' do |
344 | | - assert_equal 'smallint', connection.type_to_sql(:integer, 2) |
345 | | - assert_equal 'smallint', connection.type_to_sql(:integer, 1) |
346 | | - end |
347 | | - |
348 | | - it 'create bigints when limit is greateer than 4' do |
349 | | - assert_equal 'bigint', connection.type_to_sql(:integer, 5) |
350 | | - assert_equal 'bigint', connection.type_to_sql(:integer, 6) |
351 | | - assert_equal 'bigint', connection.type_to_sql(:integer, 7) |
352 | | - assert_equal 'bigint', connection.type_to_sql(:integer, 8) |
353 | | - end |
354 | | - |
355 | | - it 'create floats when no limit supplied' do |
356 | | - assert_equal 'float(8)', connection.type_to_sql(:float) |
357 | | - end |
| 315 | + describe 'schema statements' do |
358 | 316 |
|
359 | | - it 'create floats when limit is supplied' do |
360 | | - assert_equal 'float(27)', connection.type_to_sql(:float, 27) |
361 | | - end |
| 317 | + it 'create integers when no limit supplied' do |
| 318 | + assert_equal 'integer', connection.type_to_sql(:integer) |
| 319 | + end |
362 | 320 |
|
| 321 | + it 'create integers when limit is 4' do |
| 322 | + assert_equal 'integer', connection.type_to_sql(:integer, 4) |
363 | 323 | end |
364 | 324 |
|
365 | | - end |
| 325 | + it 'create integers when limit is 3' do |
| 326 | + assert_equal 'integer', connection.type_to_sql(:integer, 3) |
| 327 | + end |
366 | 328 |
|
367 | | - describe 'For indexes' do |
| 329 | + it 'create smallints when limit is less than 3' do |
| 330 | + assert_equal 'smallint', connection.type_to_sql(:integer, 2) |
| 331 | + assert_equal 'smallint', connection.type_to_sql(:integer, 1) |
| 332 | + end |
368 | 333 |
|
369 | | - before do |
370 | | - @desc_index_name = 'idx_credit_limit_test_desc' |
371 | | - connection.execute "CREATE INDEX [#{@desc_index_name}] ON [accounts] (credit_limit DESC)" |
| 334 | + it 'create bigints when limit is greateer than 4' do |
| 335 | + assert_equal 'bigint', connection.type_to_sql(:integer, 5) |
| 336 | + assert_equal 'bigint', connection.type_to_sql(:integer, 6) |
| 337 | + assert_equal 'bigint', connection.type_to_sql(:integer, 7) |
| 338 | + assert_equal 'bigint', connection.type_to_sql(:integer, 8) |
372 | 339 | end |
373 | 340 |
|
374 | | - after do |
375 | | - connection.execute "DROP INDEX [#{@desc_index_name}] ON [accounts]" |
| 341 | + it 'create floats when no limit supplied' do |
| 342 | + assert_equal 'float(8)', connection.type_to_sql(:float) |
376 | 343 | end |
377 | 344 |
|
378 | | - it 'have indexes with descending order' do |
379 | | - assert connection.indexes('accounts').find { |i| i.name == @desc_index_name } |
| 345 | + it 'create floats when limit is supplied' do |
| 346 | + assert_equal 'float(27)', connection.type_to_sql(:float, 27) |
380 | 347 | end |
381 | 348 |
|
382 | 349 | end |
383 | 350 |
|
384 | | - describe 'For views' do |
| 351 | + describe 'indexes' do |
385 | 352 |
|
386 | | - describe 'using connection.views' do |
| 353 | + let(:desc_index_name) { 'idx_credit_limit_test_desc' } |
387 | 354 |
|
388 | | - it 'return an array' do |
389 | | - assert_instance_of Array, connection.views |
390 | | - end |
391 | | - |
392 | | - it 'find CustomersView table name' do |
393 | | - connection.views.must_include 'customers_view' |
394 | | - end |
395 | | - |
396 | | - it 'work with dynamic finders' do |
397 | | - name = 'MetaSkills' |
398 | | - customer = CustomersView.create! name: name |
399 | | - assert_equal customer, CustomersView.find_by_name(name) |
| 355 | + it 'have indexes with descending order' do |
| 356 | + begin |
| 357 | + connection.execute "CREATE INDEX [#{desc_index_name}] ON [accounts] (credit_limit DESC)" |
| 358 | + assert connection.indexes('accounts').find { |i| i.name == desc_index_name } |
| 359 | + ensure |
| 360 | + connection.execute "DROP INDEX [#{desc_index_name}] ON [accounts]" |
400 | 361 | end |
| 362 | + end |
401 | 363 |
|
402 | | - it 'not contain system views' do |
403 | | - systables = ['sysconstraints','syssegments'] |
404 | | - systables.each do |systable| |
405 | | - assert !connection.views.include?(systable), "This systable #{systable} should not be in the views array." |
406 | | - end |
407 | | - end |
| 364 | + end |
408 | 365 |
|
409 | | - it 'allow the connection#view_information method to return meta data on the view' do |
410 | | - view_info = connection.send(:view_information,'customers_view') |
411 | | - assert_equal('customers_view', view_info['TABLE_NAME']) |
412 | | - assert_match(/CREATE VIEW customers_view/, view_info['VIEW_DEFINITION']) |
413 | | - end |
| 366 | + describe 'views' do |
414 | 367 |
|
415 | | - it 'allow the connection#view_table_name method to return true table_name for the view' do |
416 | | - assert_equal 'customers', connection.send(:view_table_name,'customers_view') |
417 | | - assert_equal 'topics', connection.send(:view_table_name,'topics'), 'No view here, the same table name should come back.' |
418 | | - end |
| 368 | + # Using connection.views |
419 | 369 |
|
| 370 | + it 'return an array' do |
| 371 | + assert_instance_of Array, connection.views |
420 | 372 | end |
421 | 373 |
|
422 | | - describe 'used by a class for table_name' do |
423 | | - |
424 | | - describe 'with same column names' do |
| 374 | + it 'find SSTestCustomersView table name' do |
| 375 | + connection.views.must_include 'sst_customers_view' |
| 376 | + end |
425 | 377 |
|
426 | | - it 'have matching column objects' do |
427 | | - columns = ['id','name','balance'] |
428 | | - assert !CustomersView.columns.blank? |
429 | | - assert_equal columns.size, CustomersView.columns.size |
430 | | - columns.each do |colname| |
431 | | - assert_instance_of ActiveRecord::ConnectionAdapters::SQLServerColumn, |
432 | | - CustomersView.columns_hash[colname], |
433 | | - "Column name #{colname.inspect} was not found in these columns #{CustomersView.columns.map(&:name).inspect}" |
434 | | - end |
435 | | - end |
| 378 | + it 'work with dynamic finders' do |
| 379 | + name = 'MetaSkills' |
| 380 | + customer = SSTestCustomersView.create! name: name |
| 381 | + assert_equal customer, SSTestCustomersView.find_by_name(name) |
| 382 | + end |
436 | 383 |
|
437 | | - it 'find identity column' do |
438 | | - assert CustomersView.columns_hash['id'].primary |
439 | | - end |
| 384 | + it 'not contain system views' do |
| 385 | + systables = ['sysconstraints','syssegments'] |
| 386 | + systables.each do |systable| |
| 387 | + assert !connection.views.include?(systable), "This systable #{systable} should not be in the views array." |
| 388 | + end |
| 389 | + end |
440 | 390 |
|
441 | | - it 'find default values' do |
442 | | - assert_equal 0, CustomersView.new.balance |
443 | | - end |
| 391 | + it 'allow the connection#view_information method to return meta data on the view' do |
| 392 | + view_info = connection.send(:view_information,'sst_customers_view') |
| 393 | + assert_equal('sst_customers_view', view_info['TABLE_NAME']) |
| 394 | + assert_match(/CREATE VIEW sst_customers_view/, view_info['VIEW_DEFINITION']) |
| 395 | + end |
444 | 396 |
|
445 | | - it 'respond true to table_exists?' do |
446 | | - assert CustomersView.table_exists? |
447 | | - end |
| 397 | + it 'allow the connection#view_table_name method to return true table_name for the view' do |
| 398 | + assert_equal 'customers', connection.send(:view_table_name,'sst_customers_view') |
| 399 | + assert_equal 'topics', connection.send(:view_table_name,'topics'), 'No view here, the same table name should come back.' |
| 400 | + end |
448 | 401 |
|
449 | | - it 'have correct table name for all column objects' do |
450 | | - assert CustomersView.columns.all?{ |c| c.table_name == 'customers_view' }, |
451 | | - CustomersView.columns.map(&:table_name).inspect |
452 | | - end |
| 402 | + # With same column names |
453 | 403 |
|
| 404 | + it 'have matching column objects' do |
| 405 | + columns = ['id','name','balance'] |
| 406 | + assert !SSTestCustomersView.columns.blank? |
| 407 | + assert_equal columns.size, SSTestCustomersView.columns.size |
| 408 | + columns.each do |colname| |
| 409 | + assert_instance_of ActiveRecord::ConnectionAdapters::SQLServerColumn, |
| 410 | + SSTestCustomersView.columns_hash[colname], |
| 411 | + "Column name #{colname.inspect} was not found in these columns #{SSTestCustomersView.columns.map(&:name).inspect}" |
454 | 412 | end |
| 413 | + end |
455 | 414 |
|
456 | | - describe 'with aliased column names' do |
457 | | - |
458 | | - it 'have matching column objects' do |
459 | | - columns = ['id','pretend_null'] |
460 | | - assert !StringDefaultsView.columns.blank? |
461 | | - assert_equal columns.size, StringDefaultsView.columns.size |
462 | | - columns.each do |colname| |
463 | | - assert_instance_of ActiveRecord::ConnectionAdapters::SQLServerColumn, |
464 | | - StringDefaultsView.columns_hash[colname], |
465 | | - "Column name #{colname.inspect} was not found in these columns #{StringDefaultsView.columns.map(&:name).inspect}" |
466 | | - end |
467 | | - end |
| 415 | + it 'find identity column' do |
| 416 | + pk_name = connection.primary_key(SSTestCustomersView.table_name) |
| 417 | + pk_name.must_equal 'id' |
| 418 | + pk_column = SSTestCustomersView.columns_hash[pk_name] |
| 419 | + pk_column.must_be :primary? |
| 420 | + end |
468 | 421 |
|
469 | | - it 'find identity column' do |
470 | | - assert StringDefaultsView.columns_hash['id'].primary |
471 | | - end |
| 422 | + it 'find default values' do |
| 423 | + assert_equal 0, SSTestCustomersView.new.balance |
| 424 | + end |
472 | 425 |
|
473 | | - it 'find default values' do |
474 | | - assert_equal 'null', StringDefaultsView.new.pretend_null, |
475 | | - StringDefaultsView.columns_hash['pretend_null'].inspect |
476 | | - end |
| 426 | + it 'respond true to table_exists?' do |
| 427 | + assert SSTestCustomersView.table_exists? |
| 428 | + end |
477 | 429 |
|
478 | | - it 'respond true to table_exists?' do |
479 | | - assert StringDefaultsView.table_exists? |
480 | | - end |
| 430 | + it 'have correct table name for all column objects' do |
| 431 | + assert SSTestCustomersView.columns.all?{ |c| c.table_name == 'sst_customers_view' }, |
| 432 | + SSTestCustomersView.columns.map(&:table_name).inspect |
| 433 | + end |
481 | 434 |
|
482 | | - it 'have correct table name for all column objects' do |
483 | | - assert StringDefaultsView.columns.all?{ |c| c.table_name == 'string_defaults_view' }, |
484 | | - StringDefaultsView.columns.map(&:table_name).inspect |
485 | | - end |
| 435 | + # With aliased column names |
486 | 436 |
|
| 437 | + it 'have matching column objects' do |
| 438 | + columns = ['id','pretend_null'] |
| 439 | + assert !StringDefaultsView.columns.blank? |
| 440 | + assert_equal columns.size, StringDefaultsView.columns.size |
| 441 | + columns.each do |colname| |
| 442 | + assert_instance_of ActiveRecord::ConnectionAdapters::SQLServerColumn, |
| 443 | + StringDefaultsView.columns_hash[colname], |
| 444 | + "Column name #{colname.inspect} was not found in these columns #{StringDefaultsView.columns.map(&:name).inspect}" |
487 | 445 | end |
| 446 | + end |
488 | 447 |
|
| 448 | + it 'find identity column' do |
| 449 | + assert StringDefaultsView.columns_hash['id'].primary |
489 | 450 | end |
490 | 451 |
|
491 | | - describe 'doing identity inserts' do |
| 452 | + it 'find default values' do |
| 453 | + assert_equal 'null', StringDefaultsView.new.pretend_null, |
| 454 | + StringDefaultsView.columns_hash['pretend_null'].inspect |
| 455 | + end |
492 | 456 |
|
493 | | - before do |
494 | | - @view_insert_sql = "INSERT INTO [customers_view] ([id],[name],[balance]) VALUES (420,'Microsoft',0)" |
495 | | - end |
| 457 | + it 'respond true to table_exists?' do |
| 458 | + assert StringDefaultsView.table_exists? |
| 459 | + end |
496 | 460 |
|
497 | | - it 'respond true/tablename to #query_requires_identity_insert?' do |
498 | | - assert_equal '[customers_view]', connection.send(:query_requires_identity_insert?,@view_insert_sql) |
499 | | - end |
| 461 | + it 'have correct table name for all column objects' do |
| 462 | + assert StringDefaultsView.columns.all?{ |c| c.table_name == 'string_defaults_view' }, |
| 463 | + StringDefaultsView.columns.map(&:table_name).inspect |
| 464 | + end |
500 | 465 |
|
501 | | - it 'be able to do an identity insert' do |
502 | | - assert_nothing_raised { connection.execute(@view_insert_sql) } |
503 | | - assert CustomersView.find(420) |
504 | | - end |
| 466 | + # Doing identity inserts |
| 467 | + |
| 468 | + let(:view_insert_sql) { "INSERT INTO [sst_customers_view] ([id],[name],[balance]) VALUES (420,'Microsoft',0)" } |
505 | 469 |
|
| 470 | + it 'respond true/tablename to #query_requires_identity_insert?' do |
| 471 | + assert_equal '[sst_customers_view]', connection.send(:query_requires_identity_insert?, view_insert_sql) |
506 | 472 | end |
507 | 473 |
|
508 | | - describe 'that have more than 4000 chars for their defintion' do |
| 474 | + it 'be able to do an identity insert' do |
| 475 | + assert_nothing_raised { connection.execute( view_insert_sql) } |
| 476 | + assert SSTestCustomersView.find(420) |
| 477 | + end |
509 | 478 |
|
510 | | - it 'cope with null returned for the defintion' do |
511 | | - assert_nothing_raised() { StringDefaultsBigView.columns } |
512 | | - end |
| 479 | + # That have more than 4000 chars for their defintion |
513 | 480 |
|
514 | | - it 'using alternate view defintion still be able to find real default' do |
515 | | - assert_equal 'null', StringDefaultsBigView.new.pretend_null, |
516 | | - StringDefaultsBigView.columns_hash['pretend_null'].inspect |
517 | | - end |
| 481 | + it 'cope with null returned for the defintion' do |
| 482 | + assert_nothing_raised() { StringDefaultsBigView.columns } |
| 483 | + end |
518 | 484 |
|
| 485 | + it 'using alternate view defintion still be able to find real default' do |
| 486 | + assert_equal 'null', StringDefaultsBigView.new.pretend_null, |
| 487 | + StringDefaultsBigView.columns_hash['pretend_null'].inspect |
519 | 488 | end |
520 | 489 |
|
521 | 490 | end |
|
0 commit comments