Skip to content

Commit

Permalink
Merge pull request #777 from ingoncalves/adds-price-filters
Browse files Browse the repository at this point in the history
Allows filtering prices by currency and product
  • Loading branch information
alexmamonchik committed Oct 24, 2023
2 parents 247f864 + c9d6f38 commit b5d3be7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/stripe_mock/request_handlers/prices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ def list_prices(route, method_url, params, headers)
end
end

if params.key?(:currency)
price_data.select! do |price|
params[:currency] == price[:currency]
end
end

if params.key?(:product)
price_data.select! do |price|
params[:product] == price[:product]
end
end

Data.mock_list_object(price_data.first(limit), params.merge!(limit: limit))
end
end
Expand Down
44 changes: 42 additions & 2 deletions spec/shared_stripe_examples/price_examples.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'spec_helper'

shared_examples 'Price API' do
let(:product) { stripe_helper.create_product }
let(:product_id) { product.id }
let(:product_id) { "product_id_1" }
let(:product) { stripe_helper.create_product(id: product_id) }

let(:other_product_id) { "product_id_2" }
let(:other_product) { stripe_helper.create_product(id: other_product_id) }

let(:price_attributes) { {
:id => "price_abc123",
Expand All @@ -26,6 +29,7 @@

before(:each) do
product
other_product
end

it "creates a stripe price" do
Expand Down Expand Up @@ -125,6 +129,42 @@
expect(two.map &:amount).to include(98765)
end

it "retrieves prices filtering by currency" do
5.times do | i|
stripe_helper.create_price(id: "usd price #{i}", product: product_id, amount: 11, currency: 'usd')
stripe_helper.create_price(id: "brl price #{i}", product: product_id, amount: 11, currency: 'brl')
end

all = Stripe::Price.list()
expect(all.count).to eq(10)

usd = Stripe::Price.list({currency: 'usd'})
expect(usd.count).to eq(5)
expect(usd.all? {|p| p.currency == 'usd' }).to be_truthy

brl = Stripe::Price.list({currency: 'brl'})
expect(brl.count).to eq(5)
expect(brl.all? {|p| p.currency == 'brl' }).to be_truthy
end

it "retrieves prices filtering by product" do
5.times do | i|
stripe_helper.create_price(id: "product 1 price #{i}", product: product_id)
stripe_helper.create_price(id: "product 2 price #{i}", product: other_product_id)
end

all = Stripe::Price.list()
expect(all.count).to eq(10)

product_prices = Stripe::Price.list({product: product.id})
expect(product_prices.count).to eq(5)
expect(product_prices.all? {|p| p.product == product.id }).to be_truthy

other_product_prices = Stripe::Price.list({product: other_product.id})
expect(other_product_prices.count).to eq(5)
expect(other_product_prices.all? {|p| p.product == other_product.id }).to be_truthy
end

describe "Validations", :live => true do
include_context "stripe validator"
let(:params) { stripe_helper.create_price_params(product: product_id) }
Expand Down

0 comments on commit b5d3be7

Please sign in to comment.