| 1 |
require 'erb' |
|---|
| 2 |
require 'yaml' |
|---|
| 3 |
require 'csv' |
|---|
| 4 |
require 'active_support/test_case' |
|---|
| 5 |
|
|---|
| 6 |
if RUBY_VERSION < '1.9' |
|---|
| 7 |
module YAML |
|---|
| 8 |
class Omap |
|---|
| 9 |
def keys; map { |k, v| k } end |
|---|
| 10 |
def values; map { |k, v| v } end |
|---|
| 11 |
end |
|---|
| 12 |
end |
|---|
| 13 |
end |
|---|
| 14 |
|
|---|
| 15 |
if defined? ActiveRecord |
|---|
| 16 |
class FixtureClassNotFound < ActiveRecord::ActiveRecordError |
|---|
| 17 |
end |
|---|
| 18 |
else |
|---|
| 19 |
class FixtureClassNotFound < StandardError |
|---|
| 20 |
end |
|---|
| 21 |
end |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 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 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 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 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash) |
|---|
| 445 |
DEFAULT_FILTER_RE = /\.ya?ml$/ |
|---|
| 446 |
|
|---|
| 447 |
@@all_cached_fixtures = {} |
|---|
| 448 |
|
|---|
| 449 |
def self.reset_cache(connection = nil) |
|---|
| 450 |
connection ||= ActiveRecord::Base.connection |
|---|
| 451 |
@@all_cached_fixtures[connection.object_id] = {} |
|---|
| 452 |
end |
|---|
| 453 |
|
|---|
| 454 |
def self.cache_for_connection(connection) |
|---|
| 455 |
@@all_cached_fixtures[connection.object_id] ||= {} |
|---|
| 456 |
@@all_cached_fixtures[connection.object_id] |
|---|
| 457 |
end |
|---|
| 458 |
|
|---|
| 459 |
def self.fixture_is_cached?(connection, table_name) |
|---|
| 460 |
cache_for_connection(connection)[table_name] |
|---|
| 461 |
end |
|---|
| 462 |
|
|---|
| 463 |
def self.cached_fixtures(connection, keys_to_fetch = nil) |
|---|
| 464 |
if keys_to_fetch |
|---|
| 465 |
fixtures = cache_for_connection(connection).values_at(*keys_to_fetch) |
|---|
| 466 |
else |
|---|
| 467 |
fixtures = cache_for_connection(connection).values |
|---|
| 468 |
end |
|---|
| 469 |
fixtures.size > 1 ? fixtures : fixtures.first |
|---|
| 470 |
end |
|---|
| 471 |
|
|---|
| 472 |
def self.cache_fixtures(connection, fixtures) |
|---|
| 473 |
cache_for_connection(connection).update(fixtures.index_by { |f| f.table_name }) |
|---|
| 474 |
end |
|---|
| 475 |
|
|---|
| 476 |
def self.instantiate_fixtures(object, table_name, fixtures, load_instances = true) |
|---|
| 477 |
object.instance_variable_set "@#{table_name.to_s.gsub('.','_')}", fixtures |
|---|
| 478 |
if load_instances |
|---|
| 479 |
ActiveRecord::Base.silence do |
|---|
| 480 |
fixtures.each do |name, fixture| |
|---|
| 481 |
begin |
|---|
| 482 |
object.instance_variable_set "@#{name}", fixture.find |
|---|
| 483 |
rescue FixtureClassNotFound |
|---|
| 484 |
nil |
|---|
| 485 |
end |
|---|
| 486 |
end |
|---|
| 487 |
end |
|---|
| 488 |
end |
|---|
| 489 |
end |
|---|
| 490 |
|
|---|
| 491 |
def self.instantiate_all_loaded_fixtures(object, load_instances = true) |
|---|
| 492 |
all_loaded_fixtures.each do |table_name, fixtures| |
|---|
| 493 |
Fixtures.instantiate_fixtures(object, table_name, fixtures, load_instances) |
|---|
| 494 |
end |
|---|
| 495 |
end |
|---|
| 496 |
|
|---|
| 497 |
cattr_accessor :all_loaded_fixtures |
|---|
| 498 |
self.all_loaded_fixtures = {} |
|---|
| 499 |
|
|---|
| 500 |
def self.create_fixtures(fixtures_directory, table_names, class_names = {}) |
|---|
| 501 |
table_names = [table_names].flatten.map { |n| n.to_s } |
|---|
| 502 |
connection = block_given? ? yield : ActiveRecord::Base.connection |
|---|
| 503 |
|
|---|
| 504 |
table_names_to_fetch = table_names.reject { |table_name| fixture_is_cached?(connection, table_name) } |
|---|
| 505 |
|
|---|
| 506 |
unless table_names_to_fetch.empty? |
|---|
| 507 |
ActiveRecord::Base.silence do |
|---|
| 508 |
connection.disable_referential_integrity do |
|---|
| 509 |
fixtures_map = {} |
|---|
| 510 |
|
|---|
| 511 |
fixtures = table_names_to_fetch.map do |table_name| |
|---|
| 512 |
fixtures_map[table_name] = Fixtures.new(connection, File.split(table_name.to_s).last, class_names[table_name.to_sym], File.join(fixtures_directory, table_name.to_s)) |
|---|
| 513 |
end |
|---|
| 514 |
|
|---|
| 515 |
all_loaded_fixtures.update(fixtures_map) |
|---|
| 516 |
|
|---|
| 517 |
connection.transaction(Thread.current['open_transactions'].to_i == 0) do |
|---|
| 518 |
fixtures.reverse.each { |fixture| fixture.delete_existing_fixtures } |
|---|
| 519 |
fixtures.each { |fixture| fixture.insert_fixtures } |
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
if connection.respond_to?(:reset_pk_sequence!) |
|---|
| 523 |
table_names.each do |table_name| |
|---|
| 524 |
connection.reset_pk_sequence!(table_name) |
|---|
| 525 |
end |
|---|
| 526 |
end |
|---|
| 527 |
end |
|---|
| 528 |
|
|---|
| 529 |
cache_fixtures(connection, fixtures) |
|---|
| 530 |
end |
|---|
| 531 |
end |
|---|
| 532 |
end |
|---|
| 533 |
cached_fixtures(connection, table_names) |
|---|
| 534 |
end |
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
def self.identify(label) |
|---|
| 540 |
label.to_s.hash.abs |
|---|
| 541 |
end |
|---|
| 542 |
|
|---|
| 543 |
attr_reader :table_name |
|---|
| 544 |
|
|---|
| 545 |
def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE) |
|---|
| 546 |
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter |
|---|
| 547 |
@class_name = class_name || |
|---|
| 548 |
(ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize) |
|---|
| 549 |
@table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix |
|---|
| 550 |
@table_name = class_name.table_name if class_name.respond_to?(:table_name) |
|---|
| 551 |
@connection = class_name.connection if class_name.respond_to?(:connection) |
|---|
| 552 |
read_fixture_files |
|---|
| 553 |
end |
|---|
| 554 |
|
|---|
| 555 |
def delete_existing_fixtures |
|---|
| 556 |
@connection.delete "DELETE FROM #{@connection.quote_table_name(table_name)}", 'Fixture Delete' |
|---|
| 557 |
end |
|---|
| 558 |
|
|---|
| 559 |
def insert_fixtures |
|---|
| 560 |
now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now |
|---|
| 561 |
now = now.to_s(:db) |
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
if is_a?(Hash) |
|---|
| 565 |
delete('DEFAULTS') |
|---|
| 566 |
else |
|---|
| 567 |
delete(assoc('DEFAULTS')) |
|---|
| 568 |
end |
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
habtm_fixtures = Hash.new do |h, habtm| |
|---|
| 572 |
h[habtm] = HabtmFixtures.new(@connection, habtm.options[:join_table], nil, nil) |
|---|
| 573 |
end |
|---|
| 574 |
|
|---|
| 575 |
each do |label, fixture| |
|---|
| 576 |
row = fixture.to_hash |
|---|
| 577 |
|
|---|
| 578 |
if model_class && model_class < ActiveRecord::Base |
|---|
| 579 |
|
|---|
| 580 |
if model_class.record_timestamps |
|---|
| 581 |
timestamp_column_names.each do |name| |
|---|
| 582 |
row[name] = now unless row.key?(name) |
|---|
| 583 |
end |
|---|
| 584 |
end |
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
row.each do |key, value| |
|---|
| 588 |
row[key] = label if value == "$LABEL" |
|---|
| 589 |
end |
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
if has_primary_key_column? && !row.include?(primary_key_name) |
|---|
| 593 |
row[primary_key_name] = Fixtures.identify(label) |
|---|
| 594 |
end |
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
reflection_class = |
|---|
| 598 |
if row.include?(inheritance_column_name) |
|---|
| 599 |
row[inheritance_column_name].constantize rescue model_class |
|---|
| 600 |
else |
|---|
| 601 |
model_class |
|---|
| 602 |
end |
|---|
| 603 |
|
|---|
| 604 |
reflection_class.reflect_on_all_associations.each do |association| |
|---|
| 605 |
case association.macro |
|---|
| 606 |
when :belongs_to |
|---|
| 607 |
|
|---|
| 608 |
fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s |
|---|
| 609 |
|
|---|
| 610 |
if association.name.to_s != fk_name && value = row.delete(association.name.to_s) |
|---|
| 611 |
if association.options[:polymorphic] |
|---|
| 612 |
if value.sub!(/\s*\(([^\)]*)\)\s*$/, "") |
|---|
| 613 |
target_type = $1 |
|---|
| 614 |
target_type_name = (association.options[:foreign_type] || "#{association.name}_type").to_s |
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
row[target_type_name] = target_type |
|---|
| 618 |
end |
|---|
| 619 |
end |
|---|
| 620 |
|
|---|
| 621 |
row[fk_name] = Fixtures.identify(value) |
|---|
| 622 |
end |
|---|
| 623 |
when :has_and_belongs_to_many |
|---|
| 624 |
if (targets = row.delete(association.name.to_s)) |
|---|
| 625 |
targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/) |
|---|
| 626 |
join_fixtures = habtm_fixtures[association] |
|---|
| 627 |
|
|---|
| 628 |
targets.each do |target| |
|---|
| 629 |
join_fixtures["#{label}_#{target}"] = Fixture.new( |
|---|
| 630 |
{ association.primary_key_name => row[primary_key_name], |
|---|
| 631 |
association.association_foreign_key => Fixtures.identify(target) }, nil) |
|---|
| 632 |
end |
|---|
| 633 |
end |
|---|
| 634 |
end |
|---|
| 635 |
end |
|---|
| 636 |
end |
|---|
| 637 |
|
|---|
| 638 |
@connection.insert_fixture(fixture, @table_name) |
|---|
| 639 |
end |
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
habtm_fixtures.values.each do |fixture| |
|---|
| 643 |
fixture.delete_existing_fixtures |
|---|
| 644 |
fixture.insert_fixtures |
|---|
| 645 |
end |
|---|
| 646 |
end |
|---|
| 647 |
|
|---|
| 648 |
private |
|---|
| 649 |
class HabtmFixtures < ::Fixtures |
|---|
| 650 |
def read_fixture_files; end |
|---|
| 651 |
end |
|---|
| 652 |
|
|---|
| 653 |
def model_class |
|---|
| 654 |
unless defined?(@model_class) |
|---|
| 655 |
@model_class = |
|---|
| 656 |
if @class_name.nil? || @class_name.is_a?(Class) |
|---|
| 657 |
@class_name |
|---|
| 658 |
else |
|---|
| 659 |
@class_name.constantize rescue nil |
|---|
| 660 |
end |
|---|
| 661 |
end |
|---|
| 662 |
|
|---|
| 663 |
@model_class |
|---|
| 664 |
end |
|---|
| 665 |
|
|---|
| 666 |
def primary_key_name |
|---|
| 667 |
@primary_key_name ||= model_class && model_class.primary_key |
|---|
| 668 |
end |
|---|
| 669 |
|
|---|
| 670 |
def has_primary_key_column? |
|---|
| 671 |
@has_primary_key_column ||= model_class && primary_key_name && |
|---|
| 672 |
model_class.columns.find { |c| c.name == primary_key_name } |
|---|
| 673 |
end |
|---|
| 674 |
|
|---|
| 675 |
def timestamp_column_names |
|---|
| 676 |
@timestamp_column_names ||= %w(created_at created_on updated_at updated_on).select do |name| |
|---|
| 677 |
column_names.include?(name) |
|---|
| 678 |
end |
|---|
| 679 |
end |
|---|
| 680 |
|
|---|
| 681 |
def inheritance_column_name |
|---|
| 682 |
@inheritance_column_name ||= model_class && model_class.inheritance_column |
|---|
| 683 |
end |
|---|
| 684 |
|
|---|
| 685 |
def column_names |
|---|
| 686 |
@column_names ||= @connection.columns(@table_name).collect(&:name) |
|---|
| 687 |
end |
|---|
| 688 |
|
|---|
| 689 |
def read_fixture_files |
|---|
| 690 |
if File.file?(yaml_file_path) |
|---|
| 691 |
read_yaml_fixture_files |
|---|
| 692 |
elsif File.file?(csv_file_path) |
|---|
| 693 |
read_csv_fixture_files |
|---|
| 694 |
end |
|---|
| 695 |
end |
|---|
| 696 |
|
|---|
| 697 |
def read_yaml_fixture_files |
|---|
| 698 |
yaml_string = "" |
|---|
| 699 |
Dir["#{@fixture_path}/**/*.yml"].select { |f| test(?f, f) }.each do |subfixture_path| |
|---|
| 700 |
yaml_string << IO.read(subfixture_path) |
|---|
| 701 |
end |
|---|
| 702 |
yaml_string << IO.read(yaml_file_path) |
|---|
| 703 |
|
|---|
| 704 |
if yaml = parse_yaml_string(yaml_string) |
|---|
| 705 |
|
|---|
| 706 |
yaml_value = |
|---|
| 707 |
if yaml.respond_to?(:type_id) && yaml.respond_to?(:value) |
|---|
| 708 |
yaml.value |
|---|
| 709 |
else |
|---|
| 710 |
[yaml] |
|---|
| 711 |
end |
|---|
| 712 |
|
|---|
| 713 |
yaml_value.each do |fixture| |
|---|
| 714 |
raise Fixture::FormatError, "Bad data for #{@class_name} fixture named #{fixture}" unless fixture.respond_to?(:each) |
|---|
| 715 |
fixture.each do |name, data| |
|---|
| 716 |
unless data |
|---|
| 717 |
raise Fixture::FormatError, "Bad data for #{@class_name} fixture named #{name} (nil)" |
|---|
| 718 |
end |
|---|
| 719 |
|
|---|
| 720 |
self[name] = Fixture.new(data, model_class) |
|---|
| 721 |
end |
|---|
| 722 |
end |
|---|
| 723 |
end |
|---|
| 724 |
end |
|---|
| 725 |
|
|---|
| 726 |
def read_csv_fixture_files |
|---|
| 727 |
reader = CSV.parse(erb_render(IO.read(csv_file_path))) |
|---|
| 728 |
header = reader.shift |
|---|
| 729 |
i = 0 |
|---|
| 730 |
reader.each do |row| |
|---|
| 731 |
data = {} |
|---|
| 732 |
row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell.to_s.strip } |
|---|
| 733 |
self["#{Inflector::underscore(@class_name)}_#{i+=1}"] = Fixture.new(data, model_class) |
|---|
| 734 |
end |
|---|
| 735 |
end |
|---|
| 736 |
|
|---|
| 737 |
def yaml_file_path |
|---|
| 738 |
"#{@fixture_path}.yml" |
|---|
| 739 |
end |
|---|
| 740 |
|
|---|
| 741 |
def csv_file_path |
|---|
| 742 |
@fixture_path + ".csv" |
|---|
| 743 |
end |
|---|
| 744 |
|
|---|
| 745 |
def yaml_fixtures_key(path) |
|---|
| 746 |
File.basename(@fixture_path).split(".").first |
|---|
| 747 |
end |
|---|
| 748 |
|
|---|
| 749 |
def parse_yaml_string(fixture_content) |
|---|
| 750 |
YAML::load(erb_render(fixture_content)) |
|---|
| 751 |
rescue => error |
|---|
| 752 |
raise Fixture::FormatError, "a YAML error occurred parsing #{yaml_file_path}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}" |
|---|
| 753 |
end |
|---|
| 754 |
|
|---|
| 755 |
def erb_render(fixture_content) |
|---|
| 756 |
ERB.new(fixture_content).result |
|---|
| 757 |
end |
|---|
| 758 |
end |
|---|
| 759 |
|
|---|
| 760 |
class Fixture |
|---|
| 761 |
include Enumerable |
|---|
| 762 |
|
|---|
| 763 |
class FixtureError < StandardError |
|---|
| 764 |
end |
|---|
| 765 |
|
|---|
| 766 |
class FormatError < FixtureError |
|---|
| 767 |
end |
|---|
| 768 |
|
|---|
| 769 |
attr_reader :model_class |
|---|
| 770 |
|
|---|
| 771 |
def initialize(fixture, model_class) |
|---|
| 772 |
@fixture = fixture |
|---|
| 773 |
@model_class = model_class.is_a?(Class) ? model_class : model_class.constantize rescue nil |
|---|
| 774 |
end |
|---|
| 775 |
|
|---|
| 776 |
def class_name |
|---|
| 777 |
@model_class.name if @model_class |
|---|
| 778 |
end |
|---|
| 779 |
|
|---|
| 780 |
def each |
|---|
| 781 |
@fixture.each { |item| yield item } |
|---|
| 782 |
end |
|---|
| 783 |
|
|---|
| 784 |
def [](key) |
|---|
| 785 |
@fixture[key] |
|---|
| 786 |
end |
|---|
| 787 |
|
|---|
| 788 |
def to_hash |
|---|
| 789 |
@fixture |
|---|
| 790 |
end |
|---|
| 791 |
|
|---|
| 792 |
def key_list |
|---|
| 793 |
columns = @fixture.keys.collect{ |column_name| ActiveRecord::Base.connection.quote_column_name(column_name) } |
|---|
| 794 |
columns.join(", ") |
|---|
| 795 |
end |
|---|
| 796 |
|
|---|
| 797 |
def value_list |
|---|
| 798 |
list = @fixture.inject([]) do |fixtures, (key, value)| |
|---|
| 799 |
col = model_class.columns_hash[key] if model_class.respond_to?(:ancestors) && model_class.ancestors.include?(ActiveRecord::Base) |
|---|
| 800 |
fixtures << ActiveRecord::Base.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r") |
|---|
| 801 |
end |
|---|
| 802 |
list * ', ' |
|---|
| 803 |
end |
|---|
| 804 |
|
|---|
| 805 |
def find |
|---|
| 806 |
if model_class |
|---|
| 807 |
model_class.find(self[model_class.primary_key]) |
|---|
| 808 |
else |
|---|
| 809 |
raise FixtureClassNotFound, "No class attached to find." |
|---|
| 810 |
end |
|---|
| 811 |
end |
|---|
| 812 |
end |
|---|
| 813 |
|
|---|
| 814 |
module Test |
|---|
| 815 |
module Unit |
|---|
| 816 |
class TestCase |
|---|
| 817 |
setup :setup_fixtures |
|---|
| 818 |
teardown :teardown_fixtures |
|---|
| 819 |
|
|---|
| 820 |
superclass_delegating_accessor :fixture_path |
|---|
| 821 |
superclass_delegating_accessor :fixture_table_names |
|---|
| 822 |
superclass_delegating_accessor :fixture_class_names |
|---|
| 823 |
superclass_delegating_accessor :use_transactional_fixtures |
|---|
| 824 |
superclass_delegating_accessor :use_instantiated_fixtures |
|---|
| 825 |
superclass_delegating_accessor :pre_loaded_fixtures |
|---|
| 826 |
|
|---|
| 827 |
self.fixture_table_names = [] |
|---|
| 828 |
self.use_transactional_fixtures = false |
|---|
| 829 |
self.use_instantiated_fixtures = true |
|---|
| 830 |
self.pre_loaded_fixtures = false |
|---|
| 831 |
|
|---|
| 832 |
@@already_loaded_fixtures = {} |
|---|
| 833 |
self.fixture_class_names = {} |
|---|
| 834 |
|
|---|
| 835 |
class << self |
|---|
| 836 |
def set_fixture_class(class_names = {}) |
|---|
| 837 |
self.fixture_class_names = self.fixture_class_names.merge(class_names) |
|---|
| 838 |
end |
|---|
| 839 |
|
|---|
| 840 |
def fixtures(*table_names) |
|---|
| 841 |
if table_names.first == :all |
|---|
| 842 |
table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"] |
|---|
| 843 |
table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') } |
|---|
| 844 |
else |
|---|
| 845 |
table_names = table_names.flatten.map { |n| n.to_s } |
|---|
| 846 |
end |
|---|
| 847 |
|
|---|
| 848 |
self.fixture_table_names |= table_names |
|---|
| 849 |
require_fixture_classes(table_names) |
|---|
| 850 |
setup_fixture_accessors(table_names) |
|---|
| 851 |
end |
|---|
| 852 |
|
|---|
| 853 |
def try_to_load_dependency(file_name) |
|---|
| 854 |
require_dependency file_name |
|---|
| 855 |
rescue LoadError => e |
|---|
| 856 |
|
|---|
| 857 |
|
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
if ActiveRecord::Base.logger |
|---|
| 861 |
ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}") |
|---|
| 862 |
end |
|---|
| 863 |
end |
|---|
| 864 |
|
|---|
| 865 |
def require_fixture_classes(table_names = nil) |
|---|
| 866 |
(table_names || fixture_table_names).each do |table_name| |
|---|
| 867 |
file_name = table_name.to_s |
|---|
| 868 |
file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names |
|---|
| 869 |
try_to_load_dependency(file_name) |
|---|
| 870 |
end |
|---|
| 871 |
end |
|---|
| 872 |
|
|---|
| 873 |
def setup_fixture_accessors(table_names = nil) |
|---|
| 874 |
table_names = [table_names] if table_names && !table_names.respond_to?(:each) |
|---|
| 875 |
(table_names || fixture_table_names).each do |table_name| |
|---|
| 876 |
table_name = table_name.to_s.tr('.', '_') |
|---|
| 877 |
|
|---|
| 878 |
define_method(table_name) do |*fixtures| |
|---|
| 879 |
force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload |
|---|
| 880 |
|
|---|
| 881 |
@fixture_cache[table_name] ||= {} |
|---|
| 882 |
|
|---|
| 883 |
instances = fixtures.map do |fixture| |
|---|
| 884 |
@fixture_cache[table_name].delete(fixture) if force_reload |
|---|
| 885 |
|
|---|
| 886 |
if @loaded_fixtures[table_name][fixture.to_s] |
|---|
| 887 |
@fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find |
|---|
| 888 |
else |
|---|
| 889 |
raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" |
|---|
| 890 |
end |
|---|
| 891 |
end |
|---|
| 892 |
|
|---|
| 893 |
instances.size == 1 ? instances.first : instances |
|---|
| 894 |
end |
|---|
| 895 |
end |
|---|
| 896 |
end |
|---|
| 897 |
|
|---|
| 898 |
def uses_transaction(*methods) |
|---|
| 899 |
@uses_transaction = [] unless defined?(@uses_transaction) |
|---|
| 900 |
@uses_transaction.concat methods.map(&:to_s) |
|---|
| 901 |
end |
|---|
| 902 |
|
|---|
| 903 |
def uses_transaction?(method) |
|---|
| 904 |
@uses_transaction = [] unless defined?(@uses_transaction) |
|---|
| 905 |
@uses_transaction.include?(method.to_s) |
|---|
| 906 |
end |
|---|
| 907 |
end |
|---|
| 908 |
|
|---|
| 909 |
def use_transactional_fixtures? |
|---|
| 910 |
use_transactional_fixtures && |
|---|
| 911 |
!self.class.uses_transaction?(method_name) |
|---|
| 912 |
end |
|---|
| 913 |
|
|---|
| 914 |
def setup_fixtures |
|---|
| 915 |
return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? |
|---|
| 916 |
|
|---|
| 917 |
if pre_loaded_fixtures && !use_transactional_fixtures |
|---|
| 918 |
raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' |
|---|
| 919 |
end |
|---|
| 920 |
|
|---|
| 921 |
@fixture_cache = {} |
|---|
| 922 |
|
|---|
| 923 |
|
|---|
| 924 |
if use_transactional_fixtures? |
|---|
| 925 |
if @@already_loaded_fixtures[self.class] |
|---|
| 926 |
@loaded_fixtures = @@already_loaded_fixtures[self.class] |
|---|
| 927 |
else |
|---|
| 928 |
load_fixtures |
|---|
| 929 |
@@already_loaded_fixtures[self.class] = @loaded_fixtures |
|---|
| 930 |
end |
|---|
| 931 |
ActiveRecord::Base.send :increment_open_transactions |
|---|
| 932 |
ActiveRecord::Base.connection.begin_db_transaction |
|---|
| 933 |
|
|---|
| 934 |
else |
|---|
| 935 |
Fixtures.reset_cache |
|---|
| 936 |
@@already_loaded_fixtures[self.class] = nil |
|---|
| 937 |
load_fixtures |
|---|
| 938 |
end |
|---|
| 939 |
|
|---|
| 940 |
|
|---|
| 941 |
instantiate_fixtures if use_instantiated_fixtures |
|---|
| 942 |
end |
|---|
| 943 |
|
|---|
| 944 |
def teardown_fixtures |
|---|
| 945 |
return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? |
|---|
| 946 |
|
|---|
| 947 |
unless use_transactional_fixtures? |
|---|
| 948 |
Fixtures.reset_cache |
|---|
| 949 |
end |
|---|
| 950 |
|
|---|
| 951 |
|
|---|
| 952 |
if use_transactional_fixtures? && Thread.current['open_transactions'] != 0 |
|---|
| 953 |
ActiveRecord::Base.connection.rollback_db_transaction |
|---|
| 954 |
Thread.current['open_transactions'] = 0 |
|---|
| 955 |
end |
|---|
| 956 |
ActiveRecord::Base.verify_active_connections! |
|---|
| 957 |
end |
|---|
| 958 |
|
|---|
| 959 |
private |
|---|
| 960 |
def load_fixtures |
|---|
| 961 |
@loaded_fixtures = {} |
|---|
| 962 |
fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names) |
|---|
| 963 |
unless fixtures.nil? |
|---|
| 964 |
if fixtures.instance_of?(Fixtures) |
|---|
| 965 |
@loaded_fixtures[fixtures.table_name] = fixtures |
|---|
| 966 |
else |
|---|
| 967 |
fixtures.each { |f| @loaded_fixtures[f.table_name] = f } |
|---|
| 968 |
end |
|---|
| 969 |
end |
|---|
| 970 |
end |
|---|
| 971 |
|
|---|
| 972 |
|
|---|
| 973 |
@@required_fixture_classes = false |
|---|
| 974 |
|
|---|
| 975 |
def instantiate_fixtures |
|---|
| 976 |
if pre_loaded_fixtures |
|---|
| 977 |
raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty? |
|---|
| 978 |
unless @@required_fixture_classes |
|---|
| 979 |
self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys |
|---|
| 980 |
@@required_fixture_classes = true |
|---|
| 981 |
end |
|---|
| 982 |
Fixtures.instantiate_all_loaded_fixtures(self, load_instances?) |
|---|
| 983 |
else |
|---|
| 984 |
raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil? |
|---|
| 985 |
@loaded_fixtures.each do |table_name, fixtures| |
|---|
| 986 |
Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?) |
|---|
| 987 |
end |
|---|
| 988 |
end |
|---|
| 989 |
end |
|---|
| 990 |
|
|---|
| 991 |
def load_instances? |
|---|
| 992 |
use_instantiated_fixtures != :no_instances |
|---|
| 993 |
end |
|---|
| 994 |
end |
|---|
| 995 |
end |
|---|
| 996 |
end |
|---|