Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created May 29, 2012 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastoj/2830093 to your computer and use it in GitHub Desktop.
Save mastoj/2830093 to your computer and use it in GitHub Desktop.
Trying to reproduce a raven db error
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Raven.Abstractions.Data;
using Raven.Client.Document;
using Raven.Client.Indexes;
namespace RavenDBError
{
[TestFixture]
public class RavenPolymorphismTests
{
[Test]
public void TestA()
{
IEnumerable<MyInterface> objects = new List<MyInterface>
{
new ImplA() {CommonInt = 0, IntA = 0, StringA = "0"},
new ImplB() {CommonInt = 2, IntB = 2, StringB = "2"},
new ImplA() {CommonInt = 1, IntA = 1, StringA = "1"},
new ImplB() {CommonInt = 3, IntB = 3, StringB = "3"}
};
var ravenStore = new RavenStore();
ravenStore.DeleteCollection();
ravenStore.InsertBatch(objects);
var objectsFromRaven = ravenStore.GetAll();
for (int i = 0; i < objects.Count(); i++)
{
Assert.AreEqual(objects.ElementAt(i), objectsFromRaven.ElementAt(i));
}
}
}
public class MyObjects_ById : AbstractIndexCreationTask<MyInterface>
{
public MyObjects_ById()
{
Map = myObjects => from myObject in myObjects select new { Id = myObject.Id };
}
public static string IndexName { get { return "MyObjects/ById"; } }
}
public class RavenStore
{
private DocumentStore _documentStore;
public RavenStore()
{
_documentStore = new DocumentStore
{
Url = "http://localhost:8090/",
Conventions =
{
FindTypeTagName = type =>
{
if (typeof(MyInterface).IsAssignableFrom(type))
{
return "MyObjects";
}
return DocumentConvention.DefaultTypeTagName(type);
}
}
};
_documentStore.Initialize();
IndexCreation.CreateIndexes(typeof(MyObjects_ById).Assembly, _documentStore);
}
public void InsertBatch(IEnumerable<MyInterface> eventBatch)
{
using (var session = _documentStore.OpenSession())
{
foreach (var domainEvent in eventBatch)
{
session.Store(domainEvent);
}
session.SaveChanges();
}
}
public IEnumerable<MyInterface> GetAll()
{
using (var session = _documentStore.OpenSession())
{
var obects = session.Query<MyInterface>(MyObjects_ById.IndexName);
return obects;
}
}
internal void DeleteCollection()
{
using (var session = _documentStore.OpenSession())
{
_documentStore.DatabaseCommands.DeleteByIndex(MyObjects_ById.IndexName, new IndexQuery());
session.SaveChanges();
}
}
}
public interface MyInterface
{
Guid Id { get; set; }
int CommonInt { get; set; }
}
abstract class BaseClass : MyInterface
{
public Guid Id { get; set; }
public int CommonInt { get; set; }
// override object.Equals
public override bool Equals(object obj)
{
var otherObj = obj as BaseClass;
if (obj == null)
{
return false;
}
return otherObj.CommonInt == CommonInt && otherObj.Id == Id;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
class ImplA : BaseClass
{
public int IntA { get; set; }
public string StringA { get; set; }
public override bool Equals(object obj)
{
var otherObj = obj as ImplA;
if(otherObj == null)
{
return false;
}
if (IntA == otherObj.IntA && StringA == otherObj.StringA)
{
return base.Equals(obj);
}
return false;
}
}
class ImplB : BaseClass
{
public int IntB { get; set; }
public string StringB { get; set; }
public override bool Equals(object obj)
{
var otherObj = obj as ImplB;
if (otherObj == null)
{
return false;
}
if (IntB == otherObj.IntB && StringB == otherObj.StringB)
{
return base.Equals(obj);
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment