Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a test for using IfEntityIs as a member

  • Loading branch information...
commit 18666234dc1b07f17b3193b966fbf8fee034caf9 1 parent 0844b13
@ayende ayende authored
View
12 Raven.Database/Linq/PrivateExtensions/DynamicExtensionMethods.cs
@@ -1,7 +1,10 @@
-using System.Collections;
+using System;
+using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using Raven.Abstractions.Data;
+using Raven.Abstractions.Linq;
namespace Raven.Database.Linq.PrivateExtensions
{
@@ -11,6 +14,13 @@ namespace Raven.Database.Linq.PrivateExtensions
/// </summary>
public class DynamicExtensionMethods
{
+ public static object IfEntityIs(dynamic o, string entityName)
+ {
+ if (string.Equals(o[Constants.Metadata][Constants.RavenEntityName], entityName, StringComparison.InvariantCultureIgnoreCase))
+ return o;
+ return new DynamicNullObject();
+ }
+
public static string Reverse(string str)
{
var stringBuilder = new StringBuilder(str.Length);
View
220 Raven.Tests/MailingList/Everett.cs
@@ -0,0 +1,220 @@
+using System;
+using System.Linq;
+using Newtonsoft.Json;
+using Raven.Client.Document;
+using Raven.Client.Indexes;
+using Raven.Client.Linq;
+using Xunit;
+
+namespace Raven.Tests.MailingList
+{
+ public class transform_results_loading_document_from_property_on_loaded_document : RavenTest
+ {
+ public abstract class Account
+ {
+ public string Id { get; set; }
+ public string Name { get; set; }
+ public string Number { get; set; }
+ }
+
+ public class ClickTransaction
+ {
+ public string Id { get; set; }
+
+ public string AccountId { get; set; }
+
+ public int Quantity { get; set; }
+
+ public string DesignName { get; set; }
+
+ public string DesignId { get; set; }
+
+ public string UserEmail { get; set; }
+
+ public string ClickAllocationId { get; set; }
+
+ public DateTime Date { get; set; }
+
+ public string ComputerName { get; set; }
+
+ public string Type { get; set; }
+ }
+
+ public class ClickTransactionDailyDebitReportResult
+ {
+ public string AccountDealerId { get; set; }
+ public string AccountDealerName { get; set; }
+ public string AccountDealerNumber { get; set; }
+ public string AccountId { get; set; }
+ public string AccountName { get; set; }
+ public string AccountNumber { get; set; }
+ public string AccountType { get; set; }
+ public DateTime ClickTransactionDate { get; set; }
+ public int ClickTransactionQuantity { get; set; }
+ public string DesignName { get; set; }
+ public string UserEmail { get; set; }
+ }
+
+ public class ClickTransactions_DailyDebitReport : AbstractIndexCreationTask<ClickTransaction, ClickTransactions_DailyDebitReport.Result>
+ {
+ public class Result
+ {
+ public string AccountId { get; set; }
+ public DateTime Date { get; set; }
+ public string DesignName { get; set; }
+ public int Quantity { get; set; }
+ public string Type { get; set; }
+ public string UserEmail { get; set; }
+ }
+
+ public ClickTransactions_DailyDebitReport()
+ {
+ Map = docs =>
+ from doc in docs
+ where doc.Type == "Debit"
+ select new
+ {
+ doc.AccountId,
+ doc.Date,
+ doc.DesignName,
+ doc.Quantity,
+ doc.UserEmail,
+ };
+
+ Reduce = results =>
+ from result in results
+ group result by new { result.AccountId, result.DesignName, result.UserEmail, Date = result.Date.ToString("yyyyMMdd") } into g
+ select new
+ {
+ g.Key.AccountId,
+ Date = g.Select(x => x.Date.Date).FirstOrDefault(),
+ g.Key.DesignName,
+ Quantity = g.Sum(x => x.Quantity),
+ g.Key.UserEmail
+ };
+
+ TransformResults = (database, results) =>
+ from result in results
+ let account = database.Load<Account>(result.AccountId)
+ let customer = account.IfEntityIs<Customer>("Accounts")
+ let dealer = database.Load<Dealer>(customer.DealerId)
+ select new
+ {
+ AccountId = account.Id,
+ AccountName = account.Name,
+ AccountNumber = account.Number,
+ AccountDealerId = dealer != null ? dealer.Id : null,
+ AccountDealerName = dealer != null ? dealer.Name : null,
+ AccountDealerNumber = dealer != null ? dealer.Number : null,
+ ClickTransactionQuantity = result.Quantity,
+ ClickTransactionDate = result.Date,
+ ClickTransactionType = result.Type,
+ DesignName = result.DesignName,
+ UserEmail = result.UserEmail,
+ customer = customer
+ };
+ }
+ }
+
+ public class Customer : Account
+ {
+ public string Id { get; set; }
+ public string DealerId { get; set; }
+ }
+
+ public class Dealer : Account
+ {
+ public string Id { get; set; }
+ }
+
+ protected override void ModifyStore(Raven.Client.Embedded.EmbeddableDocumentStore documentStore)
+ {
+ base.ModifyStore(documentStore);
+ documentStore.Conventions = new DocumentConvention
+ {
+ CustomizeJsonSerializer = serializer => serializer.TypeNameHandling = TypeNameHandling.All,
+ DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites,
+ FindTypeTagName = type =>
+ {
+ if (typeof(Account).IsAssignableFrom(type))
+ return "Accounts";
+
+ return DocumentConvention.DefaultTypeTagName(type);
+ },
+ };
+ }
+
+ private void TestScenario(Raven.Client.Embedded.EmbeddableDocumentStore store)
+ {
+ using (var session = store.OpenSession())
+ {
+ session.Store(new Dealer { Id = "accounts/1", Name = "Dealer" });
+ session.Store(new Customer { Id = "accounts/2", Name = "Customer", DealerId = "accounts/1" });
+ session.Store(new ClickTransaction
+ {
+ AccountId = "accounts/2",
+ Date = DateTime.Today,
+ Quantity = 100,
+ Type = "Debit",
+ DesignName = "N009876",
+ UserEmail = "user@customer.com"
+ });
+ session.SaveChanges();
+ }
+
+ WaitForIndexing(store);
+
+ using (var session = store.OpenSession())
+ {
+ var result = session.Advanced.LuceneQuery<ClickTransactionDailyDebitReportResult, ClickTransactions_DailyDebitReport>()
+ .WaitForNonStaleResultsAsOfNow()
+ .ToArray()
+ .FirstOrDefault();
+
+ Assert.Equal("Dealer", result.AccountDealerName);
+ }
+ }
+
+ [Fact]
+ public void defining_TransformResults_manually_with_text()
+ {
+ using (var store = NewDocumentStore())
+ {
+ var indexTask = new ClickTransactions_DailyDebitReport() { Conventions = store.Conventions };
+ var definition = indexTask.CreateIndexDefinition();
+ definition.TransformResults = @"from result in results
+let account = Database.Load(result.AccountId)
+let dealer = Database.Load(account.DealerId)
+select new
+{
+ AccountId = account.Id,
+ AccountName = account.Name,
+ AccountNumber = account.Number,
+ AccountDealerId = dealer != null ? dealer.Id : null,
+ AccountDealerName = dealer != null ? dealer.Name : null,
+ AccountDealerNumber = dealer != null ? dealer.Number : null,
+ ClickTransactionQuantity = result.Quantity,
+ ClickTransactionDate = result.Date,
+ ClickTransactionType = result.Type,
+ DesignName = result.DesignName,
+ UserEmail = result.UserEmail
+}";
+ store.DatabaseCommands.PutIndex(indexTask.IndexName, definition);
+
+ TestScenario(store);
+ }
+ }
+
+ [Fact]
+ public void defining_TransformResults_using_features_of_AbstractIndexCreationTask()
+ {
+ using (var store = NewDocumentStore())
+ {
+ var clickTransactionsDailyDebitReport = new ClickTransactions_DailyDebitReport();
+ clickTransactionsDailyDebitReport.Execute(store);
+
+ TestScenario(store);
+ }
+ }
+ }
+}
View
1  Raven.Tests/Raven.Tests.csproj
@@ -563,6 +563,7 @@
<Compile Include="LocalClientTest.cs" />
<Compile Include="MailingList\DynamicQueryIndexSelection.cs" />
<Compile Include="MailingList\EnumAsInts.cs" />
+ <Compile Include="MailingList\Everett.cs" />
<Compile Include="MailingList\GuidProjection.cs" />
<Compile Include="MailingList\LazyStats.cs" />
<Compile Include="MailingList\RavenDbPutTest.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.