Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor embedded testing to a different class
  • Loading branch information
Fitzchak Yitzchaki committed Apr 3, 2012
1 parent 04364c4 commit c6c7964
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 147 deletions.
1 change: 1 addition & 0 deletions Raven.Tests/Raven.Tests.csproj
Expand Up @@ -770,6 +770,7 @@
<Compile Include="Storage\ReduceStaleness.cs" />
<Compile Include="Storage\SimilarIndexNames.cs" />
<Compile Include="Stress\BigDoc.cs" />
<Compile Include="Suggestions\SuggestionsLazy.cs" />
<Compile Include="Suggestions\Person.cs" />
<Compile Include="Suggestions\Suggestions.cs" />
<Compile Include="Suggestions\SuggestionsHelper.cs" />
Expand Down
244 changes: 97 additions & 147 deletions Raven.Tests/Suggestions/Suggestions.cs
Expand Up @@ -4,198 +4,148 @@
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Linq;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Linq;
using Raven.Database.Indexing;
using Raven.Tests.Bugs;
using Xunit;
using System.Linq;

namespace Raven.Tests.Suggestions
{
public class Suggestions : LocalClientTest, IDisposable
{
#region IDisposable Members
private readonly IDocumentStore documentStore;

public void Dispose()
public Suggestions()
{

documentStore = NewDocumentStore();
}

#endregion

protected DocumentStore DocumentStore { get; set; }
public void Dispose()
{
documentStore.Dispose();
}

[Fact]
public void ExactMatch()
{
using (var store = NewDocumentStore())
documentStore.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = documentStore.OpenSession())
{
store.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = store.OpenSession())
{
s.Store(new User { Name = "Ayende" });
s.Store(new User { Name = "Oren" });
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = store.OpenSession())
{
var suggestionQueryResult = s.Advanced.DatabaseCommands.Suggest("Test",
new SuggestionQuery
{
Field = "Name",
Term = "Oren",
MaxSuggestions = 10,
});

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
s.Store(new User {Name = "Ayende"});
s.Store(new User {Name = "Oren"});
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}
}

[Fact]
public void UsingLinq()
{
using (var store = NewDocumentStore())
using (var s = documentStore.OpenSession())
{
store.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = store.OpenSession())
{
s.Store(new User { Name = "Ayende" });
s.Store(new User { Name = "Oren" });
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = store.OpenSession())
{
var suggestionQueryResult = s.Query<User>("test")
.Where(x => x.Name == "Oren")
.Suggest();

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
var suggestionQueryResult = s.Advanced.DatabaseCommands.Suggest("Test",
new SuggestionQuery
{
Field = "Name",
Term = "Oren",
MaxSuggestions = 10,
});

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
}

[Fact]
public void UsingLinq_Lazy()
public void UsingLinq()
{
using(GetNewServer())
using (var store = new DocumentStore
documentStore.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = documentStore.OpenSession())
{
Url = "http://localhost:8079"
}.Initialize())
s.Store(new User {Name = "Ayende"});
s.Store(new User {Name = "Oren"});
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = documentStore.OpenSession())
{
store.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = store.OpenSession())
{
s.Store(new User { Name = "Ayende" });
s.Store(new User { Name = "Oren" });
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = store.OpenSession())
{
var oldRequests = s.Advanced.NumberOfRequests;

var suggestionQueryResult = s.Query<User>("test")
.Where(x => x.Name == "Oren")
.SuggestLazy();

Assert.Equal(oldRequests, s.Advanced.NumberOfRequests);
Assert.Equal(1, suggestionQueryResult.Value.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Value.Suggestions[0]);

Assert.Equal(oldRequests + 1, s.Advanced.NumberOfRequests);
}
var suggestionQueryResult = s.Query<User>("test")
.Where(x => x.Name == "Oren")
.Suggest();

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
}

[Fact]
public void UsingLinq_WithOptions()
{
using (var store = NewDocumentStore())
documentStore.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = documentStore.OpenSession())
{
store.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = store.OpenSession())
{
s.Store(new User { Name = "Ayende" });
s.Store(new User { Name = "Oren" });
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = store.OpenSession())
{
var suggestionQueryResult = s.Query<User>("test")
.Where(x => x.Name == "Orin")
.Suggest(new SuggestionQuery { Accuracy = 0.4f });

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
s.Store(new User {Name = "Ayende"});
s.Store(new User {Name = "Oren"});
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = documentStore.OpenSession())
{
var suggestionQueryResult = s.Query<User>("test")
.Where(x => x.Name == "Orin")
.Suggest(new SuggestionQuery {Accuracy = 0.4f});

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
}


[Fact]
public void WithTypo()
{
using (var store = NewDocumentStore())
documentStore.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = documentStore.OpenSession())
{
s.Store(new User {Name = "Ayende"});
s.Store(new User {Name = "Oren"});
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = documentStore.OpenSession())
{
store.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = store.OpenSession())
{
s.Store(new User { Name = "Ayende" });
s.Store(new User { Name = "Oren" });
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = store.OpenSession())
{
var suggestionQueryResult = s.Advanced.DatabaseCommands.Suggest("Test",
new SuggestionQuery
{
Field = "Name",
Term = "Oern",
MaxSuggestions = 10,
Accuracy = 0.2f,
Distance = StringDistanceTypes.Levenshtein
});

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
var suggestionQueryResult = s.Advanced.DatabaseCommands.Suggest("Test",
new SuggestionQuery
{
Field = "Name",
Term = "Oern",
MaxSuggestions = 10,
Accuracy = 0.2f,
Distance = StringDistanceTypes.Levenshtein
});

Assert.Equal(1, suggestionQueryResult.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Suggestions[0]);
}
}
}
}
}
53 changes: 53 additions & 0 deletions Raven.Tests/Suggestions/SuggestionsLazy.cs
@@ -0,0 +1,53 @@
//-----------------------------------------------------------------------
// <copyright file="SuggestionsLazy.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client.Document;
using Raven.Client.Linq;
using Raven.Tests.Bugs;
using Xunit;

namespace Raven.Tests.Suggestions
{
public class SuggestionsLazy : LocalClientTest
{
[Fact]
public void UsingLinq()
{
using(GetNewServer())
using (var store = new DocumentStore{Url = "http://localhost:8079"}.Initialize())
{
store.DatabaseCommands.PutIndex("Test", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }",
});
using (var s = store.OpenSession())
{
s.Store(new User { Name = "Ayende" });
s.Store(new User { Name = "Oren" });
s.SaveChanges();

s.Query<User>("Test").Customize(x => x.WaitForNonStaleResults()).ToList();
}

using (var s = store.OpenSession())
{
var oldRequests = s.Advanced.NumberOfRequests;

var suggestionQueryResult = s.Query<User>("test")
.Where(x => x.Name == "Oren")
.SuggestLazy();

Assert.Equal(oldRequests, s.Advanced.NumberOfRequests);
Assert.Equal(1, suggestionQueryResult.Value.Suggestions.Length);
Assert.Equal("oren", suggestionQueryResult.Value.Suggestions[0]);

Assert.Equal(oldRequests + 1, s.Advanced.NumberOfRequests);
}
}
}
}
}

0 comments on commit c6c7964

Please sign in to comment.