lhs 20.1.4 → 21.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -4
- data/lib/lhs/rspec.rb +1 -10
- data/lib/lhs/test/stubbable_records.rb +34 -0
- data/lib/lhs/version.rb +1 -1
- data/spec/stubs/all_spec.rb +25 -2
- metadata +2 -2
- data/lib/lhs/test/stub.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80228cca0e4b14a7a21cf408db24114f85648afccc58fbc658fbe7ef579b69a9
|
4
|
+
data.tar.gz: 774070abf3a426ef7f68c9ee707c540f53c91499f8576220fcae2e610baca1bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33f14c2fe5cd5e6cc9a8bdbee060d247bc3f62fe9a3dfde38ff87aadb4370de371996455a4ea55865c2ee13aa7b3728e405c73ad7185e29ec48a8f2a657b750d
|
7
|
+
data.tar.gz: 8aebc91f90fe095c4dc7d0d10dd8c389c0b1935928299421fce9df6c6245caa69a4991ce9f9a73758b5e316ea871a4437b7128243413634e4e7949ce96ac9405
|
data/README.md
CHANGED
@@ -2537,17 +2537,21 @@ It will initialize a MemoryStore cache for LHC::Caching interceptor and resets t
|
|
2537
2537
|
|
2538
2538
|
#### Stub
|
2539
2539
|
|
2540
|
-
LHS offers stub helpers that simplify stubbing https request to your apis.
|
2540
|
+
LHS offers stub helpers that simplify stubbing https request to your apis through your defined Records.
|
2541
2541
|
|
2542
|
-
#####
|
2542
|
+
##### stub_all
|
2543
2543
|
|
2544
|
-
`
|
2544
|
+
`Record.stub_all(url, items, additional_options)`
|
2545
2545
|
|
2546
2546
|
```ruby
|
2547
2547
|
# your_spec.rb
|
2548
2548
|
|
2549
2549
|
before do
|
2550
|
-
LHS
|
2550
|
+
class Record < LHS::Record
|
2551
|
+
endpoint 'https://records'
|
2552
|
+
end
|
2553
|
+
|
2554
|
+
Record.stub_all(
|
2551
2555
|
'https://records',
|
2552
2556
|
200.times.map{ |index| { name: "Item #{index}" } },
|
2553
2557
|
headers: {
|
@@ -2561,6 +2565,28 @@ GET https://records?limit=100
|
|
2561
2565
|
GET https://records?limit=100&offset=100
|
2562
2566
|
```
|
2563
2567
|
|
2568
|
+
LHS also uses Record configuration when stubbing all.
|
2569
|
+
```ruby
|
2570
|
+
# your_spec.rb
|
2571
|
+
|
2572
|
+
before do
|
2573
|
+
class Record < LHS::Record
|
2574
|
+
configuration limit_key: :per_page, pagination_strategy: :page, pagination_key: :page
|
2575
|
+
|
2576
|
+
endpoint 'https://records'
|
2577
|
+
end
|
2578
|
+
|
2579
|
+
Record.stub_all(
|
2580
|
+
'https://records',
|
2581
|
+
200.times.map{ |index| { name: "Item #{index}" } }
|
2582
|
+
)
|
2583
|
+
end
|
2584
|
+
```
|
2585
|
+
```
|
2586
|
+
GET https://records?per_page=100
|
2587
|
+
GET https://records?per_page=100&page=2
|
2588
|
+
```
|
2589
|
+
|
2564
2590
|
### Test query chains
|
2565
2591
|
|
2566
2592
|
#### By explicitly resolving the chain: fetch
|
data/lib/lhs/rspec.rb
CHANGED
@@ -1,19 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'lhs'
|
4
|
-
require 'lhs/test/
|
4
|
+
require 'lhs/test/stubbable_records'
|
5
5
|
|
6
6
|
RSpec.configure do |config|
|
7
7
|
config.before(:each) do
|
8
8
|
LHS.config.request_cycle_cache.clear
|
9
9
|
end
|
10
|
-
|
11
|
-
config.before(:all) do
|
12
|
-
|
13
|
-
module LHS
|
14
|
-
def self.stub
|
15
|
-
LHS::Test::Stub
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
10
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'local_uri'
|
4
|
+
require 'webmock'
|
5
|
+
|
6
|
+
class LHS::Record
|
7
|
+
DEFAULT_LIMIT = LHS::Pagination::Base::DEFAULT_LIMIT
|
8
|
+
|
9
|
+
def self.stub_all(url, items, options = {})
|
10
|
+
extend WebMock::API
|
11
|
+
|
12
|
+
items.each_slice(DEFAULT_LIMIT).with_index do |(*batch), index|
|
13
|
+
uri = LocalUri::URI.new(url)
|
14
|
+
uri.query.merge!(
|
15
|
+
limit_key(:parameter) => DEFAULT_LIMIT
|
16
|
+
)
|
17
|
+
offset = pagination_class.page_to_offset(index + 1, DEFAULT_LIMIT)
|
18
|
+
unless index.zero?
|
19
|
+
uri.query.merge!(
|
20
|
+
pagination_key(:parameter) => offset
|
21
|
+
)
|
22
|
+
end
|
23
|
+
request_stub = stub_request(:get, uri.to_s)
|
24
|
+
request_stub.with(options) if options.present?
|
25
|
+
request_stub.to_return(
|
26
|
+
body: {
|
27
|
+
items: batch,
|
28
|
+
offset: index.zero? ? 0 : offset,
|
29
|
+
total: items.length
|
30
|
+
}.to_json
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/lhs/version.rb
CHANGED
data/spec/stubs/all_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe LHS do
|
|
10
10
|
endpoint 'https://records'
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
Record.stub_all(
|
14
14
|
'https://records',
|
15
15
|
200.times.map { |index| { name: "Item #{index}" } },
|
16
16
|
headers: {
|
@@ -33,7 +33,7 @@ describe LHS do
|
|
33
33
|
endpoint 'https://records'
|
34
34
|
end
|
35
35
|
|
36
|
-
|
36
|
+
Record.stub_all(
|
37
37
|
'https://records',
|
38
38
|
200.times.map { |index| { name: "Item #{index}" } }
|
39
39
|
)
|
@@ -46,4 +46,27 @@ describe LHS do
|
|
46
46
|
expect(records.first.name).to eq 'Item 0'
|
47
47
|
end
|
48
48
|
end
|
49
|
+
|
50
|
+
context 'with configured record' do
|
51
|
+
|
52
|
+
before do
|
53
|
+
class Record < LHS::Record
|
54
|
+
configuration limit_key: :per_page, pagination_strategy: :page, pagination_key: :page
|
55
|
+
|
56
|
+
endpoint 'https://records'
|
57
|
+
end
|
58
|
+
|
59
|
+
Record.stub_all(
|
60
|
+
'https://records',
|
61
|
+
200.times.map { |index| { name: "Item #{index}" } }
|
62
|
+
)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'stubs all requests with record configurations for pagination' do
|
66
|
+
records = Record.all.fetch
|
67
|
+
expect(records.count).to eq 200
|
68
|
+
expect(records.length).to eq 200
|
69
|
+
expect(records.first.name).to eq 'Item 0'
|
70
|
+
end
|
71
|
+
end
|
49
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 21.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhs/graphs/contributors
|
@@ -329,7 +329,7 @@ files:
|
|
329
329
|
- lib/lhs/railtie.rb
|
330
330
|
- lib/lhs/record.rb
|
331
331
|
- lib/lhs/rspec.rb
|
332
|
-
- lib/lhs/test/
|
332
|
+
- lib/lhs/test/stubbable_records.rb
|
333
333
|
- lib/lhs/unprocessable.rb
|
334
334
|
- lib/lhs/version.rb
|
335
335
|
- script/ci/build.sh
|
data/lib/lhs/test/stub.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'local_uri'
|
4
|
-
require 'webmock'
|
5
|
-
module LHS
|
6
|
-
module Test
|
7
|
-
class Stub
|
8
|
-
extend WebMock::API
|
9
|
-
DEFAULT_LIMIT = LHS::Pagination::Base::DEFAULT_LIMIT
|
10
|
-
|
11
|
-
def self.all(url, items, options = {})
|
12
|
-
items.each_slice(DEFAULT_LIMIT).with_index do |(*batch), index|
|
13
|
-
uri = LocalUri::URI.new(url)
|
14
|
-
uri.query.merge!(limit: DEFAULT_LIMIT)
|
15
|
-
uri.query.merge!(offset: DEFAULT_LIMIT * index) unless index.zero?
|
16
|
-
request_stub = stub_request(:get, uri.to_s)
|
17
|
-
request_stub.with(options) if options.present?
|
18
|
-
request_stub.to_return(
|
19
|
-
body: {
|
20
|
-
items: batch,
|
21
|
-
offset: index * DEFAULT_LIMIT,
|
22
|
-
total: items.length
|
23
|
-
}.to_json
|
24
|
-
)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|