filter_factory 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -2
- data/.rspec +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -4
- data/Gemfile.lock +141 -0
- data/README.md +2 -0
- data/lib/filter_factory/field.rb +1 -1
- data/lib/filter_factory/filter.rb +18 -16
- data/lib/filter_factory/mongoid/condition.rb +17 -11
- data/lib/filter_factory/mongoid/filter.rb +3 -3
- data/lib/filter_factory/version.rb +1 -1
- data/lib/filter_factory.rb +11 -11
- data/spec/factories/factories.rb +1 -3
- data/spec/filter_factory/active_record/model_spec.rb +70 -84
- data/spec/filter_factory/field_spec.rb +31 -33
- data/spec/filter_factory/filter_spec.rb +88 -88
- data/spec/filter_factory/mongoid/model_spec.rb +69 -83
- data/spec/models/ar_post.rb +1 -1
- data/spec/spec_helper.rb +18 -26
- metadata +11 -10
@@ -1,60 +1,58 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe FilterFactory::Field do
|
4
|
-
describe
|
5
|
-
it
|
3
|
+
RSpec.describe FilterFactory::Field do
|
4
|
+
describe '#initialize' do
|
5
|
+
it 'creates field with name & condition specified' do
|
6
6
|
field = described_class.new(:name, :eq)
|
7
|
-
field.
|
8
|
-
field.alias.should == :name
|
9
|
-
field.name.should == :name
|
10
|
-
field.condition.should == :eq
|
7
|
+
expect([field.alias, field.name, field.condition]).to eq([:name, :name, :eq])
|
11
8
|
end
|
12
9
|
|
13
|
-
it
|
10
|
+
it 'creates field with name, condition & options specified' do
|
14
11
|
field = described_class.new(:name, :eq, alias: :my_name)
|
15
|
-
field.
|
16
|
-
field.alias.should == :my_name
|
17
|
-
field.name.should == :name
|
18
|
-
field.condition.should == :eq
|
12
|
+
expect([field.alias, field.name, field.condition]).to eq([:my_name, :name, :eq])
|
19
13
|
end
|
20
14
|
|
21
|
-
it
|
22
|
-
expect{ described_class.new }.to raise_error(ArgumentError)
|
15
|
+
it 'raises error if name is not specified' do
|
16
|
+
expect { described_class.new }.to raise_error(ArgumentError)
|
23
17
|
end
|
24
18
|
|
25
|
-
it
|
26
|
-
expect{ described_class.new(:name) }.to raise_error(ArgumentError)
|
19
|
+
it 'raises error if condition is not specified' do
|
20
|
+
expect { described_class.new(:name) }.to raise_error(ArgumentError)
|
27
21
|
end
|
28
22
|
|
29
|
-
it
|
30
|
-
expect{ described_class.new(:name, :my_eq) }.to raise_error(ArgumentError)
|
23
|
+
it 'raises error if wrong condition specified' do
|
24
|
+
expect { described_class.new(:name, :my_eq) }.to raise_error(ArgumentError)
|
31
25
|
end
|
32
26
|
end
|
33
27
|
|
34
|
-
describe
|
35
|
-
it
|
28
|
+
describe '#==' do
|
29
|
+
it 'equals to obj if it is an instance of Field & it has equal name, condition & alias' do
|
36
30
|
f1 = described_class.new(:name, :eq)
|
37
31
|
f2 = described_class.new(:name, :eq, alias: :name)
|
38
|
-
f1
|
32
|
+
expect(f1 == f2).to eq(true)
|
39
33
|
end
|
40
34
|
|
41
|
-
it
|
35
|
+
it 'is not equal to obj if it is not an instance of Field' do
|
42
36
|
f = described_class.new(:name, :eq)
|
43
|
-
f
|
44
|
-
f.should_not == {}
|
45
|
-
f.should_not == 0
|
37
|
+
expect([f == [], f == {}, f == 0]).to eq([false, false, false])
|
46
38
|
end
|
47
39
|
|
48
|
-
it
|
49
|
-
|
40
|
+
it 'is not equal to obj if it is an instance of Field & it does not have equal name' do
|
41
|
+
f1 = described_class.new(:name, :eq)
|
42
|
+
f2 = described_class.new(:my_name, :eq)
|
43
|
+
expect(f1 == f2).to eq(false)
|
50
44
|
end
|
51
45
|
|
52
|
-
it
|
53
|
-
|
46
|
+
it 'is not equal to obj if it is an instance of Field & it does not have equal condition' do
|
47
|
+
f1 = described_class.new(:name, :eq)
|
48
|
+
f2 = described_class.new(:name, :ne)
|
49
|
+
expect(f1 == f2).to eq(false)
|
54
50
|
end
|
55
51
|
|
56
|
-
it
|
57
|
-
|
52
|
+
it 'is not equal to obj if it is an instance of Field & it does not have equal alias' do
|
53
|
+
f1 = described_class.new(:name, :eq)
|
54
|
+
f2 = described_class.new(:name, :eq, alias: :my_name)
|
55
|
+
expect(f1 == f2).to eq(false)
|
58
56
|
end
|
59
57
|
end
|
60
|
-
end
|
58
|
+
end
|
@@ -1,128 +1,128 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe FilterFactory::Filter do
|
4
|
-
|
5
|
-
|
3
|
+
RSpec.describe FilterFactory::Filter do
|
4
|
+
describe '::create' do
|
5
|
+
it 'creates filter and execute block' do
|
6
|
+
test_fields = [[:field1, :eq], [:field2, :eq], [:field3, :eq]]
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
filter = described_class.create do
|
9
|
+
test_fields.each do |arr|
|
10
|
+
field *arr
|
11
|
+
end
|
10
12
|
end
|
11
|
-
end
|
12
13
|
|
13
|
-
|
14
|
+
expect(filter.fields.map {|f| [f.name, f.condition]}).to eq(test_fields)
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
it 'defines singleton method for defined field by its name if no alias option specified' do
|
18
|
+
filter = described_class.create do
|
19
|
+
field :name, :eq
|
20
|
+
end
|
17
21
|
|
18
|
-
|
19
|
-
filter = described_class.create do
|
20
|
-
field :name, :eq
|
22
|
+
expect(filter).to respond_to(:name, :'name=')
|
21
23
|
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
+
it 'defines singleton method for defined field by its alias if alias option specified' do
|
26
|
+
filter = described_class.create do
|
27
|
+
field :name, :eq, alias: :my_name
|
28
|
+
end
|
25
29
|
|
26
|
-
|
27
|
-
filter = described_class.create do
|
28
|
-
field :name, :eq, alias: :my_name
|
30
|
+
expect(filter).to respond_to(:my_name, :'my_name=')
|
29
31
|
end
|
30
32
|
|
31
|
-
|
33
|
+
it 'raises error if duplicate field definition found' do
|
34
|
+
expect {
|
35
|
+
described_class.create do
|
36
|
+
field :name, :eq
|
37
|
+
field :surname, :regex, alias: :last_name
|
38
|
+
field :name, :eq, alias: :name
|
39
|
+
end
|
40
|
+
}.to raise_error(FilterFactory::Filter::DuplicateFieldError)
|
41
|
+
end
|
32
42
|
end
|
33
43
|
|
34
|
-
|
35
|
-
|
36
|
-
|
44
|
+
describe '#<field_name>, #<field_name>=' do
|
45
|
+
it 'gets field value' do
|
46
|
+
filter = described_class.create do
|
47
|
+
field :name, :eq
|
48
|
+
end
|
49
|
+
|
50
|
+
filter.fields.first.value = 'sample name'
|
51
|
+
expect(filter.name).to eq('sample name')
|
37
52
|
end
|
38
53
|
|
39
|
-
|
40
|
-
|
41
|
-
|
54
|
+
it 'sets field value' do
|
55
|
+
filter = described_class.create do
|
56
|
+
field :name, :eq
|
57
|
+
end
|
42
58
|
|
43
|
-
|
44
|
-
|
45
|
-
field :name, :eq
|
59
|
+
filter.name = 'sample name'
|
60
|
+
expect(filter.fields.first.value).to eq('sample name')
|
46
61
|
end
|
47
|
-
|
48
|
-
filter.name = "sample name"
|
49
|
-
filter.fields.first.value.should == "sample name"
|
50
62
|
end
|
51
63
|
|
52
|
-
|
53
|
-
|
54
|
-
|
64
|
+
describe '#attributes, #attributes=' do
|
65
|
+
it 'respond_tos attributes & attributes= methods' do
|
66
|
+
filter = described_class.create do
|
67
|
+
field :name, :eq
|
68
|
+
end
|
69
|
+
|
70
|
+
expect(filter).to respond_to(:attributes, :attributes=)
|
55
71
|
end
|
56
72
|
|
57
|
-
|
58
|
-
|
73
|
+
it 'returns valid attributes' do
|
74
|
+
filter = described_class.create do
|
75
|
+
field :name, :eq
|
76
|
+
field :surname, :regex, alias: :last_name
|
77
|
+
end
|
78
|
+
filter.name = 'test name'
|
59
79
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
field :surname, :regex, alias: :last_name
|
80
|
+
expected_attributes = HashWithIndifferentAccess.new(name: 'test name', last_name: nil)
|
81
|
+
|
82
|
+
expect(filter.attributes).to eq(expected_attributes)
|
64
83
|
end
|
65
|
-
filter.name = "test name"
|
66
84
|
|
67
|
-
|
85
|
+
it 'fills filter values from hash' do
|
86
|
+
filter = described_class.create do
|
87
|
+
field :name, :eq
|
88
|
+
field :surname, :regex, alias: :last_name
|
89
|
+
end
|
90
|
+
|
91
|
+
attributes = { name: 'my test name', last_name: 'surname here' }
|
92
|
+
filter.attributes = attributes
|
93
|
+
expect(filter.attributes).to eq(HashWithIndifferentAccess.new(attributes))
|
94
|
+
end
|
68
95
|
end
|
69
96
|
|
70
|
-
|
71
|
-
|
72
|
-
|
97
|
+
describe '#get_field' do
|
98
|
+
it 'responds to #get_field method' do
|
99
|
+
expect(subject).to respond_to(:get_field)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'returns valid field by calling #get_field' do
|
103
|
+
filter = described_class.create do
|
73
104
|
field :name, :eq
|
74
105
|
field :surname, :regex, alias: :last_name
|
75
|
-
field :name, :eq, alias: :name
|
76
106
|
end
|
77
|
-
end.to raise_error(FilterFactory::Filter::DuplicateFieldError)
|
78
|
-
end
|
79
107
|
|
80
|
-
|
81
|
-
|
82
|
-
end
|
108
|
+
field_name = filter.get_field(:name)
|
109
|
+
expect([field_name.class, field_name.condition]).to eq([FilterFactory::Field, :eq])
|
83
110
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
111
|
+
field_surname = filter.get_field(:surname)
|
112
|
+
expect([field_surname.class, field_surname.condition]).to eq([FilterFactory::Field, :regex])
|
113
|
+
|
114
|
+
expect(filter.get_field(:my_name)).to be_nil
|
88
115
|
end
|
89
|
-
filter.get_field(:name).should be_instance_of(FilterFactory::Field)
|
90
|
-
filter.get_field(:name).condition.should == :eq
|
91
|
-
filter.get_field(:surname).should be_instance_of(FilterFactory::Field)
|
92
|
-
filter.get_field(:surname).condition.should == :regex
|
93
|
-
filter.get_field(:my_name).should be_nil
|
94
116
|
end
|
95
117
|
|
96
118
|
described_class::CONDITIONS.each do |condition|
|
97
|
-
it
|
98
|
-
|
119
|
+
it 'responds to #{condition} method' do
|
120
|
+
expect(subject).to respond_to(condition)
|
99
121
|
end
|
100
122
|
|
101
|
-
it "
|
123
|
+
it "defines field with '#{condition}' condition" do
|
102
124
|
filter = described_class.create{ public_send(condition, :name) }
|
103
|
-
filter.get_field(:name).condition.
|
125
|
+
expect(filter.get_field(:name).condition).to eq(condition)
|
104
126
|
end
|
105
127
|
end
|
106
|
-
|
107
|
-
it "should fill filter values from hash" do
|
108
|
-
filter = described_class.create do
|
109
|
-
field :name, :eq
|
110
|
-
field :surname, :regex, alias: :last_name
|
111
|
-
end
|
112
|
-
|
113
|
-
attributes = {name: "my test name", last_name: "surname here"}
|
114
|
-
filter.attributes = attributes
|
115
|
-
filter.attributes.should == HashWithIndifferentAccess.new(attributes)
|
116
|
-
end
|
117
|
-
|
118
|
-
#describe "#conditions" do
|
119
|
-
# it "should return result of query method for each of the condition as a Method object" do
|
120
|
-
# filter = described_class.create do
|
121
|
-
# field :name, :eq
|
122
|
-
# end
|
123
|
-
#
|
124
|
-
# filter.name = "sample name"
|
125
|
-
# filter.conditions.first.should be_a(Method)
|
126
|
-
# end
|
127
|
-
#end
|
128
|
-
end
|
128
|
+
end
|
@@ -1,152 +1,138 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe MPost do
|
4
|
-
|
5
|
-
@posts = FactoryGirl.create_list(:mongoid_post, 10)
|
6
|
-
end
|
3
|
+
RSpec.describe MPost do
|
4
|
+
let!(:posts) { FactoryGirl.create_list(:mongoid_post, 5) }
|
7
5
|
|
8
|
-
it
|
9
|
-
described_class.
|
6
|
+
it 'responds to ::filter method' do
|
7
|
+
expect(described_class).to respond_to(:filter)
|
10
8
|
end
|
11
9
|
|
12
|
-
it
|
13
|
-
sample =
|
10
|
+
it 'executes filter methods chain' do
|
11
|
+
sample = posts.sample
|
14
12
|
|
15
13
|
filter = FilterFactory.create do
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
eq :title
|
15
|
+
eq :author, alias: :user
|
16
|
+
gte :views
|
19
17
|
end
|
20
18
|
|
21
19
|
filter.title = sample.title
|
22
20
|
filter.user = sample.author
|
23
21
|
|
24
|
-
described_class.filter(filter).to_a.
|
22
|
+
expect(described_class.filter(filter).to_a).to eq([sample])
|
25
23
|
end
|
26
24
|
|
27
|
-
it
|
28
|
-
sample =
|
25
|
+
it 'returns records with column values equal to specified value' do
|
26
|
+
sample = posts.sample
|
29
27
|
|
30
|
-
filter = FilterFactory.create
|
31
|
-
field :id, :eq
|
32
|
-
end
|
28
|
+
filter = FilterFactory.create { eq :id }
|
33
29
|
filter.id = sample.id
|
34
30
|
|
35
|
-
described_class.filter(filter).to_a.
|
31
|
+
expect(described_class.filter(filter).to_a).to eq([sample])
|
36
32
|
end
|
37
33
|
|
38
|
-
it
|
39
|
-
sample =
|
34
|
+
it 'returns records with column values not equal specified value' do
|
35
|
+
sample = posts.sample
|
40
36
|
|
41
|
-
filter = FilterFactory.create
|
42
|
-
field :id, :ne
|
43
|
-
end
|
37
|
+
filter = FilterFactory.create { ne :id }
|
44
38
|
filter.id = sample.id
|
45
39
|
|
46
|
-
|
40
|
+
expected_result = posts.reject { |p| p.id == sample.id }
|
41
|
+
expect(described_class.filter(filter).to_a.sort).to eq(expected_result)
|
47
42
|
end
|
48
43
|
|
49
|
-
it
|
50
|
-
sample =
|
44
|
+
it 'returns records with column values less than specified value' do
|
45
|
+
sample = posts.sample
|
51
46
|
|
52
|
-
filter = FilterFactory.create
|
53
|
-
field :views, :lt
|
54
|
-
end
|
47
|
+
filter = FilterFactory.create { lt :views }
|
55
48
|
filter.views = sample.views
|
56
49
|
|
57
|
-
|
50
|
+
expected_result = posts.select { |p| p.views < sample.views }.map(&:id).sort
|
51
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
58
52
|
end
|
59
53
|
|
60
|
-
it
|
61
|
-
sample =
|
54
|
+
it 'returns records with column values less than or equal to specified value' do
|
55
|
+
sample = posts.sample
|
62
56
|
|
63
|
-
filter = FilterFactory.create
|
64
|
-
field :views, :lte
|
65
|
-
end
|
57
|
+
filter = FilterFactory.create { lte :views }
|
66
58
|
filter.views = sample.views
|
67
59
|
|
68
|
-
|
60
|
+
expected_result = posts.select { |p| p.views <= sample.views }.map(&:id).sort
|
61
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
69
62
|
end
|
70
63
|
|
71
|
-
it
|
72
|
-
sample =
|
64
|
+
it 'returns records with column values greater than specified value' do
|
65
|
+
sample = posts.sample
|
73
66
|
|
74
|
-
filter = FilterFactory.create
|
75
|
-
field :views, :gt
|
76
|
-
end
|
67
|
+
filter = FilterFactory.create { gt :views }
|
77
68
|
filter.views = sample.views
|
78
69
|
|
79
|
-
|
70
|
+
expected_result = posts.select { |p| p.views > sample.views }.map(&:id).sort
|
71
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
80
72
|
end
|
81
73
|
|
82
|
-
it
|
83
|
-
sample =
|
74
|
+
it 'returns records with column values greater than or equal to specified value' do
|
75
|
+
sample = posts.sample
|
84
76
|
|
85
|
-
filter = FilterFactory.create
|
86
|
-
field :views, :gte
|
87
|
-
end
|
77
|
+
filter = FilterFactory.create { gte :views }
|
88
78
|
filter.views = sample.views
|
89
79
|
|
90
|
-
|
80
|
+
expected_result = posts.select { |p| p.views >= sample.views }.map(&:id).sort
|
81
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
91
82
|
end
|
92
83
|
|
93
|
-
it
|
94
|
-
sample =
|
95
|
-
sample.each{|r| r.update_attribute(:opts, [1, 2, 3, 4])}
|
84
|
+
it 'returns records with column values all equal to specified value' do
|
85
|
+
sample = posts.sample(2)
|
86
|
+
sample.each { |r| r.update_attribute(:opts, [1, 2, 3, 4]) }
|
96
87
|
|
97
|
-
filter = FilterFactory.create
|
98
|
-
field :opts, :all
|
99
|
-
end
|
88
|
+
filter = FilterFactory.create { all :opts }
|
100
89
|
filter.opts = [1, 2, 3, 4]
|
101
90
|
|
102
|
-
|
91
|
+
expected_result = sample.map(&:id).sort
|
92
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
103
93
|
end
|
104
94
|
|
105
|
-
it
|
106
|
-
sample =
|
95
|
+
it 'returns records with column values in specified values' do
|
96
|
+
sample = posts.sample(3)
|
107
97
|
|
108
98
|
filter = FilterFactory.create do
|
109
|
-
field :id,
|
99
|
+
field :id, :in
|
110
100
|
end
|
111
101
|
filter.id = sample.map(&:id)
|
112
102
|
|
113
|
-
|
103
|
+
expected_result = sample.map(&:id).sort
|
104
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
114
105
|
end
|
115
106
|
|
116
|
-
it
|
117
|
-
sample =
|
107
|
+
it 'returns records with column values not in specified values' do
|
108
|
+
sample = posts.sample(3)
|
118
109
|
|
119
|
-
filter = FilterFactory.create
|
120
|
-
field :id, :nin
|
121
|
-
end
|
110
|
+
filter = FilterFactory.create { nin :id }
|
122
111
|
filter.id = sample.map(&:id)
|
123
112
|
|
124
|
-
|
113
|
+
expected_result = (posts.map(&:id) - sample.map(&:id)).sort
|
114
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
125
115
|
end
|
126
116
|
|
127
|
-
it
|
128
|
-
sample =
|
129
|
-
sample.each_with_index{|r,i| r.update_attribute(:title, "my_title_#{i}")}
|
130
|
-
|
131
|
-
filter = FilterFactory.create do
|
132
|
-
field :title, :regex
|
133
|
-
end
|
117
|
+
it 'returns records with column values which match the specified regexp' do
|
118
|
+
sample = posts.sample(3)
|
119
|
+
sample.each_with_index { |r, i| r.update_attribute(:title, "my_title_#{i}") }
|
134
120
|
|
121
|
+
filter = FilterFactory.create { regex :title }
|
135
122
|
filter.title = '_title_'
|
136
123
|
|
137
|
-
|
124
|
+
expected_result = sample.map(&:id).sort
|
125
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
138
126
|
end
|
139
127
|
|
140
|
-
it
|
141
|
-
sample =
|
142
|
-
sample.each{|r| r.update_attribute(:not_exists, rand(0..25))}
|
143
|
-
|
144
|
-
filter = FilterFactory.create do
|
145
|
-
field :not_exists, :exists
|
146
|
-
end
|
128
|
+
it 'returns records with existing column values' do
|
129
|
+
sample = posts.sample(3)
|
130
|
+
sample.each { |r| r.update_attribute(:not_exists, rand(0..25)) }
|
147
131
|
|
132
|
+
filter = FilterFactory.create { exists :not_exists }
|
148
133
|
filter.not_exists = true
|
149
134
|
|
150
|
-
|
135
|
+
expected_result = sample.map(&:id).sort
|
136
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
151
137
|
end
|
152
|
-
end
|
138
|
+
end
|
data/spec/models/ar_post.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
class ARPost < ActiveRecord::Base
|
2
|
-
end
|
2
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,56 +2,48 @@ MODELS = File.join(File.dirname(__FILE__), 'models')
|
|
2
2
|
|
3
3
|
require 'rubygems'
|
4
4
|
|
5
|
+
require 'mysql2'
|
5
6
|
require 'mongoid'
|
6
7
|
require 'active_record'
|
7
|
-
require 'database_cleaner'
|
8
8
|
require 'factory_girl'
|
9
9
|
|
10
10
|
require 'filter_factory'
|
11
11
|
|
12
|
-
|
12
|
+
require 'models/ar_post'
|
13
|
+
require 'models/m_post'
|
13
14
|
|
14
15
|
Mongoid.configure do |config|
|
15
16
|
config.connect_to 'mongoid_filter_factory_test'
|
16
17
|
end
|
17
|
-
|
18
|
-
|
18
|
+
Mongoid.logger.level = Logger::ERROR
|
19
|
+
Mongo::Logger.logger.level = Logger::ERROR
|
20
|
+
|
21
|
+
# create mysql database
|
22
|
+
client = Mysql2::Client.new(host: '127.0.0.1', username: 'root', password: nil)
|
23
|
+
client.query('CREATE DATABASE IF NOT EXISTS active_record_filter_factory_test')
|
24
|
+
client.close
|
19
25
|
|
20
26
|
ActiveRecord::Base.establish_connection(
|
21
|
-
adapter:
|
22
|
-
database:
|
27
|
+
adapter: 'mysql2',
|
28
|
+
database: 'active_record_filter_factory_test'
|
23
29
|
)
|
24
|
-
|
30
|
+
|
31
|
+
# create mysql table if not exists
|
32
|
+
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS ar_posts')
|
25
33
|
ActiveRecord::Base.connection.create_table(:ar_posts) do |t|
|
26
34
|
t.string :title
|
27
35
|
t.string :author
|
28
36
|
t.integer :views
|
29
37
|
end
|
30
|
-
#ActiveRecord::Base.logger = Logger.new($stdout)
|
31
|
-
#ActiveRecord::Base.logger.level = Logger::DEBUG
|
32
38
|
|
33
39
|
FactoryGirl.definition_file_paths = [File.join(File.dirname(__FILE__), 'factories')]
|
34
40
|
FactoryGirl.find_definitions
|
35
41
|
|
36
42
|
RSpec.configure do |config|
|
37
43
|
config.mock_with :rspec
|
38
|
-
config.color_enabled = true
|
39
|
-
|
40
|
-
config.before(:suite) do
|
41
|
-
mongoid = DatabaseCleaner[:mongoid]
|
42
|
-
mongoid.strategy = :truncation
|
43
|
-
mongoid.clean_with(:truncation)
|
44
|
-
|
45
|
-
active_record = DatabaseCleaner[:active_record]
|
46
|
-
active_record.strategy = :truncation
|
47
|
-
active_record.clean_with(:truncation)
|
48
|
-
end
|
49
|
-
|
50
|
-
config.before(:each) do
|
51
|
-
DatabaseCleaner.start
|
52
|
-
end
|
53
44
|
|
54
45
|
config.after(:each) do
|
55
|
-
|
46
|
+
ARPost.delete_all
|
47
|
+
MPost.delete_all
|
56
48
|
end
|
57
|
-
end
|
49
|
+
end
|