This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix math in InventoryPurchaseItem.costPerUnit and add math util for r…
…ounding (#864) * Add unit model test InventoryPurchaseItem - Add unit model test inv-purchase InventoryPurchaseItem * Fix incorrect rounding add math util - Fix incorrect calculation `InventoryPurchaseItem.costPerUnit` to round property to the nearest 100th - Add test for demostrating issue in `inv-purchase-test.js` "costPerUnit properly round" - Add math util with function `round100` - Add tests for `round100` function * Fix rounding in mixin number-format and unit test - Fix rounding in `_numberFormat` for number-format mixin - Add mixin unit tests for number-format mixin * Fix incorrect directory for number-format-test * Use NumberFormat for InventoryPurchaseItem - Use `NumberFormat` mixin for `InventoryPurchaseItem` and use in `costPerUnit` method - Remove math util as no longer needed * Remove duplicate number-format-test * Prefer _numberFormat to _round100
- Loading branch information
Showing
4 changed files
with
123 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import NumberFormat from 'hospitalrun/mixins/number-format'; | ||
import { moduleFor, test } from 'ember-qunit'; | ||
import Ember from 'ember'; | ||
|
||
moduleFor('mixin:number-format', 'Unit | Mixin | number-format'); | ||
|
||
test('_calculateTotal', function(assert) { | ||
let records = [5, 5, 6].map((id) => Ember.Object.create({ id })); | ||
let numberFormat = Ember.Object.extend(NumberFormat).create({ records }); | ||
|
||
assert.strictEqual(numberFormat._calculateTotal('records', 'id'), 16, 'Should add property array'); | ||
}); | ||
|
||
test('_calculateTotal property name', function(assert) { | ||
let records = [5, 2, 3].map((id) => Ember.Object.create({ id })); | ||
let numberFormat = Ember.Object.extend(NumberFormat).create(); | ||
|
||
assert.strictEqual(numberFormat._calculateTotal(records, 'id'), 10, 'Should add passed in array'); | ||
}); | ||
|
||
test('_calculateTotal invalid number', function(assert) { | ||
let records = [5, 'test', 3].map((id) => Ember.Object.create({ id })); | ||
let numberFormat = Ember.Object.extend(NumberFormat).create({ records }); | ||
|
||
assert.strictEqual(numberFormat._calculateTotal('records', 'id'), 8, 'Should treat invalid number as 0'); | ||
}); | ||
|
||
test('_numberFormat', function(assert) { | ||
let numberFormat = Ember.Object.extend(NumberFormat).create(); | ||
|
||
assert.strictEqual(numberFormat._numberFormat(), undefined, 'Should return undefined for no argument'); | ||
assert.strictEqual(numberFormat._numberFormat('test'), undefined, 'Should return undefined for no number'); | ||
assert.strictEqual(numberFormat._numberFormat(12), '12', 'Should return basic int as string'); | ||
assert.strictEqual(numberFormat._numberFormat(12, true), 12, 'Should return basic int as number'); | ||
assert.strictEqual(numberFormat._numberFormat(12.2, true), 12.2, 'Should round tenths properly'); | ||
assert.strictEqual(numberFormat._numberFormat(12.2), '12.20', 'Should pad decial to two places'); | ||
assert.strictEqual(numberFormat._numberFormat(35.555, true), 35.56, 'Should round 35.555 to 35.56'); | ||
assert.strictEqual(numberFormat._numberFormat(35.555), '35.56', 'Should return 35.555 as string "35.56"'); | ||
}); | ||
|
||
test('_getValidNumber', function(assert) { | ||
let numberFormat = Ember.Object.extend(NumberFormat).create(); | ||
|
||
assert.strictEqual(numberFormat._getValidNumber(), 0, 'Should return 0 for no argument'); | ||
assert.strictEqual(numberFormat._getValidNumber('test'), 0, 'Should return 0 for invalid number'); | ||
assert.strictEqual(numberFormat._getValidNumber(NaN), 0, 'Should return 0 for NaN'); | ||
assert.strictEqual(numberFormat._getValidNumber('12.2'), 12.2, 'Should convert string to number'); | ||
assert.strictEqual(numberFormat._getValidNumber(1), 1, 'Should return basic int'); | ||
}); | ||
|
||
test('_validNumber', function(assert) { | ||
let numberFormat = Ember.Object.extend(NumberFormat).create(); | ||
|
||
assert.strictEqual(numberFormat._validNumber(1), true, 'Should return true for basic int'); | ||
assert.strictEqual(numberFormat._validNumber(1.5), true, 'Should return true for float'); | ||
assert.strictEqual(numberFormat._validNumber('1'), true, 'Should return true for numeric string'); | ||
assert.strictEqual(numberFormat._validNumber(-1), false, 'Should return false for negative'); | ||
assert.strictEqual(numberFormat._validNumber('test'), false, 'Should return false for non numeric string'); | ||
}); |
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,55 @@ | ||
import { moduleForModel, test } from 'ember-qunit'; | ||
|
||
import { testValidPropertyValues, testInvalidPropertyValues } from '../../helpers/validate-properties'; | ||
|
||
moduleForModel('inv-purchase', 'Unit | Model | inv-purchase', { | ||
needs: [ | ||
'ember-validations@validator:local/numericality', | ||
'ember-validations@validator:local/presence' | ||
] | ||
}); | ||
|
||
test('costPerUnit', function(assert) { | ||
let inventoryPurchaseItem = this.subject({ | ||
purchaseCost: 12.50, | ||
originalQuantity: 5 | ||
}); | ||
|
||
assert.strictEqual(inventoryPurchaseItem.get('costPerUnit'), 2.5); | ||
}); | ||
|
||
test('costPerUnit properly round', function(assert) { | ||
let inventoryPurchaseItem = this.subject({ | ||
purchaseCost: 71.11, | ||
originalQuantity: 2 | ||
}); | ||
|
||
assert.strictEqual(inventoryPurchaseItem.get('costPerUnit'), 35.56); | ||
}); | ||
|
||
test('costPerUnit invalid input', function(assert) { | ||
let inventoryPurchaseItem = this.subject({ | ||
purchaseCost: 0, | ||
originalQuantity: 5 | ||
}); | ||
|
||
assert.strictEqual(inventoryPurchaseItem.get('costPerUnit'), 0); | ||
}); | ||
|
||
test('costPerUnit 0 input', function(assert) { | ||
let inventoryPurchaseItem = this.subject({ | ||
purchaseCost: 12.50, | ||
originalQuantity: 0 | ||
}); | ||
|
||
assert.strictEqual(inventoryPurchaseItem.get('costPerUnit'), 0); | ||
}); | ||
|
||
testValidPropertyValues('purchaseCost', [123, 123.0, '123']); | ||
testInvalidPropertyValues('purchaseCost', ['test', undefined]); | ||
|
||
testValidPropertyValues('originalQuantity', [0, 123, '0']); | ||
testInvalidPropertyValues('originalQuantity', [-1, '-1', undefined]); | ||
|
||
testValidPropertyValues('vendor', ['test']); | ||
testInvalidPropertyValues('vendor', [undefined]); |