|
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using Raven.Database.Indexing;
using Raven.Database.Json;
using Raven.Database.Linq;
using Xunit;
namespace Raven.Tests.Linq
{
public class PerformingQueries
{
private const string query =
@"
from doc in docs
where doc.type == ""page""
select new { Key = doc.title, Value = doc.content, Size = doc.size };
";
[Fact]
public void Can_query_json()
{
var documents =
GetDocumentsFromString(
@"[
{'type':'page', title: 'hello', content: 'foobar', size: 2, '@metadata': {'@id': 1}},
{'type':'page', title: 'there', content: 'foobar 2', size: 3, '@metadata': {'@id': 2} },
{'type':'revision', size: 4, _id: 3}
]");
var transformer = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query });
var compiledQuery = transformer.GenerateInstance();
var actual = compiledQuery.MapDefinition(documents)
.Cast<object>().ToArray();
var expected = new[]
{
"{ Key = hello, Value = foobar, Size = 2, __document_id = 1 }",
"{ Key = there, Value = foobar 2, Size = 3, __document_id = 2 }"
};
Assert.Equal(expected.Length, actual.Length);
for (var i = 0; i < expected.Length; i++)
{
Assert.Equal(expected[i], actual[i].ToString());
}
}
public static IEnumerable<dynamic> GetDocumentsFromString(string json)
{
return JArray.Parse(json).Cast<JObject>().Select(JsonToExpando.Convert);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.