Skip to content

Commit

Permalink
tests: update unit tests to use async based resource handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
pablopalacios committed Aug 18, 2024
1 parent 7d5e3b8 commit dfe26ea
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 150 deletions.
103 changes: 30 additions & 73 deletions tests/mock/MockErrorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@
var MockErrorService = {
resource: 'mock_error_service',

// ------------------------------------------------------------------
// CRUD Methods
// ------------------------------------------------------------------
read: async function ({ req, params }) {
const meta = this.meta || params.meta;
this.meta = null;

/**
* read operation (read as in CRUD).
* @method read
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
read: function (req, resource, params, config, callback) {
if (
req.query &&
req.query.cors &&
Expand All @@ -39,87 +27,56 @@ var MockErrorService = {
params[key] = value;
}
}
callback(
{
return {
err: {
statusCode: parseInt(params.statusCode),
output: params.output,
message: params.message,
read: 'error',
},
null,
this.meta || params.meta,
);
this.meta = null;
data: null,
meta,
};
},
/**
* create operation (create as in CRUD).
* @method create
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} body The JSON object that contains the resource data that is being created
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
create: function (req, resource, params, body, config, callback) {
callback(
{

create: async function ({ params }) {
const meta = this.meta || params.meta;
this.meta = null;

return {
err: {
statusCode: parseInt(params.statusCode),
message: params.message,
output: params.output,
create: 'error',
},
null,
this.meta || params.meta,
);
this.meta = null;
data: null,
meta,
};
},
/**
* update operation (update as in CRUD).
* @method update
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} body The JSON object that contains the resource data that is being updated
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
update: function (req, resource, params, body, config, callback) {
callback(
{

update: async function ({ params }) {
return {
err: {
statusCode: parseInt(params.statusCode),
message: params.message,
output: params.output,
update: 'error',
},
null,
);
data: null,
};
},
/**
* delete operation (delete as in CRUD).
* @method delete
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
delete: function (req, resource, params, config, callback) {
callback(
{

delete: async function ({ params }) {
return {
err: {
statusCode: parseInt(params.statusCode),
message: params.message,
output: params.output,
delete: 'error',
},
null,
);
data: null,
};
},
};

Expand Down
113 changes: 36 additions & 77 deletions tests/mock/MockService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@
var MockService = {
resource: 'mock_service',

// ------------------------------------------------------------------
// CRUD Methods
// ------------------------------------------------------------------
read: async function ({ req, resource, params }) {
const meta = this.meta || params.meta;
this.meta = null;

/**
* read operation (read as in CRUD).
* @method read
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
read: function (req, resource, params, config, callback) {
if (
req.query &&
req.query.cors &&
Expand All @@ -39,9 +27,9 @@ var MockService = {
params[key] = value;
}
}
callback(
null,
{

return {
data: {
operation: {
name: 'read',
success: true,
Expand All @@ -51,26 +39,16 @@ var MockService = {
params: params,
},
},
this.meta || params.meta,
);
this.meta = null;
meta,
};
},
/**
* create operation (create as in CRUD).
* @method create
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} body The JSON object that contains the resource data that is being created
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
create: function (req, resource, params, body, config, callback) {
callback(
null,
{

create: async function ({ resource, params }) {
const meta = this.meta || params.meta;
this.meta = null;

return {
data: {
operation: {
name: 'create',
success: true,
Expand All @@ -80,26 +58,17 @@ var MockService = {
params: params,
},
},
this.meta || params.meta,
);
this.meta = null;
err: null,
meta,
};
},
/**
* update operation (update as in CRUD).
* @method update
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} body The JSON object that contains the resource data that is being updated
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
update: function (req, resource, params, body, config, callback) {
callback(
null,
{

update: async function ({ resource, params }) {
const meta = this.meta || params.meta;
this.meta = null;

return {
data: {
operation: {
name: 'update',
success: true,
Expand All @@ -109,25 +78,16 @@ var MockService = {
params: params,
},
},
this.meta || params.meta,
);
this.meta = null;
meta,
};
},
/**
* delete operation (delete as in CRUD).
* @method delete
* @param {Object} req The request object from connect/express
* @param {String} resource The resource name
* @param {Object} params The parameters identify the resource, and along with information
* carried in query and matrix parameters in typical REST API
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
* @static
*/
delete: function (req, resource, params, config, callback) {
callback(
null,
{

delete: async function ({ resource, params }) {
const meta = this.meta || params.meta;
this.meta = null;

return {
data: {
operation: {
name: 'delete',
success: true,
Expand All @@ -137,9 +97,8 @@ var MockService = {
params: params,
},
},
this.meta || params.meta,
);
this.meta = null;
meta,
};
},
};

Expand Down

0 comments on commit dfe26ea

Please sign in to comment.