grape-entity 1.0.1 → 1.0.4

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.
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Grape::Entity::Exposure::NestingExposure::NestedExposures do
6
- subject(:nested_exposures) { described_class.new([]) }
7
-
8
- describe '#deep_complex_nesting?(entity)' do
9
- it 'is reset when additional exposure is added' do
10
- subject << Grape::Entity::Exposure.new(:x, {})
11
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
12
- subject.deep_complex_nesting?(subject)
13
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to_not be_nil
14
- subject << Grape::Entity::Exposure.new(:y, {})
15
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
16
- end
17
-
18
- it 'is reset when exposure is deleted' do
19
- subject << Grape::Entity::Exposure.new(:x, {})
20
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
21
- subject.deep_complex_nesting?(subject)
22
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to_not be_nil
23
- subject.delete_by(:x)
24
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
25
- end
26
-
27
- it 'is reset when exposures are cleared' do
28
- subject << Grape::Entity::Exposure.new(:x, {})
29
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
30
- subject.deep_complex_nesting?(subject)
31
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to_not be_nil
32
- subject.clear
33
- expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
34
- end
35
- end
36
-
37
- describe '.delete_by' do
38
- subject { nested_exposures.delete_by(*attributes) }
39
-
40
- let(:attributes) { [:id] }
41
-
42
- before do
43
- nested_exposures << Grape::Entity::Exposure.new(:id, {})
44
- end
45
-
46
- it 'deletes matching exposure' do
47
- is_expected.to eq []
48
- end
49
-
50
- context "when given attribute doesn't exists" do
51
- let(:attributes) { [:foo] }
52
-
53
- it 'deletes matching exposure' do
54
- is_expected.to eq(nested_exposures)
55
- end
56
- end
57
- end
58
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Grape::Entity::Exposure::RepresentExposure do
6
- subject(:exposure) { described_class.new(:foo, {}, {}, double, double) }
7
-
8
- describe '#setup' do
9
- subject { exposure.setup(using_class_name, subexposure) }
10
-
11
- let(:using_class_name) { double(:using_class_name) }
12
- let(:subexposure) { double(:subexposure) }
13
-
14
- it 'sets using_class_name' do
15
- expect { subject }.to change(exposure, :using_class_name).to(using_class_name)
16
- end
17
-
18
- it 'sets subexposure' do
19
- expect { subject }.to change(exposure, :subexposure).to(subexposure)
20
- end
21
-
22
- context 'when using_class is set' do
23
- before do
24
- exposure.using_class
25
- end
26
-
27
- it 'resets using_class' do
28
- expect { subject }.to change(exposure, :using_class)
29
- end
30
- end
31
- end
32
- end
@@ -1,102 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Grape::Entity::Exposure do
6
- let(:fresh_class) { Class.new(Grape::Entity) }
7
- let(:model) { double(attributes) }
8
- let(:attributes) do
9
- {
10
- name: 'Bob Bobson',
11
- email: 'bob@example.com',
12
- birthday: Time.gm(2012, 2, 27),
13
- fantasies: ['Unicorns', 'Double Rainbows', 'Nessy'],
14
- characteristics: [
15
- { key: 'hair_color', value: 'brown' }
16
- ],
17
- friends: [
18
- double(name: 'Friend 1', email: 'friend1@example.com', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
19
- double(name: 'Friend 2', email: 'friend2@example.com', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
20
- ]
21
- }
22
- end
23
- let(:entity) { fresh_class.new(model) }
24
- subject { fresh_class.find_exposure(:name) }
25
-
26
- describe '#key' do
27
- it 'returns the attribute if no :as is set' do
28
- fresh_class.expose :name
29
- expect(subject.key(entity)).to eq :name
30
- end
31
-
32
- it 'returns the :as alias if one exists' do
33
- fresh_class.expose :name, as: :nombre
34
- expect(subject.key(entity)).to eq :nombre
35
- end
36
-
37
- it 'returns the result if :as is a proc' do
38
- fresh_class.expose :name, as: proc { object.name.reverse }
39
- expect(subject.key(entity)).to eq(model.name.reverse)
40
- end
41
-
42
- it 'returns the result if :as is a lambda' do
43
- fresh_class.expose :name, as: ->(obj, _opts) { obj.name.reverse }
44
- expect(subject.key(entity)).to eq(model.name.reverse)
45
- end
46
- end
47
-
48
- describe '#conditions_met?' do
49
- it 'only passes through hash :if exposure if all attributes match' do
50
- fresh_class.expose :name, if: { condition1: true, condition2: true }
51
-
52
- expect(subject.conditions_met?(entity, {})).to be false
53
- expect(subject.conditions_met?(entity, condition1: true)).to be false
54
- expect(subject.conditions_met?(entity, condition1: true, condition2: true)).to be true
55
- expect(subject.conditions_met?(entity, condition1: false, condition2: true)).to be false
56
- expect(subject.conditions_met?(entity, condition1: true, condition2: true, other: true)).to be true
57
- end
58
-
59
- it 'looks for presence/truthiness if a symbol is passed' do
60
- fresh_class.expose :name, if: :condition1
61
-
62
- expect(subject.conditions_met?(entity, {})).to be false
63
- expect(subject.conditions_met?(entity, condition1: true)).to be true
64
- expect(subject.conditions_met?(entity, condition1: false)).to be false
65
- expect(subject.conditions_met?(entity, condition1: nil)).to be false
66
- end
67
-
68
- it 'looks for absence/falsiness if a symbol is passed' do
69
- fresh_class.expose :name, unless: :condition1
70
-
71
- expect(subject.conditions_met?(entity, {})).to be true
72
- expect(subject.conditions_met?(entity, condition1: true)).to be false
73
- expect(subject.conditions_met?(entity, condition1: false)).to be true
74
- expect(subject.conditions_met?(entity, condition1: nil)).to be true
75
- end
76
-
77
- it 'only passes through proc :if exposure if it returns truthy value' do
78
- fresh_class.expose :name, if: ->(_, opts) { opts[:true] }
79
-
80
- expect(subject.conditions_met?(entity, true: false)).to be false
81
- expect(subject.conditions_met?(entity, true: true)).to be true
82
- end
83
-
84
- it 'only passes through hash :unless exposure if any attributes do not match' do
85
- fresh_class.expose :name, unless: { condition1: true, condition2: true }
86
-
87
- expect(subject.conditions_met?(entity, {})).to be true
88
- expect(subject.conditions_met?(entity, condition1: true)).to be true
89
- expect(subject.conditions_met?(entity, condition1: true, condition2: true)).to be false
90
- expect(subject.conditions_met?(entity, condition1: false, condition2: true)).to be true
91
- expect(subject.conditions_met?(entity, condition1: true, condition2: true, other: true)).to be false
92
- expect(subject.conditions_met?(entity, condition1: false, condition2: false)).to be true
93
- end
94
-
95
- it 'only passes through proc :unless exposure if it returns falsy value' do
96
- fresh_class.expose :name, unless: ->(_, opts) { opts[:true] == true }
97
-
98
- expect(subject.conditions_met?(entity, true: false)).to be true
99
- expect(subject.conditions_met?(entity, true: true)).to be false
100
- end
101
- end
102
- end
@@ -1,91 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Grape::Entity do
6
- it 'except option for nested entity', :aggregate_failures do
7
- module EntitySpec
8
- class Address < Grape::Entity
9
- expose :post, if: :full
10
- expose :city
11
- expose :street
12
- expose :house
13
- end
14
-
15
- class AddressWithString < Grape::Entity
16
- self.hash_access = :string
17
- expose :post, if: :full
18
- expose :city
19
- expose :street
20
- expose :house, expose_nil: false
21
- end
22
-
23
- class Company < Grape::Entity
24
- expose :full_name, if: :full
25
- expose :name
26
- expose :address do |c, o|
27
- Address.represent c[:address], Grape::Entity::Options.new(o.opts_hash.except(:full))
28
- end
29
- end
30
-
31
- class CompanyWithString < Grape::Entity
32
- self.hash_access = :string
33
- expose :full_name, if: :full
34
- expose :name
35
- expose :address do |c, o|
36
- AddressWithString.represent c['address'], Grape::Entity::Options.new(o.opts_hash.except(:full))
37
- end
38
- end
39
- end
40
-
41
- company = {
42
- full_name: 'full_name',
43
- name: 'name',
44
- address: {
45
- post: '123456',
46
- city: 'city',
47
- street: 'street',
48
- house: 'house',
49
- something_else: 'something_else'
50
- }
51
- }
52
-
53
- company_with_string = {
54
- 'full_name' => 'full_name',
55
- 'name' => 'name',
56
- 'address' => {
57
- 'post' => '123456',
58
- 'city' => 'city',
59
- 'street' => 'street',
60
- 'house' => 'house',
61
- 'something_else' => 'something_else'
62
- }
63
- }
64
-
65
- company_without_house_with_string = {
66
- 'full_name' => 'full_name',
67
- 'name' => 'name',
68
- 'address' => {
69
- 'post' => '123456',
70
- 'city' => 'city',
71
- 'street' => 'street',
72
- 'something_else' => 'something_else'
73
- }
74
- }
75
-
76
- expect(EntitySpec::CompanyWithString.represent(company_with_string).serializable_hash).to eq \
77
- company.slice(:name).merge(address: company[:address].slice(:city, :street, :house))
78
-
79
- expect(EntitySpec::CompanyWithString.represent(company_without_house_with_string).serializable_hash).to eq \
80
- company.slice(:name).merge(address: company[:address].slice(:city, :street))
81
-
82
- expect(EntitySpec::CompanyWithString.represent(company_with_string, full: true).serializable_hash).to eq \
83
- company.slice(:full_name, :name).merge(address: company[:address].slice(:city, :street, :house))
84
-
85
- expect(EntitySpec::Company.represent(company).serializable_hash).to eq \
86
- company.slice(:name).merge(address: company[:address].slice(:city, :street, :house))
87
-
88
- expect(EntitySpec::Company.represent(company, full: true).serializable_hash).to eq \
89
- company.slice(:full_name, :name).merge(address: company[:address].slice(:city, :street, :house))
90
- end
91
- end
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Grape::Entity::Options do
6
- module EntitySpec
7
- class Crystalline
8
- attr_accessor :prop1, :prop2, :prop3
9
-
10
- def initialize
11
- @prop1 = 'value1'
12
- @prop2 = 'value2'
13
- @prop3 = 'value3'
14
- end
15
- end
16
-
17
- class CrystallineEntity < Grape::Entity
18
- expose :prop1, if: ->(_, options) { options.fetch(:signal) }
19
- expose :prop2, if: ->(_, options) { options.fetch(:beam, 'destructive') == 'destructive' }
20
- end
21
- end
22
-
23
- context '#fetch' do
24
- it 'without passing in a required option raises KeyError' do
25
- expect { EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new).as_json }.to raise_error KeyError
26
- end
27
-
28
- it 'passing in a required option will expose the values' do
29
- crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true)
30
- expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2')
31
- end
32
-
33
- it 'with an option that is not default will not expose that value' do
34
- crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, beam: 'intermittent')
35
- expect(crystalline_entity.as_json).to eq(prop1: 'value1')
36
- end
37
- end
38
-
39
- context '#dig', skip: !{}.respond_to?(:dig) do
40
- let(:model_class) do
41
- Class.new do
42
- attr_accessor :prop1
43
-
44
- def initialize
45
- @prop1 = 'value1'
46
- end
47
- end
48
- end
49
-
50
- let(:entity_class) do
51
- Class.new(Grape::Entity) do
52
- expose :prop1, if: ->(_, options) { options.dig(:first, :second) == :nested }
53
- end
54
- end
55
-
56
- it 'without passing in a expected option hide the value' do
57
- entity = entity_class.represent(model_class.new, first: { invalid: :nested })
58
- expect(entity.as_json).to eq({})
59
- end
60
-
61
- it 'passing in a expected option will expose the values' do
62
- entity = entity_class.represent(model_class.new, first: { second: :nested })
63
- expect(entity.as_json).to eq(prop1: 'value1')
64
- end
65
- end
66
- end
data/spec/spec_helper.rb DELETED
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'simplecov'
4
- require 'coveralls'
5
-
6
- # This works around the hash extensions not being automatically included in ActiveSupport < 4
7
- require 'active_support/version'
8
- require 'active_support/core_ext/hash' if ActiveSupport::VERSION &&
9
- ActiveSupport::VERSION::MAJOR &&
10
- ActiveSupport::VERSION::MAJOR < 4
11
-
12
- # Skip code covarge on Ruby >= 3.1
13
- # See https://github.com/simplecov-ruby/simplecov/issues/1003
14
- unless RUBY_VERSION >= '3.1'
15
- SimpleCov.start do
16
- add_filter 'spec/'
17
- end
18
-
19
- Coveralls.wear! unless RUBY_PLATFORM.eql? 'java'
20
- end
21
-
22
- $LOAD_PATH.unshift(File.dirname(__FILE__))
23
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
24
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))
25
-
26
- require 'rubygems'
27
- require 'bundler'
28
-
29
- Bundler.require :default, :test
30
-
31
- RSpec.configure(&:raise_errors_for_deprecations!)