-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from TransitApp/feature/add-fares
Add fare attributes and fare rules
- Loading branch information
Showing
8 changed files
with
477 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict'; | ||
|
||
const { expect } = require('chai'); | ||
const { Gtfs } = require('../index'); | ||
|
||
describe('Tests on GTFS fare attributes', () => { | ||
it('Tests on gtfs.getNumberOfFareAttributes(), .add…(), .remove…() and .getFareAttributeWithId', (done) => { | ||
const path = `${__dirname}/samples/1`; | ||
const gtfs = new Gtfs(path); | ||
const fareAttribute1 = buildFareAttribute(1); | ||
const fareAttribute2 = buildFareAttribute(2); | ||
const fareAttribute3 = buildFareAttribute(3); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(0); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(false); | ||
|
||
gtfs.addFareAttributes([fareAttribute1, fareAttribute2]); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(2); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(true); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(true); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(false); | ||
|
||
gtfs.addFareAttribute(fareAttribute3); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(3); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(true); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(true); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(true); | ||
|
||
gtfs.removeFareAttribute(fareAttribute2); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(2); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(true); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(true); | ||
|
||
gtfs.removeFareAttributes([fareAttribute1, fareAttribute3]); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(0); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(false); | ||
|
||
done(); | ||
}); | ||
|
||
it('Tests on gtfs.setFareAttributes() and .resetFareAttributes', (done) => { | ||
const path = `${__dirname}/samples/1`; | ||
const gtfs = new Gtfs(path); | ||
const fareAttribute1 = buildFareAttribute(1); | ||
const fareAttribute2 = buildFareAttribute(2); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(0); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(false); | ||
|
||
gtfs.setIndexedFareAttributes(new Map([ | ||
['fareAttribute.fare_id.1', fareAttribute1], | ||
['fareAttribute.fare_id.2', fareAttribute2], | ||
])); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(2); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(true); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(true); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(false); | ||
|
||
gtfs.resetFareAttributes(); | ||
|
||
expect(gtfs.getNumberOfFareAttributes()).to.equal(0); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.1') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.2') !== undefined).to.equal(false); | ||
expect(gtfs.getFareAttributeWithId('fareAttribute.fare_id.3') !== undefined).to.equal(false); | ||
|
||
done(); | ||
}); | ||
|
||
it('Tests on gtfs.forEachFareAttribute()', (done) => { | ||
const path = `${__dirname}/samples/1`; | ||
const gtfs = new Gtfs(path); | ||
gtfs.addFareAttributes([buildFareAttribute(1), buildFareAttribute(3)]); | ||
|
||
const fareIds = []; | ||
gtfs.forEachFareAttribute(fareAttribute => fareIds.push(fareAttribute.fare_id)); | ||
|
||
expect(fareIds.sort()).to.deep.equal(['fareAttribute.fare_id.1', 'fareAttribute.fare_id.3']); | ||
|
||
done(); | ||
}); | ||
|
||
it('Tests on gtfs.getIndexedFareAttributes()', (done) => { | ||
const path = `${__dirname}/samples/1`; | ||
const gtfs = new Gtfs(path); | ||
gtfs.addFareAttributes([buildFareAttribute(1), buildFareAttribute(3)]); | ||
|
||
const fareIds = []; | ||
const indexedFareAttributes = gtfs.getIndexedFareAttributes(); | ||
indexedFareAttributes.forEach(fareAttribute => fareIds.push(fareAttribute.fare_id)); | ||
|
||
expect(fareIds.sort()).to.deep.equal(['fareAttribute.fare_id.1', 'fareAttribute.fare_id.3']); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
function buildFareAttribute(integer) { | ||
return { | ||
'fare_id': `fareAttribute.fare_id.${integer}`, | ||
'price': `fareAttribute.price.${integer}`, | ||
'currency_type': `fareAttribute.currency_type.${integer}`, | ||
'payment_method': `fareAttribute.payment_method.${integer}`, | ||
'transfers': `fareAttribute.transfers.${integer}`, | ||
'agency_id': `fareAttribute.agency_id.${integer}`, | ||
'transfer_duration': `fareAttribute.transfer_duration.${integer}`, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
'use strict'; | ||
|
||
const { expect } = require('chai'); | ||
const { Gtfs } = require('../index'); | ||
|
||
describe('Tests on GTFS fare rules', () => { | ||
it('Tests on gtfs.getNumberOfFareRules(), .addFareRules?(), .getFareRules?() and .hasFareRule', (done) => { | ||
const path = `${__dirname}/samples/1`; | ||
const gtfs = new Gtfs(path); | ||
const fareRule1 = buildFareRule(1); | ||
const fareRule2 = buildFareRule(2); | ||
const fareRule3 = buildFareRule(3); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(0); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(false); | ||
|
||
gtfs.addFareRules([fareRule1, fareRule2]); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(2); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(true); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(true); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(false); | ||
|
||
gtfs.addFareRule(fareRule3); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(3); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(true); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(true); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(true); | ||
|
||
gtfs.removeFareRule(fareRule2); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(2); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(true); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(true); | ||
|
||
gtfs.removeFareRules([fareRule1, fareRule3]); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(0); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(false); | ||
|
||
done(); | ||
}); | ||
|
||
it('Tests on gtfs.setFareRules() and .resetFareRules', (done) => { | ||
const path = `${__dirname}/samples/1`; | ||
const gtfs = new Gtfs(path); | ||
const fareRule1 = buildFareRule(1); | ||
const fareRule2 = buildFareRule(2); | ||
const fareRule3 = buildFareRule(3); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(0); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(false); | ||
|
||
gtfs.setFareRules(new Set([fareRule1, fareRule2])); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(2); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(true); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(true); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(false); | ||
|
||
gtfs.resetFareRules(); | ||
|
||
expect(gtfs.getNumberOfFareRules()).to.equal(0); | ||
expect(gtfs.hasFareRule(fareRule1)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule2)).to.equal(false); | ||
expect(gtfs.hasFareRule(fareRule3)).to.equal(false); | ||
|
||
done(); | ||
}); | ||
|
||
it('Tests on gtfs.forEachFareRule()', (done) => { | ||
const path = `${__dirname}/samples/1`; | ||
const gtfs = new Gtfs(path); | ||
gtfs.addFareRules([buildFareRule(1), buildFareRule(3)]); | ||
|
||
const fareIds = []; | ||
gtfs.forEachFareRule(fareRule => fareIds.push(fareRule.fare_id)); | ||
|
||
expect(fareIds.sort()).to.deep.equal(['fareRule.fare_id.1', 'fareRule.fare_id.3']); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
function buildFareRule(integer) { | ||
return { | ||
'fare_id': `fareRule.fare_id.${integer}`, | ||
'route_id': `fareRule.route_id.${integer}`, | ||
'origin_id': `fareRule.origin_id.${integer}`, | ||
'destination_id': `fareRule.destination_id.${integer}`, | ||
'contains_id': `fareRule.contains_id.${integer}`, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters