/
active_record_spec.rb
82 lines (63 loc) · 2.65 KB
/
active_record_spec.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true
require 'spec_helper'
$client_count = 0
describe SchemaMonkey::ActiveRecord do
When(:result) {
SchemaMonkey.register(client)
SchemaMonkey.insert
ActiveRecord::Base.establish_connection :schema_dev
ActiveRecord::Base.connection
}
[false, true].each do |class_methods|
suffix = class_methods ? " class methods" : ""
[:prepend, :include].each do |mode|
context "with #{mode} general module"+suffix do
Given(:client) { make_client('ConnectionAdapters::SchemaStatements', mode: mode, class_methods: class_methods) }
Then { expect_inserted(ActiveRecord::ConnectionAdapters::SchemaStatements, client.module, mode, class_methods) }
end
SchemaMonkey::DBMS.each do |dbm|
context "with #{mode} #{dbm} module"+suffix do
Given(:client) { make_client("ConnectionAdapters::#{dbm}::SchemaStatements", mode: mode, class_methods: class_methods) }
SchemaMonkey::DBMS.each do |loaded_dbm|
context "if using #{loaded_dbm}", loaded_dbm.to_s.downcase.to_sym => :only do
if dbm == loaded_dbm
Then { expect_inserted(ActiveRecord::ConnectionAdapters::SchemaStatements, client.module, mode, class_methods) }
else
Then { expect_not_inserted(ActiveRecord::ConnectionAdapters::SchemaStatements, client.module, class_methods) }
end
end
end
end
end
end
end
context "with invalid module" do
Given(:client) { make_client('ConnectionAdapters::NoSuchModule') }
Then { expect(result).to have_failed(SchemaMonkey::InsertionError) }
end
private
def expect_inserted(base, mod, mode, class_methods)
base = base.singleton_class if class_methods
actual = base.ancestors.select{|a| [base, mod].include? a}
expected = case mode
when :prepend then [mod, base]
when :include then [base, mod]
end
expect(actual).to eq expected
end
def expect_not_inserted(base, mod, class_methods)
base = base.singleton_class if class_methods
expect(base.ancestors).not_to include mod
end
def make_client(path, mode: nil, class_methods: nil)
name = "TestActiveRecord#{$client_count}"
$client_count += 1
Object.send :remove_const, name if Object.const_defined? name
Object.const_set name, Module.new.tap { |client|
path += "::ClassMethods" if class_methods
sub = SchemaMonkey::Module.mkpath(client, 'ActiveRecord::' + path)
client.singleton_class.send(:define_method, :module) { sub }
sub.singleton_class.send(:define_method, :included) {|base|} if mode == :include
}
end
end