filter_factory 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,60 +1,58 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
- describe FilterFactory::Field do
4
- describe "#initialize" do
5
- it "should create field with name & condition specified" do
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.should be_instance_of(described_class)
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 "should create field with name, condition & options specified" do
10
+ it 'creates field with name, condition & options specified' do
14
11
  field = described_class.new(:name, :eq, alias: :my_name)
15
- field.should be_instance_of(described_class)
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 "should raise error if name is not specified" do
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 "should raise error if condition is not specified" do
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 "should raise error if wrong condition specified" do
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 "#==" do
35
- it "should be equal to obj if it is an instance of Field & it has equal name, condition & alias" do
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.should == f2
32
+ expect(f1 == f2).to eq(true)
39
33
  end
40
34
 
41
- it "should not be equal to obj if it is not an instance of Field" do
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.should_not == []
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 "should not be equal to obj if it is an instance of Field & it does not have equal name" do
49
- described_class.new(:name, :eq).should_not == described_class.new(:my_name, :eq)
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 "should not be equal to obj if it is an instance of Field & it does not have equal condition" do
53
- described_class.new(:name, :eq).should_not == described_class.new(:name, :ne)
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 "should not be equal to obj if it is an instance of Field & it does not have equal alias" do
57
- described_class.new(:name, :eq).should_not == described_class.new(:name, :eq, alias: :my_name)
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 "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
- describe FilterFactory::Filter do
4
- it "should create filter and execute block" do
5
- test_fields = [[:field1, :eq], [:field2, :eq], [:field3, :eq]]
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
- filter = described_class.create do
8
- test_fields.each do |arr|
9
- field *arr
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
- filter.should be_a(described_class)
14
+ expect(filter.fields.map {|f| [f.name, f.condition]}).to eq(test_fields)
15
+ end
14
16
 
15
- filter.fields.map{|f| [f.name, f.condition]}.should == test_fields
16
- end
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
- it "should define singleton method for defined field by its name if no alias option specified" do
19
- filter = described_class.create do
20
- field :name, :eq
22
+ expect(filter).to respond_to(:name, :'name=')
21
23
  end
22
24
 
23
- filter.should respond_to(:name, :'name=')
24
- end
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
- it "should define singleton method for defined field by its alias if alias option specified" do
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
- filter.should respond_to(:my_name, :'my_name=')
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
- it "should get field value" do
35
- filter = described_class.create do
36
- field :name, :eq
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
- filter.fields.first.value = "sample name"
40
- filter.name.should == "sample name"
41
- end
54
+ it 'sets field value' do
55
+ filter = described_class.create do
56
+ field :name, :eq
57
+ end
42
58
 
43
- it "should set field value" do
44
- filter = described_class.create do
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
- it "should respond_to attributes & attributes= methods" do
53
- filter = described_class.create do
54
- field :name, :eq
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
- filter.should respond_to(:attributes, :attributes=)
58
- end
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
- it "should return valid attributes" do
61
- filter = described_class.create do
62
- field :name, :eq
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
- filter.attributes.should == HashWithIndifferentAccess.new({name: "test name", last_name: nil})
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
- it "should raise error if duplicate field definition found" do
71
- expect do
72
- described_class.create do
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
- it "should respond to #get_field method" do
81
- should respond_to(:get_field)
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
- it "should return valid field by calling #get_field" do
85
- filter = described_class.create do
86
- field :name, :eq
87
- field :surname, :regex, alias: :last_name
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 "should respond to #{condition} method" do
98
- should respond_to(condition)
119
+ it 'responds to #{condition} method' do
120
+ expect(subject).to respond_to(condition)
99
121
  end
100
122
 
101
- it "should define field with '#{condition}' condition" do
123
+ it "defines field with '#{condition}' condition" do
102
124
  filter = described_class.create{ public_send(condition, :name) }
103
- filter.get_field(:name).condition.should == 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 "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
- describe MPost do
4
- before(:each) do
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 "should respond to ::filter method" do
9
- described_class.should respond_to(:filter)
6
+ it 'responds to ::filter method' do
7
+ expect(described_class).to respond_to(:filter)
10
8
  end
11
9
 
12
- it "should execute filter methods chain" do
13
- sample = @posts.sample
10
+ it 'executes filter methods chain' do
11
+ sample = posts.sample
14
12
 
15
13
  filter = FilterFactory.create do
16
- field :title, :eq
17
- field :author, :eq, alias: :user
18
- field :views, :gte
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.should == [sample]
22
+ expect(described_class.filter(filter).to_a).to eq([sample])
25
23
  end
26
24
 
