• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-ruby / #5867

18 Apr 2024 09:24PM UTC coverage: 92.724% (-4.8%) from 97.485%
#5867

push

github

ramya-stripe
Bump version to 11.2.0

10067 of 10857 relevant lines covered (92.72%)

258.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.85
/test/stripe/list_object_test.rb
1
# frozen_string_literal: true
2

3
require File.expand_path("../test_helper", __dir__)
1✔
4

5
module Stripe
1✔
6
  class ListObjectTest < Test::Unit::TestCase
1✔
7
    should "provide .empty_list" do
1✔
8
      list = Stripe::ListObject.empty_list
1✔
9
      assert list.empty?
1✔
10
    end
11

12
    should "provide #count via enumerable" do
1✔
13
      list = Stripe::ListObject.construct_from(data: [{ object: "charge" }])
1✔
14
      assert_equal 1, list.count
1✔
15
    end
16

17
    should "provide #each" do
1✔
18
      arr = [
1✔
19
        { id: 1 },
20
        { id: 2 },
21
        { id: 3 },
22
      ]
23
      expected = Util.convert_to_stripe_object(arr, {})
1✔
24
      list = Stripe::ListObject.construct_from(data: arr)
1✔
25
      assert_equal expected, list.each.to_a
1✔
26
    end
27

28
    should "provide #reverse_each" do
1✔
29
      arr = [
1✔
30
        { id: 1 },
31
        { id: 2 },
32
        { id: 3 },
33
      ]
34
      expected = Util.convert_to_stripe_object(arr.reverse, {})
1✔
35
      list = Stripe::ListObject.construct_from(data: arr)
1✔
36
      assert_equal expected, list.reverse_each.to_a
1✔
37
    end
38

39
    should "provide #auto_paging_each that supports forward pagination" do
1✔
40
      arr = [
1✔
41
        { id: 1 },
42
        { id: 2 },
43
        { id: 3 },
44
        { id: 4 },
45
        { id: 5 },
46
        { id: 6 },
47
      ]
48
      expected = Util.convert_to_stripe_object(arr, {})
1✔
49

50
      # Initial list object to page on. Notably, its last data element will be
51
      # used as a cursor to fetch the next page.
52
      list = TestListObject.construct_from(data: [{ id: 1 }],
1✔
53
                                           has_more: true,
54
                                           url: "/things",
55
                                           object: "list")
56
      list.filters = { limit: 3 }
1✔
57

58
      # The test will start with the synthetic list object above, and use it as
59
      # a starting point to fetch two more pages. The second page indicates
60
      # that there are no more elements by setting `has_more` to `false`, and
61
      # iteration stops.
62
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
63
        .with(query: { starting_after: "1", limit: "3" })
×
64
        .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }, { id: 4 }], has_more: true, url: "/things",
×
65
                                       object: "list"))
66
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
67
        .with(query: { starting_after: "4", limit: "3" })
×
68
        .to_return(body: JSON.generate(data: [{ id: 5 }, { id: 6 }], has_more: false, url: "/things",
×
69
                                       object: "list"))
70

71
      assert_equal expected, list.auto_paging_each.to_a
1✔
72
    end
73

74
    should "provide #auto_paging_each that supports backward pagination with `ending_before`" do
1✔
75
      arr = [
1✔
76
        { id: 6 },
77
        { id: 5 },
78
        { id: 4 },
79
        { id: 3 },
80
        { id: 2 },
81
        { id: 1 },
82
      ]
83
      expected = Util.convert_to_stripe_object(arr, {})
1✔
84

85
      # Initial list object to page on. Notably, its first data element will be
86
      # used as a cursor to fetch the next page.
87
      list = TestListObject.construct_from(data: [{ id: 6 }],
1✔
88
                                           has_more: true,
89
                                           url: "/things",
90
                                           object: "list")
91

92
      # We also add an `ending_before` filter on the list to simulate backwards
93
      # pagination.
94
      list.filters = { ending_before: 7, limit: 3 }
1✔
95

96
      # The test will start with the synthetic list object above, and use it as
97
      # a starting point to fetch two more pages. The second page indicates
98
      # that there are no more elements by setting `has_more` to `false`, and
99
      # iteration stops.
100
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
101
        .with(query: { ending_before: "6", limit: "3" })
×
102
        .to_return(body: JSON.generate(data: [{ id: 3 }, { id: 4 }, { id: 5 }], has_more: true, url: "/things",
×
103
                                       object: "list"))
