lhs 2.2.2 → 3.0.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.
- checksums.yaml +4 -4
- data/README.md +296 -30
- data/lhs.gemspec +3 -3
- data/lib/lhs.rb +0 -7
- data/lib/lhs/collection.rb +4 -4
- data/lib/lhs/concerns/item/destroy.rb +2 -2
- data/lib/lhs/concerns/item/save.rb +3 -3
- data/lib/lhs/concerns/item/update.rb +2 -2
- data/lib/lhs/concerns/item/validation.rb +4 -5
- data/lib/lhs/concerns/{service → record}/all.rb +2 -2
- data/lib/lhs/concerns/{service → record}/batch.rb +3 -3
- data/lib/lhs/concerns/{service → record}/create.rb +4 -3
- data/lib/lhs/concerns/{service → record}/endpoints.rb +6 -6
- data/lib/lhs/concerns/{service → record}/find.rb +3 -2
- data/lib/lhs/concerns/{service → record}/find_by.rb +3 -2
- data/lib/lhs/concerns/{service → record}/first.rb +1 -1
- data/lib/lhs/concerns/{service → record}/includes.rb +4 -2
- data/lib/lhs/concerns/{service → record}/mapping.rb +1 -1
- data/lib/lhs/concerns/{service → record}/model.rb +1 -1
- data/lib/lhs/concerns/{service → record}/request.rb +12 -12
- data/lib/lhs/concerns/{service → record}/where.rb +3 -2
- data/lib/lhs/data.rb +25 -53
- data/lib/lhs/endpoint.rb +2 -2
- data/lib/lhs/item.rb +8 -2
- data/lib/lhs/proxy.rb +2 -2
- data/lib/lhs/record.rb +42 -0
- data/lib/lhs/version.rb +1 -1
- data/spec/collection/meta_data_spec.rb +2 -2
- data/spec/collection/without_object_items_spec.rb +1 -1
- data/spec/data/item_spec.rb +2 -14
- data/spec/data/merge_spec.rb +3 -3
- data/spec/data/raw_spec.rb +1 -1
- data/spec/data/respond_to_spec.rb +3 -3
- data/spec/data/root_spec.rb +2 -2
- data/spec/data/to_json_spec.rb +2 -2
- data/spec/endpoint/for_url_spec.rb +1 -1
- data/spec/item/delegate_spec.rb +2 -2
- data/spec/item/destroy_spec.rb +4 -4
- data/spec/item/errors_spec.rb +4 -4
- data/spec/item/getter_spec.rb +2 -2
- data/spec/item/internal_data_structure_spec.rb +1 -1
- data/spec/item/save_spec.rb +3 -4
- data/spec/item/setter_spec.rb +2 -2
- data/spec/item/update_spec.rb +2 -2
- data/spec/item/validation_spec.rb +47 -75
- data/spec/proxy/load_spec.rb +2 -2
- data/spec/{service → record}/all_spec.rb +7 -7
- data/spec/{service → record}/build_spec.rb +5 -4
- data/spec/{service → record}/create_spec.rb +6 -5
- data/spec/{service → record}/creation_failed_spec.rb +6 -5
- data/spec/record/definitions_spec.rb +29 -0
- data/spec/{service → record}/endpoint_misconfiguration_spec.rb +3 -3
- data/spec/{service → record}/endpoint_options_spec.rb +4 -4
- data/spec/{service → record}/endpoints_spec.rb +15 -15
- data/spec/{service → record}/find_by_spec.rb +8 -8
- data/spec/{service → record}/find_each_spec.rb +3 -3
- data/spec/{service → record}/find_in_batches_spec.rb +6 -6
- data/spec/{service → record}/find_spec.rb +10 -9
- data/spec/{service → record}/first_spec.rb +7 -6
- data/spec/{service → record}/includes_spec.rb +7 -7
- data/spec/{service → record}/mapping_spec.rb +27 -16
- data/spec/{service → record}/model_name_spec.rb +2 -2
- data/spec/{service → record}/new_spec.rb +4 -3
- data/spec/{service → record}/request_spec.rb +3 -3
- data/spec/record/where_spec.rb +34 -0
- data/spec/support/cleanup_endpoints.rb +1 -1
- data/spec/support/{cleanup_services.rb → cleanup_records.rb} +2 -2
- metadata +60 -67
- data/docs/collections.md +0 -28
- data/docs/data.md +0 -39
- data/docs/items.md +0 -79
- data/docs/service.jpg +0 -0
- data/docs/service.pdf +2 -650
- data/docs/services.md +0 -266
- data/lib/lhs/concerns/service/build.rb +0 -19
- data/lib/lhs/service.rb +0 -18
- data/spec/data/pagination_spec.rb +0 -60
- data/spec/dummy/README.rdoc +0 -28
- data/spec/service/where_spec.rb +0 -33
data/lib/lhs/proxy.rb
CHANGED
@@ -17,8 +17,8 @@ class LHS::Proxy
|
|
17
17
|
|
18
18
|
def reload!
|
19
19
|
fail 'No href found' unless _data.href
|
20
|
-
|
21
|
-
data =
|
20
|
+
record = _data._root._record_class
|
21
|
+
data = record.request(url: _data.href, method: :get)
|
22
22
|
_data.merge_raw!(data)
|
23
23
|
self._loaded = true
|
24
24
|
self
|
data/lib/lhs/record.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
Dir[File.dirname(__FILE__) + '/concerns/record/*.rb'].each {|file| require file }
|
2
|
+
|
3
|
+
class LHS::Record
|
4
|
+
include All
|
5
|
+
include Batch
|
6
|
+
include Create
|
7
|
+
include Endpoints
|
8
|
+
include Find
|
9
|
+
include FindBy
|
10
|
+
include First
|
11
|
+
include Mapping
|
12
|
+
include Model
|
13
|
+
include Includes
|
14
|
+
include Request
|
15
|
+
include Where
|
16
|
+
|
17
|
+
def initialize(data = nil)
|
18
|
+
data = LHS::Data.new({}, nil, self.class) unless data
|
19
|
+
data = LHS::Data.new(data, nil, self.class) unless data.is_a?(LHS::Data)
|
20
|
+
define_singleton_method(:_data) { data }
|
21
|
+
if data._proxy.is_a?(LHS::Item) and data._raw.is_a?(Hash)
|
22
|
+
data._raw.each { |k, v| instance_variable_set("@#{k}", v) }
|
23
|
+
elsif data._proxy.is_a? LHS::Collection
|
24
|
+
instance_variable_set('@collection', data._collection.raw)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.build(data = nil)
|
29
|
+
self.new(data)
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def method_missing(name, *args, &block)
|
35
|
+
_data.send(name, *args, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def respond_to_missing?(name, include_all = false)
|
39
|
+
(_data.root_item? && _data._root._record_class.instance_methods.include?(name)) ||
|
40
|
+
_data._proxy.respond_to?(name, include_all)
|
41
|
+
end
|
42
|
+
end
|
data/lib/lhs/version.rb
CHANGED
@@ -29,11 +29,11 @@ describe LHS::Collection do
|
|
29
29
|
|
30
30
|
before(:each) do
|
31
31
|
LHC.config.placeholder('datastore', datastore)
|
32
|
-
class Feedback < LHS::
|
32
|
+
class Feedback < LHS::Record
|
33
33
|
endpoint ':datastore/feedbacks'
|
34
34
|
endpoint ':datastore/feedbacks/:id'
|
35
35
|
end
|
36
|
-
class User < LHS::
|
36
|
+
class User < LHS::Record
|
37
37
|
endpoint ':datastore/users'
|
38
38
|
endpoint ':datastore/users/:id'
|
39
39
|
end
|
data/spec/data/item_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Data do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
@@ -14,7 +14,7 @@ describe LHS::Data do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:data) do
|
17
|
-
LHS::Data.new(json, nil,
|
17
|
+
LHS::Data.new(json, nil, Record)
|
18
18
|
end
|
19
19
|
|
20
20
|
let(:item) do
|
@@ -41,16 +41,4 @@ describe LHS::Data do
|
|
41
41
|
expect(item.valid_from).to be_kind_of Date
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
45
|
-
context 'different date time formats' do
|
46
|
-
let(:item) do
|
47
|
-
item = data[0]
|
48
|
-
item._raw[:created_date] = '2016-07-09T13:45:00+00:00'
|
49
|
-
item
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'returns TimeWithZone if string can be parsed as date_time' do
|
53
|
-
expect(item.created_date).to be_kind_of ActiveSupport::TimeWithZone
|
54
|
-
end
|
55
|
-
end
|
56
44
|
end
|
data/spec/data/merge_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Data do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
@@ -12,14 +12,14 @@ describe LHS::Data do
|
|
12
12
|
let(:data) do
|
13
13
|
LHS::Data.new({
|
14
14
|
href: 'http://www.local.ch/v2/stuff'
|
15
|
-
}, nil,
|
15
|
+
}, nil, Record)
|
16
16
|
end
|
17
17
|
|
18
18
|
let(:loaded_data) do
|
19
19
|
LHS::Data.new({
|
20
20
|
href: 'http://www.local.ch/v2/stuff',
|
21
21
|
id: '123123123'
|
22
|
-
}, nil,
|
22
|
+
}, nil, Record)
|
23
23
|
end
|
24
24
|
|
25
25
|
context 'merging' do
|
data/spec/data/raw_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Data do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/entries/:entry_id/content-ads/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
9
9
|
endpoint ':datastore/v2/feedbacks'
|
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Data do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
map :test_mapping?, ->(item){ true }
|
8
8
|
end
|
9
9
|
end
|
@@ -11,14 +11,14 @@ describe LHS::Data do
|
|
11
11
|
context '#respond_to?' do
|
12
12
|
|
13
13
|
it 'it is true for mappings that are defined' do
|
14
|
-
data = described_class.new({'campaign' => {'id' => 123}}, nil,
|
14
|
+
data = described_class.new({'campaign' => {'id' => 123}}, nil, Record)
|
15
15
|
|
16
16
|
expect(data.respond_to?(:test_mapping?)).to be(true)
|
17
17
|
end
|
18
18
|
|
19
19
|
# proxy for this example is LHC::Collection which implements total
|
20
20
|
it 'it is true for calls forwarded to proxy' do
|
21
|
-
data = described_class.new({'items' => [{'campaign' => {'id' => 123}}]}, nil,
|
21
|
+
data = described_class.new({'items' => [{'campaign' => {'id' => 123}}]}, nil, Record)
|
22
22
|
|
23
23
|
expect(data.respond_to?(:total)).to be(true)
|
24
24
|
end
|
data/spec/data/root_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Data do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/entries/:entry_id/content-ads/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
9
9
|
endpoint ':datastore/v2/feedbacks'
|
@@ -13,7 +13,7 @@ describe LHS::Data do
|
|
13
13
|
context 'root' do
|
14
14
|
|
15
15
|
it 'is navigateable from nested data' do
|
16
|
-
root = LHS::Data.new({'items' => [{'campaign' => {'id' => 123}}]}, nil,
|
16
|
+
root = LHS::Data.new({'items' => [{'campaign' => {'id' => 123}}]}, nil, Record)
|
17
17
|
child = root.first
|
18
18
|
leafe = child.campaign
|
19
19
|
expect(leafe._root).to eq root
|
data/spec/data/to_json_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Data do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
@@ -14,7 +14,7 @@ describe LHS::Data do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:collection) do
|
17
|
-
LHS::Data.new(json, nil,
|
17
|
+
LHS::Data.new(json, nil, Record)
|
18
18
|
end
|
19
19
|
|
20
20
|
let(:item) do
|
@@ -5,7 +5,7 @@ describe LHS::Endpoint do
|
|
5
5
|
context 'for url' do
|
6
6
|
|
7
7
|
before(:each) do
|
8
|
-
class
|
8
|
+
class Record < LHS::Record
|
9
9
|
endpoint ':datastore/entries/:entry_id/content-ads/:campaign_id/feedbacks'
|
10
10
|
endpoint ':datastore/:campaign_id/feedbacks'
|
11
11
|
endpoint ':datastore/feedbacks'
|
data/spec/item/delegate_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Item do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
@@ -14,7 +14,7 @@ describe LHS::Item do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:data) do
|
17
|
-
LHS::Data.new(json, nil,
|
17
|
+
LHS::Data.new(json, nil, Record)
|
18
18
|
end
|
19
19
|
|
20
20
|
let(:item) do
|
data/spec/item/destroy_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe LHS::Item do
|
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
LHC.config.placeholder('datastore', datastore)
|
7
|
-
class
|
7
|
+
class Record < LHS::Record
|
8
8
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
9
9
|
endpoint ':datastore/v2/feedbacks'
|
10
10
|
end
|
@@ -19,7 +19,7 @@ describe LHS::Item do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
let(:data) do
|
22
|
-
LHS::Data.new(json, nil,
|
22
|
+
LHS::Data.new(json, nil, Record)
|
23
23
|
end
|
24
24
|
|
25
25
|
let(:item) do
|
@@ -43,7 +43,7 @@ describe LHS::Item do
|
|
43
43
|
|
44
44
|
context 'includes and empty response' do
|
45
45
|
before(:each) do
|
46
|
-
class
|
46
|
+
class AnotherRecord < LHS::Record
|
47
47
|
endpoint ':datastore/v2/:campaign_id/restaurants'
|
48
48
|
end
|
49
49
|
end
|
@@ -56,7 +56,7 @@ describe LHS::Item do
|
|
56
56
|
.to_return(status: 200, body: data.to_json)
|
57
57
|
stub_request(:get, "#{datastore}/v2/restaurants/1")
|
58
58
|
.to_return(status: 200, body: {name: 'Casa Ferlin'}.to_json)
|
59
|
-
item =
|
59
|
+
item = Record.includes(:restaurant).find(1)
|
60
60
|
item.destroy
|
61
61
|
end
|
62
62
|
end
|
data/spec/item/errors_spec.rb
CHANGED
@@ -42,7 +42,7 @@ describe LHS::Item do
|
|
42
42
|
|
43
43
|
before(:each) do
|
44
44
|
LHC.config.placeholder(:datastore, datastore)
|
45
|
-
class
|
45
|
+
class Record < LHS::Record
|
46
46
|
endpoint ':datastore/:campaign_id/feedbacks'
|
47
47
|
endpoint ':datastore/feedbacks'
|
48
48
|
end
|
@@ -53,7 +53,7 @@ describe LHS::Item do
|
|
53
53
|
it 'parses fields correctly when creation failed' do
|
54
54
|
stub_request(:post, "#{datastore}/feedbacks")
|
55
55
|
.to_return(status: 400, body: error_format_fields.to_json)
|
56
|
-
record =
|
56
|
+
record = Record.build
|
57
57
|
record.name = 'Steve'
|
58
58
|
result = record.save
|
59
59
|
expect(result).to eq false
|
@@ -68,7 +68,7 @@ describe LHS::Item do
|
|
68
68
|
it 'parses field errors correctly when creation failed' do
|
69
69
|
stub_request(:post, "#{datastore}/feedbacks")
|
70
70
|
.to_return(status: 400, body: error_format_field_errors.to_json)
|
71
|
-
record =
|
71
|
+
record = Record.build
|
72
72
|
record.name = 'Steve'
|
73
73
|
result = record.save
|
74
74
|
expect(result).to eq false
|
@@ -85,7 +85,7 @@ describe LHS::Item do
|
|
85
85
|
it 'provides access to raw error data' do
|
86
86
|
stub_request(:post, "#{datastore}/feedbacks")
|
87
87
|
.to_return(status: 400, body: error_format_field_errors.to_json)
|
88
|
-
record =
|
88
|
+
record = Record.build
|
89
89
|
record.save
|
90
90
|
expect(record.errors.raw).to be
|
91
91
|
json = JSON.parse(record.errors.raw)
|
data/spec/item/getter_spec.rb
CHANGED
@@ -3,14 +3,14 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Item do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
let(:data) do
|
13
|
-
LHS::Data.new({addresses: [{business: {identities: [{name: 'Löwenzorn'}]}}]}, nil,
|
13
|
+
LHS::Data.new({addresses: [{business: {identities: [{name: 'Löwenzorn'}]}}]}, nil, Record)
|
14
14
|
end
|
15
15
|
|
16
16
|
context 'item getter' do
|
data/spec/item/save_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Item do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
@@ -14,7 +14,7 @@ describe LHS::Item do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:data) do
|
17
|
-
LHS::Data.new(json, nil,
|
17
|
+
LHS::Data.new(json, nil, Record)
|
18
18
|
end
|
19
19
|
|
20
20
|
let(:item) do
|
@@ -24,8 +24,7 @@ describe LHS::Item do
|
|
24
24
|
context 'save' do
|
25
25
|
|
26
26
|
it 'persists changes on the backend' do
|
27
|
-
stub_request(:post, item.href)
|
28
|
-
.with(body: item._raw.merge(name: 'Steve').to_json)
|
27
|
+
stub_request(:post, item.href).with(body: item._raw.merge(name: 'Steve').to_json)
|
29
28
|
item.name = 'Steve'
|
30
29
|
expect(item.save).to eq true
|
31
30
|
end
|
data/spec/item/setter_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Item do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
@@ -14,7 +14,7 @@ describe LHS::Item do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:data) do
|
17
|
-
LHS::Data.new(json, nil,
|
17
|
+
LHS::Data.new(json, nil, Record)
|
18
18
|
end
|
19
19
|
|
20
20
|
let(:item) do
|
data/spec/item/update_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|
3
3
|
describe LHS::Item do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
class
|
6
|
+
class Record < LHS::Record
|
7
7
|
endpoint ':datastore/v2/:campaign_id/feedbacks'
|
8
8
|
endpoint ':datastore/v2/feedbacks'
|
9
9
|
end
|
@@ -14,7 +14,7 @@ describe LHS::Item do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:data) do
|
17
|
-
LHS::Data.new(json, nil,
|
17
|
+
LHS::Data.new(json, nil, Record)
|
18
18
|
end
|
19
19
|
|
20
20
|
let(:item) do
|
@@ -4,112 +4,84 @@ describe LHS::Item do
|
|
4
4
|
|
5
5
|
let(:datastore) { 'http://local.ch' }
|
6
6
|
|
7
|
-
|
7
|
+
before(:each) do
|
8
|
+
LHC.config.placeholder('datastore', datastore)
|
9
|
+
class User < LHS::Record
|
10
|
+
endpoint ':datastore/v2/users', validates: true
|
11
|
+
end
|
12
|
+
mock_validation
|
13
|
+
end
|
8
14
|
|
9
15
|
let(:mock_validation) do
|
10
16
|
successful_validation
|
11
17
|
end
|
12
18
|
|
13
19
|
let(:successful_validation) do
|
14
|
-
stub_request(:post, "#{datastore}/v2/users
|
20
|
+
stub_request(:post, "#{datastore}/v2/users?persist=false").to_return(body: '{}')
|
15
21
|
end
|
16
22
|
|
17
|
-
|
18
|
-
|
23
|
+
let(:failing_validation) do
|
24
|
+
stub_request(:post, "#{datastore}/v2/users?persist=false")
|
25
|
+
.to_return(status: 400,
|
26
|
+
body: {
|
27
|
+
field_errors: [{code: "UNSUPPORTED_PROPERTY_VALUE", "path" => [ "email" ]}]
|
28
|
+
}.to_json
|
29
|
+
)
|
19
30
|
end
|
20
31
|
|
21
|
-
context '
|
22
|
-
|
23
|
-
let(:validation_parameter) { 'publish' }
|
24
|
-
|
32
|
+
context 'valid data' do
|
25
33
|
let(:user) do
|
26
34
|
User.build(email: 'steve@local.ch')
|
27
35
|
end
|
28
36
|
|
29
|
-
before(:each) do
|
30
|
-
LHC.config.placeholder('datastore', datastore)
|
31
|
-
class User < LHS::Service
|
32
|
-
endpoint ':datastore/v2/users', validates: 'publish'
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
37
|
it 'validates' do
|
37
38
|
expect(user.valid?).to eq true
|
38
39
|
end
|
39
40
|
|
41
|
+
it 'turns to be invalid if validating on changed, invalid data' do
|
42
|
+
expect(user.valid?).to eq true
|
43
|
+
user.email = 'not a valid email'
|
44
|
+
failing_validation
|
45
|
+
expect(user.valid?).to eq false
|
46
|
+
expect(user.errors[:email]).to be
|
47
|
+
end
|
40
48
|
end
|
41
49
|
|
42
|
-
context '
|
50
|
+
context 'invalid data' do
|
43
51
|
|
44
|
-
|
45
|
-
|
46
|
-
class User < LHS::Service
|
47
|
-
endpoint ':datastore/v2/users', validates: true
|
48
|
-
end
|
52
|
+
let(:user) do
|
53
|
+
User.build(email: 'im not an email address')
|
49
54
|
end
|
50
55
|
|
51
|
-
let(:
|
52
|
-
|
53
|
-
.to_return(status: 400,
|
54
|
-
body: {
|
55
|
-
field_errors: [{ code: "UNSUPPORTED_PROPERTY_VALUE", "path" => ["email"] }]
|
56
|
-
}.to_json
|
57
|
-
)
|
56
|
+
let(:mock_validation) do
|
57
|
+
failing_validation
|
58
58
|
end
|
59
59
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'validates' do
|
66
|
-
expect(user.valid?).to eq true
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'turns to be invalid if validating on changed, invalid data' do
|
70
|
-
expect(user.valid?).to eq true
|
71
|
-
user.email = 'not a valid email'
|
72
|
-
failing_validation
|
73
|
-
expect(user.valid?).to eq false
|
74
|
-
expect(user.errors[:email]).to be
|
75
|
-
end
|
60
|
+
it 'does not validate and provides error messages' do
|
61
|
+
expect(user.valid?).to eq false
|
62
|
+
expect(user.errors[:email]).to be
|
76
63
|
end
|
77
64
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
failing_validation
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'does not validate and provides error messages' do
|
88
|
-
expect(user.valid?).to eq false
|
89
|
-
expect(user.errors[:email]).to be
|
90
|
-
end
|
91
|
-
|
92
|
-
it 'resets errors when revalidating' do
|
93
|
-
expect(user.valid?).to eq false
|
94
|
-
user.email = 'steve@local.ch'
|
95
|
-
successful_validation
|
96
|
-
expect(user.valid?).to eq true
|
97
|
-
expect(user.errors).to be_nil
|
98
|
-
end
|
65
|
+
it 'resets errors when revalidating' do
|
66
|
+
expect(user.valid?).to eq false
|
67
|
+
user.email = 'steve@local.ch'
|
68
|
+
successful_validation
|
69
|
+
expect(user.valid?).to eq true
|
70
|
+
expect(user.errors).to be_nil
|
99
71
|
end
|
72
|
+
end
|
100
73
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
74
|
+
context 'endpoint does not support validations' do
|
75
|
+
before(:each) do
|
76
|
+
class Favorite < LHS::Record
|
77
|
+
endpoint ':datastore/v2/favorites'
|
106
78
|
end
|
79
|
+
end
|
107
80
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
end
|
81
|
+
it 'fails when trying to use an endpoint for validations that does not support it' do
|
82
|
+
expect(->{
|
83
|
+
Favorite.build.valid?
|
84
|
+
}).to raise_error('Endpoint does not support validations!')
|
113
85
|
end
|
114
86
|
end
|
115
87
|
end
|