mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-09 21:30:05 +00:00
fix: file uploader error handling and tests
This commit is contained in:
93
src/test/FileUploader.spec.ts
Normal file
93
src/test/FileUploader.spec.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { FileUploader } from '../FileUploader'
|
||||
import { UploadFile } from '../types';
|
||||
|
||||
(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve(sampleResponse),
|
||||
})
|
||||
);
|
||||
|
||||
it('should upload successfully', async (done) => {
|
||||
const fileUploader =
|
||||
new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
const sasJob = 'test/upload'
|
||||
const files: UploadFile[] = [
|
||||
{
|
||||
file: new File([''], 'testfile'),
|
||||
fileName: 'testfile'
|
||||
}
|
||||
];
|
||||
const params = {table: 'libtable'}
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then(
|
||||
(res: any) => {
|
||||
if (JSON.stringify(res) === JSON.stringify(JSON.parse(sampleResponse))) done()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw no files error', async (done) => {
|
||||
const fileUploader =
|
||||
new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
const sasJob = 'test/upload'
|
||||
const files: UploadFile[] = [];
|
||||
const params = {table: 'libtable'}
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then(
|
||||
(res: any) => {},
|
||||
(err: any) => {
|
||||
if (err.error.message === 'At least one file must be provided.') done()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw no sasJob error', async (done) => {
|
||||
const fileUploader =
|
||||
new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
const sasJob = ''
|
||||
const files: UploadFile[] = [
|
||||
{
|
||||
file: new File([''], 'testfile'),
|
||||
fileName: 'testfile'
|
||||
}
|
||||
];
|
||||
const params = {table: 'libtable'}
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then(
|
||||
(res: any) => {},
|
||||
(err: any) => {
|
||||
if (err.error.message === 'sasJob must be provided.') done()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const sampleResponse = `{
|
||||
"SYSUSERID": "cas",
|
||||
"_DEBUG":" ",
|
||||
"SYS_JES_JOB_URI": "/jobExecution/jobs/000-000-000-000",
|
||||
"_PROGRAM" : "/Public/app/editors/loadfile",
|
||||
"SYSCC" : "0",
|
||||
"SYSJOBID" : "117382",
|
||||
"SYSWARNINGTEXT" : ""
|
||||
}`
|
||||
35
src/test/utils/parseGeneratedCode.spec.ts
Normal file
35
src/test/utils/parseGeneratedCode.spec.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { parseGeneratedCode } from '../../utils/index'
|
||||
|
||||
it('should parse generated code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
|
||||
const parsedGeneratedCode = parseGeneratedCode(sampleResponse)
|
||||
|
||||
expect(parsedGeneratedCode).toBeTruthy()
|
||||
|
||||
const generatedCodeLines = parsedGeneratedCode.split('\r\n')
|
||||
|
||||
expect(generatedCodeLines.length).toEqual(5)
|
||||
expect(generatedCodeLines[0].startsWith('MPRINT(MM_WEBIN)')).toBeTruthy()
|
||||
expect(generatedCodeLines[1].startsWith('MPRINT(MM_WEBLEFT)')).toBeTruthy()
|
||||
expect(generatedCodeLines[2].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
|
||||
expect(generatedCodeLines[3].startsWith('MPRINT(MM_WEBRIGHT)')).toBeTruthy()
|
||||
expect(generatedCodeLines[4].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
/* tslint:disable */
|
||||
const sampleResponse = `<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
|
||||
6 @file mm_webout.sas
|
||||
7 @brief Send data to/from SAS Stored Processes
|
||||
8 @details This macro should be added to the start of each Stored Process,
|
||||
9 **immediately** followed by a call to:
|
||||
10 %webout(OPEN)
|
||||
MPRINT(MM_WEBIN): ;
|
||||
MPRINT(MM_WEBLEFT): filename _temp temp lrecl=999999;
|
||||
MPRINT(MM_WEBOUT): data _null_;
|
||||
MPRINT(MM_WEBRIGHT): file _temp;
|
||||
MPRINT(MM_WEBOUT): if upcase(symget('_debug'))='LOG' then put '>>weboutBEGIN<<';
|
||||
`
|
||||
/* tslint:enable */
|
||||
35
src/test/utils/parseSourceCode.spec.ts
Normal file
35
src/test/utils/parseSourceCode.spec.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { parseSourceCode } from '../../utils/index'
|
||||
|
||||
it('should parse SAS9 source code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
|
||||
const parsedSourceCode = parseSourceCode(sampleResponse)
|
||||
|
||||
expect(parsedSourceCode).toBeTruthy()
|
||||
|
||||
const sourceCodeLines = parsedSourceCode.split('\r\n')
|
||||
|
||||
expect(sourceCodeLines.length).toEqual(5)
|
||||
expect(sourceCodeLines[0].startsWith('6')).toBeTruthy()
|
||||
expect(sourceCodeLines[1].startsWith('7')).toBeTruthy()
|
||||
expect(sourceCodeLines[2].startsWith('8')).toBeTruthy()
|
||||
expect(sourceCodeLines[3].startsWith('9')).toBeTruthy()
|
||||
expect(sourceCodeLines[4].startsWith('10')).toBeTruthy()
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
/* tslint:disable */
|
||||
const sampleResponse = `<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
|
||||
6 @file mm_webout.sas
|
||||
7 @brief Send data to/from SAS Stored Processes
|
||||
8 @details This macro should be added to the start of each Stored Process,
|
||||
9 **immediately** followed by a call to:
|
||||
10 %webout(OPEN)
|
||||
MPRINT(MM_WEBIN): ;
|
||||
MPRINT(MM_WEBLEFT): filename _temp temp lrecl=999999;
|
||||
MPRINT(MM_WEBOUT): data _null_;
|
||||
MPRINT(MM_WEBRIGHT): file _temp;
|
||||
MPRINT(MM_WEBOUT): if upcase(symget('_debug'))='LOG' then put '>>weboutBEGIN<<';
|
||||
`
|
||||
/* tslint:enable */
|
||||
Reference in New Issue
Block a user