104
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
105
        .with(query: { ending_before: "3", limit: "3" })
×
106
        .to_return(body: JSON.generate(data: [{ id: 1 }, { id: 2 }], has_more: false, url: "/things",
×
107
                                       object: "list"))
108

109
      assert_equal expected, list.auto_paging_each.to_a
1✔
110
    end
111

112
    should "provide #auto_paging_each that responds to a block" do
1✔
113
      arr = [
1✔
114
        { id: 1 },
115
        { id: 2 },
116
        { id: 3 },
117
      ]
118
      expected = Util.convert_to_stripe_object(arr, {})
1✔
119

120
      list = TestListObject.construct_from(data: [{ id: 1 }],
1✔
121
                                           has_more: true,
122
                                           url: "/things",
123
                                           object: "list")
124
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
125
        .with(query: { starting_after: "1" })
×
126
        .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }], has_more: false, object: "list"))
×
127

128
      actual = []
1✔
129
      list.auto_paging_each do |obj|
1✔
130
        actual << obj
3✔
131
      end
132

133
      assert_equal expected, actual
1✔
134
    end
135

136
    should "provide #empty?" do
1✔
137
      list = Stripe::ListObject.construct_from(data: [])
1✔
138
      assert list.empty?
1✔
139
      list = Stripe::ListObject.construct_from(data: [{}])
1✔
140
      refute list.empty?
1✔
141
    end
142

143
    #
144
    # next_page
145
    #
146

147
    should "fetch a next page through #next_page" do
1✔
148
      list = TestListObject.construct_from(data: [{ id: 1 }],
1✔
149
                                           has_more: true,
150
                                           url: "/things",
151
                                           object: "list")
152
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
153
        .with(query: { starting_after: "1" })
×
154
        .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false, object: "list"))
×
155
      next_list = list.next_page
1✔
156
      refute next_list.empty?
1✔
157
    end
158

159
    should "fetch a next page through #next_page and respect limit" do
1✔
160
      list = TestListObject.construct_from(data: [{ id: 1 }],
1✔
161
                                           has_more: true,
162
                                           url: "/things",
163
                                           object: "list")
164
      list.filters = { expand: ["data.source"], limit: 3 }
1✔
165
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
166
        .with(query: { "expand[]" => "data.source", "limit" => "3", "starting_after" => "1" })
×
167
        .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false, object: "list"))
×
168
      next_list = list.next_page
1✔
169
      assert_equal({ expand: ["data.source"], limit: 3, starting_after: 1 }, next_list.filters)
1✔
170
    end
171

172
    should "fetch an empty page through #next_page" do
1✔
173
      list = TestListObject.construct_from(data: [{ id: 1 }],
1✔
174
                                           has_more: false,
175
                                           url: "/things",
176
                                           object: "list")
177
      next_list = list.next_page
1✔
178
      assert_equal Stripe::ListObject.empty_list, next_list
1✔
179
    end
180

181
    #
182
    # previous_page
183
    #
184

185
    should "fetch a next page through #previous_page" do
1✔
186
      list = TestListObject.construct_from(data: [{ id: 2 }],
1✔
187
                                           has_more: true,
188
                                           url: "/things",
189
                                           object: "list")
190
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
191
        .with(query: { ending_before: "2" })
×
192
        .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false, object: "list"))
×
193
      next_list = list.previous_page
1✔
194
      refute next_list.empty?
1✔
195
    end
196

197
    should "fetch a next page through #previous_page and respect limit" do
1✔
198
      list = TestListObject.construct_from(data: [{ id: 2 }],
1✔
199
                                           has_more: true,
200
                                           url: "/things",
201
                                           object: "list")
202
      list.filters = { expand: ["data.source"], limit: 3 }
1✔
203
      stub_request(:get, "#{Stripe.api_base}/things")
1✔
204
        .with(query: { "expand[]" => "data.source", "limit" => "3", "ending_before" => "2" })
×
205
        .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false, object: "list"))
×
206
      next_list = list.previous_page
1✔
207
      assert_equal({ ending_before: 2, expand: ["data.source"], limit: 3 }, next_list.filters)
1✔
208
    end
209
  end
210
end
211

212
# A helper class with a URL that allows us to try out pagination.
213
class TestListObject < Stripe::ListObject
1✔
214
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc