feat: added saveRecord method
This commit is contained in:
@@ -27,6 +27,7 @@ import { testFieldData } from './testData/testFieldData';
|
||||
import { type AxiosResponse } from 'axios';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { SaveRecordResponse } from '../src/models/SaveRecordResponse';
|
||||
|
||||
describe('ApiResponse', function () {
|
||||
it('should be defined', function () {
|
||||
@@ -1801,4 +1802,43 @@ describe('ApiResponse', function () {
|
||||
}).to.throw();
|
||||
});
|
||||
});
|
||||
|
||||
describe('asSaveRecordResponseType', function () {
|
||||
it('should be defined', function () {
|
||||
expect(ApiResponse.prototype.asSaveRecordResponseType).to.not.be
|
||||
.undefined;
|
||||
});
|
||||
|
||||
it('should be a function', function () {
|
||||
expect(ApiResponse.prototype.asSaveRecordResponseType).to.be.a(
|
||||
'function'
|
||||
);
|
||||
});
|
||||
|
||||
it('should take no parameters', function () {
|
||||
expect(ApiResponse.prototype.asSaveRecordResponseType).to.have.lengthOf(
|
||||
0
|
||||
);
|
||||
});
|
||||
|
||||
it('should return a SaveRecordResponse', function () {
|
||||
const mockResponseData = {
|
||||
id: 1,
|
||||
warnings: ['warning 1', 'warning 2'],
|
||||
};
|
||||
|
||||
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
|
||||
const saveRecordResponse = apiResponse.asSaveRecordResponseType();
|
||||
|
||||
expect(saveRecordResponse).to.be.an.instanceof(
|
||||
ApiResponse<SaveRecordResponse>
|
||||
);
|
||||
expect(saveRecordResponse.data).to.be.an.instanceof(SaveRecordResponse);
|
||||
expect(saveRecordResponse.data).to.have.property('id', 1);
|
||||
expect(saveRecordResponse.data)
|
||||
.to.have.property('warnings')
|
||||
.that.is.an('array')
|
||||
.that.has.lengthOf(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -32,6 +32,7 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
import * as sinon from 'sinon';
|
||||
import { QueryRecordsRequest } from '../src/models/QueryRecordsRequest';
|
||||
import { SaveRecordRequest } from '../src/models/SaveRecordRequest';
|
||||
|
||||
describe('OnspringClient', function () {
|
||||
const baseUrl = 'https://api.onspring.dev';
|
||||
@@ -3757,4 +3758,265 @@ describe('OnspringClient', function () {
|
||||
expect(result).to.have.property('data', null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveRecord', function () {
|
||||
it('should be defined', function () {
|
||||
expect(OnspringClient.prototype.saveRecord).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should be a function', function () {
|
||||
expect(OnspringClient.prototype.saveRecord).to.be.a('function');
|
||||
});
|
||||
|
||||
it('should return a promise', function () {
|
||||
expect(
|
||||
new OnspringClient(baseUrl, apiKey).saveRecord(
|
||||
new SaveRecordRequest(1, 1)
|
||||
)
|
||||
).to.be.instanceOf(Promise);
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an api response when request is successful at creating a record using a SaveRecordRequest', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'put').returns(
|
||||
Promise.resolve({
|
||||
status: 201,
|
||||
statusText: 'Created',
|
||||
data: { id: 1, warnings: ['warning'] },
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.saveRecord(new SaveRecordRequest(1, null));
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse);
|
||||
expect(result).to.have.property('statusCode', 201);
|
||||
expect(result).to.have.property('isSuccessful', true);
|
||||
expect(result).to.have.property('message', '');
|
||||
expect(result).to.have.property('data');
|
||||
expect(result.data).to.have.property('id', 1);
|
||||
expect(result.data)
|
||||
.to.have.property('warnings')
|
||||
.that.is.an('array')
|
||||
.that.has.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an api response when request is successful at updating a record using a SaveRecordRequest', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'put').returns(
|
||||
Promise.resolve({
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
data: { id: 1, warnings: ['warning'] },
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.saveRecord(new SaveRecordRequest(1, 1));
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse);
|
||||
expect(result).to.have.property('statusCode', 200);
|
||||
expect(result).to.have.property('isSuccessful', true);
|
||||
expect(result).to.have.property('message', '');
|
||||
expect(result).to.have.property('data');
|
||||
expect(result.data).to.have.property('id', 1);
|
||||
expect(result.data)
|
||||
.to.have.property('warnings')
|
||||
.that.is.an('array')
|
||||
.that.has.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an api response when request is successful at creating a record using a Record', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'put').returns(
|
||||
Promise.resolve({
|
||||
status: 201,
|
||||
statusText: 'Created',
|
||||
data: { id: 1, warnings: ['warning'] },
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.saveRecord(new Record(1, null));
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse);
|
||||
expect(result).to.have.property('statusCode', 201);
|
||||
expect(result).to.have.property('isSuccessful', true);
|
||||
expect(result).to.have.property('message', '');
|
||||
expect(result).to.have.property('data');
|
||||
expect(result.data).to.have.property('id', 1);
|
||||
expect(result.data)
|
||||
.to.have.property('warnings')
|
||||
.that.is.an('array')
|
||||
.that.has.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an api response when request is successful at updating a record using a Record', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'put').returns(
|
||||
Promise.resolve({
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
data: { id: 1, warnings: ['warning'] },
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.saveRecord(new Record(1, 1));
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse);
|
||||
expect(result).to.have.property('statusCode', 200);
|
||||
expect(result).to.have.property('isSuccessful', true);
|
||||
expect(result).to.have.property('message', '');
|
||||
expect(result).to.have.property('data');
|
||||
expect(result.data).to.have.property('id', 1);
|
||||
expect(result.data)
|
||||
.to.have.property('warnings')
|
||||
.that.is.an('array')
|
||||
.that.has.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an api response when request receives a 401 response', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'put').returns(
|
||||
Promise.resolve({
|
||||
status: 401,
|
||||
statusText: 'Unauthorized',
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.saveRecord(new SaveRecordRequest(1, 1));
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse);
|
||||
expect(result).to.have.property('statusCode', 401);
|
||||
expect(result).to.have.property('isSuccessful', false);
|
||||
expect(result).to.have.property('message', undefined);
|
||||
expect(result).to.have.property('data', null);
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an api response when request receives a 403 response', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'put').returns(
|
||||
Promise.resolve({
|
||||
status: 403,
|
||||
statusText: 'Forbidden',
|
||||
data: { message: 'Forbidden' },
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.saveRecord(new SaveRecordRequest(1, 1));
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse);
|
||||
expect(result).to.have.property('statusCode', 403);
|
||||
expect(result).to.have.property('isSuccessful', false);
|
||||
expect(result).to.have.property('message', 'Forbidden');
|
||||
expect(result).to.have.property('data', null);
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an api response when request receives a 404 response', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'put').returns(
|
||||
Promise.resolve({
|
||||
status: 404,
|
||||
statusText: 'Not Found',
|
||||
data: { message: 'Not Found' },
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.saveRecord(new SaveRecordRequest(1, 1));
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse);
|
||||
expect(result).to.have.property('statusCode', 404);
|
||||
expect(result).to.have.property('isSuccessful', false);
|
||||
expect(result).to.have.property('message', 'Not Found');
|
||||
expect(result).to.have.property('data', null);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { SaveRecordResponse } from '../src/models/SaveRecordResponse';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('SaveRecordResponse', function () {
|
||||
it('should be defined', function () {
|
||||
expect(SaveRecordResponse).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should have a constructor', function () {
|
||||
expect(SaveRecordResponse).to.have.property('constructor');
|
||||
});
|
||||
|
||||
it('should have 1 parameter', function () {
|
||||
expect(SaveRecordResponse).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should have a id property', function () {
|
||||
expect(new SaveRecordResponse(1)).to.have.property('id');
|
||||
});
|
||||
|
||||
it('should have a warnings property', function () {
|
||||
expect(new SaveRecordResponse(1)).to.have.property('warnings');
|
||||
});
|
||||
|
||||
it('should have a constructor that sets its properties correctly', function () {
|
||||
const response = new SaveRecordResponse(1);
|
||||
expect(response.id).to.equal(1);
|
||||
expect(response.warnings).to.be.an('array').that.has.lengthOf(0);
|
||||
});
|
||||
|
||||
it('should have a constructor that sets its properties correctly when passed warnings', function () {
|
||||
const response = new SaveRecordResponse(1, ['warning1', 'warning2']);
|
||||
expect(response.id).to.equal(1);
|
||||
expect(response.warnings).to.be.an('array').that.has.lengthOf(2);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user