/
have_db_column_matcher_spec.rb
119 lines (99 loc) · 3.69 KB
/
have_db_column_matcher_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveRecord::HaveDbColumnMatcher, type: :model do
it 'accepts an existing database column' do
expect(model(nickname: :string)).to have_db_column(:nickname)
end
it 'rejects a nonexistent database column' do
expect(define_model(:employee).new).not_to have_db_column(:nickname)
end
context '#of_type' do
it 'accepts a column of correct type' do
expect(model(nickname: :string)).
to have_db_column(:nickname).of_type(:string)
end
it 'rejects a nonexistent database column' do
expect(define_model(:superhero).new).
not_to have_db_column(:nickname).of_type(:string)
end
it 'rejects a column of wrong type' do
expect(model(nickname: :integer)).
not_to have_db_column(:nickname).of_type(:string)
end
end
context 'with precision option' do
it 'accepts a column of correct precision' do
expect(with_table(:salary, :decimal, precision: 5)).
to have_db_column(:salary).with_options(precision: 5)
end
it 'rejects a column of wrong precision' do
expect(with_table(:salary, :decimal, precision: 6)).
not_to have_db_column(:salary).with_options(precision: 5)
end
end
context 'with limit option' do
it 'accepts a column of correct limit' do
expect(with_table(:email, :string, limit: 255)).
to have_db_column(:email).with_options(limit: 255)
end
it 'rejects a column of wrong limit' do
expect(with_table(:email, :string, limit: 100)).
not_to have_db_column(:email).with_options(limit: 255)
end
end
context 'with default option' do
it 'accepts a column with correct default' do
expect(with_table(:admin, :boolean, default: false)).
to have_db_column(:admin).with_options(default: false)
end
it 'rejects a column with wrong default' do
expect(with_table(:admin, :boolean, default: true)).
not_to have_db_column(:admin).with_options(default: false)
end
end
context 'with null option' do
it 'accepts a column of correct null' do
expect(with_table(:admin, :boolean, null: false)).
to have_db_column(:admin).with_options(null: false)
end
it 'rejects a column of wrong null' do
expect(with_table(:admin, :boolean, null: true)).
not_to have_db_column(:admin).with_options(null: false)
end
end
context 'with scale option' do
it 'accepts a column of correct scale' do
expect(with_table(:salary, :decimal, precision: 10, scale: 2)).
to have_db_column(:salary).with_options(scale: 2)
end
it 'rejects a column of wrong scale' do
expect(with_table(:salary, :decimal, precision: 10, scale: 4)).
not_to have_db_column(:salary).with_options(scale: 2)
end
end
context 'with primary option' do
it 'accepts a column that is primary' do
expect(with_table(:custom_id, :integer, primary: true)).
to have_db_column(:id).with_options(primary: true)
end
it 'rejects a column that is not primary' do
expect(with_table(:whatever, :integer, primary: false)).
not_to have_db_column(:whatever).with_options(primary: true)
end
end
context 'with invalid argument option' do
it 'raises an error with the unknown options' do
expect {
have_db_column(:salary).with_options(preccision: 5, primaryy: true)
}.to raise_error('Unknown option(s): :preccision, :primaryy')
end
end
def model(options = {})
define_model(:employee, options).new
end
def with_table(column_name, column_type, options)
create_table 'employees' do |table|
table.__send__(column_type, column_name, **options)
end
define_model_class('Employee').new
end
end