Skip to content

Commit f038e40

Browse files
author
Jeff Treuting
committed
Added test for anonymous type in the selector
I also noticed that the selector is being applied after the results are coming back from the server so we aren't getting the benefit of returning less data over the wire. We need to fix this in general but it will take a few changes and should be done on the other branch. So I'll add that as an issue on the Wiki on github.
1 parent d1dc6d3 commit f038e40

5 files changed

Lines changed: 127 additions & 86 deletions

File tree

SharpRepository.CouchDbRepository/Linq/QueryGeneration/CouchDbApiGeneratorExpressionTreeVisitor.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ public static string GetCouchDbApiExpression (Expression linqExpression)
1717
}
1818

1919
private readonly StringBuilder _expression = new StringBuilder ();
20-
// private readonly ParameterAggregator _parameterAggregator;
2120

2221
private CouchDbApiGeneratorExpressionTreeVisitor()
2322
{
24-
// _parameterAggregator = parameterAggregator;
2523
}
2624

2725
public string GetCouchDbApiExpression()
@@ -135,6 +133,13 @@ protected override Expression VisitConstantExpression (ConstantExpression expres
135133
return expression;
136134
}
137135

136+
protected override Expression VisitNewExpression(NewExpression expression)
137+
{
138+
return expression;
139+
140+
return base.VisitNewExpression(expression);
141+
}
142+
138143
protected override Expression VisitMethodCallExpression (MethodCallExpression expression)
139144
{
140145
// In production code, handle this via method lookup tables.

SharpRepository.CouchDbRepository/Linq/QueryGeneration/QueryPartsAggregator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ public string BuildCouchDbApiPostData()
5656
stringBuilder.AppendFormat("emit({0}, ", !String.IsNullOrEmpty(OrderBy) ? OrderBy : "doc._id");
5757

5858
// TODO: use the SelectParts to only return the properties that are needed by emitting {Name: "Jeff", Title: "Awesome"}
59-
stringBuilder.Append("doc);}\"}");
59+
60+
var select = "doc"; // return the entire thing by default
61+
if (!String.IsNullOrEmpty(SelectPart))
62+
select = SelectPart;
63+
64+
stringBuilder.Append(select + ");}\"}");
6065

6166
return stringBuilder.ToString();
6267
}

SharpRepository.Repository/LinqRepositoryBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ protected override T FindQuery(ISpecification<T> criteria, IQueryOptions<T> quer
4242
return criteria.SatisfyingEntityFrom(query);
4343
}
4444

45+
// TODO: change to IQueryable<T> so that we can use it from the calls with a selector like GetAll(x => x.Name), right now it is bringing back the entire object and then doing the Select using Linq to Objects
4546
protected override IEnumerable<T> GetAllQuery()
4647
{
48+
// return BaseQuery(); // we should be doing this so that it can stay a query and not pull into memory for the GetAllQuery with the selector param to use as well
4749
return BaseQuery().ToList();
4850
}
4951

@@ -57,6 +59,7 @@ protected override IEnumerable<T> GetAllQuery(IQueryOptions<T> queryOptions)
5759
return queryOptions.Apply(query).ToList();
5860
}
5961

62+
// TODO: change to IQueryable<T> so that we can use it from the calls with a selector like GetAll(x => x.Name), right now it is bringing back the entire object and then doing the Select using Linq to Objects
6063
protected override IEnumerable<T> FindAllQuery(ISpecification<T> criteria)
6164
{
6265
var query = BaseQuery(criteria.FetchStrategy);

SharpRepository.Repository/RepositoryBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected RepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null)
5252
public abstract IQueryable<T> AsQueryable();
5353

5454
// These are the actual implementation that the derived class needs to implement
55-
protected abstract IEnumerable<T> GetAllQuery();
55+
protected abstract IEnumerable<T> GetAllQuery();
5656
protected abstract IEnumerable<T> GetAllQuery(IQueryOptions<T> queryOptions);
5757

