Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

LINQ to CQL is deserializing to objects and passes all tests even tho…

…ugh limited
  • Loading branch information...
commit f6369d29e8b6fd3d35606dda964851e94ef339b3 1 parent 1d0ca56
Nick Berardi nberardi authored
2  src/Linq/CqlObjectQuery.cs
View
@@ -56,7 +56,7 @@ IEnumerator IEnumerable.GetEnumerator()
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
- var result = CqlQueryEvaluator.GetCql(Expression);
+ var result = CqlQueryEvaluator.GetCql(Expression, _family.ObjectConventions);
var fluentObjects = _family.Cql(result);
var serializer = ObjectSerializerFactory.Get(typeof(T));
35 src/ObjectSerializer/ReflectionObjectSerializer.cs
View
@@ -15,8 +15,43 @@ public ReflectionObjectSerializer(Type type)
_type = type;
}
+ private object FrameworkTypeRowDeserializer(ICqlRow row, ObjectSerializerConventions conventions)
+ {
+ if (row.Columns.Count > 0)
+ return Convert.ChangeType(row.Columns[0].ColumnValue, _type);
+
+ return Convert.ChangeType(row.Key, _type);
+ }
+
+ private object AnonymousRowDeserializer(ICqlRow row, ObjectSerializerConventions conventions)
+ {
+ var props = _type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
+ var args = new List<object>();
+
+ foreach (var prop in props)
+ {
+ var name = prop.Name;
+ if (conventions.KeyPropertyNames.Contains(name, conventions.AreKeyPropertyNamesCaseSensitive ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal))
+ {
+ args.Add(Convert.ChangeType(row.Key, prop.PropertyType));
+ continue;
+ }
+
+ if (row.Columns.Any(x => x.ColumnName == name))
+ args.Add(Convert.ChangeType(row[name], prop.PropertyType));
+ }
+
+ return Activator.CreateInstance(_type, args.ToArray());
+ }
+
private object RowDeserializer(ICqlRow row, ObjectSerializerConventions conventions)
{
+ if (Type.GetTypeCode(_type) != TypeCode.Object)
+ return FrameworkTypeRowDeserializer(row, conventions);
+
+ if (_type.Name.Contains("AnonymousType"))
+ return AnonymousRowDeserializer(row, conventions);
+
var obj = Activator.CreateInstance(_type);
foreach (var prop in _type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
48 test/FluentCassandra.Tests/Linq/LinqToCqlObjectsCassandraTests.cs
View
@@ -53,12 +53,13 @@ public void SELECT()
public void SELECT_One_Column()
{
var query = _family.AsObjectQueryable<User>().Select(x => x.Age);
- var actual = query.ToList();
+ var actual = query.ToList().OrderBy(x => x).ToList();
+ var users = _users.OrderBy(x => x.Age).ToList();
- Assert.AreEqual(_users.Length, actual.Count);
- for (int i = 0; i < _users.Length; i++)
+ Assert.AreEqual(users.Count, actual.Count);
+ for (int i = 0; i < users.Count; i++)
{
- var objUser = _users[i];
+ var objUser = users[i];
var dbAge = actual[i];
Assert.AreEqual(objUser.Age, dbAge);
@@ -69,12 +70,13 @@ public void SELECT_One_Column()
public void SELECT_Two_Columns()
{
var query = _family.AsObjectQueryable<User>().Select(x => new { x.Age, x.Name });
- var actual = query.ToList();
+ var actual = query.ToList().OrderBy(x => x.Age).ToList();
+ var users = _users.OrderBy(x => x.Age).ToList();
- Assert.AreEqual(_users.Length, actual.Count);
- for (int i = 0; i < _users.Length; i++)
+ Assert.AreEqual(users.Count, actual.Count);
+ for (int i = 0; i < users.Count; i++)
{
- var objUser = _users[i];
+ var objUser = users[i];
var dbUser = actual[i];
Assert.AreEqual(objUser.Age, dbUser.Age);
@@ -92,13 +94,13 @@ public void WHERE_Using_KEY()
where f.Id == 2
select f;
var response = query.ToList();
- dynamic actual = response.FirstOrDefault();
+ var actual = response.FirstOrDefault();
Assert.AreEqual(1, response.Count);
- Assert.AreEqual(expected.Id, actual.Key);
- Assert.AreEqual(expected.Name, (string)actual.Name);
- Assert.AreEqual(expected.Email, (string)actual.Email);
- Assert.AreEqual(expected.Age, (int)actual.Age);
+ Assert.AreEqual(expected.Id, actual.Id);
+ Assert.AreEqual(expected.Name, actual.Name);
+ Assert.AreEqual(expected.Email, actual.Email);
+ Assert.AreEqual(expected.Age, actual.Age);
}
[Test]
@@ -111,13 +113,13 @@ public void WHERE_Using_KEY_And_One_Parameter()
where f.Id == 2 && f.Age == 23
select f;
var response = query.ToList();
- dynamic actual = response.FirstOrDefault();
+ var actual = response.FirstOrDefault();
Assert.AreEqual(1, response.Count);
- Assert.AreEqual(expected.Id, actual.Key);
- Assert.AreEqual(expected.Name, (string)actual.Name);
- Assert.AreEqual(expected.Email, (string)actual.Email);
- Assert.AreEqual(expected.Age, (int)actual.Age);
+ Assert.AreEqual(expected.Id, actual.Id);
+ Assert.AreEqual(expected.Name, actual.Name);
+ Assert.AreEqual(expected.Email, actual.Email);
+ Assert.AreEqual(expected.Age, actual.Age);
}
[Test]
@@ -130,13 +132,13 @@ public void WHERE_One_Parameter()
where f.Age == 23
select f;
var response = query.ToList();
- dynamic actual = response.FirstOrDefault();
+ var actual = response.FirstOrDefault();
Assert.AreEqual(1, response.Count);
- Assert.AreEqual(expected.Id, actual.Key);
- Assert.AreEqual(expected.Name, (string)actual.Name);
- Assert.AreEqual(expected.Email, (string)actual.Email);
- Assert.AreEqual(expected.Age, (int)actual.Age);
+ Assert.AreEqual(expected.Id, actual.Id);
+ Assert.AreEqual(expected.Name, actual.Name);
+ Assert.AreEqual(expected.Email, actual.Email);
+ Assert.AreEqual(expected.Age, actual.Age);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.