27
- it "should return records with column' values equal to specified value" do
28
- sample = @posts.sample
25
+ it 'returns records with column values equal to specified value' do
26
+ sample = posts.sample
29
27
 
30
- filter = FilterFactory.create do
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.should == [sample]
31
+ expect(described_class.filter(filter).to_a).to eq([sample])
36
32
  end
37
33
 
38
- it "should return records with column' values not equal specified value" do
39
- sample = @posts.sample
34
+ it 'returns records with column values not equal specified value' do
35
+ sample = posts.sample
40
36
 
41
- filter = FilterFactory.create do
42
- field :id, :ne
43
- end
37
+ filter = FilterFactory.create { ne :id }
44
38
  filter.id = sample.id
45
39
 
46
- described_class.filter(filter).to_a.sort.should == @posts.reject{|p| p.id == sample.id}
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 "should return records with column' values less than specified value" do
50
- sample = @posts.sample
44
+ it 'returns records with column values less than specified value' do
45
+ sample = posts.sample
51
46
 
52
- filter = FilterFactory.create do
53
- field :views, :lt
54
- end
47
+ filter = FilterFactory.create { lt :views }
55
48
  filter.views = sample.views
56
49
 
57
- described_class.filter(filter).map(&:id).sort.should == @posts.select{|p| p.views < sample.views}.map(&:id).sort
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 "should return records with column' values less than or equal to specified value" do
61
- sample = @posts.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 do
64
- field :views, :lte
65
- end
57
+ filter = FilterFactory.create { lte :views }
66
58
  filter.views = sample.views
67
59
 
68
- described_class.filter(filter).map(&:id).sort.should == @posts.select{|p| p.views <= sample.views}.map(&:id).sort
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 "should return records with column' values greater than specified value" do
72
- sample = @posts.sample
64
+ it 'returns records with column values greater than specified value' do
65
+ sample = posts.sample
73
66
 
74
- filter = FilterFactory.create do
75
- field :views, :gt
76
- end
67
+ filter = FilterFactory.create { gt :views }
77
68
  filter.views = sample.views
78
69
 
79
- described_class.filter(filter).map(&:id).sort.should == @posts.select{|p| p.views > sample.views}.map(&:id).sort
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 "should return records with column' values greater than or equal to specified value" do
83
- sample = @posts.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 do
86
- field :views, :gte
87
- end
77
+ filter = FilterFactory.create { gte :views }
88
78
  filter.views = sample.views
89
79
 
90
- described_class.filter(filter).map(&:id).sort.should == @posts.select{|p| p.views >= sample.views}.map(&:id).sort
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 "should return records with column' values all equal to specified value" do
94
- sample = @posts.sample(2)
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 do
98
- field :opts, :all
99
- end
88
+ filter = FilterFactory.create { all :opts }
100
89
  filter.opts = [1, 2, 3, 4]
101
90
 
102
- described_class.filter(filter).map(&:id).sort.should == sample.map(&:id).sort
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 "should return records with column' values in specified values" do
106
- sample = @posts.sample(3)
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, :in
99
+ field :id, :in
110
100
  end
111
101
  filter.id = sample.map(&:id)
112
102
 
113
- described_class.filter(filter).map(&:id).sort.should == sample.map(&:id).sort
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 "should return records with column' values not in specified values" do
117
- sample = @posts.sample(3)
107
+ it 'returns records with column values not in specified values' do
108
+ sample = posts.sample(3)
118
109
 
119
- filter = FilterFactory.create do
120
- field :id, :nin
121
- end
110
+ filter = FilterFactory.create { nin :id }
122
111
  filter.id = sample.map(&:id)
123
112
 
124
- described_class.filter(filter).map(&:id).sort.should == (@posts.map(&:id) - sample.map(&:id)).sort
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 "should return records with column' values which match the specified regexp" do
128
- sample = @posts.sample(3)
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
- described_class.filter(filter).map(&:id).sort.should == sample.map(&:id).sort
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 "should return records with existing column' values" do
141
- sample = @posts.sample(3)
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
- described_class.filter(filter).map(&:id).sort.should == sample.map(&:id).sort
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
@@ -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
- Dir["#{MODELS}/*.rb"].each { |f| require f }
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
- #Mongoid.logger = Logger.new($stdout)
18
- #Moped.logger = Logger.new($stdout)
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: "mysql2",
22
- database: "active_record_filter_factory_test"
27
+ adapter: 'mysql2',
28
+ database: 'active_record_filter_factory_test'
23
29
  )
24
- ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS ar_posts")
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
- DatabaseCleaner.clean
46
+ ARPost.delete_all
47
+ MPost.delete_all
56
48
  end
57
- end
49
+ end