Skip to content

Commit

Permalink
Fixed unit tests and completed docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Jun 22, 2020
1 parent c85e81f commit 4c32ff3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Solr field types creation from model

### Changed
- Solr fields schema creation from model usage
- `ping()` method now throws when the request fails instead returning false

## [1.5.0] - 2020-06-11
### Added
Expand Down
36 changes: 34 additions & 2 deletions README.md
Expand Up @@ -87,14 +87,15 @@ Get the current fields schema frol Solr
- Rejects `SolrError`: When something bad occurs

### ***async*** `updateSchema(model)`
Update the fields schema by syncing the current fields schema in Solr with the defined schema in the model static getter `schema`.
Update the fields types and schema by syncing the current fields schema in Solr with the defined schema in the model static getter `schema`* and `fieldTypes`**.

- model `Model`: A model instance

- Resolves `Boolean`: `true` if the operation was successful
- Rejects `SolrError`: When something bad occurs

If you need details about how to define the fields schema in the model, see the schema apart [below](#schema)
*If you need details about how to define the fields schema in the model, see the schema apart [below](#Fields-schema)
**If you need details about how to define the field types in the model, see the field types apart [below](#Field-types)

#### Fields schema

Expand Down Expand Up @@ -201,6 +202,36 @@ These fields stores an object with multiple properties that **can be of differen

It will show as a **full object** on `get` operations.

#### Field types
Custom field types can be defined in the model static getter `fieldTypes` like this:

```js
class MyModel extends Model {

static get fieldTypes(){

return {
myFieldType: {
class: 'solr.TextField',
indexed: false,
stored: true
// ...
}
}
}

static get schema(){

return {
myCustomField: { type: 'myFieldType' }
// ...
}
}
}
```

The field type content accepts native Solr field type properties, please check the [Solr field types documentation for more information](https://lucene.apache.org/solr/guide/8_5/field-type-definitions-and-properties.html#field-type-definitions-and-properties).

### ***async*** `insert(model, item)`
Inserts one document in a solr core

Expand Down Expand Up @@ -426,6 +457,7 @@ Removes one or more documents in a solr core
Checks if the Solr host and core is online

- Resolves `Boolean`: `true` if the Solr ping status is `'OK'`, `false` otherwise
- Rejects: `SolrError`: When something bad occurs

## Errors

Expand Down
4 changes: 4 additions & 0 deletions lib/helpers/schema.js
Expand Up @@ -145,6 +145,10 @@ class Schema {
}

static _getType(type) {

if(!type)
return FIELD_TYPES.string;

return FIELD_TYPES[type] || { type };
}

Expand Down
10 changes: 9 additions & 1 deletion lib/solr.js
Expand Up @@ -447,7 +447,15 @@ class Solr {

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

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

try {

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

} catch(err) {
throw err;
}

try {

Expand Down
38 changes: 37 additions & 1 deletion tests/solr-test.js
Expand Up @@ -57,7 +57,7 @@ describe('Solr', () => {
return {
custom: { type: 'someFieldType' },
otherCustom: { type: 'otherFieldType' },
string: { type: 'string' },
string: true,
number: { type: 'number' },
float: { type: 'float' },
double: { type: 'double' },
Expand Down Expand Up @@ -1056,6 +1056,27 @@ describe('Solr', () => {
});
});

describe('getFieldTypes()', () => {

it('Should call Solr GET api to get the field types', async () => {

const request = nock(host)
.get(endpoints.schemaFieldTypes)
.reply(200, {
responseHeader: {
status: 0
},
fieldTypes: builtFieldTypes
});

const currentSchemas = await solr.getFieldTypes();

assert.deepStrictEqual(currentSchemas, builtFieldTypes);

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

describe('updateSchema()', () => {

it('Should call Solr POST api to update the schema and reload the core', async () => {
Expand Down Expand Up @@ -1380,6 +1401,21 @@ describe('Solr', () => {
request.done();
});

it('Should return false when the Solr ping status response is invalid', async () => {

const request = nock(host)
.get(endpoints.ping)
.reply(200, {
responseHeader: {
status: 0
}
});

assert.deepEqual(await solr.ping(), false);

request.done();
});

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

nock.disableNetConnect();
Expand Down

0 comments on commit 4c32ff3

Please sign in to comment.