-
Notifications
You must be signed in to change notification settings - Fork 19
/
spec_helper.rb
63 lines (52 loc) · 1.57 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
require "bundler/setup"
require "pry"
require "rspec/its"
require "timescaledb"
require 'timescaledb/toolkit'
require "dotenv"
require "database_cleaner/active_record"
require "active_support/testing/time_helpers"
require_relative "support/active_record/models"
require_relative "support/active_record/schema"
Dotenv.load! if File.exist?(".env")
ActiveSupport.on_load(:active_record_postgresqladapter) do
self.datetime_type = :timestamptz
end
ActiveRecord::Base.establish_connection(ENV['PG_URI_TEST'])
Timescaledb.establish_connection(ENV['PG_URI_TEST'])
def destroy_all_chunks_for!(klass)
sql = <<-SQL
SELECT drop_chunks('#{klass.table_name}', '#{1.week.from_now}'::date)
SQL
ActiveRecord::Base.connection.execute(sql)
end
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.before(:suite) do
Time.zone = 'UTC'
end
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.before(:each) do |example|
DatabaseCleaner.strategy = example.metadata.fetch(:database_cleaner_strategy, :transaction)
DatabaseCleaner.start
end
config.after(:each) do
retries = 3
begin
DatabaseCleaner.clean
rescue ActiveRecord::StatementInvalid => e
if e.message =~ /deadlock detected/ && (retries -= 1) > 0
sleep 0.1
retry
else
raise
end
end
end
config.include ActiveSupport::Testing::TimeHelpers
end