Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex-stripe committed Sep 12, 2018
1 parent a88881c commit 4f410af
Showing 1 changed file with 71 additions and 70 deletions.
141 changes: 71 additions & 70 deletions test/autoPagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,32 @@ var stripe = require('../lib/stripe')(

var expect = require('chai').expect;

// Filter on older items to reduce race conditions in CI with other tests.
var CREATED = {lt: Math.round(new Date(2018, 1, 1) / 1000)};
var LIMIT = 7;
var PRODUCT_URL = 'https://example.com/' + Math.random();
var TOTAL_OBJECTS = 8;

describe('auto pagination', function() {
this.timeout(20000);

var email = 'test.' + Date.now() + '@example.com';
var realCustomerIds;
var realProductIds;
before(function() {
return new Promise(function(resolve) {
// Fetch customers
Promise.all([
stripe.customers.list({limit: LIMIT, created: CREATED}).then(function(customers) {
realCustomerIds = customers.data.map(function(item) {
return item.id;
});
}),
// Setup products (maybe only do products instead of customers)?
Promise.all([
stripe.products.create({name: 'test' + Math.random(), type: 'good', url: PRODUCT_URL}),
stripe.products.create({name: 'test' + Math.random(), type: 'good', url: PRODUCT_URL}),
stripe.products.create({name: 'test' + Math.random(), type: 'good', url: PRODUCT_URL}),
stripe.products.create({name: 'test' + Math.random(), type: 'good', url: PRODUCT_URL}),
]).then(function() {
// re-fetch to ensure correct order
return stripe.products.list({type: 'good', url: PRODUCT_URL}).then(function(products) {
realProductIds = products.data.map(function(product) {return product.id});
})
}),
]).then(resolve);
var createReqs = [];
for (var i = 0; i < TOTAL_OBJECTS; i++) {
createReqs.push(stripe.customers.create({email: email}));
}
return Promise.all(createReqs).then(function() {
// re-fetch to ensure correct order
return stripe.customers.list({email: email}).then(function(customers) {
realCustomerIds = customers.data.map(function(customer) {return customer.id});
})
}).then(resolve);
});
});

after(function() {
realProductIds.forEach(function(productId) {
stripe.products.del(productId);
realCustomerIds.forEach(function(customerId) {
stripe.customers.del(customerId);
});
});

Expand All @@ -71,8 +59,8 @@ describe('auto pagination', function() {
}
}

stripe.customers.list({limit: 3, created: CREATED}).autoPagingEach(onCustomer, onDone);
})).to.eventually.deep.equal(realCustomerIds);
stripe.customers.list({limit: 3, email: email}).autoPagingEach(onCustomer, onDone);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});

it('lets you ignore the second arg and `return false` to break', function() {
Expand All @@ -92,8 +80,8 @@ describe('auto pagination', function() {
}
}

stripe.customers.list({limit: 3, created: CREATED}).autoPagingEach(onCustomer, onDone);
})).to.eventually.deep.equal(realCustomerIds);
stripe.customers.list({limit: 3, email: email}).autoPagingEach(onCustomer, onDone);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});

it('can use a promise instead of a callback for onDone', function() {
Expand All @@ -109,58 +97,71 @@ describe('auto pagination', function() {
resolve(customerIds);
}

stripe.customers.list({limit: 3, created: CREATED}).autoPagingEach(onCustomer).then(onDone).catch(reject);
})).to.eventually.deep.equal(realCustomerIds);
stripe.customers.list({limit: 3, email: email}).autoPagingEach(onCustomer).then(onDone).catch(reject);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});

it('handles the end of a list properly when the last page is full', function() {
return expect(new Promise(function(resolve, reject) {
var productIds = [];
return stripe.products.list({url: PRODUCT_URL, limit: 2})
.autoPagingEach(function(product) { productIds.push(product.id); })
var customerIds = [];
return stripe.customers.list({email: email, limit: TOTAL_OBJECTS / 2})
.autoPagingEach(function(customer) { customerIds.push(customer.id); })
.catch(reject)
.then(function() {
resolve(productIds);
})
})).to.eventually.deep.equal(realProductIds);
resolve(customerIds);
});
})).to.eventually.deep.equal(realCustomerIds);
});

it('handles the end of a list properly when the last page is not full', function() {
return expect(new Promise(function(resolve, reject) {
var productIds = [];
return stripe.products.list({url: PRODUCT_URL, limit: 3})
.autoPagingEach(function(product) { productIds.push(product.id); })
var customerIds = [];
return stripe.customers.list({email: email, limit: TOTAL_OBJECTS - 2})
.autoPagingEach(function(customer) { customerIds.push(customer.id); })
.catch(reject)
.then(function() {
resolve(productIds);
resolve(customerIds);
});
})).to.eventually.deep.equal(realProductIds);
})).to.eventually.deep.equal(realCustomerIds);
});

