Skip to content

Added ListModels functionality for Firebase ML #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed tests
  • Loading branch information
ifielker committed Feb 27, 2020
commit 1af87277cee027f2d3427789485954558ddf8c1d
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5322,7 +5322,7 @@ declare namespace admin.machineLearning {
* token. For the last page, an empty list of models and no page token
* are returned.
*/
listModels(options: ListModelOptions): Promise<ListModelsResult>;
listModels(options?: ListModelOptions): Promise<ListModelsResult>;

/**
* Deletes a model from Firebase ML.
Expand Down
1 change: 0 additions & 1 deletion src/machine-learning/machine-learning-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ export class MachineLearningApiClient {
return resp.data as T;
})
.catch((err) => {
//console.log(`SendRequest got error: ${JSON.stringify(err)}`);
throw this.toFirebaseError(err);
});
}
Expand Down
11 changes: 8 additions & 3 deletions src/machine-learning/machine-learning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,20 @@ export class MachineLearning implements FirebaseServiceInterface {
* token. For the last page, an empty list of models and no page token are
* returned.
*/
public listModels(options: ListModelsOptions): Promise<ListModelsResult> {
public listModels(options: ListModelsOptions = {}): Promise<ListModelsResult> {
return this.client.listModels(options)
.then((resp) => {
if (resp == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for validator.isNonNullObject(resp) here. Then you can simplify the subsequent checks to just if (resp.models) and if (resp.nextPageToken).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

throw new FirebaseMachineLearningError(
'invalid-argument',
`Invalid ListModels response: ${JSON.stringify(resp)}`);
}
let models: Model[] = [];
if (resp.models) {
if (resp && resp.models) {
models = resp.models.map((rs) => new Model(rs));
}
const result: ListModelsResult = {models};
if (resp.nextPageToken) {
if (resp && resp.nextPageToken) {
result.pageToken = resp.nextPageToken;
}
return result;
Expand Down
46 changes: 17 additions & 29 deletions test/integration/machine-learning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('admin.machineLearning', () => {
});

it('updates the tflite file', () => {
Promise.all([
return Promise.all([
createTemporaryModel(),
uploadModelToGcs('model1.tflite', 'valid_model.tflite')])
.then(([model, fileName]) => {
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('admin.machineLearning', () => {

describe('listModels()', () => {
it('resolves with a list of models', () => {
Promise.all([
return Promise.all([
createTemporaryModel({displayName: 'node-integration-list1a'}),
createTemporaryModel({displayName: 'node-integration-list1b'}),
])
Expand All @@ -338,14 +338,11 @@ describe('admin.machineLearning', () => {
expect(modelList.models).to.deep.include(model2);
expect(modelList.pageToken).to.be.empty;
});
})
.catch((err) => {
throw err;
});
});

it('respects page size', () => {
Promise.all([
return Promise.all([
createTemporaryModel({displayName: 'node-integration-list2a'}),
createTemporaryModel({displayName: 'node-integration-list2b'}),
createTemporaryModel({displayName: 'node-integration-list2c'}),
Expand All @@ -356,9 +353,6 @@ describe('admin.machineLearning', () => {
expect(modelList.models.length).to.equal(2);
expect(modelList.pageToken).not.to.be.empty;
});
})
.catch((err) => {
throw err;
});
});

Expand All @@ -375,7 +369,7 @@ describe('admin.machineLearning', () => {
});

it('filters by displayName prefix', () => {
Promise.all([
return Promise.all([
createTemporaryModel({displayName: 'node-integration-list4a'}),
createTemporaryModel({displayName: 'node-integration-list4b'}),
createTemporaryModel({displayName: 'node-integration-list4c'}),
Expand All @@ -389,14 +383,11 @@ describe('admin.machineLearning', () => {
expect(modelList.models).to.deep.include(model3);
expect(modelList.pageToken).to.be.empty;
});
})
.catch((err) => {
throw err;
});
});

it('filters by tag', () => {
Promise.all([
return Promise.all([
createTemporaryModel({displayName: 'node-integration-list5a', tags: ['node-integration-tag5']}),
createTemporaryModel({displayName: 'node-integration-list5b', tags: ['node-integration-tag5']}),
createTemporaryModel({displayName: 'node-integration-list5c', tags: ['node-integration-tag5']}),
Expand All @@ -410,14 +401,11 @@ describe('admin.machineLearning', () => {
expect(modelList.models).to.deep.include(model3);
expect(modelList.pageToken).to.be.empty;
});
})
.catch((err) => {
throw err;
});
});

it('handles pageTokens properly', () => {
Promise.all([
return Promise.all([
createTemporaryModel({displayName: 'node-integration-list6a'}),
createTemporaryModel({displayName: 'node-integration-list6b'}),
createTemporaryModel({displayName: 'node-integration-list6c'}),
Expand All @@ -427,31 +415,31 @@ describe('admin.machineLearning', () => {
.then((modelList) => {
expect(modelList.models.length).to.equal(2);
expect(modelList.pageToken).not.to.be.empty;
return admin.machineLearning().listModels({pageToken: modelList.pageToken, pageSize: 10})
return admin.machineLearning().listModels({
filter: 'displayName:node-integration-list6*',
pageSize: 2,
pageToken: modelList.pageToken})
.then((modelList2) => {
expect(modelList2.models.length).to.be.at.least(1);
expect(modelList2.pageToken).to.be.empty;
})
.catch((err) => {
throw err;
});
});
})
.catch((err) => {
throw err;
});
});

it('successfully returns an empty list of models', () => {
return admin.machineLearning().listModels({filter: 'displayName=non-existing-node-integration-model'})
return admin.machineLearning().listModels({filter: 'displayName=non-existing-model'})
.then((modelList) => {
expect(modelList.models.length).to.equal(0);
expect(modelList.pageToken).to.be.empty;
})
.catch((err) => {
throw err;
});
});

it('rejects with invalid argument if the filter is invalid', () => {
return admin.machineLearning().listModels({filter: 'invalidFilterItem=foo'})
.should.eventually.be.rejected.and.have.property(
'code', 'machine-learning/invalid-argument');
});
});

describe('deleteModel()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,10 @@ describe('MachineLearningApiClient', () => {
stubs.push(stub);
return apiClient.listModels(options)
.then((resp) => {
expect(resp.models.length).to.equal(2);
expect(resp.models[0]).to.deep.equal(MODEL_RESPONSE);
expect(resp.models[1]).to.deep.equal(MODEL_RESPONSE2);
expect(resp.models).not.to.be.empty;
expect(resp.models!.length).to.equal(2);
expect(resp.models![0]).to.deep.equal(MODEL_RESPONSE);
expect(resp.models![1]).to.deep.equal(MODEL_RESPONSE2);
expect(stub).to.have.been.calledOnce.and.calledWith({
method: 'GET',
url: `${BASE_URL}/projects/test-project/models`,
Expand Down
16 changes: 2 additions & 14 deletions test/unit/machine-learning/machine-learning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,29 +364,17 @@ describe('MachineLearning', () => {
.stub(MachineLearningApiClient.prototype, 'listModels')
.resolves(null);
stubs.push(stub);
return machineLearning.listModels({})
return machineLearning.listModels()
.should.eventually.be.rejected.and.have.property(
'message', 'Invalid ListModels response: null');
});

it('should reject when API response does not contain models', () => {
const response: any = deepCopy(LIST_MODELS_RESPONSE);
response.models = '';
const stub = sinon
.stub(MachineLearningApiClient.prototype, 'listModels')
.resolves(response);
stubs.push(stub);
return machineLearning.listModels({})
.should.eventually.be.rejected.and.have.property(
'message', `Invalid ListModels response: ${JSON.stringify(response)}`);
});

it('should resolve with Models on success', () => {
const stub = sinon
.stub(MachineLearningApiClient.prototype, 'listModels')
.resolves(LIST_MODELS_RESPONSE);
stubs.push(stub);
return machineLearning.listModels({})
return machineLearning.listModels()
.then((result) => {
expect(result.models.length).equals(2);
expect(result.models[0]).to.deep.equal(MODEL1);
Expand Down