Permalink
Browse files

Hooked LINQ off of MongoCollection.

  • Loading branch information...
1 parent a84e10e commit effec2b43b845c31fef64fb01176ee2a6acdb260 @atheken committed May 30, 2010
@@ -2,6 +2,8 @@
using System.Linq;
using Xunit;
using Norm.Configuration;
+using Norm.Linq;
+using Norm.Tests.Helpers;
namespace Norm.Tests
{
@@ -25,9 +27,11 @@ public void CountShouldReturn3WhenThreeProductsInDB()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 22 });
session.Add(new TestProduct { Name = "3", Price = 33 });
- var result = session.Products.Count();
- Assert.Equal(3, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ var productQueryable = session.Products;
+
+ Assert.Equal(3, productQueryable.Count());
+
+ Assert.Equal(false, productQueryable.QueryStructure().IsComplex);
}
}
[Fact]
@@ -38,9 +42,10 @@ public void CountShouldReturn2WhenThreeProductsInDBAndWherePriceGreaterThan20()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 22 });
session.Add(new TestProduct { Name = "3", Price = 33 });
- var result = session.Products.Where(x => x.Price > 20).Count();
+ var queryable = session.Products;
+ var result = queryable.Where(x => x.Price > 20).Count();
Assert.Equal(2, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -53,9 +58,10 @@ public void Count_Should_Return_One_When_Id_Matches()
session.Add(new TestProduct { Name = "1", Price = 40, _id = target });
session.Add(new TestProduct { Name = "2", Price = 22, _id = ObjectId.NewObjectId() });
session.Add(new TestProduct { Name = "3", Price = 33, _id = ObjectId.NewObjectId() });
- var result = session.Products.Where(x => x._id == target).Count();
+ var queryable = session.Products;
+ var result = queryable.Where(x => x._id == target).Count();
Assert.Equal(1, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
[Fact]
@@ -66,9 +72,10 @@ public void SumShouldReturn60WhenThreeProductsInDBWIthSumPrice60()
session.Add(new TestProduct { Name = "dd", Price = 10 });
session.Add(new TestProduct { Name = "ss", Price = 20 });
session.Add(new TestProduct { Name = "asdasddds", Price = 30 });
- var result = session.Products.Sum(x => x.Price);
+ var queryable = session.Products;
+ var result = queryable.Sum(x => x.Price);
Assert.Equal(60, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
[Fact]
@@ -79,9 +86,10 @@ public void SumShouldReturn30WhenThreeProductsInDBWIthSumPrice60AndPriceLessThan
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
- var result = session.Products.Where(x => x.Price < 30).Sum(x => x.Price);
+ var queryable = session.Products;
+ var result =queryable.Where(x => x.Price < 30).Sum(x => x.Price);
Assert.Equal(30, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -97,9 +105,10 @@ public void SumShouldReturn30WhenThreeProductsInDBWIthSumPrice60AndNameContains1
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
- var result = session.Products.Where(x => names.Contains(x.Name)).Sum(x => x.Price);
+ var queryable = session.Products;
+ var result = queryable.Where(x => names.Contains(x.Name)).Sum(x => x.Price);
Assert.Equal(30, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -111,9 +120,10 @@ public void AvgShouldReturn20WhenThreeProductsInDBWIthSumPrice60()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
- var result = session.Products.Average(x => x.Price);
+ var queryable = session.Products;
+ var result = queryable.Average(x => x.Price);
Assert.Equal(20, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -125,9 +135,10 @@ public void AvgShouldReturn15WhenThreeProductsInDBWIthSumPrice60AndLessThan30()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
- var result = session.Products.Where(x => x.Price < 30).Average(x => x.Price);
+ var queryable = session.Products;
+ var result = queryable.Where(x => x.Price < 30).Average(x => x.Price);
Assert.Equal(15, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -140,10 +151,10 @@ public void AvgShouldReturn500Point5WhenSumOfAllNumbersUpTo1000()
{
session.Add(new TestProduct { Name = i.ToString(), Price = i + 1 });
}
-
- var result = session.Products.Average(x => x.Price);
+ var queryable = session.Products;
+ var result = queryable.Average(x => x.Price);
Assert.Equal(500.5, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -155,9 +166,10 @@ public void MinShouldReturn10WhenThreeProductsInDBWIthSumPrice60AndLowestIs10()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
- var result = session.Products.Min(x => x.Price);
+ var queryable = session.Products;
+ var result = queryable.Min(x => x.Price);
Assert.Equal(10, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -169,9 +181,10 @@ public void MaxShouldReturn30WhenThreeProductsInDBWIthSumPrice60AndHighestIs30()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
- var result = session.Products.Max(x => x.Price);
+ var queryable = session.Products;
+ var result = queryable.Max(x => x.Price);
Assert.Equal(30, result);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
[Fact]
@@ -182,9 +195,9 @@ public void AnyShouldReturnTrueWhenProductPrice10()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
-
- Assert.True(session.Products.Any(x => x.Price == 10));
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ var queryable = session.Products;
+ Assert.True(queryable.Any(x => x.Price == 10));
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -196,9 +209,9 @@ public void AnyShouldReturnTrueWhenProductPrice10AndWhere()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
-
- Assert.True(session.Products.Where(x => x.Price < 30).Any(x => x.Price == 10));
- Assert.Equal(true, session.TranslationResults.IsComplex);
+ var queryable = session.Products;
+ Assert.True(queryable.Where(x => x.Price < 30).Any(x => x.Price == 10));
+ Assert.Equal(true, queryable.QueryStructure().IsComplex);
}
}
@@ -210,9 +223,9 @@ public void AnyShouldReturnTrueWhenProductList()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
-
- Assert.True(session.Products.Any());
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ var queryable = session.Products;
+ Assert.True(queryable.Any());
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -224,9 +237,9 @@ public void AnyShouldReturnFalseWhenProductPrice100()
session.Add(new TestProduct { Name = "1", Price = 10 });
session.Add(new TestProduct { Name = "2", Price = 20 });
session.Add(new TestProduct { Name = "3", Price = 30 });
-
- Assert.False(session.Products.Any(x => x.Price == 100));
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ var queryable = session.Products;
+ Assert.False(queryable.Any(x => x.Price == 100));
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -2,6 +2,7 @@
using System.Linq;
using Xunit;
using Norm.Configuration;
+using Norm.Tests.Helpers;
namespace Norm.Tests
{
@@ -27,9 +28,10 @@ public void OneProductShouldBeReturnedWhenNestedSupplierQueried()
session.Add(new TestProduct { Name = "Test3", Price = 10, Supplier = new Supplier { Name = "Steve" } });
session.Add(new TestProduct { Name = "Test4", Price = 22 });
session.Add(new TestProduct { Name = "Test5", Price = 33 });
- var products = session.Products.Where(x => x.Supplier.Name == "Steve").ToList();
+ var queryable = session.Products;
+ var products = queryable.Where(x => x.Supplier.Name == "Steve").ToList();
Assert.Equal(1, products.Count);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -42,9 +44,10 @@ public void SupplierShouldBeQuerybleByDateMath()
session.Add(new TestProduct { Name = "Test3", Price = 10, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2000, 2, 1), Address = add } });
session.Add(new TestProduct { Name = "Test4", Price = 22, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2001, 2, 1) } });
session.Add(new TestProduct { Name = "Test5", Price = 33, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2002, 2, 1) } });
- var products = session.Products.Where(x => x.Supplier.CreatedOn.Year < 2001).ToList();
+ var queryable = session.Products;
+ var products = queryable.Where(x => x.Supplier.CreatedOn.Year < 2001).ToList();
Assert.Equal(1, products.Count);
- Assert.Equal(true, session.TranslationResults.IsComplex);
+ Assert.Equal(true, queryable.QueryStructure().IsComplex);
}
}
@@ -57,9 +60,10 @@ public void SupplierShouldBeQuerybleByAddress()
session.Add(new TestProduct { Name = "Test3", Price = 10, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2000, 2, 1), Address = add } });
session.Add(new TestProduct { Name = "Test4", Price = 22, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2001, 2, 1) } });
session.Add(new TestProduct { Name = "Test5", Price = 33, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2002, 2, 1) } });
- var products = session.Products.Where(x => x.Supplier.Address.State == "HI").ToList();
+ var queryable = session.Products;
+ var products = queryable.Where(x => x.Supplier.Address.State == "HI").ToList();
Assert.Equal(1, products.Count);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -72,9 +76,10 @@ public void SupplierShouldBeQuerybleByAddressAndWorkWithAndExpression()
session.Add(new TestProduct { Name = "Test3", Price = 10, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2000, 2, 1), Address = add } });
session.Add(new TestProduct { Name = "Test4", Price = 22, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2001, 2, 1) } });
session.Add(new TestProduct { Name = "Test5", Price = 33, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2002, 2, 1) } });
- var products = session.Products.Where(x => x.Supplier.Address.State == "HI" && x.Price == 10).ToList();
+ var queryable = session.Products;
+ var products = queryable.Where(x => x.Supplier.Address.State == "HI" && x.Price == 10).ToList();
Assert.Equal(1, products.Count);
- Assert.Equal(false, session.TranslationResults.IsComplex);
+ Assert.Equal(false, queryable.QueryStructure().IsComplex);
}
}
@@ -87,9 +92,10 @@ public void SupplierShouldBeQuerybleByAddressAndWorkWithOrExpression()
session.Add(new TestProduct { Name = "Test3", Price = 10, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2000, 2, 1), Address = add } });
session.Add(new TestProduct { Name = "Test4", Price = 22, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2001, 2, 1) } });
session.Add(new TestProduct { Name = "Test5", Price = 33, Supplier = new Supplier { Name = "Steve", CreatedOn = new DateTime(2002, 2, 1) } });
- var products = session.Products.Where(x => x.Supplier.Address.State == "HI" || x.Price == 33).ToList();
+ var queryable = session.Products;
+ var products = queryable.Where(x => x.Supplier.Address.State == "HI" || x.Price == 33).ToList();
Assert.Equal(2, products.Count);
- Assert.Equal(true, session.TranslationResults.IsComplex);
+ Assert.Equal(true, queryable.QueryStructure().IsComplex);
}
}
@@ -111,12 +117,12 @@ public void InventorySubqueryShouldReturnOneForTwoProducts()
p.Inventory.Add(new InventoryChange() { AmountChanged = -1 });
session.Add(p);
+ var queryable = session.Products;
+ var products = queryable.Where(x => x.Inventory.Count() > 2).ToArray();
- var products = session.Products.Where(x => x.Inventory.Count() > 2).ToArray();
-
Assert.Equal(1, products.Count());
Assert.Equal("Test3", products[0].Name);
- Assert.Equal(true, session.TranslationResults.IsComplex);
+ Assert.Equal(true, queryable.QueryStructure().IsComplex);
}
}
}
Oops, something went wrong.

0 comments on commit effec2b

Please sign in to comment.