Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Created November 24, 2011 22:37
Show Gist options
  • Select an option

  • Save mattwarren/1392443 to your computer and use it in GitHub Desktop.

Select an option

Save mattwarren/1392443 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Abstractions.Extensions;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Xunit;
using Raven.Client;
using Raven.Abstractions.Indexing;
using Raven.Tests;
using System.ComponentModel.Composition.Hosting;
using Raven.Abstractions.Data;
namespace Raven.Tryouts
{
class Program
{
static void Main()
{
new DistinctFieldValueTest().CanDoPatchingOnEmptyObjects_Embedded();
}
}
public class DistinctFieldValueTest : RavenTest
{
[Fact]
public void CanDoPatchingOnEmptyObjects_Embedded()
{
using (var store = NewDocumentStore())
{
ExecuteTest(store);
}
}
private void ExecuteTest(IDocumentStore store)
{
using (var session = store.OpenSession())
{
session.Store(new Patron
{
Id = "patrons/1",
Privileges = new List<Privilege> { new Privilege { Level = "Silver", Code = 12312, EndDate = "12/12/2012" } },
Phones = new List<Phone> { new Phone { Cell = "123123", Home = "9783041284", Office = "1234123412" } },
MiddleName = "asdfasdfasdf",
FirstName = "asdfasdfasdf"
});
session.Store(new Patron
{
Id = "patrons/2",
Privileges = new List<Privilege>(), //delibrately left empty
Phones = new List<Phone> { new Phone { Cell = "444444", Home = "321321321", Office = "88888888" } },
MiddleName = "middle",
FirstName = "first"
});
session.SaveChanges();
}
//Add the index we need
IndexCreation.CreateIndexes(new CompositionContainer(new TypeCatalog(typeof(PatronByPrivilege))), store);
//Ensure that the index has updated
using (var session = store.OpenSession())
{
var allValues = session.Query<object>("PatronByPrivilege")
.Customize(x => x.WaitForNonStaleResults())
.ToList();
}
store.DatabaseCommands.UpdateByIndex("PatronByPrivilege",
//Only apply the patch to Patrons that have a privilege
new IndexQuery { Query = "HasPrivileges:true" },
new[]
{
new PatchRequest
{
Type = PatchCommandType.Modify,
Name = "Privileges",
Position = 0,
Nested = new[]
{
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "Level",
Value = "Gold"
},
}
}
}, allowStale: false);
using (var session = store.OpenSession())
{
//Check that a patron with privileges has been modified
var patron1 = session.Load<Patron>("patrons/1");
Assert.Equal("Gold", patron1.Privileges[0].Level);
//Check that a patron without privileges is not modified
var patron2 = session.Load<Patron>("patrons/2");
Assert.Equal(0, patron2.Privileges.Count);
}
}
}
public class PatronByPrivilege : AbstractIndexCreationTask<Patron>
{
public override IndexDefinition CreateIndexDefinition()
{
return new IndexDefinitionBuilder<Patron>
{
Map = docs => from doc in docs
where doc.Privileges != null
select new { HasPrivileges = doc.Privileges.Count == 0 ? false : true }
}.ToIndexDefinition(Conventions);
}
}
public class Patron
{
public string Id { get; set; }
public List<Privilege> Privileges { get; set; }
public List<Phone> Phones { get; set; }
public string MiddleName { get; set; }
public string FirstName { get; set; }
}
public class Privilege
{
public string Level { get; set; }
public int Code { get; set; }
public String EndDate { get; set; }
}
public class Phone
{
public string Cell { get; set; }
public string Home { get; set; }
public string Office { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment