Skip to content

Commit

Permalink
feat/patch: add Preprocessed API Testsuite
Browse files Browse the repository at this point in the history
ADD Implmentation for Preprocessing on Test
COVER all Preprocessed responses

TODO - documentation for v1.2.0
  • Loading branch information
hubtwork committed May 14, 2021
1 parent 9ac2456 commit bff3cbc
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 62 deletions.
8 changes: 4 additions & 4 deletions src/__test__/apiclient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(true)
if (response.data) {
const data: testDataType = response.data || undefined
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(false)
expect(response.errorMessage).toEqual('Unexpected HTTP Status Code : 404')
})
Expand All @@ -95,7 +95,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(false)
expect(response.errorMessage).toEqual('No response from the server')
})
Expand All @@ -115,7 +115,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(false)
expect(response.errorMessage).toEqual('Error occured during setup request')
})
Expand Down
12 changes: 11 additions & 1 deletion src/__test__/mock/mock_apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosResponse, Method } from 'axios'
import { ApiClientResponse, PapagoDetectLanguageReturnType, PapagoKoreanNameRomanizerReturnType, PapagoTranslationReturnType } from '../../types/return_types'
import { ApiClientResponse, PapagoDetectLanguageReturnType, PapagoKoreanNameRomanizerReturnType, PapagoTranslationReturnType, SearchMessageRequestReturnType, SearchMessageResultReturnType, SendSMSReturnType } from '../../types/return_types'
import { ResponseTranslator, SupportedServices } from '../../types/service_translator'
import { ServiceError } from '../../utils/errors'

Expand All @@ -21,7 +21,9 @@ export class MockApiClient {
try {
if (serviceError) throw serviceError
const val = await this.createRequest<T>(apiRequest)
console.log(val)
const preprocessed = this.preprocessingServerResponse(val, apiRequest) as P
console.log(val, preprocessed)
return {
isSuccess: true,
data: val,
Expand Down Expand Up @@ -80,6 +82,14 @@ export class MockApiClient {

private preprocessingServerResponse(val: object, apiRequest: ApiRequest) {
switch(apiRequest.service) {
// SMS
case SupportedServices.SENS_SEND_SMS:
return ResponseTranslator.sensSendSMS(val as SendSMSReturnType)
case SupportedServices.SENS_SEARCH_MESSAGE_REQUEST:
return ResponseTranslator.sensSearchMessageRequest(val as SearchMessageRequestReturnType)
case SupportedServices.SENS_SEARCH_MESSAGE_RESULT:
return ResponseTranslator.sensSearchMessageResult(val as SearchMessageResultReturnType)
// PAPAGO
case SupportedServices.PAPAGO_TRANSLATION:
return ResponseTranslator.papagoTranslation(val as PapagoTranslationReturnType)
case SupportedServices.PAPAGO_LANGUAGE_DETECTION:
Expand Down
18 changes: 11 additions & 7 deletions src/__test__/mock/mock_smsClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Method } from "axios"
import { NCPAuthKeyType, SMSserviceAuthType } from "../../types/auth_types"
import { SendSMSParamType } from "../../types/param_types"
import { SENS_preprocessed_SendSMS } from "../../types/processing_types"
import { SENS_preprocessed_SearchMessageRequest, SENS_preprocessed_SearchMessageResult, SENS_preprocessed_SendSMS } from "../../types/processing_types"
import { ApiClientResponse, SearchMessageRequestReturnType, SearchMessageResultReturnType, SendSMSReturnType } from "../../types/return_types"
import { SupportedServices } from "../../types/service_translator"
import { generateApiSignature } from "../../utils/helper"
Expand Down Expand Up @@ -58,7 +58,7 @@ export class MockSMS {
return this.client.request<SendSMSReturnType, SENS_preprocessed_SendSMS>(apiRequest)
}

public async searchMessageRequest(requestId: string): Promise<ApiClientResponse<SearchMessageRequestReturnType>> {
public async searchMessageRequest(requestId: string): Promise<ApiClientResponse<SearchMessageRequestReturnType, SENS_preprocessed_SearchMessageRequest>> {
// construct path and method
const path = `/sms/v2/services/${this.smsAuth.serviceId}/messages?requestId=${requestId}`
const method: Method = 'GET'
Expand All @@ -73,12 +73,14 @@ export class MockSMS {
const apiRequest: ApiRequest = {
path: path,
method: method,
headers: headers
headers: headers,

service: SupportedServices.SENS_SEARCH_MESSAGE_REQUEST
}
return this.client.request<SearchMessageRequestReturnType>(apiRequest)
return this.client.request<SearchMessageRequestReturnType, SENS_preprocessed_SearchMessageRequest>(apiRequest)
}

public async searchMessageResult(messageId: string): Promise<ApiClientResponse<SearchMessageResultReturnType>> {
public async searchMessageResult(messageId: string): Promise<ApiClientResponse<SearchMessageResultReturnType, SENS_preprocessed_SearchMessageResult>> {
// construct path and method
const path = `/sms/v2/services/${this.smsAuth.serviceId}/messages/${messageId}`
const method: Method = 'GET'
Expand All @@ -93,9 +95,11 @@ export class MockSMS {
const apiRequest: ApiRequest = {
path: path,
method: method,
headers: headers
headers: headers,

service: SupportedServices.SENS_SEARCH_MESSAGE_RESULT
}
return this.client.request<SearchMessageResultReturnType>(apiRequest)
return this.client.request<SearchMessageResultReturnType, SENS_preprocessed_SearchMessageResult>(apiRequest)
}


Expand Down
14 changes: 12 additions & 2 deletions src/__test__/papago/detect_language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,32 @@ describe('PAPAGO.DetectLanguage TestSuite', () => {

axios.mockImplementationOnce(() =>
Promise.resolve({
langCode: 'ko'
status: 200,
statusText: 'OK',
data : {
langCode: 'ko'
}
})
)
const text = '이게 된다고?'

const response = await client.detectLanguage(text)
expect(response.isSuccess).toEqual(true)
// check response data parsing
expect(response.data !== null).toEqual(true)
if (response.data) {
console.log('data detected')
const data: PapagoDetectLanguageReturnType = response.data
expect(data !== undefined).toEqual(true)
expect(data.langCode === 'ko').toEqual(true)
}
// check response preprocessed data
expect(response.preprocessed !== null).toEqual(true)
if (response.preprocessed) {
console.log('preprocessed complete')
const preprocessed = response.preprocessed
const preprocessed: PAPAGO_preprocessed_LanguageDetction = response.preprocessed
expect(typeof preprocessed.detected).toBe('string')
expect(preprocessed.detected).toBe('ko')
}

})
Expand Down
52 changes: 34 additions & 18 deletions src/__test__/papago/korean_name_romanizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios')
import { MockPAPAGO } from '../mock/mock_papago'
import { NaverOpenApiAuthType } from '../../types/auth_types'
import { PapagoKoreanNameRomanizerReturnType } from '../../types/return_types'
import { PAPAGO_preprocessed_KoreanNameRomanizer } from '../../types/processing_types'

jest.mock('axios')

Expand Down Expand Up @@ -29,30 +30,36 @@ describe('PAPAGO.KoreanNameRomanizer TestSuite', () => {

axios.mockImplementationOnce(() =>
Promise.resolve({
aResult: [
{
sFirstName: '허',
aItems: [
{ name: 'Heo Jae', score: '100' },
{ name: 'Huh Jae', score: '60' },
{ name: 'Hur Jae', score: '45' },
{ name: 'Hu Jae', score: '26' },
{ name: 'Heo Je', score: '10' },
{ name: 'Heo Jea', score: '10' },
{ name: 'Huh Je', score: '6' },
{ name: 'Huh Jea', score: '6' },
{ name: 'Hur Je', score: '4' },
{ name: 'Hur Jea', score: '4' }
]
}
]
status: 200,
statusText: 'OK',
data : {
aResult: [
{
sFirstName: '허 ',
aItems: [
{ name: 'Heo Jae', score: '100' },
{ name: 'Huh Jae', score: '60' },
{ name: 'Hur Jae', score: '45' },
{ name: 'Hu Jae', score: '26' },
{ name: 'Heo Je', score: '10' },
{ name: 'Heo Jea', score: '10' },
{ name: 'Huh Je', score: '6' },
{ name: 'Huh Jea', score: '6' },
{ name: 'Hur Je', score: '4' },
{ name: 'Hur Jea', score: '4' }
]
}
]
}
})
)

const name = "허재"

const response = await client.koreanNameRominizer(name)
expect(response.isSuccess).toEqual(true)
// check response data parsing
expect(response.data !== null).toEqual(true)
if (response.data) {
console.log('data detected')
const data: PapagoKoreanNameRomanizerReturnType = response.data || undefined
Expand All @@ -61,6 +68,15 @@ describe('PAPAGO.KoreanNameRomanizer TestSuite', () => {
expect(data.aResult[0].sFirstName.length > 0).toEqual(true)
expect(data.aResult[0].aItems.length > 0).toEqual(true)
}
// check response preprocessed data
expect(response.preprocessed !== null).toEqual(true)
if (response.preprocessed) {
console.log('preprocessed complete')
const preprocessed: PAPAGO_preprocessed_KoreanNameRomanizer = response.preprocessed
expect(typeof preprocessed.firstName).toBe('string')
expect(preprocessed.firstName).toBe('허 ')
expect(preprocessed.bestMatched).not.toBeNull()
}

})

Expand Down
37 changes: 28 additions & 9 deletions src/__test__/papago/translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios')
import { MockPAPAGO } from '../mock/mock_papago'
import { NaverOpenApiAuthType } from '../../types/auth_types'
import { PapagoTranslationReturnType } from '../../types/return_types'
import { PAPAGO_preprocessed_Translation } from '../../types/processing_types'

jest.mock('axios')

Expand Down Expand Up @@ -29,20 +30,26 @@ describe('PAPAGO.Translation TestSuite', () => {

axios.mockImplementationOnce(() =>
Promise.resolve({
'@type': 'response',
'@service': 'naverservice.nmt.proxy',
'@version': '1.0.0',
result: {
srcLangType: 'ko',
tarLangType: 'en',
translatedText: 'Really?'
status: 200,
statusText: 'OK',
data : {
message: {
'@type': 'response',
'@service': 'naverservice.nmt.proxy',
'@version': '1.0.0',
result: {
srcLangType: 'ko',
tarLangType: 'en',
translatedText: 'Really?'
}
}
}
})
)

const source = 'ko'
const target = 'en'
const text = '이게 진짜 된다고?'
const text = '정말로?'

const response = await client.translation(source, target, text)
expect(response.isSuccess).toEqual(true)
Expand All @@ -52,12 +59,24 @@ describe('PAPAGO.Translation TestSuite', () => {
expect(data !== undefined).toEqual(true)
expect(data.message !== undefined).toEqual(true)
expect(data.message['@type'] === 'response').toEqual(true)
expect(data.message['@service']).toEqual(true)
expect(data.message['@service']).toBe('naverservice.nmt.proxy')
expect(data.message.result.srcLangType === source).toEqual(true)
expect(data.message.result.tarLangType === target).toEqual(true)
expect((data.message.result.srcLangType !== data.message.result.tarLangType) && (data.message.result.translatedText !== text)).toEqual(true)
}

expect(response.preprocessed).not.toBeNull()
if (response.preprocessed) {
console.log('preprocessd complete')
const preprocessed: PAPAGO_preprocessed_Translation = response.preprocessed
expect(typeof preprocessed.source).toBe('string')
expect(preprocessed.source).toBe('ko')
expect(typeof preprocessed.target).toBe('string')
expect(preprocessed.target).toBe('en')
expect(typeof preprocessed.translated).toBe('string')
expect(preprocessed.translated).toBe('Really?')
}

})

test('Invalid Request with empty source', async () => {
Expand Down
19 changes: 17 additions & 2 deletions src/__test__/sms/search_sms_request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios')
import { MockSMS } from '../mock/mock_smsClient';
import { SearchMessageRequestReturnType } from '../../types/return_types';
import { NCPAuthKeyType, SMSserviceAuthType } from '../../types/auth_types';
import { SENS_preprocessed_SearchMessageRequest } from '../../types/processing_types';

jest.mock('axios')

Expand Down Expand Up @@ -35,8 +36,9 @@ describe('SMS.SearchSmsRequest TestSuite', () => {

axios.mockImplementationOnce(() =>
Promise.resolve({
isSuccess: true,
data: {
status: 200,
statusText: 'OK',
data : {
statusCode: '202',
statusName: 'success',
requestId: '3a4cb63856b04f93aa43805188d6f695',
Expand Down Expand Up @@ -64,6 +66,19 @@ describe('SMS.SearchSmsRequest TestSuite', () => {
expect(data.messages[0].countryCode).toEqual('82')
expect(data.messages[0].requestTime.match(/(\d{4})-(\d{2})-(\d{2})(\s)(\d{2}):(\d{2}):(\d{2})/) !== null).toEqual(true)
}


expect(response.preprocessed).not.toBeNull()
if (response.preprocessed) {
console.log('preprocessd complete')
const preprocessed: SENS_preprocessed_SearchMessageRequest = response.preprocessed
expect(typeof preprocessed.requestId).toBe('string')
expect(preprocessed.requestId).toBe('3a4cb63856b04f93aa43805188d6f695')
expect(typeof preprocessed.result).toBe('string')
expect(preprocessed.result).toBe('success')
expect(preprocessed.messageIds.length).toBe(1)
}

})

test('Successful request for searchMessageRequest with multi Message', async () => {
Expand Down
15 changes: 13 additions & 2 deletions src/__test__/sms/search_sms_result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios')
import { MockSMS } from '../mock/mock_smsClient';
import { SearchMessageResultReturnType } from '../../types/return_types';
import { NCPAuthKeyType, SMSserviceAuthType } from '../../types/auth_types';
import { SENS_preprocessed_SearchMessageResult } from '../../types/processing_types';

jest.mock('axios')

Expand Down Expand Up @@ -35,8 +36,9 @@ describe('SMS.SearchSmsResult TestSuite', () => {

axios.mockImplementationOnce(() =>
Promise.resolve({
isSuccess: true,
data: {
status: 200,
statusText: 'OK',
data : {
statusCode: '202',
statusName: 'success',
messages: [ {
Expand Down Expand Up @@ -71,6 +73,15 @@ describe('SMS.SearchSmsResult TestSuite', () => {
expect(data.messages[0].requestTime.match(/(\d{4})-(\d{2})-(\d{2})(\s)(\d{2}):(\d{2}):(\d{2})/) !== null).toEqual(true)
expect(data.messages[0].requestTime !== data.messages[0].completeTime).toEqual(true)
}

expect(response.preprocessed).not.toBeNull()
if (response.preprocessed) {
console.log('preprocessd complete')
const preprocessed: SENS_preprocessed_SearchMessageResult = response.preprocessed
expect(typeof preprocessed.result).toBe('string')
expect(preprocessed.result).toBe('success')
expect(preprocessed.messages.length).toBe(1)
}
})

test('Invalid Response with http status Code 401', async () => {
Expand Down
Loading

0 comments on commit bff3cbc

Please sign in to comment.