5858
public IEnumerable<T> GetAll()
@@ -72,6 +72,7 @@ public IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selecto
7272
{
7373
if (selector == null) throw new ArgumentNullException("selector");
7474

75+
// TODO: change to GetAllQuery which should be IQueryable<> so that the selector is done on the server side instead of inmemory with the resulting objects
7576
return GetAll(queryOptions)
7677
.AsQueryable()
7778
.Select(selector);
Lines changed: 109 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,110 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using NUnit.Framework;
4-
using SharpRepository.Repository;
5-
using SharpRepository.Repository.Queries;
6-
using SharpRepository.Tests.Integration.TestAttributes;
7-
using SharpRepository.Tests.Integration.TestObjects;
8-
using Should;
9-
10-
namespace SharpRepository.Tests.Integration
11-
{
12-
[TestFixture]
13-
public class RepositoryGetAllTests : TestBase
14-
{
15-
[ExecuteForAllRepositories]
16-
public void GetAll_Should_Return_Every_Item(IRepository<Contact, string> repository)
17-
{
18-
for (int i = 1; i <= 5; i++)
19-
{
20-
var contact = new Contact { Name = "Test User " + i };
21-
repository.Add(contact);
22-
}
23-
24-
IEnumerable<Contact> result = repository.GetAll().ToList();
25-
result.Count().ShouldEqual(5);
26-
}
27-
28-
[ExecuteForAllRepositories]
29-
public void GetAll_Should_Return_Every_Items_With_Paging(IRepository<Contact, string> repository)
30-
{
31-
const int resultingPage = 2;
32-
const int pageSize = 2;
33-
const int totalItems = 5;
34-
35-
var queryOptions = new PagingOptions<Contact>(resultingPage, pageSize, "Name");
36-
37-
for (int i = 1; i <= totalItems; i++)
38-
{
39-
var contact = new Contact { Name = "Test User " + i };
40-
repository.Add(contact);
41-
}
42-
43-
IEnumerable<Contact> result = repository.GetAll(queryOptions).ToList();
44-
result.Count().ShouldEqual(pageSize);
45-
queryOptions.TotalItems.ShouldEqual(totalItems);
46-
result.First().Name.ShouldEqual("Test User 3");
47-
}
48-
49-
[ExecuteForAllRepositories]
50-
public void GetAll_With_Selector_Should_Return_Every_Item(IRepository<Contact, string> repository)
51-
{
52-
for (int i = 1; i <= 5; i++)
53-
{
54-
var contact = new Contact { Name = "Test User " + i };
55-
repository.Add(contact);
56-
}
57-
58-
var result = repository.GetAll(c => c.Name);
59-
result.Count().ShouldEqual(5);
60-
}
61-
62-
[ExecuteForAllRepositories]
63-
public void GetAll_With_Selector_Should_Return_Every_Items_With_Paging(IRepository<Contact, string> repository)
64-
{
65-
const int resultingPage = 2;
66-
const int pageSize = 2;
67-
const int totalItems = 5;
68-
69-
var queryOptions = new PagingOptions<Contact>(resultingPage, pageSize, "Name");
70-
71-
for (int i = 1; i <= totalItems; i++)
72-
{
73-
var contact = new Contact { Name = "Test User " + i };
74-
repository.Add(contact);
75-
}
76-
77-
var result = repository.GetAll(c => c.Name, queryOptions).ToList();
78-
result.Count().ShouldEqual(pageSize);
79-
queryOptions.TotalItems.ShouldEqual(totalItems);
80-
result.First().ShouldEqual("Test User 3");
81-
}
82-
}
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using SharpRepository.Repository;
5+
using SharpRepository.Repository.Queries;
6+
using SharpRepository.Tests.Integration.TestAttributes;
7+
using SharpRepository.Tests.Integration.TestObjects;
8+
using Should;
9+
10+
namespace SharpRepository.Tests.Integration
11+
{
12+
[TestFixture]
13+
public class RepositoryGetAllTests : TestBase
14+
{
15+
[ExecuteForAllRepositories]
16+
public void GetAll_Should_Return_Every_Item(IRepository<Contact, string> repository)
17+
{
18+
for (int i = 1; i <= 5; i++)
19+
{
20+
var contact = new Contact { Name = "Test User " + i };
21+
repository.Add(contact);
22+
}
23+
24+
IEnumerable<Contact> result = repository.GetAll().ToList();
25+
result.Count().ShouldEqual(5);
26+
}
27+
28+
[ExecuteForAllRepositories]
29+
public void GetAll_Should_Return_Every_Items_With_Paging(IRepository<Contact, string> repository)
30+
{
31+
const int resultingPage = 2;
32+
const int pageSize = 2;
33+
const int totalItems = 5;
34+
35+
var queryOptions = new PagingOptions<Contact>(resultingPage, pageSize, "Name");
36+
37+
for (int i = 1; i <= totalItems; i++)
38+
{
39+
var contact = new Contact { Name = "Test User " + i };
40+
repository.Add(contact);
41+
}
42+
43+
IEnumerable<Contact> result = repository.GetAll(queryOptions).ToList();
44+
result.Count().ShouldEqual(pageSize);
45+
queryOptions.TotalItems.ShouldEqual(totalItems);
46+
result.First().Name.ShouldEqual("Test User 3");
47+
}
48+
49+
[ExecuteForAllRepositories]
50+
public void GetAll_With_Selector_Should_Return_Every_Item(IRepository<Contact, string> repository)
51+
{
52+
for (int i = 1; i <= 5; i++)
53+
{
54+
var contact = new Contact { Name = "Test User " + i };
55+
repository.Add(contact);
56+
}
57+
58+
var result = repository.GetAll(c => c.Name);
59+
60+
// changed from .Count() to this to actually get the results and not just the Count of the query which won't necessarily use the selector at all
61+
var total = 0;
62+
foreach (var item in result)
63+
{
64+
total++;
65+
}
66+
total.ShouldEqual(5);
67+
}
68+
69+
[ExecuteForAllRepositories]
70+
public void GetAll_With_Anonymous_Selector_Should_Return_Every_Item(IRepository<Contact, string> repository)
71+
{
72+
for (int i = 1; i <= 5; i++)
73+
{
74+
var contact = new Contact { Name = "Test User " + i };
75+
repository.Add(contact);
76+
}
77+
78+
var result = repository.GetAll(c => new { c.Name, c.ContactTypeId});
79+
80+
// changed from .Count() to this to actually get the results and not just the Count of the query which won't necessarily use the selector at all and might do it all server side
81+
var total = 0;
82+
foreach (var item in result)
83+
{
84+
total++;
85+
}
86+
total.ShouldEqual(5);
87+
}
88+
89+
[ExecuteForAllRepositories]
90+
public void GetAll_With_Selector_Should_Return_Every_Items_With_Paging(IRepository<Contact, string> repository)
91+
{
92+
const int resultingPage = 2;
93+
const int pageSize = 2;
94+
const int totalItems = 5;
95+
96+
var queryOptions = new PagingOptions<Contact>(resultingPage, pageSize, "Name");
97+
98+
for (int i = 1; i <= totalItems; i++)
99+
{
100+
var contact = new Contact { Name = "Test User " + i };
101+
repository.Add(contact);
102+
}
103+
104+
var result = repository.GetAll(c => c.Name, queryOptions).ToList();
105+
result.Count().ShouldEqual(pageSize);
106+
queryOptions.TotalItems.ShouldEqual(totalItems);
107+
result.First().ShouldEqual("Test User 3");
108+
}
109+
}
83110
}

0 commit comments

Comments
 (0)