Module: RealDataTests::RSpecHelper

Defined in:
lib/real_data_tests/rspec_helper.rb

Defined Under Namespace

Classes: SqlBlock

Instance Method Summary collapse

Instance Method Details

#load_real_test_data(name) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/real_data_tests/rspec_helper.rb', line 36

def load_real_test_data(name)
  dump_path = File.join(RealDataTests.configuration.dump_path, "#{name}.sql")
  raise Error, "Test data file not found: #{dump_path}" unless File.exist?(dump_path)
  ActiveRecord::Base.transaction do
    # Disable foreign key checks
    ActiveRecord::Base.connection.execute('SET session_replication_role = replica;')
    begin
      # Load the SQL dump quietly
      result = system("psql #{connection_options} -q < #{dump_path}")
      raise Error, "Failed to load test data: #{dump_path}" unless result
    ensure
      # Re-enable foreign key checks
      ActiveRecord::Base.connection.execute('SET session_replication_role = DEFAULT;')
    end
  end
end

#load_real_test_data_native(name) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/real_data_tests/rspec_helper.rb', line 53

def load_real_test_data_native(name)
  dump_path = File.join(RealDataTests.configuration.dump_path, "#{name}.sql")
  raise Error, "Test data file not found: #{dump_path}" unless File.exist?(dump_path)

  sql_content = File.read(dump_path)
  blocks = parse_sql_blocks(sql_content)

  ActiveRecord::Base.transaction do
    connection = ActiveRecord::Base.connection

    # Disable foreign key checks
    connection.execute('SET session_replication_role = replica;')

    begin
      blocks.each_with_index do |block, index|
        execute_block(block, index + 1, blocks.length)
      end
    ensure
      connection.execute('SET session_replication_role = DEFAULT;')
    end
  end
end