Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added more tests and fixed bugs raised by those tests

  • Loading branch information...
commit 43e03c45cae3398c6a39441918d3fe0ebf6faf7b 1 parent 30a0a9e
@phillip-haydon authored
View
42 src/Raven.DynamicSession.Tests/DynamicSessionFixture.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using NUnit.Framework;
+using Raven.DynamicSession.Tests;
+
+namespace Raven.DynamicSession.TestConsole
+{
+ [TestFixture]
+ public class DynamicSessionFixture : EmbeddedRavenDBFixture
+ {
+ [Test]
+ [ExpectedException(typeof(MissingMethodException))]
+ public void When_invoking_method_off_session_should_throw_excption()
+ {
+ var store = GetStore();
+
+ using (dynamic session = store.OpenDynamicSession())
+ {
+ session.load(123);
+ }
+ }
+ }
+
+ [TestFixture]
+ public class ChainBuilderFixture : EmbeddedRavenDBFixture
+ {
+ [Test]
+ [ExpectedException(typeof(MissingMethodException))]
+ public void When_invoking_unknown_method_should_throw_exception()
+ {
+ var store = GetStore();
+
+ using (dynamic session = store.OpenDynamicSession())
+ {
+ session.posts.banana();
+ }
+ }
+ }
+}
View
16 src/Raven.DynamicSession.Tests/EmbeddedRavenDBFixture.cs
@@ -9,24 +9,14 @@ public abstract class EmbeddedRavenDBFixture
{
protected IDocumentStore GetStore()
{
- return (new EmbeddableDocumentStore
+ var store = (new EmbeddableDocumentStore
{
RunInMemory = true
}).Initialize();
- }
-
- protected void Configure(IDocumentStore store, Type type, string clrPlaceHolder)
- {
- //TODO: Wrap up the conventions for Raven.DynamicSession
-store.Conventions.FindClrType = (id, doc, metadata) =>
-{
- var clrType = metadata.Value<string>(DynamicSession.DynamicClrTypePlaceHolder);
- if (clrType.Equals(clrPlaceHolder, StringComparison.OrdinalIgnoreCase))
- return type.FullName;
+ store.ConfigureDynamimcSession();
- return metadata.Value<string>(Constants.RavenClrType);
-};
+ return store;
}
}
}
View
4 src/Raven.DynamicSession.Tests/InsertFixture.cs
@@ -10,7 +10,7 @@ public void Can_insert_with_lowercase_insert_keyword()
{
//Arrange
var store = GetStore();
- Configure(store, typeof (Banana), "Bananas");
+ DynamicSession.AddClrType("Bananas", typeof(Banana));
using (dynamic session = store.OpenDynamicSession())
{
@@ -39,7 +39,7 @@ public void Can_insert_with_uppercase_insert_keyword()
{
//Arrange
var store = GetStore();
- Configure(store, typeof(Banana), "Bananas");
+ DynamicSession.AddClrType("Bananas", typeof(Banana));
using (dynamic session = store.OpenDynamicSession())
{
View
2  src/Raven.DynamicSession.Tests/QueryGetFixture.cs
@@ -135,7 +135,7 @@ public void Can_query_post_after_dynamic_insert()
{
//Arrange
var store = GetStore();
- Configure(store, typeof(Post), "Posts");
+ DynamicSession.AddClrType("Posts", typeof(Post));
using (dynamic session = store.OpenDynamicSession())
{
View
1  src/Raven.DynamicSession.Tests/Raven.DynamicSession.Tests.csproj
@@ -93,6 +93,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="DynamicSessionFixture.cs" />
<Compile Include="EmbeddedRavenDBFixture.cs" />
<Compile Include="InsertFixture.cs" />
<Compile Include="QueryGetFixture.cs" />
View
62 src/Raven.DynamicSession/DynamicSession.cs
@@ -1,6 +1,9 @@
using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
+using Raven.Abstractions.Data;
using Raven.Client;
namespace Raven.DynamicSession
@@ -8,6 +11,18 @@ namespace Raven.DynamicSession
public class DynamicSession : DynamicObject, IDisposable
{
protected IDocumentSession Session { get; set; }
+ public static IDictionary<string, string> ClrTypeConversions { get; private set; }
+
+ static DynamicSession()
+ {
+ ClrTypeConversions = new ConcurrentDictionary<string, string>();
+ }
+
+ public static void AddClrType(string name, Type of)
+ {
+ if (!ClrTypeConversions.ContainsKey(name.ToLower()))
+ ClrTypeConversions.Add(name.ToLower(), of.FullName);
+ }
public static string DynamicClrTypePlaceHolder
{
@@ -24,7 +39,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
result = new ChainBuilder(this, Session, binder.Name);
return true;
}
-
+
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
throw new MissingMethodException("There are no methods off the root Dynamic Session, first value"
@@ -51,32 +66,32 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
{
case "load":
case "get":
- {
- if (args.Length != 1)
{
- throw new ArgumentException("Get/Load should have 1 parameter, 1:Id");
- }
+ if (args.Length != 1)
+ {
+ throw new ArgumentException("Get/Load should have 1 parameter, 1:Id");
+ }
- HandleLoad(args, out result);
- return true;
- }
+ HandleLoad(args, out result);
+ return true;
+ }
case "insert":
- {
- if (args.Length != 2)
{
- throw new ArgumentException("Insert should have 2 parameters, 1:ObjectToStore, 2:Id");
- }
+ if (args.Length != 2)
+ {
+ throw new ArgumentException("Insert should have 2 parameters, 1:ObjectToStore, 2:Id");
+ }
- return HandleInsert(args, out result);
- }
+ return HandleInsert(args, out result);
+ }
case "all":
- {
- result = Session.Advanced.LuceneQuery<dynamic>()
- .WhereEquals("@metadata.Raven-Entity-Name", CollectionName)
- .ToList();
+ {
+ result = Session.Advanced.LuceneQuery<dynamic>()
+ .WhereEquals("@metadata.Raven-Entity-Name", CollectionName)
+ .ToList();
- return true;
- }
+ return true;
+ }
}
throw new MissingMethodException("Method {0} does not exist. Allowed methods are: Get/Load, Insert, All");
@@ -97,7 +112,7 @@ private void HandleLoad(object[] args, out object result)
}
else
{
- id = (string) args[0];
+ id = (string)args[0];
}
result = Session.Load<dynamic>(id);
@@ -120,7 +135,7 @@ private bool HandleInsert(object[] args, out object result)
}
else
{
- id = (string) args[1];
+ id = (string)args[1];
}
Session.Store(objectToStore, id);
@@ -129,6 +144,9 @@ private bool HandleInsert(object[] args, out object result)
metadata["Raven-Entity-Name"] = CollectionName;
metadata[DynamicClrTypePlaceHolder] = CollectionName;
+ if (DynamicSession.ClrTypeConversions.ContainsKey(CollectionName.ToLower()))
+ metadata[Constants.RavenClrType] = ClrTypeConversions[CollectionName.ToLower()];
+
result = objectToStore;
return true;
}
View
17 src/Raven.DynamicSession/DynamicSessionExtension.cs
@@ -1,4 +1,6 @@
-using Raven.Client;
+using System;
+using Raven.Abstractions.Data;
+using Raven.Client;
namespace Raven.DynamicSession
{
@@ -8,5 +10,18 @@ public static DynamicSession OpenDynamicSession(this IDocumentStore store)
{
return new DynamicSession(store.OpenSession());
}
+
+ public static void ConfigureDynamimcSession(this IDocumentStore store)
+ {
+ store.Conventions.FindClrType = (id, doc, metadata) =>
+ {
+ var clrType = (metadata.Value<string>(DynamicSession.DynamicClrTypePlaceHolder) ?? "").ToLower();
+
+ if (DynamicSession.ClrTypeConversions.ContainsKey(clrType))
+ return DynamicSession.ClrTypeConversions[clrType];
+
+ return metadata.Value<string>(Constants.RavenClrType);
+ };
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.