Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Jun 19, 2020
1 parent 55bfa52 commit c85e81f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 36 deletions.
1 change: 1 addition & 0 deletions lib/helpers/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Endpoint {
ping: '{{core}}/admin/ping',
schema: '{{core}}/schema',
schemaFields: '{{core}}/schema/fields',
schemaFieldTypes: '{{core}}/schema/fieldtypes',
admin: '{{core}}/admin'
};
}
Expand Down
49 changes: 26 additions & 23 deletions lib/helpers/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ const IGNORED_FIELDS = [
'id'
];

const FIELD_DEFAULTS = {
multiValued: false,
indexed: true,
stored: true
};

const FIELD_TYPES = {
string: 'string',
boolean: 'boolean',
date: 'pdate',
number: 'pint',
float: 'pfloat',
double: 'pdouble',
long: 'plong',
text: 'text_general'
string: { type: 'string', ...FIELD_DEFAULTS },
boolean: { type: 'boolean', ...FIELD_DEFAULTS },
date: { type: 'pdate', ...FIELD_DEFAULTS },
number: { type: 'pint', ...FIELD_DEFAULTS },
float: { type: 'pfloat', ...FIELD_DEFAULTS },
double: { type: 'pdouble', ...FIELD_DEFAULTS },
long: { type: 'plong', ...FIELD_DEFAULTS },
text: { type: 'text_general', ...FIELD_DEFAULTS }
};

