Skip to content

Commit

Permalink
More tests for multikey index
Browse files Browse the repository at this point in the history
  • Loading branch information
lbnascimento committed Mar 25, 2020
1 parent 78c5a04 commit ba6fab8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
72 changes: 65 additions & 7 deletions LiteDB.Tests/Engine/Index_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public void Index_With_No_Name()
var users = db.GetCollection("users");
var indexes = db.GetCollection("$indexes");

users.Insert(new BsonDocument {["name"] = new BsonDocument {["first"] = "John", ["last"] = "Doe"}});
users.Insert(new BsonDocument {["name"] = new BsonDocument {["first"] = "Marco", ["last"] = "Pollo"}});
users.Insert(new BsonDocument { ["name"] = new BsonDocument { ["first"] = "John", ["last"] = "Doe" } });
users.Insert(new BsonDocument { ["name"] = new BsonDocument { ["first"] = "Marco", ["last"] = "Pollo" } });

// no index name defined
users.EnsureIndex("name.last");
Expand All @@ -36,11 +36,11 @@ public void Index_Order()
var col = db.GetCollection("col");
var indexes = db.GetCollection("$indexes");

col.Insert(new BsonDocument {{"text", "D"}});
col.Insert(new BsonDocument {{"text", "A"}});
col.Insert(new BsonDocument {{"text", "E"}});
col.Insert(new BsonDocument {{"text", "C"}});
col.Insert(new BsonDocument {{"text", "B"}});
col.Insert(new BsonDocument { { "text", "D" } });
col.Insert(new BsonDocument { { "text", "A" } });
col.Insert(new BsonDocument { { "text", "E" } });
col.Insert(new BsonDocument { { "text", "C" } });
col.Insert(new BsonDocument { { "text", "B" } });

col.EnsureIndex("text");

Expand Down Expand Up @@ -139,5 +139,63 @@ public void EnsureIndex_Invalid_Arguments()
Assert.Equal("expression", exn.ParamName);
}
}

[Fact]
public void MultiKey_Index_Test()
{
using var db = new LiteDatabase("filename=:memory:");
var col = db.GetCollection("customers", BsonAutoId.Int32);
col.EnsureIndex("$.Phones[*].Type");

var doc1 = new BsonDocument
{
["Name"] = "John Doe",
["Phones"] = new BsonArray
(
new BsonDocument
{
["Type"] = "Mobile",
["Number"] = "9876-5432"
},
new BsonDocument
{
["Type"] = "Fixed",
["Number"] = "3333-3333"
}
)
};

var doc2 = new BsonDocument
{
["Name"] = "Jane Doe",
["Phones"] = new BsonArray
(
new BsonDocument
{
["Type"] = "Fixed",
["Number"] = "3000-0000"
}
)
};

col.Insert(doc1);
col.Insert(doc2);

var query1 = "select $ from customers where $.Phones[*].Type any = 'Mobile'";
var query2 = "select $ from customers where $.Phones[*].Type any = 'Fixed'";

var explain1 = db.Execute("explain " + query1).First();
Assert.True(!explain1["index"]["mode"].AsString.Contains("_id"));

var explain2 = db.Execute("explain " + query2).First();
Assert.True(!explain2["index"]["mode"].AsString.Contains("_id"));


var result1 = db.Execute(query1).ToArray();
Assert.True(result1.Length == 1);

var result2 = db.Execute(query2).ToArray();
Assert.True(result2.Length == 2);
}
}
}
2 changes: 1 addition & 1 deletion LiteDB/Document/BsonArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public BsonArray(List<BsonValue> array)
this.AddRange(array);
}

public BsonArray(BsonValue[] array)
public BsonArray(params BsonValue[] array)
: this()
{
if (array == null) throw new ArgumentNullException(nameof(array));
Expand Down

0 comments on commit ba6fab8

Please sign in to comment.