Skip to content

Instantly share code, notes, and snippets.

@jasondentler
Created October 11, 2012 09:54
Show Gist options
  • Save jasondentler/3871341 to your computer and use it in GitHub Desktop.
Save jasondentler/3871341 to your computer and use it in GitHub Desktop.
Raven Sanity Check
using NUnit.Framework;
using Raven.Abstractions.Data;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Database.Json;
using Raven.Json.Linq;
using SharpTestsEx;
namespace RavenDB.SanityCheck
{
[TestFixture]
public class ScriptedPatchTests
{
private const string Email = "[email protected]";
private RavenJObject GetCustomerDocument()
{
return RavenJObject.FromObject(new Customer()
{
Name = "Jason Dentler",
Address = new Address()
{
Line1 = "123 Somewhere",
City = "Cityville",
State = "Texas",
PostalCode = "77777"
}
});
}
[Test]
public void StupidSimpleTest()
{
var doc = GetCustomerDocument();
const string script = "this.Email = \"" + Email + "\";";
var patch = new ScriptedPatchRequest() {Script = script};
var result = new ScriptedJsonPatcher().Apply(doc, patch);
result["Email"].Should().Not.Be.Null();
result["Email"].Value<string>().Should().Be.EqualTo(Email);
}
[Test]
public void SimpleVariableTest()
{
var doc = GetCustomerDocument();
const string script = "this.Email = email;";
var patch = new ScriptedPatchRequest()
{
Script = script,
Values = {{"email", Email}}
};
var result = new ScriptedJsonPatcher().Apply(doc, patch);
result["Email"].Should().Not.Be.Null();
result["Email"].Value<string>().Should().Be.EqualTo(Email);
}
[Test]
public void ComplexVariableTest()
{
var doc = GetCustomerDocument();
const string script = "this.Email = data.Email;";
var patch = new ScriptedPatchRequest()
{
Script = script,
Values = { { "data", new ComplexType { Email = Email } } }
};
var result = new ScriptedJsonPatcher().Apply(doc, patch);
result["Email"].Should().Not.Be.Null();
result["Email"].Value<string>().Should().Be.EqualTo(Email);
}
[Test]
public void ComplexVariableTest2()
{
var doc = GetCustomerDocument();
const string script = "this.ComplexType = data;";
var patch = new ScriptedPatchRequest()
{
Script = script,
Values = { { "data", new ComplexType { Email = Email } } }
};
var result = new ScriptedJsonPatcher().Apply(doc, patch);
result["ComplexType"].Should().Not.Be.Null();
}
[Test]
public void CanUseRavenJObjectVariable()
{
var doc = GetCustomerDocument();
const string script = "this.Email = data.Email;";
var patch = new ScriptedPatchRequest()
{
Script = script,
Values = { { "data", RavenJObject.FromObject(new { Email }) } }
};
var result = new ScriptedJsonPatcher().Apply(doc, patch);
result["Email"].Should().Not.Be.Null();
result["Email"].Value<string>().Should().Be.EqualTo(Email);
}
private class Customer
{
public string Name { get; set; }
public Address Address { get; set; }
}
private class Address
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}
private class ComplexType
{
public string Email { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment