Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests for multikey index and params BsonArray constructor #1591

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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