class Schema {
Expand Down Expand Up @@ -52,10 +58,12 @@ class Schema {

static _buildFieldTypesQuery(fieldTypes, currentFieldTypes) {

fieldTypes = this._buildFieldTypes(fieldTypes);

currentFieldTypes = currentFieldTypes.filter(fieldType => fieldTypes.find(({ name }) => name === fieldType.name));

const fieldTypesToAdd = this._difference(fieldTypes, currentFieldTypes);
const fieldTypesToReplace = this._difference(currentFieldTypes, fieldTypesToAdd)
const fieldTypesToReplace = this._difference(fieldTypes, fieldTypesToAdd)
.filter(fieldType => !currentFieldTypes.some(currentFieldType => this._areEqualObjects(currentFieldType, fieldType)));

return {
Expand All @@ -65,19 +73,19 @@ class Schema {
}

static _buildFieldTypes(fieldTypes) {

return Object.entries(fieldTypes).map(([name, properties]) => ({ name, ...properties }));
}

static _buildFieldsQuery(schema, currentSchemas) {

const builtSchema = this._buildSchema(schema);
schema = this._buildSchema(schema);

const fieldsToAdd = this._difference(builtSchema, currentSchemas);
const fieldsToAdd = this._difference(schema, currentSchemas);

const fieldsToReplace = this._difference(builtSchema, fieldsToAdd)
const fieldsToReplace = this._difference(schema, fieldsToAdd)
.filter(field => !currentSchemas.some(currentField => this._areEqualObjects(currentField, field)));

const fieldsToDelete = this._difference(currentSchemas, builtSchema)
const fieldsToDelete = this._difference(currentSchemas, schema)
.map(({ name }) => ({ name }));

return {
Expand Down Expand Up @@ -110,10 +118,7 @@ class Schema {

return [{
name: field,
type: this._getType(type),
multiValued: false,
indexed: true,
stored: true
...this._getType(type)
}];
}

Expand All @@ -123,10 +128,8 @@ class Schema {

return [{
name: field,
type: this._getType(type),
multiValued: true,
indexed: true,
stored: true
...this._getType(type),
multiValued: true
}];
}

Expand All @@ -142,7 +145,7 @@ class Schema {
}

static _getType(type) {
return FIELD_TYPES[type] || type;
return FIELD_TYPES[type] || { type };
}

static _difference(schemaA, schemaB) {
Expand Down
6 changes: 3 additions & 3 deletions lib/solr.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class Solr {
*/
async getFieldTypes() {

const endpoint = Endpoint.create(Endpoint.presets.schemaFields, this.url, this.core);
const endpoint = Endpoint.create(Endpoint.presets.schemaFieldTypes, this.url, this.core);

const res = await this.request.get(endpoint);

Expand Down Expand Up @@ -447,9 +447,9 @@ class Solr {

const endpoint = Endpoint.create(Endpoint.presets.ping, this.url, this.core);

try {
const res = await this.request.get(endpoint);

const res = await this.request.get(endpoint);
try {

Response.validate(res, {
status: 'string'
Expand Down
114 changes: 104 additions & 10 deletions tests/solr-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,33 @@ describe('Solr', () => {
};
}

static get fieldTypes() {
return {
someFieldType: {
class: 'solr.TextField',
indexed: true,
stored: true,
multiValued: false
},
otherFieldType: {
class: 'solr.StrField',
indexed: false,
stored: true,
multiValued: true
},
anotherFieldType: {
class: 'solr.TextField',
indexed: true,
stored: true
}
};
}

static get schema() {
return {
string: true,
custom: { type: 'someFieldType' },
otherCustom: { type: 'otherFieldType' },
string: { type: 'string' },
number: { type: 'number' },
float: { type: 'float' },
double: { type: 'double' },
Expand All @@ -55,6 +79,14 @@ describe('Solr', () => {
}

const builtSchemas = [
{
name: 'custom',
type: 'someFieldType'
},
{
name: 'otherCustom',
type: 'otherFieldType'
},
{
name: 'string',
type: 'string',
Expand Down Expand Up @@ -134,6 +166,38 @@ describe('Solr', () => {
}
];

const builtFieldTypes = [
{
name: 'someFieldType',
class: 'solr.TextField',
indexed: true,
stored: true,
multiValued: false
},
{
name: '_internal_solr_field',
class: 'solr.FooBar',
someProp: true
},
{
name: 'otherFieldType',
class: 'solr.StrField',
indexed: false,
stored: true,
multiValued: true
},
{
name: 'deprecatedFieldType',
class: 'solr.StrField'
},
{
name: 'anotherFieldType',
class: 'solr.strField',
indexed: true,
stored: true
}
];

const host = 'http://some-host.com';

const endpoints = {
Expand All @@ -142,6 +206,7 @@ describe('Solr', () => {
get: '/solr/some-core/query',
schema: '/solr/some-core/schema',
schemaFields: '/solr/some-core/schema/fields',
schemaFieldTypes: '/solr/some-core/schema/fieldtypes',
ping: '/solr/some-core/admin/ping'
};

Expand Down Expand Up @@ -989,7 +1054,6 @@ describe('Solr', () => {

request.done();
});

});

describe('updateSchema()', () => {
Expand All @@ -1003,6 +1067,9 @@ describe('Solr', () => {
type: 'string'
};

sandbox.stub(Solr.prototype, 'getFieldTypes')
.resolves(builtFieldTypes.slice(1));

sandbox.stub(Solr.prototype, 'getSchema')
.resolves([...currentSchema, deprecatedField]);

Expand All @@ -1011,8 +1078,9 @@ describe('Solr', () => {

const request = nock(host)
.post(endpoints.schema, {
'add-field-type': [builtFieldTypes[0]],
'replace-field-type': [{ ...builtFieldTypes[4], class: 'solr.TextField' }],
'add-field': builtSchemas.slice(3),
'replace-field': [],
'delete-field': [{ name: deprecatedField.name }]
})
.reply(200, {
Expand All @@ -1030,6 +1098,9 @@ describe('Solr', () => {

it('Should replace only the fields in Solr that are not equal that current schemas', async () => {

sandbox.stub(Solr.prototype, 'getFieldTypes')
.returns([...builtFieldTypes.slice(0, 3), { ...builtFieldTypes[4], class: 'solr.TextField' }]);

sandbox.stub(Solr.prototype, 'getSchema')
.resolves([{ ...builtSchemas[0], type: 'text' }, ...builtSchemas.slice(1)]);

Expand All @@ -1038,9 +1109,7 @@ describe('Solr', () => {

const request = nock(host)
.post(endpoints.schema, {
'add-field': [],
'replace-field': [builtSchemas[0]],
'delete-field': []
'replace-field': [builtSchemas[0]]
})
.reply(200, {
responseHeader: {
Expand All @@ -1060,6 +1129,12 @@ describe('Solr', () => {
sandbox.stub(FakeModel, 'schema')
.get(() => undefined);

sandbox.stub(FakeModel, 'fieldTypes')
.get(() => undefined);

sandbox.stub(Solr.prototype, 'getFieldTypes')
.returns([]);

sandbox.stub(Solr.prototype, 'getSchema')
.resolves(builtSchemas);

Expand All @@ -1068,8 +1143,6 @@ describe('Solr', () => {

const request = nock(host)
.post(endpoints.schema, {
'add-field': [],
'replace-field': [],
'delete-field': builtSchemas.map(({ name }) => ({ name }))
})
.reply(200, {
Expand All @@ -1087,6 +1160,9 @@ describe('Solr', () => {

it('Shouldn\'t call Solr POST api to update the schema when there are not changes to made', async () => {

sandbox.stub(Solr.prototype, 'getFieldTypes')
.returns([...builtFieldTypes.slice(0, 3), { ...builtFieldTypes[4], class: 'solr.TextField' }]);

sandbox.stub(Solr.prototype, 'getSchema')
.resolves(builtSchemas);

Expand Down Expand Up @@ -1304,11 +1380,29 @@ describe('Solr', () => {
request.done();
});

it('Should return false when the Solr ping request fails', async () => {
it('Should reject when the Solr ping request fails', async () => {

nock.disableNetConnect();

assert.deepEqual(await solr.ping(), false);
await assert.rejects(solr.ping(), {
name: 'SolrError',
code: SolrError.codes.REQUEST_FAILED
});
});

it('Should reject when the Solr ping fails due timeout', async () => {

const request = nock(host)
.get(endpoints.ping)
.delay(5000)
.reply(200);

await assert.rejects(solr.ping(), {
name: 'SolrError',
code: SolrError.codes.REQUEST_TIMEOUT
});

request.done();
});
});
});

0 comments on commit c85e81f

Please sign in to comment.