forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GSoC'24] M1.2 - Add API endpoint for fetching translations in bulk (o…
…ppia#20386) * Add API endpoint for fetching translations in bulk * Write backend test for bulk translations API * Gate API call behind feature flag * Create frontend service for the backend API calls * Fix typos * Fix lint issues
- Loading branch information
Showing
5 changed files
with
307 additions
and
2 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
111 changes: 111 additions & 0 deletions
111
...ges/exploration-editor-page/services/entity-bulk-translations-backend-api.service.spec.ts
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,111 @@ | ||
// Copyright 2024 The Oppia Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS-IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @fileoverview Unit tests for EntityBulkTranslationsBackendApiService. | ||
*/ | ||
|
||
import { | ||
HttpClientTestingModule, | ||
HttpTestingController, | ||
} from '@angular/common/http/testing'; | ||
import {fakeAsync, flushMicrotasks, TestBed, tick} from '@angular/core/testing'; | ||
import {EntityBulkTranslationsBackendApiService} from './entity-bulk-translations-backend-api.service'; | ||
|
||
describe('Entity Bulk Translations Backend Api Service', () => { | ||
let translationApiService: EntityBulkTranslationsBackendApiService; | ||
let httpTestingController: HttpTestingController; | ||
let successHandler = jasmine.createSpy('success'); | ||
let failHandler = jasmine.createSpy('fail'); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
}); | ||
httpTestingController = TestBed.inject(HttpTestingController); | ||
translationApiService = TestBed.inject( | ||
EntityBulkTranslationsBackendApiService | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
httpTestingController.verify(); | ||
}); | ||
|
||
it('should fetch entity translations in bulk', fakeAsync(() => { | ||
let entityId: string = 'entity1'; | ||
let entityType: string = 'exploration'; | ||
let entityVersion: number = 5; | ||
translationApiService | ||
.fetchEntityBulkTranslationsAsync(entityId, entityType, entityVersion) | ||
.then(successHandler, failHandler); | ||
let req = httpTestingController.expectOne( | ||
'/entity_translations_bulk_handler/exploration/entity1/5' | ||
); | ||
expect(req.request.method).toEqual('GET'); | ||
req.flush( | ||
{ | ||
hi: { | ||
entity_id: 'entity1', | ||
entity_type: 'exploration', | ||
entity_version: 5, | ||
language_code: 'hi', | ||
translations: { | ||
feedback_3: { | ||
content_format: 'html', | ||
content_value: '<p>This is feedback 1.</p>', | ||
needs_update: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
status: 200, | ||
statusText: 'Success.', | ||
} | ||
); | ||
tick(); | ||
flushMicrotasks(); | ||
|
||
expect(successHandler).toHaveBeenCalled(); | ||
})); | ||
|
||
it('should handle backend failure', fakeAsync(() => { | ||
let entityId: string = 'entity1'; | ||
let entityType: string = 'exploration'; | ||
let entityVersion: number = 5; | ||
translationApiService | ||
.fetchEntityBulkTranslationsAsync(entityId, entityType, entityVersion) | ||
.then(successHandler, failHandler); | ||
let req = httpTestingController.expectOne( | ||
'/entity_translations_bulk_handler/exploration/entity1/5' | ||
); | ||
expect(req.request.method).toEqual('GET'); | ||
req.flush( | ||
{ | ||
error: 'Some error occurred in the backend.', | ||
}, | ||
{ | ||
status: 500, | ||
statusText: 'Internal Server Error', | ||
} | ||
); | ||
|
||
flushMicrotasks(); | ||
|
||
expect(failHandler).toHaveBeenCalledWith( | ||
'Some error occurred in the backend.' | ||
); | ||
})); | ||
}); |
76 changes: 76 additions & 0 deletions
76
...es/pages/exploration-editor-page/services/entity-bulk-translations-backend-api.service.ts
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,76 @@ | ||
// Copyright 2024 The Oppia Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS-IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @fileoverview Service to fetch all translations for an entity from the backend. | ||
*/ | ||
|
||
import {downgradeInjectable} from '@angular/upgrade/static'; | ||
import {HttpClient} from '@angular/common/http'; | ||
import {Injectable} from '@angular/core'; | ||
import {UrlInterpolationService} from 'domain/utilities/url-interpolation.service'; | ||
import {LanguageCodeToEntityTranslations} from 'services/entity-translations.services'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class EntityBulkTranslationsBackendApiService { | ||
ENTITY_TRANSLATIONS_BULK_HANDLER_URL_TEMPLATE: string = | ||
'/entity_translations_bulk_handler/<entity_type>/<entity_id>/<entity_version>'; | ||
|
||
constructor( | ||
private httpClient: HttpClient, | ||
private urlInterpolationService: UrlInterpolationService | ||
) {} | ||
|
||
private _getUrl(entityId: string, entityType: string, entityVersion: number) { | ||
return this.urlInterpolationService.interpolateUrl( | ||
this.ENTITY_TRANSLATIONS_BULK_HANDLER_URL_TEMPLATE, | ||
{ | ||
entity_id: entityId, | ||
entity_type: entityType, | ||
entity_version: String(entityVersion), | ||
} | ||
); | ||
} | ||
|
||
async fetchEntityBulkTranslationsAsync( | ||
entityId: string, | ||
entityType: string, | ||
entityVersion: number | ||
): Promise<LanguageCodeToEntityTranslations> { | ||
return new Promise((resolve, reject) => { | ||
this.httpClient | ||
.get<LanguageCodeToEntityTranslations>( | ||
this._getUrl(entityId, entityType, entityVersion) | ||
) | ||
.toPromise() | ||
.then( | ||
response => { | ||
resolve(response); | ||
}, | ||
errorResponse => { | ||
reject(errorResponse.error.error); | ||
} | ||
); | ||
}); | ||
} | ||
} | ||
|
||
angular | ||
.module('oppia') | ||
.factory( | ||
'EntityBulkTranslationsBackendApiService', | ||
downgradeInjectable(EntityBulkTranslationsBackendApiService) | ||
); |
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