File: model_spec.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (197 lines) | stat: -rw-r--r-- 4,793 bytes parent folder | download | duplicates (3)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'spec_helper'

describe Virtus, '.model' do
  shared_examples_for 'a model with constructor' do
    it 'accepts attribute hash' do
      instance = subject.new(:name => 'Jane')
      expect(instance.name).to eql('Jane')
    end
  end

  shared_examples_for 'a model with mass-assignment' do
    let(:attributes) do
      { :name => 'Jane', :something => nil }
    end

    before do
      instance.attributes = attributes
    end

    it 'accepts attribute hash' do
      expect(instance.attributes).to eql(attributes)
    end
  end

  shared_examples_for 'a model with strict mode turned off' do
    it 'has attributes with strict set to false' do
      expect(subject.send(:attribute_set)[:name]).to_not be_strict
    end
  end

  context 'with default configuration' do
    let(:mod) { Virtus.model }

    context 'with a class' do
      let(:model) { Class.new }

      subject { model }

      before do
        subject.send(:include, mod)
        subject.attribute :name, String, :default => 'Jane'
        subject.attribute :something
      end

      it_behaves_like 'a model with constructor'

      it_behaves_like 'a model with strict mode turned off'

      it_behaves_like 'a model with mass-assignment' do
        let(:instance) { subject.new }
      end

      it 'defaults to Object for attribute type' do
        expect(model.attribute_set[:something].type).to be(Axiom::Types::Object)
      end

      context 'with a sub-class' do
        subject { Class.new(model) }

        before do
          subject.attribute :age, Integer
        end

        it_behaves_like 'a model with constructor'

        it_behaves_like 'a model with strict mode turned off'

        it_behaves_like 'a model with mass-assignment' do
          let(:instance) { subject.new }

          let(:attributes) {
            { :name => 'Jane', :something => nil, :age => 23 }
          }
        end

        it 'has its own attributes' do
          expect(subject.attribute_set[:age]).to be_kind_of(Virtus::Attribute)
        end
      end
    end

    context 'with an instance' do
      subject { Class.new.new }

      before do
        subject.extend(mod)
        subject.attribute :name, String
        subject.attribute :something
      end

      it_behaves_like 'a model with strict mode turned off'

      it_behaves_like 'a model with mass-assignment' do
        let(:instance) { subject }
      end
    end
  end

  context 'when constructor is disabled' do
    subject { Class.new.send(:include, mod) }

    let(:mod) { Virtus.model(:constructor => false) }

    it 'does not accept attribute hash in the constructor' do
      expect { subject.new({}) }.to raise_error(ArgumentError)
    end
  end

  context 'when strict mode is enabled' do
    let(:mod)   { Virtus.model(:strict => true) }
    let(:model) { Class.new }

    context 'with a class' do
      subject { model.new }

      before do
        model.send(:include, mod)
        model.attribute :name, String
      end

      it 'has attributes with strict set to true' do
        expect(model.attribute_set[:name]).to be_strict
      end
    end

    context 'with an instance' do
      subject { model.new }

      before do
        subject.extend(mod)
        subject.attribute :name, String
      end

      it 'has attributes with strict set to true' do
        expect(subject.send(:attribute_set)[:name]).to be_strict
      end
    end
  end

  context 'when mass-assignment is disabled' do
    let(:mod)   { Virtus.model(:mass_assignment => false) }
    let(:model) { Class.new }

    context 'with a class' do
      subject { model.new }

      before do
        model.send(:include, mod)
      end

      it { is_expected.not_to respond_to(:attributes) }
      it { is_expected.not_to respond_to(:attributes=) }
    end

    context 'with an instance' do
      subject { model.new }

      before do
        subject.extend(mod)
      end

      it { is_expected.not_to respond_to(:attributes) }
      it { is_expected.not_to respond_to(:attributes=) }
    end
  end

  context 'when :required is set' do
    let(:mod)   { Virtus.model(:required => false) }
    let(:model) { Class.new }

    context 'with a class' do
      subject { model.new }

      before do
        model.send(:include, mod)
        model.attribute :name, String
      end

      it 'has attributes with :required option inherited from module' do
        expect(model.attribute_set[:name]).to_not be_required
      end
    end

    context 'with an instance' do
      subject { model.new }

      before do
        subject.extend(mod)
        subject.attribute :name, String
      end

      it 'has attributes with strict set to true' do
        expect(subject.send(:attribute_set)[:name]).not_to be_required
      end
    end
  end
end