Module: Cuprum::Rails::RSpec::Contracts::ActionContracts::ShouldBeAResourceActionContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/rails/rspec/contracts/action_contracts.rb

Overview

Contract validating the interface for a resourceful action.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, **options) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.

  • options (Hash)

    additional options for the conrtact.

Options Hash (**options):

  • collection_class (String, Class)

    the expected class for the resource collection.

  • require_permitted_attributes (Boolean)

    if true, should require the resource to define permitted attributes as a non-empty Array.

  • required_keywords (Array[Symbol])

    additional keywords required by the #call method.



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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/cuprum/rails/rspec/contracts/action_contracts.rb', line 83

contract do |**options|
  include Cuprum::Rails::RSpec::Contracts::ActionContracts

  let(:configured_params) do
    return params if defined?(params)

    {}
  end
  let(:configured_repository) do
    return repository if defined?(repository)

    Cuprum::Rails::Repository.new
  end
  let(:configured_request) do
    return request if defined?(request)

    Cuprum::Rails::Request.new(params: configured_params)
  end
  let(:configured_resource) do
    return resource if defined?(resource)

    # :nocov:
    Cuprum::Rails::Resource.new(entity_class: Book)
    # :nocov:
  end
  let(:configured_action_options) do
    return action_options if defined?(action_options)

    {
      repository: configured_repository,
      resource:   configured_resource
    }
  end

  define_method(:call_action) do
    action.call(request: configured_request, **configured_action_options)
  end

  include_contract 'should be an action',
    required_keywords: [:resource, *options.fetch(:required_keywords, [])]

  describe '#call' do
    next unless options[:require_permitted_attributes]

    describe 'with a permitted_attributes: nil' do
      let(:resource) do
        Cuprum::Rails::Resource.new(
          name:                 'books',
          permitted_attributes: nil
        )
      end
      let(:expected_error) do
        Cuprum::Rails::Errors::ResourceError.new(
          message:  "permitted attributes can't be blank",
          resource: configured_resource
        )
      end

      it 'should return a failing result' do
        expect(call_action)
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with a permitted_attributes: an empty Array' do
      let(:resource) do
        Cuprum::Rails::Resource.new(
          name:                 'books',
          permitted_attributes: []
        )
      end
      let(:expected_error) do
        Cuprum::Rails::Errors::ResourceError.new(
          message:  "permitted attributes can't be blank",
          resource: configured_resource
        )
      end

      it 'should return a failing result' do
        expect(call_action)
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end
  end

  describe '#collection' do
    let(:expected_collection_class) do
      next super() if defined?(super())

      options
        .fetch(:collection_class, Cuprum::Collections::Collection)
        .then { |obj| obj.is_a?(String) ? obj.constantize : obj }
    end

    before(:example) { call_action }

    include_examples 'should define reader', :collection

    it { expect(action.collection).to be_a expected_collection_class }

    it 'should set the collection name' do
      expect(action.collection.name)
        .to be == resource.name
    end

    it 'should set the entity class' do
      expect(action.collection.entity_class)
        .to be == resource.entity_class
    end

    context 'when the repository defines a matching collection' do
      let!(:existing_collection) do
        configured_repository.find_or_create(
          qualified_name: resource.qualified_name
        )
      end

      it { expect(action.collection).to be existing_collection }
    end

    context 'when there is a partially matching collection' do
      let(:configured_repository) do
        repository = super()

        repository.find_or_create(
          entity_class:   resource.entity_class,
          name:           'other_collection',
          qualified_name: resource.qualified_name
        )

        repository
      end
      let!(:existing_collection) do
        configured_repository[resource.qualified_name]
      end

      it { expect(action.collection).to be existing_collection }
    end
  end

  describe '#resource' do
    include_examples 'should define reader', :resource

    context 'when called with a resource' do
      before(:example) { call_action }

      it { expect(action.resource).to be == configured_resource }
    end
  end

  describe '#resource_id' do
    include_examples 'should define reader', :resource_id

    context 'when called with a resource' do
      let(:params)  { {} }
      let(:request) { Cuprum::Rails::Request.new(params: params) }

      before(:example) { call_action }

      context 'when the parameters do not include a primary key' do
        let(:params) { {} }

        it { expect(action.resource_id).to be nil }
      end

      context 'when the :id parameter is set' do
        let(:primary_key_value) { 0 }
        let(:params)            { { 'id' => primary_key_value } }

        it { expect(action.resource_id).to be primary_key_value }
      end
    end
  end

  describe '#resource_params' do
    include_examples 'should define reader', :resource_params

    context 'when called with a resource' do
      let(:params)  { {} }
      let(:request) { Cuprum::Rails::Request.new(params: params) }

      before(:example) { call_action }

      context 'when the parameters do not include params for the ' \
              'resource' \
      do
        let(:params) { {} }

        it { expect(action.resource_params).to be == {} }
      end

      context 'when the params for the resource are empty' do
        let(:params) { { resource.singular_name => {} } }

        it { expect(action.resource_params).to be == {} }
      end

      context 'when the parameter for the resource is not a Hash' do
        let(:params) { { resource.singular_name => 'invalid' } }

        it { expect(action.resource_params).to be == 'invalid' }
      end

      context 'when the parameters include the params for resource' do
        let(:params) do
          resource_params =
            configured_resource
              .permitted_attributes
              .then { |ary| ary || [] }
              .to_h { |attr_name| [attr_name.to_s, "#{attr_name} value"] }

          {
            configured_resource.singular_name => resource_params
          }
        end
        let(:expected) do
          params[configured_resource.singular_name]
        end

        it { expect(action.resource_params).to be == expected }
      end
    end
  end

  describe '#transaction' do
    let(:transaction_class) { resource.entity_class }

    before(:example) { call_action }

    it 'should define the private method' do
      expect(action).to respond_to(:transaction, true).with(0).arguments
    end

    it 'should yield the block' do
      expect { |block| action.send(:transaction, &block) }
        .to yield_control
    end

    it 'should wrap the block in a transaction' do
      in_transaction = false

      allow(transaction_class).to receive(:transaction) do |&block|
        in_transaction = true

        block.call

        in_transaction = false
      end

      action.send(:transaction) do
        expect(in_transaction).to be true
      end
    end

    context 'when the block contains a failing step' do
      let(:expected_error) do
        Cuprum::Error.new(message: 'Something went wrong.')
      end

      before(:example) do
        action.define_singleton_method(:failing_step) do
          error = Cuprum::Error.new(message: 'Something went wrong.')

          step { failure(error) }
        end
      end

      it 'should return the failing result' do
        expect(action.send(:transaction) { action.failing_step })
          .to be_a_failing_result
          .with_error(expected_error)
      end

      it 'should roll back the transaction' do
        rollback = false

        allow(transaction_class).to receive(:transaction) do |&block|
          block.call
        rescue ActiveRecord::Rollback
          rollback = true
        end

        action.send(:transaction) { action.failing_step }

        expect(rollback).to be true
      end
    end
  end
end