it('handles 500s after the first page correctly (callback)', function() {
it('handles errors after the first page correctly (callback)', function() {
return expect(new Promise(function(resolve, reject) {

// HACK: relying on a current bug in the stripe api where if you provide `?ids=[...]&starting_after=...`, it blows up.
return stripe.products.list({url: PRODUCT_URL, ids: realProductIds, limit: 3})
.autoPagingEach(function() {}, function(err) {
var i = 0;
function onCustomer() {
i += 1;
if (i > 4) {
throw Error('Simulated error');
}
}
return stripe.customers.list({email: email, limit: 3})
.autoPagingEach(onCustomer, function(err) {
if (err) {
resolve(err.message);
} else {
reject(Error('Expected an error, did not get one.'));
}
});
})).to.eventually.deep.equal('An unknown error occurred');
})).to.eventually.deep.equal('Simulated error');
});

it('handles 500s after the first page correctly (promise)', function() {
it('handles errors after the first page correctly (promise)', function() {
return expect(new Promise(function(resolve, reject) {
// HACK: relying on a current bug in the stripe api where if you provide `?ids=[...]&starting_after=...`, it blows up.
return stripe.products.list({url: PRODUCT_URL, ids: realProductIds, limit: 3}).autoPagingEach(function() {})
var i = 0;
function onCustomer() {
i += 1;
if (i > 4) {
throw Error('Simulated error');
}
}
return stripe.customers.list({email: email, limit: 3}).autoPagingEach(onCustomer)
.then(function() {
reject(Error('Expected an error, did not get one.'));
})
.catch(function(err) {
resolve(err.message);
});
})).to.eventually.deep.equal('An unknown error occurred');
})).to.eventually.deep.equal('Simulated error');
});
});

Expand All @@ -173,10 +174,10 @@ describe('auto pagination', function() {

it('works with `for await` when that feature exists', function() {
return expect(new Promise(function(resolve, reject) {
forAwaitUntil(stripe.customers.list({limit: 3, created: CREATED}).autoPagingEach(), LIMIT).then(function(customers) {
forAwaitUntil(stripe.customers.list({limit: 3, email: email}).autoPagingEach(), LIMIT).then(function(customers) {
resolve(customers.map(function(customer) { return customer.id; }));
});
})).to.eventually.deep.equal(realCustomerIds);
}).catch(reject);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});
}

Expand All @@ -186,16 +187,16 @@ describe('auto pagination', function() {

it('works with `await` and a while loop when await exists', function() {
return expect(new Promise(function(resolve, reject) {
awaitUntil(stripe.customers.list({limit: 3, created: CREATED}).autoPagingEach(), LIMIT).then(function(customers) {
awaitUntil(stripe.customers.list({limit: 3, email: email}).autoPagingEach(), LIMIT).then(function(customers) {
resolve(customers.map(function(customer) { return customer.id; }));
});
})).to.eventually.deep.equal(realCustomerIds);
}).catch(reject);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});
}

it('works when you call it sequentially', function() {
return expect(new Promise(function(resolve, reject) {
var iter = stripe.customers.list({limit: 3, created: CREATED}).autoPagingEach();
var iter = stripe.customers.list({limit: 3, email: email}).autoPagingEach();

var customerIds = [];
function handleIter(result) {
Expand All @@ -206,13 +207,13 @@ describe('auto pagination', function() {
}
iter.next().then(handleIter).then(function() {
resolve(customerIds);
});
})).to.eventually.deep.equal(realCustomerIds);
}).catch(reject);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});

it('gives you the same result each time when you call it multiple times in parallel', function() {
return expect(new Promise(function(resolve, reject) {
var iter = stripe.customers.list({limit: 3, created: CREATED}).autoPagingEach();
var iter = stripe.customers.list({limit: 3, email: email}).autoPagingEach();

var customerIds = []
function handleIter(result) {
Expand All @@ -239,7 +240,7 @@ describe('auto pagination', function() {
})
]).then(function() {
resolve(customerIds);
});
}).catch(reject);
})).to.eventually.deep.equal(realCustomerIds.slice(0, 4).reduce(function(acc, x) {
acc.push(x);
acc.push(x);
Expand All @@ -251,31 +252,31 @@ describe('auto pagination', function() {
describe('autoPagingToArray', function() {
it('returns a promise of an array', function() {
return expect(new Promise(function(resolve, reject) {
stripe.customers.list({limit: 3, created: CREATED}).autoPagingToArray({max: LIMIT})
stripe.customers.list({limit: 3, email: email}).autoPagingToArray({max: LIMIT})
.then(function(customers) {
return customers.map(function(customer) { return customer.id; });
})
.then(resolve)
.catch(reject);
})).to.eventually.deep.equal(realCustomerIds);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});

it('accepts an onDone callback, passing an array', function() {
return expect(new Promise(function(resolve, reject) {
stripe.customers.list({limit: 3, created: CREATED}).autoPagingToArray({max: LIMIT}, function(err, customers) {
stripe.customers.list({limit: 3, email: email}).autoPagingToArray({max: LIMIT}, function(err, customers) {
if (err) {
reject(err);
} else {
resolve(customers.map(function(customer) { return customer.id; }));
}
});
})).to.eventually.deep.equal(realCustomerIds);
})).to.eventually.deep.equal(realCustomerIds.slice(0, LIMIT));
});

it('enforces a `max` arg', function() {
return expect(new Promise(function(resolve, reject) {
try {
stripe.customers.list({limit: 3, created: CREATED}).autoPagingToArray();
stripe.customers.list({limit: 3, email: email}).autoPagingToArray();
reject(Error('Should have thrown.'));
} catch (err) {
resolve(err.message);
Expand All @@ -286,7 +287,7 @@ describe('auto pagination', function() {
it('caps the `max` arg to a reasonable ceiling', function() {
return expect(new Promise(function(resolve, reject) {
try {
stripe.customers.list({limit: 3, created: CREATED}).autoPagingToArray({max: 1000000});
stripe.customers.list({limit: 3, email: email}).autoPagingToArray({max: 1000000});
reject(Error('Should have thrown.'));
} catch (err) {
resolve(err.message);
Expand Down

0 comments on commit 4f410af

Please sign in to comment.