mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-08-24 06:33:51 +02:00
more tests, more fixes
This commit is contained in:
643
test/basic.js
643
test/basic.js
@@ -1,5 +1,5 @@
|
||||
global.self = global;
|
||||
const env = process.argv[3];
|
||||
const env = process.argv[3] && process.argv[3].startsWith("--") ? process.argv[4] : process.argv[3];
|
||||
import { expect } from "chai";
|
||||
// console.log("--------------");
|
||||
// console.log(env ? "dist/" + env + ".js" : "src/bundle.js")
|
||||
@@ -204,6 +204,9 @@ describe("Search Scoring", function(){
|
||||
expect(result.length).to.equal(1);
|
||||
expect(result).to.eql([1]);
|
||||
|
||||
result = index.search("cute dogs cats", { suggest: true });
|
||||
expect(result).to.eql([1, 6, 5, 4, 3, 2, 0]);
|
||||
|
||||
result = index.search("cute cat");
|
||||
expect(result.length).to.equal(0);
|
||||
});
|
||||
@@ -311,7 +314,10 @@ describe("Update (Sync)", function(){
|
||||
|
||||
it("Should have been updated to the index", function(){
|
||||
|
||||
const index = new Index({ tokenize: "full" });
|
||||
const index = new Index({
|
||||
fastupdate: true,
|
||||
tokenize: "full"
|
||||
});
|
||||
index.add(1, "foo");
|
||||
index.add(2, "bar");
|
||||
index.add(3, "foobar");
|
||||
@@ -365,7 +371,10 @@ describe("Remove (Sync)", function(){
|
||||
|
||||
it("Should have been removed from the index", function(){
|
||||
|
||||
const index = new Index({ tokenize: "full" });
|
||||
const index = new Index({
|
||||
fastupdate: true,
|
||||
tokenize: "full"
|
||||
});
|
||||
index.add(1, "bar");
|
||||
index.add(2, "foobar");
|
||||
index.add(3, "foo");
|
||||
@@ -382,577 +391,63 @@ describe("Remove (Sync)", function(){
|
||||
});
|
||||
});
|
||||
|
||||
// if(env !== "light") describe("Operators", function(){
|
||||
//
|
||||
// var data = [{
|
||||
// id: 2,
|
||||
// title: "Title 3",
|
||||
// body: "Body 3",
|
||||
// blacklist: "x1"
|
||||
// },{
|
||||
// id: 1,
|
||||
// title: "Title 2",
|
||||
// body: "Body 2",
|
||||
// blacklist: "x2"
|
||||
// },{
|
||||
// id: 0,
|
||||
// title: "Title 1",
|
||||
// body: "Body 1",
|
||||
// blacklist: "x3"
|
||||
// }];
|
||||
//
|
||||
// var index = new FlexSearch({
|
||||
// tokenize: "forward",
|
||||
// doc: {
|
||||
// id: "id",
|
||||
// field: ["title", "body", "blacklist"]
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// it("Should have been properly applied logic", function(){
|
||||
//
|
||||
// index.add(data);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "body",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "xxx",
|
||||
// bool: "not"
|
||||
// }])).to.have.members(data);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "title",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "xxx",
|
||||
// bool: "not"
|
||||
// }])).to.have.length(0);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "title",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "xxx",
|
||||
// bool: "not"
|
||||
// }])).to.have.members(data);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "title",
|
||||
// bool: "or"
|
||||
// }])).to.have.members(data);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "title",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "x1",
|
||||
// bool: "not"
|
||||
// }])).to.have.members([data[1], data[2]]);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "body",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "title",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "x1",
|
||||
// bool: "not"
|
||||
// }])).to.have.length(0);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "blacklist",
|
||||
// query: "x1",
|
||||
// bool: "not"
|
||||
// },{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "body",
|
||||
// bool: "or"
|
||||
// }])).to.have.members([data[1], data[2]]);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "body",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "body",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "x2",
|
||||
// bool: "not"
|
||||
// }])).to.have.members([data[0], data[2]]);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "blacklist",
|
||||
// query: "x2",
|
||||
// bool: "not"
|
||||
// },{
|
||||
// field: "title",
|
||||
// query: "body",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "body",
|
||||
// bool: "and"
|
||||
// }])).to.have.members([data[0], data[2]]);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "body",
|
||||
// bool: "or"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "x2",
|
||||
// bool: "not"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "body",
|
||||
// bool: "and"
|
||||
// }])).to.have.members([data[0], data[2]]);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "body",
|
||||
// bool: "and"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "x",
|
||||
// bool: "not"
|
||||
// }])).to.have.length(0);
|
||||
//
|
||||
// expect(index.search([{
|
||||
// field: "title",
|
||||
// query: "title",
|
||||
// bool: "not"
|
||||
// },{
|
||||
// field: "body",
|
||||
// query: "body",
|
||||
// bool: "not"
|
||||
// },{
|
||||
// field: "blacklist",
|
||||
// query: "x",
|
||||
// bool: "not"
|
||||
// }])).to.have.length(0);
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// describe("Reserved Words", function(){
|
||||
//
|
||||
// it("Should have been indexed properly", function(){
|
||||
//
|
||||
// var index = new FlexSearch({
|
||||
// encode: function(str){ return [str]; },
|
||||
// tokenize: "strict",
|
||||
// threshold: 0,
|
||||
// depth: 3
|
||||
// });
|
||||
//
|
||||
// var array = Object.getOwnPropertyNames({}.__proto__);
|
||||
// array = array.concat(Object.getOwnPropertyNames(index));
|
||||
//
|
||||
// array.push("prototype");
|
||||
// array.push("constructor");
|
||||
// array.push("__proto__");
|
||||
//
|
||||
// if(env !== "min"){
|
||||
//
|
||||
// array.push("concat");
|
||||
// array.push("hasOwnProperty");
|
||||
// array.push("length");
|
||||
// }
|
||||
//
|
||||
// for(var i = 0; i < array.length; i++){
|
||||
//
|
||||
// index.add(array[i], array[i]);
|
||||
// }
|
||||
//
|
||||
// for(var i = 0; i < array.length; i++){
|
||||
//
|
||||
// // TODO: this word is reserved and can't be indexed
|
||||
// if(array[i] === "_ctx"){
|
||||
//
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// expect(index.search(array[i])).to.have.members([array[i]]);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// // ------------------------------------------------------------------------
|
||||
// // Export / Import
|
||||
// // ------------------------------------------------------------------------
|
||||
//
|
||||
// if(env !== "light") describe("Export / Import", function(){
|
||||
//
|
||||
// var data;
|
||||
//
|
||||
// it("Should have been exported properly", function(){
|
||||
//
|
||||
// var index = new FlexSearch("match");
|
||||
//
|
||||
// index.add(0, "foo");
|
||||
// index.add(1, "bar");
|
||||
// index.add(2, "foobar");
|
||||
//
|
||||
// data = index.export();
|
||||
//
|
||||
// if(env === ""){
|
||||
//
|
||||
// expect(data).to.equal(JSON.stringify(
|
||||
// [
|
||||
// index._map,
|
||||
// index._ctx,
|
||||
// Object.keys(index._ids)
|
||||
// ]
|
||||
// ));
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// it("Should have been imported properly", function(){
|
||||
//
|
||||
// var index = new FlexSearch("match");
|
||||
//
|
||||
// index.import(data);
|
||||
//
|
||||
// expect(index.length).to.equal(3);
|
||||
//
|
||||
// expect(index.search("foo")).to.have.lengthOf(2);
|
||||
// expect(index.search("bar")).to.have.lengthOf(2);
|
||||
// expect(index.search("foobar")).to.have.lengthOf(1);
|
||||
// expect(index.search("foobar")[0]).to.equal(2);
|
||||
// });
|
||||
//
|
||||
// it("Should have been exported properly (documents)", function(){
|
||||
//
|
||||
// var index = new FlexSearch({
|
||||
//
|
||||
// tokenize: "strict",
|
||||
// threshold: 1,
|
||||
// resolution: 3,
|
||||
// depth: 1,
|
||||
// doc: {
|
||||
// id: "id",
|
||||
// field: ["title", "content"]
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// var docs = [{
|
||||
// id: 1,
|
||||
// title: "Title 2",
|
||||
// content: "foobar"
|
||||
// },{
|
||||
// id: 0,
|
||||
// title: "Title 1",
|
||||
// content: "foo"
|
||||
// },{
|
||||
// id: 2,
|
||||
// title: "Title 3",
|
||||
// content: "bar"
|
||||
// }];
|
||||
//
|
||||
// index.add(docs);
|
||||
// data = index.export();
|
||||
//
|
||||
// if(env === ""){
|
||||
//
|
||||
// expect(index.doc.index["title"].length).to.equal(3);
|
||||
// expect(data).to.equal(JSON.stringify([
|
||||
// [
|
||||
// index.doc.index["title"]._map,
|
||||
// index.doc.index["title"]._ctx,
|
||||
// Object.keys(index.doc.index["title"]._ids)
|
||||
// ],
|
||||
// [
|
||||
// index.doc.index["content"]._map,
|
||||
// index.doc.index["content"]._ctx,
|
||||
// Object.keys(index.doc.index["content"]._ids)
|
||||
// ],
|
||||
// index._doc
|
||||
// ]));
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// it("Should have been imported properly (documents)", function(){
|
||||
//
|
||||
// var index = new FlexSearch({
|
||||
//
|
||||
// tokenize: "strict",
|
||||
// threshold: 1,
|
||||
// resolution: 3,
|
||||
// depth: 1,
|
||||
// doc: {
|
||||
// id: "id",
|
||||
// field: ["title", "content"]
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// index.import(data);
|
||||
//
|
||||
// if(env === ""){
|
||||
//
|
||||
// expect(index.doc.index["title"].length).to.equal(3);
|
||||
// expect(index.doc.index["content"].length).to.equal(3);
|
||||
// }
|
||||
//
|
||||
// expect(index.search("foo")).to.have.lengthOf(1);
|
||||
// expect(index.search("bar")).to.have.lengthOf(1);
|
||||
// expect(index.search("foobar")).to.have.lengthOf(1);
|
||||
// expect(index.search("foobar")[0].id).to.equal(1);
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// // ------------------------------------------------------------------------
|
||||
// // Presets
|
||||
// // ------------------------------------------------------------------------
|
||||
//
|
||||
// describe("Presets", function(){
|
||||
//
|
||||
// it("Should have been properly initialized", function(){
|
||||
//
|
||||
// expect(FlexSearch("memory").length).to.equal(0);
|
||||
// expect(FlexSearch("speed").length).to.equal(0);
|
||||
// expect(FlexSearch("match").length).to.equal(0);
|
||||
// expect(FlexSearch("score").length).to.equal(0);
|
||||
// expect(FlexSearch("balance").length).to.equal(0);
|
||||
// expect(FlexSearch("fast").length).to.equal(0);
|
||||
// });
|
||||
//
|
||||
// it("Should have been properly extended", function(){
|
||||
//
|
||||
// var index = FlexSearch("fast");
|
||||
// index.add(0, "foobar");
|
||||
// expect(index.search("bar")).to.have.lengthOf(0);
|
||||
//
|
||||
// index = FlexSearch({preset: "speed", id: "test", tokenize: "reverse"});
|
||||
// expect(index.id).to.equal("test");
|
||||
// index.add(0, "foobar");
|
||||
// expect(index.search("bar")).to.have.lengthOf(1);
|
||||
// expect(index.search("bar")).to.have.members([0])
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// // ------------------------------------------------------------------------
|
||||
// // Feature Tests
|
||||
// // ------------------------------------------------------------------------
|
||||
//
|
||||
// describe("Add Matchers", function(){
|
||||
//
|
||||
// it("Should have been added properly", function(){
|
||||
//
|
||||
// flexsearch_forward.init({
|
||||
//
|
||||
// tokenize: "forward",
|
||||
// matcher: {
|
||||
//
|
||||
// "1": "a",
|
||||
// "2": "b",
|
||||
// "3": "c",
|
||||
// "7": "e",
|
||||
// "8": "f",
|
||||
// "[456]": "d"
|
||||
// }
|
||||
//
|
||||
// }).add(0, "12345678");
|
||||
//
|
||||
// expect(flexsearch_forward.search("12345678")).to.include(0);
|
||||
// expect(flexsearch_forward.search("abcd")).to.include(0);
|
||||
// expect(flexsearch_forward.encode("12345678")).to.eql(["abcdddef"]);
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// // ------------------------------------------------------------------------
|
||||
// // Caching
|
||||
// // ------------------------------------------------------------------------
|
||||
//
|
||||
// if(env !== "light"){
|
||||
//
|
||||
// describe("Caching", function(){
|
||||
//
|
||||
// it("Should have been cached properly", function(){
|
||||
//
|
||||
// flexsearch_cache.add(0, "foo")
|
||||
// .add(1, "bar")
|
||||
// .add(2, "foobar");
|
||||
// // fetch:
|
||||
// expect(flexsearch_cache.search("foo")).to.have.members([0, 2]);
|
||||
// expect(flexsearch_cache.search("bar")).to.have.members([1, 2]);
|
||||
// expect(flexsearch_cache.search("foobar")).to.include(2);
|
||||
//
|
||||
// // cache:
|
||||
// expect(flexsearch_cache.search("foo")).to.have.members([0, 2]);
|
||||
// expect(flexsearch_cache.search("bar")).to.have.members([1, 2]);
|
||||
// expect(flexsearch_cache.search("foobar")).to.include(2);
|
||||
//
|
||||
// // update:
|
||||
// flexsearch_cache.remove(2).update(1, "foo").add(3, "foobar");
|
||||
//
|
||||
// // fetch:
|
||||
// expect(flexsearch_cache.search("foo")).to.have.members([0, 1, 3]);
|
||||
// expect(flexsearch_cache.search("bar")).to.include(3);
|
||||
// expect(flexsearch_cache.search("foobar")).to.include(3);
|
||||
//
|
||||
// // cache:
|
||||
// expect(flexsearch_cache.search("foo")).to.have.members([0, 1, 3]);
|
||||
// expect(flexsearch_cache.search("bar")).to.include(3);
|
||||
// expect(flexsearch_cache.search("foobar")).to.include(3);
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// // ------------------------------------------------------------------------
|
||||
// // Debug Information
|
||||
// // ------------------------------------------------------------------------
|
||||
//
|
||||
// if(env !== "light" && env !== "min"){
|
||||
//
|
||||
// describe("Debug", function(){
|
||||
//
|
||||
// it("Should have been debug mode activated", function(){
|
||||
//
|
||||
// var info = flexsearch_cache.info();
|
||||
//
|
||||
// expect(info).to.have.keys([
|
||||
//
|
||||
// "id",
|
||||
// //"chars",
|
||||
// "cache",
|
||||
// "items",
|
||||
// "matcher",
|
||||
// //"memory",
|
||||
// //"sequences",
|
||||
// "resolution",
|
||||
// "worker",
|
||||
// "contextual",
|
||||
// "depth",
|
||||
// "threshold"
|
||||
// ]);
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// // ------------------------------------------------------------------------
|
||||
// // Destroy
|
||||
// // ------------------------------------------------------------------------
|
||||
//
|
||||
// describe("Destroy", function(){
|
||||
//
|
||||
// it("Should have been destroyed properly", function(){
|
||||
//
|
||||
// var index = FlexSearch()
|
||||
// .add(0, "foo")
|
||||
// .add(1, "bar");
|
||||
//
|
||||
// expect(index.search("foo")).to.include(0);
|
||||
// expect(index.search("bar")).to.include(1);
|
||||
//
|
||||
// index.destroy();
|
||||
//
|
||||
// expect(index.search("foo")).to.have.lengthOf(0);
|
||||
// expect(index.search("bar")).to.have.lengthOf(0);
|
||||
// });
|
||||
//
|
||||
// if(env !== "light") it("Should have been destroyed properly (documents)", function(){
|
||||
//
|
||||
// var data = [{id: 0, title: "foo"}, {id: 1, title: "bar"}];
|
||||
//
|
||||
// var index = FlexSearch({doc: {id: "id", field: "title"}})
|
||||
// .add(data)
|
||||
// .add(data);
|
||||
//
|
||||
// expect(index.search("foo")).to.have.members([data[0]]);
|
||||
// expect(index.search("bar")).to.have.members([data[1]]);
|
||||
//
|
||||
// index.destroy();
|
||||
//
|
||||
// expect(index.search("foo")).to.have.lengthOf(0);
|
||||
// expect(index.search("bar")).to.have.lengthOf(0);
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// // ------------------------------------------------------------------------
|
||||
// // Chaining
|
||||
// // ------------------------------------------------------------------------
|
||||
//
|
||||
// describe("Chaining", function(){
|
||||
//
|
||||
// it("Should have been chained properly", function(){
|
||||
//
|
||||
// var index = FlexSearch({tokenize: "forward", matcher: {"â": "a"}})
|
||||
// .add(0, "foo")
|
||||
// .add(1, "bar");
|
||||
//
|
||||
// expect(index.search("foo")).to.include(0);
|
||||
// expect(index.search("bar")).to.include(1);
|
||||
// expect(index.encode("bâr")).to.eql(["bar"]);
|
||||
//
|
||||
// index.remove(0).update(1, "foo").add(2, "foobâr");
|
||||
//
|
||||
// expect(index.search("foo")).to.have.members([1, 2]);
|
||||
// expect(index.search("bar")).to.have.lengthOf(0);
|
||||
// expect(index.search("foobar")).to.include(2);
|
||||
//
|
||||
// index.clear().add(0, "foo").add(1, "bar");
|
||||
//
|
||||
// expect(index.search("foo")).to.include(0);
|
||||
// expect(index.search("bar")).to.include(1);
|
||||
// expect(index.search("foobar")).to.have.lengthOf(0);
|
||||
//
|
||||
// flexsearch_cache.destroy().init().add(0, "foo").add(1, "bar");
|
||||
//
|
||||
// expect(flexsearch_cache.search("foo")).to.include(0);
|
||||
// expect(flexsearch_cache.search("bar")).to.include(1);
|
||||
// expect(flexsearch_cache.search("foobar")).to.have.lengthOf(0);
|
||||
// });
|
||||
// });
|
||||
//}
|
||||
describe("Reserved Words", function(){
|
||||
|
||||
/* Test Helpers */
|
||||
it("Should have reserved properties taken into account", function(){
|
||||
|
||||
function test_encoder(str){
|
||||
const index = new Index({
|
||||
encode: function(str){ return [str]; },
|
||||
tokenize: "strict"
|
||||
});
|
||||
|
||||
return "-[" + str.toUpperCase() + "]-";
|
||||
}
|
||||
let array = Object.getOwnPropertyNames({}.__proto__);
|
||||
array = array.concat(Object.getOwnPropertyNames(index));
|
||||
array = array.concat(Object.getOwnPropertyNames(index.map));
|
||||
array.includes("prototype") || array.push("prototype");
|
||||
array.includes("constructor") || array.push("constructor");
|
||||
array.includes("__proto__") || array.push("__proto__");
|
||||
array.includes("concat") || array.push("concat");
|
||||
array.includes("hasOwnProperty") || array.push("hasOwnProperty");
|
||||
array.includes("length") || array.push("length");
|
||||
array.includes("ctx") || array.push("ctx");
|
||||
|
||||
for(let i = 0; i < array.length; i++){
|
||||
index.add(i, array[i]);
|
||||
}
|
||||
|
||||
for(let i = 0; i < array.length; i++){
|
||||
expect(index.search(array[i])).to.eql([i]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("Presets", function(){
|
||||
|
||||
it("Should have been properly initialized", function(){
|
||||
|
||||
expect(Index("memory").reg.size).to.equal(0);
|
||||
expect(Index("performance").reg.size).to.equal(0);
|
||||
expect(Index("match").reg.size).to.equal(0);
|
||||
expect(Index("score").reg.size).to.equal(0);
|
||||
expect(Index("default").reg.size).to.equal(0);
|
||||
});
|
||||
|
||||
it("Should have been properly extended", function(){
|
||||
|
||||
let index = Index({ preset: "performance" });
|
||||
index.add(0, "foobar");
|
||||
expect(index.search("bar")).to.have.lengthOf(0);
|
||||
|
||||
index = Index({
|
||||
preset: "performance",
|
||||
tokenize: "reverse"
|
||||
});
|
||||
|
||||
index.add(0, "foobar");
|
||||
expect(index.search("bar")).to.eql([0]);
|
||||
|
||||
index = Index("match");
|
||||
index.add(0, "foobar");
|
||||
expect(index.search("foo")).to.eql([0]);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user