Skip to content

Commit

Permalink
chore: adding exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Dec 9, 2018
1 parent 37044eb commit 0d22ba3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lib/naive_bayes/multinomial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as tf from '@tensorflow/tfjs';
import { countBy, zip } from 'lodash';
import { countBy, isEmpty, zip } from 'lodash';
import { reshape, validateFitInputs, validateMatrix2D } from '../ops';
import { IMlModel, Type1DMatrix, Type2DMatrix } from '../types';

Expand Down Expand Up @@ -69,6 +69,15 @@ export class MultinomialNB<T extends number | string = number>
*/
public predict(X: Type2DMatrix<number>): T[] {
validateMatrix2D(X);
if (
isEmpty(this.classCategories) ||
isEmpty(this.multinomialDist) ||
isEmpty(this.priorProbability)
) {
throw new TypeError(
'You should fit the model first before running the predict!'
);
}
return X.map(x => this.singlePredict(x));
}

Expand Down
17 changes: 17 additions & 0 deletions test/naive_bayes/__snapshots__/multinomial.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`naive_bayes:MultinomialNB should not fit invalid inputs 1`] = `[Error: values passed to tensor(values) must be an array of numbers or booleans, or a TypedArray]`;

exports[`naive_bayes:MultinomialNB should not fit invalid inputs 2`] = `[TypeError: Number of labels=1 does not math number of samples=2]`;

exports[`naive_bayes:MultinomialNB should not fit invalid inputs 3`] = `[Error: Input cannot be an empty array]`;

exports[`naive_bayes:MultinomialNB should not predict if the model is not trained yet 1`] = `[TypeError: Cannot read property 'classCategories' of undefined]`;

exports[`naive_bayes:MultinomialNB should not predict invalid inputs 1`] = `[Error: values passed to tensor(values) must be an array of numbers or booleans, or a TypedArray]`;

exports[`naive_bayes:MultinomialNB should not predict invalid inputs 2`] = `[Error: values passed to tensor(values) must be an array of numbers or booleans, or a TypedArray]`;

exports[`naive_bayes:MultinomialNB should not predict invalid inputs 3`] = `[TypeError: The matrix is not 2D shaped: 1 of []]`;

exports[`naive_bayes:MultinomialNB should not predict invalid inputs 4`] = `[TypeError: The matrix is not 2D shaped: [1,2] of [2]]`;
20 changes: 20 additions & 0 deletions test/naive_bayes/multinomial.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MultinomialNB } from '../../src/lib/naive_bayes/multinomial';
import { matchExceptionWithSnapshot } from '../util_testing';

describe('naive_bayes:MultinomialNB', () => {
const X1 = [[6, 9], [5, 5], [9, 5]];
Expand Down Expand Up @@ -92,4 +93,23 @@ describe('naive_bayes:MultinomialNB', () => {
const result2 = nb2.predict([[1, 20]]);
expect(result2).toEqual(expected);
});

it('should not fit invalid inputs', () => {
const nb = new MultinomialNB<string>();
matchExceptionWithSnapshot(nb.fit, [null, 1]);
matchExceptionWithSnapshot(nb.fit, [[[1, 2], [1, 3]], [1]]);
matchExceptionWithSnapshot(nb.fit, [[], [1]]);
});

it('should not predict invalid inputs', () => {
const nb = new MultinomialNB<string>();
matchExceptionWithSnapshot(nb.predict, [null]);
matchExceptionWithSnapshot(nb.predict, []);
matchExceptionWithSnapshot(nb.predict, [1]);
matchExceptionWithSnapshot(nb.predict, [[1, 2]]);
});
it('should not predict if the model is not trained yet', () => {
const nb = new MultinomialNB<string>();
matchExceptionWithSnapshot(nb.predict, [[[1, 2], [3, 4]]]);
});
});

0 comments on commit 0d22ba3

Please sign in to comment.