Skip to content

Instantly share code, notes, and snippets.

@afifmohammed
Last active October 12, 2015 17:47
Show Gist options
  • Save afifmohammed/4063715 to your computer and use it in GitHub Desktop.
Save afifmohammed/4063715 to your computer and use it in GitHub Desktop.
raven transform query
namespace RavenIndexSandbox.Customers
{
using System;
using System.Collections.Generic;
using System.Linq;
using FizzWare.NBuilder;
using NUnit.Framework;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
[Serializable]
public class CustomerAccount
{
public string CustomerId { get; set; }
public string AccountId { get; set; }
public string FacebookUserId { get; set; }
}
[Serializable]
public class Customer
{
public string Id { get; set; }
public string AccountId { get; set; }
}
[Serializable]
public class OAuthAccount
{
public string Provider { get; set; }
public string ProviderUserId { get; set; }
}
[Serializable]
public class Account
{
public string Id { get; set; }
public OAuthAccount[] OAuthAccounts { get; set; }
}
public class CustomerAccounts_ByCustomerIdAndFacebookId : AbstractIndexCreationTask<Customer, CustomerAccount>
{
public CustomerAccounts_ByCustomerIdAndFacebookId()
{
Map = customers => from customer in customers
select new CustomerAccount
{
CustomerId = customer.Id,
AccountId = customer.AccountId,
FacebookUserId = ""
};
TransformResults = (db, customers) =>
from customer in customers
let account = db.Load<Account>(customer.AccountId)
let facebook = account != null ? account.OAuthAccounts.SingleOrDefault(x => x.Provider == "facebook") : null
select new CustomerAccount
{
CustomerId = customer.CustomerId,
AccountId = customer.AccountId,
FacebookUserId = facebook != null ? facebook.ProviderUserId : customer.FacebookUserId
};
}
}
[TestFixture]
public class WhenQueryingCustomerAccounts
{
private static IDocumentStore _db;
private const string Facebookid = "131";
private const string Customerid = "customers/afif";
[SetUp]
public void BeforeTest()
{
_db = new EmbeddableDocumentStore() { RunInMemory = true}.Initialize();
using (var s = _db.OpenSession())
{
_db.ExecuteIndex(new CustomerAccounts_ByCustomerIdAndFacebookId());
var oauthAccounts = new List<OAuthAccount> {new OAuthAccount {Provider = "facebook", ProviderUserId = Facebookid}}.ToArray();
var account = Builder<Account>.CreateNew()
.With(x => x.OAuthAccounts = oauthAccounts)
.With(x => x.Id = "accounts/234")
.Build();
s.Store(account);
var customer = Builder<Customer>.CreateNew()
.With(x => x.Id = Customerid)
.With(x => x.AccountId = account.Id)
.Build();
s.Store(customer);
s.SaveChanges();
}
}
[TearDown]
public void AfterTest()
{
_db.Dispose();
}
[Test]
public void ShouldFindCustomerByCustomerId()
{
using (var s = _db.OpenSession())
{
var customerAccount =
s.Query<CustomerAccount, CustomerAccounts_ByCustomerIdAndFacebookId>()
.Customize(x => x.WaitForNonStaleResults())
.SingleOrDefault(x => x.CustomerId == Customerid);
Assert.That(customerAccount, Is.Not.Null);
Assert.That(customerAccount.FacebookUserId, Is.EqualTo(Facebookid));
}
}
[Test]
public void ShouldFindCustomerByFacebookId()
{
using (var s = _db.OpenSession())
{
var customerAccount = s.Query<CustomerAccount, CustomerAccounts_ByCustomerIdAndFacebookId>()
.Customize(x => x.WaitForNonStaleResults())
.SingleOrDefault(x => x.FacebookUserId == Facebookid);
Assert.That(customerAccount, Is.Not.Null);
Assert.That(customerAccount.CustomerId, Is.EqualTo(Customerid));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment