rspec-sequel_expectations 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.
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpec::Matchers::Sequel::HaveIndexOn do
4
+ before :all do
5
+ DB.create_table(:users) do
6
+ column :name, String, index: true
7
+ column :login, String, index: true, unique: true
8
+ column :email, String
9
+ column :company_id, Integer
10
+ column :department_id, Integer
11
+ column :created_at, Time
12
+
13
+ index :email, unique: true, name: 'users_postbox'
14
+ index [:company_id, :department_id]
15
+ end
16
+ end
17
+
18
+ after :all do
19
+ DB.drop_table(:users)
20
+ end
21
+
22
+ let(:table) { :users }
23
+
24
+ let(:result) { matcher.matches?(table) }
25
+
26
+ describe 'have_index_on' do
27
+ context 'single' do
28
+ context 'when exists' do
29
+ let(:matcher) { have_index_on(:name) }
30
+
31
+ it 'should success' do
32
+ expect(result).to be(true)
33
+ end
34
+
35
+ it 'should have description' do
36
+ expect(matcher.description).to eql('have index on [:name]')
37
+ end
38
+ end
39
+
40
+ context 'when not exists' do
41
+ let(:matcher) { have_index_on(:created_at) }
42
+
43
+ it 'should fail' do
44
+ expect(result).to be(false)
45
+ end
46
+
47
+ it 'should set error message' do
48
+ expect { result }.to change {
49
+ matcher.failure_message
50
+ }.to %(expected users to #{matcher.description} but none exists)
51
+ end
52
+
53
+ it 'should set negative error message' do
54
+ expect { result }.to change {
55
+ matcher.failure_message_when_negated
56
+ }.to %(did not expect users to #{matcher.description})
57
+ end
58
+ end
59
+ end
60
+
61
+ context 'composite' do
62
+ context 'when exists' do
63
+ let(:matcher) { have_index_on([:company_id, :department_id]) }
64
+
65
+ it 'should success' do
66
+ expect(result).to be(true)
67
+ end
68
+
69
+ it 'should have description' do
70
+ expect(matcher.description).to eql('have index on [:company_id, :department_id]')
71
+ end
72
+ end
73
+
74
+ context 'when not exists' do
75
+ let(:matcher) { have_index_on([:company_id, :email]) }
76
+
77
+ it 'should fail' do
78
+ expect(result).to be(false)
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ describe 'uniqueness' do
85
+ context 'when single' do
86
+ let(:matcher) { have_unique_index_on(:email) }
87
+
88
+ it 'should success' do
89
+ expect(result).to be(true)
90
+ end
91
+
92
+ it 'should have description' do
93
+ expect(matcher.description).to eql('have unique index on [:email]')
94
+ end
95
+ end
96
+
97
+ context 'when have both index and constraint' do
98
+ let(:matcher) { have_unique_index_on(:login) }
99
+
100
+ it 'should success' do
101
+ expect(result).to be(true)
102
+ end
103
+ end
104
+
105
+ context 'when not unique' do
106
+ let(:matcher) { have_unique_index_on(:name) }
107
+
108
+ it 'should fail' do
109
+ expect(result).to be(false)
110
+ end
111
+
112
+ it 'should set error message' do
113
+ expect { result }.to change {
114
+ matcher.failure_message
115
+ }.to %(expected users to #{matcher.description} but index is non-unique)
116
+ end
117
+
118
+ it 'should set negative error message' do
119
+ expect { result }.to change {
120
+ matcher.failure_message_when_negated
121
+ }.to %(did not expect users to #{matcher.description})
122
+ end
123
+ end
124
+ end
125
+
126
+ describe 'index name' do
127
+ context 'when match' do
128
+ let(:matcher) { have_unique_index_on(:email).named('users_postbox') }
129
+
130
+ it 'should success' do
131
+ expect(result).to be(true)
132
+ end
133
+
134
+ it 'should have description' do
135
+ expect(matcher.description).to eql('have unique index on [:email] named "users_postbox"')
136
+ end
137
+ end
138
+
139
+ context 'when did not match' do
140
+ let(:matcher) { have_unique_index_on(:email).named('users_email') }
141
+
142
+ it 'should fail' do
143
+ expect(result).to be(false)
144
+ end
145
+
146
+ it 'should set error message' do
147
+ expect { result }.to change {
148
+ matcher.failure_message
149
+ }.to %(expected users to #{matcher.description} but index have name "users_postbox")
150
+ end
151
+
152
+ it 'should set negative error message' do
153
+ expect { result }.to change {
154
+ matcher.failure_message_when_negated
155
+ }.to %(did not expect users to #{matcher.description})
156
+ end
157
+ end
158
+ end
159
+
160
+ end
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpec::Matchers::Sequel::HavePrimaryKey do
4
+ before :all do
5
+ DB.create_table(:companies) do
6
+ column :id, Integer
7
+ column :country_id, Integer
8
+
9
+ primary_key [:id, :country_id], name: :companies_pk
10
+ end
11
+
12
+ DB.create_table(:users) do
13
+ primary_key :id
14
+ end
15
+
16
+ DB.create_table(:sessions) do
17
+ column :session_id, Integer
18
+ end
19
+ end
20
+
21
+ after :all do
22
+ DB.drop_table(:companies)
23
+ DB.drop_table(:users)
24
+ DB.drop_table(:sessions)
25
+ end
26
+
27
+ let(:result) { matcher.matches?(table) }
28
+
29
+ describe 'have_primary_key' do
30
+ context 'single key' do
31
+ let(:table) { :users }
32
+
33
+ context 'when match' do
34
+ let(:matcher) { have_primary_key(:id) }
35
+
36
+ it 'should success' do
37
+ expect(result).to be(true)
38
+ end
39
+
40
+ it 'should have description' do
41
+ expect(matcher.description).to eql('have primary key "id"')
42
+ end
43
+ end
44
+
45
+ context 'when did not match' do
46
+ let(:matcher) { have_primary_key(:user_id) }
47
+
48
+ it 'should fail' do
49
+ expect(result).to be(false)
50
+ end
51
+
52
+ it 'should set error message' do
53
+ expect { result }.to change {
54
+ matcher.failure_message
55
+ }.to %(expected users to #{matcher.description} but users have primary key "id")
56
+ end
57
+
58
+ it 'should set negative error message' do
59
+ expect { result }.to change {
60
+ matcher.failure_message_when_negated
61
+ }.to("did not expect users to #{matcher.description}")
62
+ end
63
+ end
64
+
65
+ context 'when without keys' do
66
+ let(:matcher) { have_primary_key(:user_id) }
67
+ let(:table) { :sessions }
68
+
69
+ it 'should fail' do
70
+ expect(result).to be(false)
71
+ end
72
+
73
+ it 'should set error message' do
74
+ expect { result }.to change {
75
+ matcher.failure_message
76
+ }.to %(expected sessions to #{matcher.description} but sessions have no primary keys)
77
+ end
78
+
79
+ it 'should set negative error message' do
80
+ expect { result }.to change {
81
+ matcher.failure_message_when_negated
82
+ }.to("did not expect sessions to #{matcher.description}")
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'compound key' do
88
+ let(:table) { :companies }
89
+
90
+ context 'when match' do
91
+ context 'single test' do
92
+ let(:matcher) { have_primary_key(:country_id) }
93
+
94
+ it 'should success' do
95
+ expect(result).to be(true)
96
+ end
97
+
98
+ it 'should have description' do
99
+ expect(matcher.description).to eql('have primary key "country_id"')
100
+ end
101
+ end
102
+
103
+ context 'compound test' do
104
+ let(:matcher) { have_primary_keys(:id, :country_id) }
105
+
106
+ it 'should success' do
107
+ expect(result).to be(true)
108
+ end
109
+
110
+ it 'should have description' do
111
+ expect(matcher.description).to eql('have primary keys [:id, :country_id]')
112
+ end
113
+ end
114
+
115
+ end
116
+
117
+ context 'when did not match' do
118
+ let(:matcher) { have_primary_keys(:id, :city_id) }
119
+
120
+ it 'should fail' do
121
+ expect(result).to be(false)
122
+ end
123
+
124
+ it 'should set error message' do
125
+ expect { result }.to change {
126
+ matcher.failure_message
127
+ }.to %(expected companies to #{matcher.description} but companies have primary keys [:id, :country_id])
128
+ end
129
+
130
+ it 'should set negative error message' do
131
+ expect { result }.to change {
132
+ matcher.failure_message_when_negated
133
+ }.to("did not expect companies to #{matcher.description}")
134
+ end
135
+ end
136
+
137
+ context 'when without keys' do
138
+ let(:matcher) { have_primary_keys(:user_id, :id) }
139
+ let(:table) { :sessions }
140
+
141
+ it 'should fail' do
142
+ expect(result).to be(false)
143
+ end
144
+
145
+ it 'should set error message' do
146
+ expect { result }.to change {
147
+ matcher.failure_message
148
+ }.to %(expected sessions to #{matcher.description} but sessions have no primary keys)
149
+ end
150
+
151
+ end
152
+ end
153
+ end
154
+
155
+ end
@@ -0,0 +1,171 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpec::Matchers::Sequel::ReferTo do
4
+ before :all do
5
+ DB.create_table(:users) do
6
+ primary_key :id
7
+ column :name, String
8
+ end
9
+
10
+ DB.create_table(:posts) do
11
+ column :text, String
12
+
13
+ foreign_key :user_id, :users, on_update: :cascade, key: :id
14
+ end
15
+ end
16
+
17
+ after :all do
18
+ DB.drop_table(:posts)
19
+ DB.drop_table(:users)
20
+ end
21
+
22
+ let(:table) { :posts }
23
+
24
+ let(:result) { matcher.matches?(table) }
25
+
26
+ describe 'refer_to' do
27
+ context 'when reference exist' do
28
+ let(:matcher) { refer_to(:users) }
29
+
30
+ it 'should success' do
31
+ expect(result).to be(true)
32
+ end
33
+
34
+ it 'should have description' do
35
+ expect(matcher.description).to eql('have reference to "users"')
36
+ end
37
+ end
38
+
39
+ context 'when reference not exist' do
40
+ let(:matcher) { refer_to(:blabla) }
41
+
42
+ it 'should fail' do
43
+ expect(result).to be(false)
44
+ end
45
+
46
+ it 'should set error message' do
47
+ expect { result }.to change {
48
+ matcher.failure_message
49
+ }.to %(expected "posts" to #{matcher.description} but "posts" does not have a reference to "blabla")
50
+ end
51
+
52
+ it 'should set negative error message' do
53
+ expect { result }.to change {
54
+ matcher.failure_message_when_negated
55
+ }.to %(did not expect "posts" to #{matcher.description})
56
+ end
57
+ end
58
+ end
59
+
60
+ describe 'from_fk' do
61
+ context 'when foreign key column exist' do
62
+ let(:matcher) { refer_to(:users).from_fk(:user_id) }
63
+
64
+ it 'should success' do
65
+ expect(result).to be(true)
66
+ end
67
+
68
+ it 'should have description' do
69
+ expect(matcher.description).to eql('have reference to "users" with column "user_id"')
70
+ end
71
+ end
72
+
73
+ context 'when foreign key column not exist' do
74
+ let(:matcher) { refer_to(:users).from_fk(:blabla_id) }
75
+
76
+ it 'should fail' do
77
+ expect(result).to be(false)
78
+ end
79
+
80
+ it 'should set error message' do
81
+ expect { result }.to change {
82
+ matcher.failure_message
83
+ }.to %(expected "posts" to #{matcher.description} but "posts" does not have a foreign key column "blabla_id")
84
+ end
85
+ end
86
+ end
87
+
88
+ describe 'with_pk' do
89
+ context 'when primary key column exist' do
90
+ let(:matcher) { refer_to(:users).to_pk(:id) }
91
+
92
+ it 'should success' do
93
+ expect(result).to be(true)
94
+ end
95
+
96
+ it 'should have description' do
97
+ expect(matcher.description).to eql('have reference to "users" with primary key column "id"')
98
+ end
99
+ end
100
+
101
+ context 'when primary key column not exist' do
102
+ let(:matcher) { refer_to(:users).to_pk(:blabla) }
103
+
104
+ it 'should fail' do
105
+ expect(result).to be(false)
106
+ end
107
+
108
+ it 'should set error message' do
109
+ expect { result }.to change {
110
+ matcher.failure_message
111
+ }.to %(expected "posts" to #{matcher.description} but "users" does not have a primary key column "blabla")
112
+ end
113
+ end
114
+ end
115
+
116
+ describe 'on_update' do
117
+ context 'when update action right' do
118
+ let(:matcher) { refer_to(:users).on_update(:cascade) }
119
+
120
+ it 'should success' do
121
+ expect(result).to be(true)
122
+ end
123
+
124
+ it 'should have description' do
125
+ expect(matcher.description).to eql('have reference to "users" with "cascade" action on update')
126
+ end
127
+ end
128
+
129
+ context 'when update action wrong' do
130
+ let(:matcher) { refer_to(:users).on_update(:blabla) }
131
+
132
+ it 'should fail' do
133
+ expect(result).to be(false)
134
+ end
135
+
136
+ it 'should set error message' do
137
+ expect { result }.to change {
138
+ matcher.failure_message
139
+ }.to %(expected "posts" to #{matcher.description} but reference does not have action "blabla" on update)
140
+ end
141
+ end
142
+ end
143
+
144
+ describe 'on_delete' do
145
+ context 'when delete action right' do
146
+ let(:matcher) { refer_to(:users).on_delete(:no_action) }
147
+
148
+ it 'should success' do
149
+ expect(result).to be(true)
150
+ end
151
+
152
+ it 'should have description' do
153
+ expect(matcher.description).to eql('have reference to "users" with "no_action" action on delete')
154
+ end
155
+ end
156
+
157
+ context 'when update action wrong' do
158
+ let(:matcher) { refer_to(:users).on_delete(:blabla) }
159
+
160
+ it 'should fail' do
161
+ expect(result).to be(false)
162
+ end
163
+
164
+ it 'should set error message' do
165
+ expect { result }.to change {
166
+ matcher.failure_message
167
+ }.to %(expected "posts" to #{matcher.description} but reference does not have action "blabla" on delete)
168
+ end
169
+ end
170
+ end
171
+ end