-
Notifications
You must be signed in to change notification settings - Fork 820
/
AsyncLuceneQueryTests.cs
130 lines (111 loc) · 4 KB
/
AsyncLuceneQueryTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Net;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
namespace Raven.Tests.Silverlight
{
using System.Collections.Generic;
using System.Threading.Tasks;
using Client.Document;
using Client.Extensions;
using Entities;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class AsyncLuceneQueryTests : RavenTestBase
{
public class User
{
public string Name { get; set; }
}
[Asynchronous]
public IEnumerable<Task> Can_query_using_async_session()
{
var dbname = GenerateNewDatabaseName();
var documentStore = new DocumentStore {Url = Url + Port};
documentStore.Initialize();
yield return documentStore.AsyncDatabaseCommands.EnsureDatabaseExistsAsync(dbname);
using (var s = documentStore.OpenAsyncSession(dbname))
{
s.Store(new User {Name = "Ayende"});
yield return s.SaveChangesAsync();
}
using (var s = documentStore.OpenAsyncSession(dbname))
{
var queryResultAsync = s.Advanced.AsyncLuceneQuery<User>()
.WhereEquals("Name", "Ayende")
.ToListAsync();
yield return queryResultAsync;
Assert.AreEqual("Ayende", queryResultAsync.Result.Item2[0].Name);
}
}
[Asynchronous]
public IEnumerable<Task> Including_a_related_entity_should_avoid_hitting_the_server_twice()
{
var dbname = GenerateNewDatabaseName();
var documentStore = new DocumentStore { Url = Url + Port };
documentStore.Initialize();
yield return documentStore.AsyncDatabaseCommands.EnsureDatabaseExistsAsync(dbname);
var customer = new Customer { Name = "Customer #1", Id = "customer/1", Email = "someone@customer.com" };
var order = new Order { Id = "orders/1", Note = "Hello", Customer = new DenormalizedReference { Id = customer.Id, Name = customer.Name } };
using (var session = documentStore.OpenAsyncSession(dbname))
{
session.Store(customer);
session.Store(order);
yield return session.SaveChangesAsync();
}
using (var session = documentStore.OpenAsyncSession(dbname))
{
var query = session.Advanced.AsyncLuceneQuery<Order>()
.Include(x => x.Customer.Id)
.WhereEquals("Id", "orders/1")
.ToListAsync();
yield return query;
Assert.AreEqual("Hello", query.Result.Item2[0].Note);
// NOTE: this call should not hit the server
var load = session.LoadAsync<Customer>(customer.Id);
yield return load;
Assert.AreEqual(1, session.Advanced.NumberOfRequests);
}
}
[Asynchronous]
public IEnumerable<Task> Querying_against_a_nonindexed_field_raises_an_exception()
{
var dbname = GenerateNewDatabaseName();
var documentStore = new DocumentStore { Url = Url + Port };
documentStore.Initialize();
yield return documentStore.AsyncDatabaseCommands.EnsureDatabaseExistsAsync(dbname);
using (var session = documentStore.OpenAsyncSession(dbname))
{
session.Store(new Customer { Name = "Customer #1", Id = "customer/1", Email = "someone@customer.com" });
yield return session.SaveChangesAsync();
}
var task = documentStore.AsyncDatabaseCommands
.ForDatabase(dbname)
.PutIndexAsync("Test", new IndexDefinition
{
Map = "from doc in docs.Companies select new { doc.Name }"
}, true);
yield return (task);
Task<QueryResult> query = null;
var indexQuery = new IndexQuery {Query = "NonIndexedField:0"};
for (int i = 0; i < 50; i++)
{
query = documentStore.AsyncDatabaseCommands
.ForDatabase(dbname)
.QueryAsync("Test", indexQuery, null);
yield return (query);
if(query.Exception != null)
{
Assert.IsInstanceOfType(query.Exception.ExtractSingleInnerException(), typeof(WebException));
yield break;
}
if (query.Result.IsStale)
{
yield return Delay(100);
continue;
}
yield break;
}
Assert.Fail("Expected to get an exception");
}
}
}