Skip to content

Commit

Permalink
Added mocha tests for the parameter building functions for solr and n…
Browse files Browse the repository at this point in the history
…amed queries inside the vocab service.

Added a feature to expose all service methods when in mocha testing mode
  • Loading branch information
andrewbrazzatti committed Dec 4, 2024
1 parent d7c32f6 commit 9fea34e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
28 changes: 24 additions & 4 deletions core/src/CoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,31 @@ export module Services.Core {
* @returns {*}
*/
public exports(): any {
let exportedMethods: any = {};
if(process.env["sails_redbox__mochaTesting"] === "true") {
const allProperties = [
...Object.getOwnPropertyNames(Object.getPrototypeOf(this)), // Prototype methods
...Object.getOwnPropertyNames(this), // Instance properties
];


const uniqueProperties = Array.from(new Set(allProperties));
uniqueProperties.forEach((property) => {
const value = (this as any)[property];

// Check if the property is a function
if (typeof value === "function" && property !== "constructor") {
exportedMethods[property] = value.bind(this); // Bind the method to maintain `this` context
}

});
console.error("Exported Methods for Mocha Testing: ", exportedMethods);
} else {
// Merge default array and custom array from child.
var methods: any = this._defaultExportedMethods.concat(this._exportedMethods);
var exportedMethods: any = {};
let methods: any = this._defaultExportedMethods.concat(this._exportedMethods);


for (var i = 0; i < methods.length; i++) {
for (let i = 0; i < methods.length; i++) {
// Check if the method exists.
if (typeof this[methods[i]] !== 'undefined') {
// Check that the method shouldn't be private. (Exception for _config, which is a sails config)
Expand All @@ -89,7 +109,7 @@ export module Services.Core {
console.error('The method "' + methods[i] + '" does not exist on the controller ' + this);
}
}

}
return exportedMethods;
}

Expand Down
2 changes: 1 addition & 1 deletion support/integration-testing/docker-compose.mocha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
- sails_redbox__apiKey=c8e844fc-8550-497f-b970-7900ec8741ca
- sails_record__baseUrl_redbox=http://redbox:9000/redbox
- sails_record__baseUrl_mint=http://203.101.226.160/mint
# - sails_log__level=verbose
- sails_redbox__mochaTesting=true
- sails_auth__default__local__default__token=d077835a-696b-4728-85cf-3ffd57152b1e
- sails_security__csrf=false
- datacite_username=${datacite_username}
Expand Down
82 changes: 82 additions & 0 deletions test/unit/services/VocabService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@


describe('The VocabService', function () {
before(function (done) {
done();
});


it('Build a named query parameter map that includes both the search string and logged in user attributes', function (done) {
const user = {
id: 1,
username: 'testuser',
email: '[email protected]',
roles: ['Guest','Researcher','Admin']};

const queryConfig = {
querySource: 'database',
databaseQuery: 'test',
queryField: {
property: 'title',
type: 'string'
},
userQueryFields: [
{
property: 'userEmail',
userValueProperty: 'email'
},
{
property: 'userRole',
userValueProperty: 'roles'
}
]
}

const queryParamMap = VocabService.buildNamedQueryParamMap(queryConfig, 'test', user);
expect(queryParamMap).to.be.an('object');
expect(queryParamMap).to.have.property('userEmail');
expect(queryParamMap.userEmail).to.equal(user.email);
expect(queryParamMap).to.have.property('userRole');
expect(queryParamMap.userRole).to.equal(user.roles);

done();
});

it('Build a solr query that includes both the search string and logged in user attributes', function (done) {
const user = {
id: 1,
username: 'testuser',
email: '[email protected]',
roles: ['Guest','Researcher','Admin']};

const queryConfig = {
querySource: 'solr',
searchQuery:{
baseQuery: 'metaMetadata_type:rdmp'
},
queryField: {
property: 'title',
type: 'text'
},
userQueryFields: [
{
property: 'userEmail',
userValueProperty: 'email'
},
{
property: 'userRole',
userValueProperty: 'roles'
}
]
}
const brand ={
id: "1"
}

const solrQuery = VocabService.buildSolrParams(brand, 'test',queryConfig, 1, 1, 'json', user);
expect(solrQuery).to.equal('metaMetadata_type:rdmp&sort=date_object_modified desc&version=2.2&start=1&rows=1&fq=metaMetadata_brandId:1&wt=json&fq=title:test*&fq=userEmail:[email protected]&fq=userRole:Guest,Researcher,Admin');
done();
});


});
2 changes: 1 addition & 1 deletion typescript/api/services/VocabService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export module Services {

if (queryConfig.userQueryFields != null) {
for(let userQueryField of queryConfig.userQueryFields) {
let searchProperty = queryConfig.queryField.property;
let searchProperty = userQueryField.property;
query = query + '&fq=' + searchProperty + ':'+ _.get(user, userQueryField.userValueProperty, null);
}
}
Expand Down

0 comments on commit 9fea34e

Please sign in to comment.