|
using System;
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 LinqTransformerCompilationTests
{
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 Will_compile_query_successfully()
{
var dynamicQueryCompiler = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query });
dynamicQueryCompiler.GenerateInstance();
var compiled = dynamicQueryCompiler.GeneratedType;
Assert.NotNull(compiled);
}
[Fact]
public void Can_create_new_instance_from_query()
{
var dynamicQueryCompiler = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query });
dynamicQueryCompiler.GenerateInstance();
var compiled = dynamicQueryCompiler.GeneratedType;
Activator.CreateInstance(compiled);
}
[Fact]
public void Can_execute_query()
{
var dynamicQueryCompiler = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query });
var generator = dynamicQueryCompiler.GenerateInstance();
var results = generator.MapDefinition(new[]
{
GetDocumentFromString(
@"
{
'@metadata': {'@id': 1},
'Type': 'page',
'Title': 'doc1',
'Content': 'Foobar',
'Size': 31
}")
,
GetDocumentFromString(
@"
{
'@metadata': {'@id': 2},
'Type': 'not a page',
}")
,
GetDocumentFromString(
@"
{
'@metadata': {'@id': 3},
'Type': 'page',
'Title': 'doc2',
'Content': 'Foobar',
'Size': 31
}")
,
}).Cast<object>().ToArray();
var expected = new[]
{
"{ Key = doc1, Value = Foobar, Size = 31, __document_id = 1 }",
"{ Key = doc2, Value = Foobar, Size = 31, __document_id = 3 }"
};
for (var i = 0; i < results.Length; i++)
{
Assert.Equal(expected[i], results[i].ToString());
}
}
public static dynamic GetDocumentFromString(string json)
{
return JsonToExpando.Convert(JObject.Parse(json));
}
}
}
|
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.