Skip to content
Browse files

all these modified files.. what to do..

  • Loading branch information...
1 parent 4b29246 commit 34a524716908c933deb104179ddf23d132c3810a @atheken committed
View
688 NoRM.Tests/CollectionFindTests/QueryTests.cs
@@ -1,345 +1,345 @@
-using System;
-using System.Linq;
-using Norm.BSON;
-using Norm.Responses;
-using Xunit;
-using Norm.Collections;
-using System.Text.RegularExpressions;
-using System.Collections.Generic;
-
-namespace Norm.Tests
-{
- public class QueryTests : IDisposable
- {
- private readonly Mongo _server;
- private BuildInfoResponse _buildInfo = null;
- private readonly MongoCollection<Person> _collection;
- public QueryTests()
- {
- var admin = new MongoAdmin("mongodb://localhost/admin?pooling=false&strict=true");
- _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
- _collection = _server.GetCollection<Person>("People");
- _buildInfo = admin.BuildInfo();
- //cause the collection to exist on the server by inserting, then deleting some things.
- _collection.Insert(new Person());
- _collection.Delete(new { });
- }
- public void Dispose()
- {
- _server.Database.DropCollection("People");
- using (var admin = new MongoAdmin("mongodb://localhost/NormTests?pooling=false"))
- {
- admin.DropDatabase();
- }
- _server.Dispose();
- }
-
- [Fact]
- public void FindUsesLimit()
- {
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Find(new { }, 3).ToArray();
- Assert.Equal(3, result.Length);
- }
-
- [Fact]
- public void Count_Works()
- {
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Count();
- Assert.Equal(4, result);
- }
-
- [Fact]
- public void Count_With_Filter_Works()
- {
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Count(new Person { Name = "AAA" });
- Assert.Equal(1,result);
- }
-
- [Fact]
- public void DateTime_GreaterThan_Qualifier_Works()
- {
- _collection.Insert(new Person { Birthday = new DateTime(1910, 1, 1) });
- _collection.Insert(new Person { Birthday = new DateTime(1920, 1, 1) });
- _collection.Insert(new Person { Birthday = new DateTime(1930, 1, 1) });
-
- var find = _collection.Find(new { Birthday = Q.GreaterThan(new DateTime(1920, 1, 1)) });
- Assert.Equal(1, find.Count());
- }
-
-
- [Fact]
- public void Where_Qualifier_Works()
- {
- _collection.Insert(new Person { Name = "Gnomey" });
- _collection.Insert(new Person { Name = "kde" });
- _collection.Insert(new Person { Name = "Elfy" });
-
- var find = _collection.Find(Q.Where("this.Name === 'Elfy';"));
- Assert.Equal(1, find.Count());
- }
-
- [Fact]
- public void Find_Uses_Limit_Orderby_And_Skip()
- {
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Find(new { Name = Q.NotEqual(new int?()) }, new { Name = OrderBy.Descending }, 3, 1).ToArray();
- Assert.Equal(3, result.Length);
- Assert.Equal("CCC", result[0].Name);
- Assert.Equal("BBB", result[1].Name);
- Assert.Equal("AAA", result[2].Name);
- }
-
- [Fact]
- public void Find_Uses_Query_And_Orderby()
- {
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Find(new { Name = Q.NotEqual("AAA") }, new { Name = OrderBy.Descending }).ToArray();
- Assert.Equal(3, result.Length);
- Assert.Equal("DDD", result[0].Name);
- Assert.Equal("CCC", result[1].Name);
- Assert.Equal("BBB", result[2].Name);
- }
-
- [Fact]
- public void Find_Uses_Query_And_Orderby_And_Limit()
- {
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Find(new { Name = Q.NotEqual("DDD") }, new { Name = OrderBy.Descending }, 2, 0).ToArray();
- Assert.Equal(2, result.Length);
- Assert.Equal("CCC", result[0].Name);
- Assert.Equal("BBB", result[1].Name);
- }
-
- [Fact]
- public void Find_Uses_Null_Qualifier()
- {
- _collection.Insert(new Person { Name = null });
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Find(new { Name = Q.IsNull() }, new { Name = OrderBy.Descending }, 2, 0).ToArray();
- Assert.Equal(1, result.Length);
- Assert.Equal(null, result[0].Name);
-
- result = _collection.Find(new { Name = Q.IsNotNull() }, new { Name = OrderBy.Descending }).ToArray();
- Assert.Equal(4, result.Length);
- Assert.Equal("DDD", result[0].Name);
- }
-
- [Fact]
- public void FindUsesLimitAndSkip()
- {
- _collection.Insert(new Person { Name = "BBB" });
- _collection.Insert(new Person { Name = "CCC" });
- _collection.Insert(new Person { Name = "AAA" });
- _collection.Insert(new Person { Name = "DDD" });
-
- var result = _collection.Find(new { }, 1, 1).ToArray();
- Assert.Equal(1, result.Length);
- Assert.Equal("CCC", result[0].Name);
- }
-
- [Fact]
- public void FindCanQueryEmbeddedArray()
- {
- _collection.Delete(new { });
- var person1 = new Person
- {
- Name = "Joe Cool",
- Address =
- {
- Street = "123 Main St",
- City = "Anytown",
- State = "CO",
- Zip = "45123"
- }
-
- };
- var person2 = new Person
- {
- Name = "Sam Cool",
- Address =
- {
- Street = "300 Main St",
- City = "Anytown",
- State = "CO",
- Zip = "45123"
- },
- Relatives = new List<string>() { "Emma", "Bruce", "Charlie" }
- };
- _collection.Insert(person1);
- _collection.Insert(person2);
-
- var elem = new Expando();
- elem["Relatives"] = "Charlie";
- var a = _collection.Find(elem).ToArray();
- Assert.Equal(1, a.Length);
- }
-
-
- [Fact]
- public void BasicQueryUsingProperty()
- {
- _collection.Insert(new Person { Name = "Lisa Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
- _collection.Insert(new Person { Name = "Joe Cool", Address = { Street = "123 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
- _collection.Insert(new Person { Name = "Sam Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
-
- var matchRegex = new Regex("^.{4}Cool$");
- var results = _collection.Find(new { Name = matchRegex }).ToArray();
- Assert.Equal(2, results.Length);
- Assert.True(results.All(y => matchRegex.IsMatch(y.Name)));
- }
-
- [Fact]
- public void BasicQueryWithSort()
- {
- //remove everything from the collection.
- _collection.Delete(new { });
-
- _collection.Insert(new Person { Name = "Third", LastContact = new DateTime(2010, 1, 1) });
- _collection.Insert(new Person { Name = "First", LastContact = new DateTime(2000, 1, 1) });
- _collection.Insert(new Person { Name = "Second", LastContact = new DateTime(2005, 1, 1) });
-
- var people = _collection.Find(new { }, new { LastContact = 1 }).ToArray();
- Assert.Equal(3, people.Length);
- Assert.Equal("First", people[0].Name);
- Assert.Equal("Second", people[1].Name);
- Assert.Equal("Third", people[2].Name);
- }
-
- [Fact]
- public void BasicQueryWithMultiSortOrdering()
- {
- //remove everything from the collection.
- _collection.Delete(new { });
-
- _collection.Insert(new Person { Name = "Third", LastContact = new DateTime(2010, 1, 1) });
- _collection.Insert(new Person { Name = "First", LastContact = new DateTime(2005, 1, 1) });
- _collection.Insert(new Person { Name = "Second", LastContact = new DateTime(2005, 1, 1) });
-
- var people = _collection.Find(new { }, new { LastContact = -1, Name = 1 }).ToArray();
- Assert.Equal(3, people.Length);
- Assert.Equal("Third", people[0].Name);
- Assert.Equal("First", people[1].Name);
- Assert.Equal("Second", people[2].Name);
- }
-
- [Fact]
- public void BasicQueryUsingChildProperty()
- {
- _collection.Insert(new Person { Name = "Joe Cool", Address = { Street = "123 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
- _collection.Insert(new Person { Name = "Sam Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
-
- var query = new Expando();
- query["Address.City"] = Q.Equals<string>("Anytown");
-
- var results = _collection.Find(query);
- Assert.Equal(2, results.Count());
- }
- [Fact]
- public void QueryWithinEmbeddedArray()
- {
-
- var post1 = new Person { Name = "First", Relatives = new List<String> { "comment1", "comment2" } };
- var post2 = new Person { Name = "Second", Relatives = new List<String> { "commentA", "commentB" } };
- _collection.Insert(post1);
- _collection.Insert(post2);
-
- var results = _collection.Find(new { Relatives = "commentA" });
- Assert.Equal("Second", results.First().Name);
- }
-
-
- [Fact]
- public void Distinct_On_Collection_Should_Return_Arrays_As_Value_If_Earlier_Than_1_5_0()
- {
- var isLessThan150 = Regex.IsMatch(_buildInfo.Version, "^([01][.][01234])");
-
- // Any version earlier than MongoDB 1.5.0
- if (isLessThan150)
- {
- _collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
- _collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
- _collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
- _collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
-
- var results = _collection.Distinct<string[]>("Relatives");
- Assert.Equal(3, results.Count());
- }
- }
-
- [Fact]
- public void Distinct_On_Collection_Should_Return_Array_Values_In_1_5_0_Or_Later()
- {
- var isLessThan150 = Regex.IsMatch(_buildInfo.Version, "^([01][.][01234])");
-
- // Any version MongoDB 1.5.0 +
- if (!isLessThan150)
- {
- _collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
- _collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
- _collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
- _collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
-
- var results = _collection.Distinct<string>("Relatives");
- Assert.Equal(4, results.Count());
- }
- }
-
- [Fact]
- public void DistinctOnSimpleProperty()
- {
- _collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
- _collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
- _collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
- _collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
-
- var results = _collection.Distinct<string>("Name");
- Assert.Equal(4, results.Count());
- }
-
- [Fact]
- public void DistinctOnComplexProperty()
- {
- _collection.Insert(new Person { Name = "Joe Cool", Address = new Address { State = "CA" } });
- _collection.Insert(new Person { Name = "Sam Cool", Address = new Address { State = "CA" } });
- _collection.Insert(new Person { Name = "Ted Cool", Address = new Address { State = "CA", Zip = "90010" } });
- _collection.Insert(new Person { Name = "Jay Cool", Address = new Address { State = "NY" } });
-
- var results = _collection.Distinct<Address>("Address");
- Assert.Equal(3, results.Count());
- }
- }
+using System;
+using System.Linq;
+using Norm.BSON;
+using Norm.Responses;
+using Xunit;
+using Norm.Collections;
+using System.Text.RegularExpressions;
+using System.Collections.Generic;
+
+namespace Norm.Tests
+{
+ public class QueryTests : IDisposable
+ {
+ private readonly Mongo _server;
+ private BuildInfoResponse _buildInfo = null;
+ private readonly MongoCollection<Person> _collection;
+ public QueryTests()
+ {
+ var admin = new MongoAdmin("mongodb://localhost/admin?pooling=false&strict=true");
+ _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
+ _collection = _server.GetCollection<Person>("People");
+ _buildInfo = admin.BuildInfo();
+ //cause the collection to exist on the server by inserting, then deleting some things.
+ _collection.Insert(new Person());
+ _collection.Delete(new { });
+ }
+ public void Dispose()
+ {
+ _server.Database.DropCollection("People");
+ using (var admin = new MongoAdmin("mongodb://localhost/NormTests?pooling=false"))
+ {
+ admin.DropDatabase();
+ }
+ _server.Dispose();
+ }
+
+ [Fact]
+ public void FindUsesLimit()
+ {
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Find(new { }, 3).ToArray();
+ Assert.Equal(3, result.Length);
+ }
+
+ [Fact]
+ public void Count_Works()
+ {
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Count();
+ Assert.Equal(4, result);
+ }
+
+ [Fact]
+ public void Count_With_Filter_Works()
+ {
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Count(new Person { Name = "AAA" });
+ Assert.Equal(1,result);
+ }
+
+ [Fact]
+ public void DateTime_GreaterThan_Qualifier_Works()
+ {
+ _collection.Insert(new Person { Birthday = new DateTime(1910, 1, 1) });
+ _collection.Insert(new Person { Birthday = new DateTime(1920, 1, 1) });
+ _collection.Insert(new Person { Birthday = new DateTime(1930, 1, 1) });
+
+ var find = _collection.Find(new { Birthday = Q.GreaterThan(new DateTime(1920, 1, 1)) });
+ Assert.Equal(1, find.Count());
+ }
+
+
+ [Fact]
+ public void Where_Qualifier_Works()
+ {
+ _collection.Insert(new Person { Name = "Gnomey" });
+ _collection.Insert(new Person { Name = "kde" });
+ _collection.Insert(new Person { Name = "Elfy" });
+
+ var find = _collection.Find(Q.Where("this.Name === 'Elfy';"));
+ Assert.Equal(1, find.Count());
+ }
+
+ [Fact]
+ public void Find_Uses_Limit_Orderby_And_Skip()
+ {
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Find(new { Name = Q.NotEqual(new int?()) }, new { Name = OrderBy.Descending }, 3, 1).ToArray();
+ Assert.Equal(3, result.Length);
+ Assert.Equal("CCC", result[0].Name);
+ Assert.Equal("BBB", result[1].Name);
+ Assert.Equal("AAA", result[2].Name);
+ }
+
+ [Fact]
+ public void Find_Uses_Query_And_Orderby()
+ {
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Find(new { Name = Q.NotEqual("AAA") }, new { Name = OrderBy.Descending }).ToArray();
+ Assert.Equal(3, result.Length);
+ Assert.Equal("DDD", result[0].Name);
+ Assert.Equal("CCC", result[1].Name);
+ Assert.Equal("BBB", result[2].Name);
+ }
+
+ [Fact]
+ public void Find_Uses_Query_And_Orderby_And_Limit()
+ {
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Find(new { Name = Q.NotEqual("DDD") }, new { Name = OrderBy.Descending }, 2, 0).ToArray();
+ Assert.Equal(2, result.Length);
+ Assert.Equal("CCC", result[0].Name);
+ Assert.Equal("BBB", result[1].Name);
+ }
+
+ [Fact]
+ public void Find_Uses_Null_Qualifier()
+ {
+ _collection.Insert(new Person { Name = null });
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Find(new { Name = Q.IsNull() }, new { Name = OrderBy.Descending }, 2, 0).ToArray();
+ Assert.Equal(1, result.Length);
+ Assert.Equal(null, result[0].Name);
+
+ result = _collection.Find(new { Name = Q.IsNotNull() }, new { Name = OrderBy.Descending }).ToArray();
+ Assert.Equal(4, result.Length);
+ Assert.Equal("DDD", result[0].Name);
+ }
+
+ [Fact]
+ public void FindUsesLimitAndSkip()
+ {
+ _collection.Insert(new Person { Name = "BBB" });
+ _collection.Insert(new Person { Name = "CCC" });
+ _collection.Insert(new Person { Name = "AAA" });
+ _collection.Insert(new Person { Name = "DDD" });
+
+ var result = _collection.Find(new { }, 1, 1).ToArray();
+ Assert.Equal(1, result.Length);
+ Assert.Equal("CCC", result[0].Name);
+ }
+
+ [Fact]
+ public void FindCanQueryEmbeddedArray()
+ {
+ _collection.Delete(new { });
+ var person1 = new Person
+ {
+ Name = "Joe Cool",
+ Address =
+ {
+ Street = "123 Main St",
+ City = "Anytown",
+ State = "CO",
+ Zip = "45123"
+ }
+
+ };
+ var person2 = new Person
+ {
+ Name = "Sam Cool",
+ Address =
+ {
+ Street = "300 Main St",
+ City = "Anytown",
+ State = "CO",
+ Zip = "45123"
+ },
+ Relatives = new List<string>() { "Emma", "Bruce", "Charlie" }
+ };
+ _collection.Insert(person1);
+ _collection.Insert(person2);
+
+ var elem = new Expando();
+ elem["Relatives"] = "Charlie";
+ var a = _collection.Find(elem).ToArray();
+ Assert.Equal(1, a.Length);
+ }
+
+
+ [Fact]
+ public void BasicQueryUsingProperty()
+ {
+ _collection.Insert(new Person { Name = "Lisa Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
+ _collection.Insert(new Person { Name = "Joe Cool", Address = { Street = "123 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
+ _collection.Insert(new Person { Name = "Sam Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
+
+ var matchRegex = new Regex("^.{4}Cool$");
+ var results = _collection.Find(new { Name = matchRegex }).ToArray();
+ Assert.Equal(2, results.Length);
+ Assert.True(results.All(y => matchRegex.IsMatch(y.Name)));
+ }
+
+ [Fact]
+ public void BasicQueryWithSort()
+ {
+ //remove everything from the collection.
+ _collection.Delete(new { });
+
+ _collection.Insert(new Person { Name = "Third", LastContact = new DateTime(2010, 1, 1) });
+ _collection.Insert(new Person { Name = "First", LastContact = new DateTime(2000, 1, 1) });
+ _collection.Insert(new Person { Name = "Second", LastContact = new DateTime(2005, 1, 1) });
+
+ var people = _collection.Find(new { }, new { LastContact = 1 }).ToArray();
+ Assert.Equal(3, people.Length);
+ Assert.Equal("First", people[0].Name);
+ Assert.Equal("Second", people[1].Name);
+ Assert.Equal("Third", people[2].Name);
+ }
+
+ [Fact]
+ public void BasicQueryWithMultiSortOrdering()
+ {
+ //remove everything from the collection.
+ _collection.Delete(new { });
+
+ _collection.Insert(new Person { Name = "Third", LastContact = new DateTime(2010, 1, 1) });
+ _collection.Insert(new Person { Name = "First", LastContact = new DateTime(2005, 1, 1) });
+ _collection.Insert(new Person { Name = "Second", LastContact = new DateTime(2005, 1, 1) });
+
+ var people = _collection.Find(new { }, new { LastContact = -1, Name = 1 }).ToArray();
+ Assert.Equal(3, people.Length);
+ Assert.Equal("Third", people[0].Name);
+ Assert.Equal("First", people[1].Name);
+ Assert.Equal("Second", people[2].Name);
+ }
+
+ [Fact]
+ public void BasicQueryUsingChildProperty()
+ {
+ _collection.Insert(new Person { Name = "Joe Cool", Address = { Street = "123 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
+ _collection.Insert(new Person { Name = "Sam Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
+
+ var query = new Expando();
+ query["Address.City"] = Q.Equals<string>("Anytown");
+
+ var results = _collection.Find(query);
+ Assert.Equal(2, results.Count());
+ }
+ [Fact]
+ public void QueryWithinEmbeddedArray()
+ {
+
+ var post1 = new Person { Name = "First", Relatives = new List<String> { "comment1", "comment2" } };
+ var post2 = new Person { Name = "Second", Relatives = new List<String> { "commentA", "commentB" } };
+ _collection.Insert(post1);
+ _collection.Insert(post2);
+
+ var results = _collection.Find(new { Relatives = "commentA" });
+ Assert.Equal("Second", results.First().Name);
+ }
+
+
+ [Fact]
+ public void Distinct_On_Collection_Should_Return_Arrays_As_Value_If_Earlier_Than_1_5_0()
+ {
+ var isLessThan150 = Regex.IsMatch(_buildInfo.Version, "^([01][.][01234])");
+
+ // Any version earlier than MongoDB 1.5.0
+ if (isLessThan150)
+ {
+ _collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
+ _collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
+ _collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
+ _collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
+
+ var results = _collection.Distinct<string[]>("Relatives");
+ Assert.Equal(3, results.Count());
+ }
+ }
+
+ [Fact]
+ public void Distinct_On_Collection_Should_Return_Array_Values_In_1_5_0_Or_Later()
+ {
+ var isLessThan150 = Regex.IsMatch(_buildInfo.Version, "^([01][.][01234])");
+
+ // Any version MongoDB 1.5.0 +
+ if (!isLessThan150)
+ {
+ _collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
+ _collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
+ _collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
+ _collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
+
+ var results = _collection.Distinct<string>("Relatives");
+ Assert.Equal(4, results.Count());
+ }
+ }
+
+ [Fact]
+ public void DistinctOnSimpleProperty()
+ {
+ _collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
+ _collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
+ _collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
+ _collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
+
+ var results = _collection.Distinct<string>("Name");
+ Assert.Equal(4, results.Count());
+ }
+
+ [Fact]
+ public void DistinctOnComplexProperty()
+ {
+ _collection.Insert(new Person { Name = "Joe Cool", Address = new Address { State = "CA" } });
+ _collection.Insert(new Person { Name = "Sam Cool", Address = new Address { State = "CA" } });
+ _collection.Insert(new Person { Name = "Ted Cool", Address = new Address { State = "CA", Zip = "90010" } });
+ _collection.Insert(new Person { Name = "Jay Cool", Address = new Address { State = "NY" } });
+
+ var results = _collection.Distinct<Address>("Address");
+ Assert.Equal(3, results.Count());
+ }
+ }
}
View
238 NoRM.Tests/CollectionUpdateTests/UpdateTests.cs
@@ -1,120 +1,120 @@
-using System;
-using System.Linq;
-using Norm.BSON;
-using Xunit;
-using Norm.Collections;
-
-namespace Norm.Tests
-{
- public class UpdateTests : IDisposable
- {
- private readonly Mongo _server;
- private readonly MongoCollection<CheeseClubContact> _collection;
- public UpdateTests()
- {
- _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
- _collection = _server.GetCollection<CheeseClubContact>("CheeseClubContacts");
- }
- public void Dispose()
- {
- _server.Database.DropCollection("CheeseClubContacts");
- using (var admin = new MongoAdmin("mongodb://localhost/NormTests?pooling=false"))
- {
- admin.DropDatabase();
- }
- _server.Dispose();
- }
-
- [Fact]
- public void Update_Multiple_With_Lambda_Works()
- {
- _collection.Insert(new CheeseClubContact { Name = "Hello" }, new CheeseClubContact { Name = "World" });
- _collection.Update(new { Name = Q.NotEqual("") }, h => h.SetValue(y => y.Name, "Cheese"), true, false);
- Assert.Equal(2, _collection.Find(new { Name = "Cheese" }).Count());
- }
-
- [Fact]
- public void BasicUsageOfUpdateOne()
- {
- var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "Cheddar" };
- _collection.Insert(aPerson);
-
- var count = _collection.Find();
- Assert.Equal(1, count.Count());
-
- var matchDocument = new { Name = "Joe" };
- aPerson.FavoriteCheese = "Gouda";
-
- _collection.UpdateOne(matchDocument, aPerson);
-
- var query = new Expando();
- query["Name"] = Q.Equals<string>("Joe");
-
- var retreivedPerson = _collection.FindOne(query);
-
- Assert.Equal("Gouda", retreivedPerson.FavoriteCheese);
- }
-
- [Fact]
- public void BasicUsageOfUpdateOneUsingObjectId()
- {
- var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "American" };
- _collection.Insert(aPerson);
-
- var results = _collection.Find();
- Assert.Equal(1, results.Count());
-
- var matchDocument = new { _id = aPerson.Id };
- aPerson.FavoriteCheese = "Velveeta";
-
- _collection.UpdateOne(matchDocument, aPerson);
-
- var query = new Expando();
- query["_id"] = Q.Equals<ObjectId>(aPerson.Id);
-
- var retreivedPerson = _collection.FindOne(query);
-
- Assert.Equal("Velveeta", retreivedPerson.FavoriteCheese);
- }
-
- [Fact]
- public void UpdateMustSpecifyEverything()
- {
- //Note, when doing an update, MongoDB replaces everything in your document except the id. So, if you don't re-specify a property, it'll disappear.
- //In this example, the cheese is gone.
-
- var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "Cheddar" };
-
- Assert.NotNull(aPerson.FavoriteCheese);
-
- _collection.Insert(aPerson);
-
- var matchDocument = new { Name = "Joe" };
- var updatesToApply = new { Name = "Joseph" };
-
- _collection.UpdateOne(matchDocument, updatesToApply);
-
- var query = new Expando();
- query["Name"] = Q.Equals<string>("Joseph");
-
- var retreivedPerson = _collection.FindOne(query);
-
- Assert.Null(retreivedPerson.FavoriteCheese);
- }
-
- [Fact]
- public void UsingUpsertToInsertOnUpdate()
- {
- //Upsert will update an existing document if it can find a match, otherwise it will insert.
- //In this scenario, we're updating our collection without inserting first.
-
- var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "American" };
-
- var matchDocument = new { Name = aPerson.Name };
- _collection.Update(matchDocument, aPerson, false, true);
-
- var results = _collection.Find();
- Assert.Equal(1, results.Count());
- }
- }
+using System;
+using System.Linq;
+using Norm.BSON;
+using Xunit;
+using Norm.Collections;
+
+namespace Norm.Tests
+{
+ public class UpdateTests : IDisposable
+ {
+ private readonly Mongo _server;
+ private readonly MongoCollection<CheeseClubContact> _collection;
+ public UpdateTests()
+ {
+ _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
+ _collection = _server.GetCollection<CheeseClubContact>("CheeseClubContacts");
+ }
+ public void Dispose()
+ {
+ _server.Database.DropCollection("CheeseClubContacts");
+ using (var admin = new MongoAdmin("mongodb://localhost/NormTests?pooling=false"))
+ {
+ admin.DropDatabase();
+ }
+ _server.Dispose();
+ }
+
+ [Fact]
+ public void Update_Multiple_With_Lambda_Works()
+ {
+ _collection.Insert(new CheeseClubContact { Name = "Hello" }, new CheeseClubContact { Name = "World" });
+ _collection.Update(new { Name = Q.NotEqual("") }, h => h.SetValue(y => y.Name, "Cheese"), true, false);
+ Assert.Equal(2, _collection.Find(new { Name = "Cheese" }).Count());
+ }
+
+ [Fact]
+ public void BasicUsageOfUpdateOne()
+ {
+ var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "Cheddar" };
+ _collection.Insert(aPerson);
+
+ var count = _collection.Find();
+ Assert.Equal(1, count.Count());
+
+ var matchDocument = new { Name = "Joe" };
+ aPerson.FavoriteCheese = "Gouda";
+
+ _collection.UpdateOne(matchDocument, aPerson);
+
+ var query = new Expando();
+ query["Name"] = Q.Equals<string>("Joe");
+
+ var retreivedPerson = _collection.FindOne(query);
+
+ Assert.Equal("Gouda", retreivedPerson.FavoriteCheese);
+ }
+
+ [Fact]
+ public void BasicUsageOfUpdateOneUsingObjectId()
+ {
+ var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "American" };
+ _collection.Insert(aPerson);
+
+ var results = _collection.Find();
+ Assert.Equal(1, results.Count());
+
+ var matchDocument = new { _id = aPerson.Id };
+ aPerson.FavoriteCheese = "Velveeta";
+
+ _collection.UpdateOne(matchDocument, aPerson);
+
+ var query = new Expando();
+ query["_id"] = Q.Equals<ObjectId>(aPerson.Id);
+
+ var retreivedPerson = _collection.FindOne(query);
+
+ Assert.Equal("Velveeta", retreivedPerson.FavoriteCheese);
+ }
+
+ [Fact]
+ public void UpdateMustSpecifyEverything()
+ {
+ //Note, when doing an update, MongoDB replaces everything in your document except the id. So, if you don't re-specify a property, it'll disappear.
+ //In this example, the cheese is gone.
+
+ var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "Cheddar" };
+
+ Assert.NotNull(aPerson.FavoriteCheese);
+
+ _collection.Insert(aPerson);
+
+ var matchDocument = new { Name = "Joe" };
+ var updatesToApply = new { Name = "Joseph" };
+
+ _collection.UpdateOne(matchDocument, updatesToApply);
+
+ var query = new Expando();
+ query["Name"] = Q.Equals<string>("Joseph");
+
+ var retreivedPerson = _collection.FindOne(query);
+
+ Assert.Null(retreivedPerson.FavoriteCheese);
+ }
+
+ [Fact]
+ public void UsingUpsertToInsertOnUpdate()
+ {
+ //Upsert will update an existing document if it can find a match, otherwise it will insert.
+ //In this scenario, we're updating our collection without inserting first.
+
+ var aPerson = new CheeseClubContact { Name = "Joe", FavoriteCheese = "American" };
+
+ var matchDocument = new { Name = aPerson.Name };
+ _collection.Update(matchDocument, aPerson, false, true);
+
+ var results = _collection.Find();
+ Assert.Equal(1, results.Count());
+ }
+ }
}
View
54 NoRM.Tests/ConnectionsTests/NormalConnectionProviderAuthenticationTests.cs
@@ -1,28 +1,28 @@
-using Xunit;
-
-namespace Norm.Tests
-{
- public class NormalConnectionProviderAuthenticationTests : AuthenticatedFixture
- {
- [Fact(Skip="authenticated connection seems to be hanging when we run this in sequence")]
- public void ThrowsExceptionIfConnectingWithInvalidCredentials()
- {
- var provider = new NormalConnectionProvider(ConnectionStringBuilder.Create(AuthenticatedConnectionString("bad", "boy")));
-
- var ex = Assert.Throws<MongoException>(() => provider.Open(null));
- Assert.Equal("auth fails", ex.Message);
- }
-
- //won't pass on some 1.3.x builds of the server, but will pass against newest, or stable (1.2.3).
- [Fact]
- public void AuthenticatesWithProperCredentials()
- {
- var provider = new NormalConnectionProvider(ConnectionStringBuilder.Create(AuthenticatedConnectionString("usr", "8e156e298e19afdc3a104ddd172a2bee")));
- var connection = provider.Open(null);
- Assert.Equal(true, connection.Client.Connected);
- provider.Close(connection);
- }
-
-
- }
+using Xunit;
+
+namespace Norm.Tests
+{
+ public class NormalConnectionProviderAuthenticationTests : AuthenticatedFixture
+ {
+ [Fact(Skip="authenticated connection seems to be hanging when we run this in sequence")]
+ public void ThrowsExceptionIfConnectingWithInvalidCredentials()
+ {
+ var provider = new NormalConnectionProvider(ConnectionStringBuilder.Create(AuthenticatedConnectionString("bad", "boy")));
+
+ var ex = Assert.Throws<MongoException>(() => provider.Open(null));
+ Assert.Equal("auth fails", ex.Message);
+ }
+
+ //won't pass on some 1.3.x builds of the server, but will pass against newest, or stable (1.2.3).
+ [Fact]
+ public void AuthenticatesWithProperCredentials()
+ {
+ var provider = new NormalConnectionProvider(ConnectionStringBuilder.Create(AuthenticatedConnectionString("usr", "8e156e298e19afdc3a104ddd172a2bee")));
+ var connection = provider.Open(null);
+ Assert.Equal(true, connection.Client.Connected);
+ provider.Close(connection);
+ }
+
+
+ }
}
View
144 NoRM.Tests/DBTypeTests/DBRefTests.cs
@@ -1,76 +1,76 @@
-using System.Collections.Generic;
-using System.Linq;
-using Norm.BSON.DbTypes;
-using Xunit;
-
-namespace Norm.Tests
-{
- public class DbRefTests
- {
- public DbRefTests()
- {
- using (var db = Mongo.Create(TestHelper.ConnectionString()))
- {
- db.GetCollection<TestProduct>().Delete(new { });
- db.GetCollection<ProductReference>().Delete(new { });
- db.GetCollection<User3>().Delete(new { });
- db.GetCollection<Role>().Delete(new { });
- }
- }
-
- [Fact]
- public void DbRefMapsToOtherDocumentsByOid()
- {
- var id = ObjectId.NewObjectId();
-
- using (var db = Mongo.Create(TestHelper.ConnectionString()))
- {
-
- db.GetCollection<TestProduct>().Insert(new TestProduct { _id = id, Name = "RefProduct" });
-
- var productReference = new DbReference<TestProduct>(id);
-
- db.GetCollection<ProductReference>().Insert(new ProductReference
- {
- Id = ObjectId.NewObjectId(),
- Name = "FullCart",
- ProductsOrdered = new[] { productReference }
- });
-
- var reference = db.GetCollection<ProductReference>().Find(new { }).First();
- var product = reference.ProductsOrdered[0].Fetch(() => db);
- Assert.Equal(id.Value, product._id.Value);
- }
-
- }
-
- [Fact]
- public void DbRefMapsToOtherDocumentsByCustomId()
- {
- const string userId = "Tim Berners-Lee";
- const string roleName = "Administrator";
-
- using (var db = Mongo.Create(TestHelper.ConnectionString()))
- {
- db.GetCollection<User3>().Insert(new User3
- {
- Id = userId,
- EmailAddress = "user@domain.com"
- });
- db.GetCollection<Role>().Insert(new Role
- {
- Id = roleName,
+using System.Collections.Generic;
+using System.Linq;
+using Norm.BSON.DbTypes;
+using Xunit;
+
+namespace Norm.Tests
+{
+ public class DbRefTests
+ {
+ public DbRefTests()
+ {
+ using (var db = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ db.GetCollection<TestProduct>().Delete(new { });
+ db.GetCollection<ProductReference>().Delete(new { });
+ db.GetCollection<User3>().Delete(new { });
+ db.GetCollection<Role>().Delete(new { });
+ }
+ }
+
+ [Fact]
+ public void DbRefMapsToOtherDocumentsByOid()
+ {
+ var id = ObjectId.NewObjectId();
+
+ using (var db = Mongo.Create(TestHelper.ConnectionString()))
+ {
+
+ db.GetCollection<TestProduct>().Insert(new TestProduct { _id = id, Name = "RefProduct" });
+
+ var productReference = new DbReference<TestProduct>(id);
+
+ db.GetCollection<ProductReference>().Insert(new ProductReference
+ {
+ Id = ObjectId.NewObjectId(),
+ Name = "FullCart",
+ ProductsOrdered = new[] { productReference }
+ });
+
+ var reference = db.GetCollection<ProductReference>().Find(new { }).First();
+ var product = reference.ProductsOrdered[0].Fetch(() => db);
+ Assert.Equal(id.Value, product._id.Value);
+ }
+
+ }
+
+ [Fact]
+ public void DbRefMapsToOtherDocumentsByCustomId()
+ {
+ const string userId = "Tim Berners-Lee";
+ const string roleName = "Administrator";
+
+ using (var db = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ db.GetCollection<User3>().Insert(new User3
+ {
+ Id = userId,
+ EmailAddress = "user@domain.com"
+ });
+ db.GetCollection<Role>().Insert(new Role
+ {
+ Id = roleName,
Users = new List<DbReference<User3, string>>
{
new DbReference<User3, string>(userId)
- }
- });
-
- var role = db.GetCollection<Role>().Find().First();
- var user = role.Users[0].Fetch(() => db);
-
- Assert.Equal(userId, user.Id);
- }
- }
- }
+ }
+ });
+
+ var role = db.GetCollection<Role>().Find().First();
+ var user = role.Users[0].Fetch(() => db);
+
+ Assert.Equal(userId, user.Id);
+ }
+ }
+ }
}
View
114 NoRM.Tests/DBTypeTests/GridFSTests.cs
@@ -1,57 +1,57 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Xunit;
-using System.IO.Compression;
-using System.Resources;
-using System.Reflection;
-using System.IO;
-using System.Security.Cryptography;
-using Norm;
-using Norm.Tests;
-using Norm.BSON.DbTypes;
-
-namespace NoRM.Tests.DBTypeTests
-{
- public class GridFSTests
- {
- private MemoryStream _randomBytes = new MemoryStream(10 * 1024 * 1024);
- private MD5 _hasher = MD5.Create();
- private byte[] _randomByteHash;
- private Mongo _db;
-
- public GridFSTests()
- {
- //construct a random 10MB stream.
- Random r = new Random(DateTime.Now.Millisecond);
- for (int i = 0; i < 1024 * 1024 * 1.5; i++)
- {
- this._randomBytes.Write(BitConverter.GetBytes(r.NextDouble()), 0, 8);
- }
- this._randomBytes.Position = 0;
- this._randomByteHash = _hasher.ComputeHash(_randomBytes);
- this._randomBytes.Position = 0;
- this._db = Mongo.Create(TestHelper.ConnectionString());
- using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- admin.DropDatabase();
- }
- }
-
- [Fact]
- public void StorageOfFileIsNotLossy()
- {
- //var fcoll = this._db.GetCollection<Object>("aCollection").GetChildCollection<GridFile>("files");
- //GridFile gf = new GridFile();
- //gf.uploadDate = DateTime.Now;
- //gf.length = this._randomBytes.Length;
- //gf.md5 = this._randomByteHash.Aggregate("", (seed, current) => seed += String.Format("{0:x2}", current));
- //gf.contentType = "application/x-octet-stream";
- //gf.filename = "Random.bin";
- //gf._id = Guid.NewGuid();
- //gf.WriteToServer(this._randomBytes, true);
- }
-
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Xunit;
+using System.IO.Compression;
+using System.Resources;
+using System.Reflection;
+using System.IO;
+using System.Security.Cryptography;
+using Norm;
+using Norm.Tests;
+using Norm.BSON.DbTypes;
+
+namespace NoRM.Tests.DBTypeTests
+{
+ public class GridFSTests
+ {
+ private MemoryStream _randomBytes = new MemoryStream(10 * 1024 * 1024);
+ private MD5 _hasher = MD5.Create();
+ private byte[] _randomByteHash;
+ private Mongo _db;
+
+ public GridFSTests()
+ {
+ //construct a random 10MB stream.
+ Random r = new Random(DateTime.Now.Millisecond);
+ for (int i = 0; i < 1024 * 1024 * 1.5; i++)
+ {
+ this._randomBytes.Write(BitConverter.GetBytes(r.NextDouble()), 0, 8);
+ }
+ this._randomBytes.Position = 0;
+ this._randomByteHash = _hasher.ComputeHash(_randomBytes);
+ this._randomBytes.Position = 0;
+ this._db = Mongo.Create(TestHelper.ConnectionString());
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ admin.DropDatabase();
+ }
+ }
+
+ [Fact]
+ public void StorageOfFileIsNotLossy()
+ {
+ //var fcoll = this._db.GetCollection<Object>("aCollection").GetChildCollection<GridFile>("files");
+ //GridFile gf = new GridFile();
+ //gf.uploadDate = DateTime.Now;
+ //gf.length = this._randomBytes.Length;
+ //gf.md5 = this._randomByteHash.Aggregate("", (seed, current) => seed += String.Format("{0:x2}", current));
+ //gf.contentType = "application/x-octet-stream";
+ //gf.filename = "Random.bin";
+ //gf._id = Guid.NewGuid();
+ //gf.WriteToServer(this._randomBytes, true);
+ }
+
+ }
+}
View
64 NoRM.Tests/ExpandoTests.cs
@@ -1,32 +1,32 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Xunit;
-using Norm.BSON;
-
-namespace NoRM.Tests
-{
- public class ExpandoTests
- {
- [Fact]
- public void Expando_Get_Should_Throw_Exception_When_Property_Doesnt_Exist()
- {
- Assert.Throws<InvalidOperationException>(() => { var a = new Expando(); a.Get<bool>("doesn't exist"); });
- }
-
- [Fact]
- public void Expando_TryGet_Should_Not_Throw_Exception_When_Property_Doesnt_Exist()
- {
- var e = new Expando();
- e["hello"] = "world";
- var outval = "";
- Assert.True(e.TryGet<String>("hello",out outval));
- Assert.False(e.TryGet<String>("hello2", out outval));
-
- bool out2;
- Assert.False(e.TryGet<bool>("hello", out out2));
-
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Xunit;
+using Norm.BSON;
+
+namespace NoRM.Tests
+{
+ public class ExpandoTests
+ {
+ [Fact]
+ public void Expando_Get_Should_Throw_Exception_When_Property_Doesnt_Exist()
+ {
+ Assert.Throws<InvalidOperationException>(() => { var a = new Expando(); a.Get<bool>("doesn't exist"); });
+ }
+
+ [Fact]
+ public void Expando_TryGet_Should_Not_Throw_Exception_When_Property_Doesnt_Exist()
+ {
+ var e = new Expando();
+ e["hello"] = "world";
+ var outval = "";
+ Assert.True(e.TryGet<String>("hello",out outval));
+ Assert.False(e.TryGet<String>("hello2", out outval));
+
+ bool out2;
+ Assert.False(e.TryGet<bool>("hello", out out2));
+
+ }
+ }
+}
View
78 NoRM.Tests/GeneralTests.cs
@@ -1,39 +1,39 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Xunit;
-using Norm.Responses;
-using Norm;
-using Norm.Tests;
-
-
-namespace NoRM.Tests
-{
- public class GeneralTests
- {
- [Fact]
- public void Get_Last_Error_Returns()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var le = mongo.LastError();
- Assert.Equal(true, le.WasSuccessful);
- }
- }
-
- [Fact]
- public void Base_Status_Message_Supports_Expando()
- {
- BaseStatusMessage bsm = new BaseStatusMessage();
- bsm["hello"] = "world";
- Assert.Equal("world",bsm["hello"]);
- Assert.Equal(bsm.AllProperties().First().PropertyName, "hello");
-
- Assert.Equal(bsm.AllProperties().First().Value, "world");
- bsm.Delete("hello");
- Assert.False(bsm.AllProperties().Any());
-
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Xunit;
+using Norm.Responses;
+using Norm;
+using Norm.Tests;
+
+
+namespace NoRM.Tests
+{
+ public class GeneralTests
+ {
+ [Fact]
+ public void Get_Last_Error_Returns()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var le = mongo.LastError();
+ Assert.Equal(true, le.WasSuccessful);
+ }
+ }
+
+ [Fact]
+ public void Base_Status_Message_Supports_Expando()
+ {
+ BaseStatusMessage bsm = new BaseStatusMessage();
+ bsm["hello"] = "world";
+ Assert.Equal("world",bsm["hello"]);
+ Assert.Equal(bsm.AllProperties().First().PropertyName, "hello");
+
+ Assert.Equal(bsm.AllProperties().First().Value, "world");
+ bsm.Delete("hello");
+ Assert.False(bsm.AllProperties().Any());
+
+ }
+ }
+}
View
64 NoRM.Tests/LinqTests/LinqTests.cs
@@ -113,38 +113,38 @@ public void LinqQueriesShouldSupportMultiplication()
Assert.Equal(1, list.Count);
Assert.Equal("1", list[0].Name);
}
- }
-
- [Fact]
- public void LinqQueriesShouldSupportBitwiseOr()
- {
- using (var session = new Session())
- {
- session.Add(new TestProduct { Name = "2", Price = 20 });
- session.Add(new TestProduct { Name = "1", Price = 10, Inventory = new List<InventoryChange>() { new InventoryChange() } });
- session.Add(new TestProduct { Name = "3", Price = 30 });
-
- var list = session.Products.Where(x => (x.Inventory.Count()|2) == 3).ToList();
- Assert.Equal(1, list.Count);
- }
- }
-
- [Fact]
- public void LinqQueriesShouldSupportBitwiseAnd()
- {
- using (var session = new Session())
- {
- session.Add(new TestProduct { Name = "1", Price = 10 ,Inventory = new List<InventoryChange>() { new InventoryChange() } });
- session.Add(new TestProduct { Name = "2", Price = 20, Inventory = new List<InventoryChange>() { new InventoryChange() } });
- session.Add(new TestProduct { Name = "3", Price = 30,Inventory = new List<InventoryChange>() {
- new InventoryChange(), new InventoryChange() } });
-
- var list = session.Products.Where(x => (x.Inventory.Count() & 2) == 2).ToList();
- Assert.Equal(1, list.Count);
-
- list = session.Products.Where(x => (x.Inventory.Count() & 1) == 1).ToList();
- Assert.Equal(2, list.Count);
- }
+ }
+
+ [Fact]
+ public void LinqQueriesShouldSupportBitwiseOr()
+ {
+ using (var session = new Session())
+ {
+ session.Add(new TestProduct { Name = "2", Price = 20 });
+ session.Add(new TestProduct { Name = "1", Price = 10, Inventory = new List<InventoryChange>() { new InventoryChange() } });
+ session.Add(new TestProduct { Name = "3", Price = 30 });
+
+ var list = session.Products.Where(x => (x.Inventory.Count()|2) == 3).ToList();
+ Assert.Equal(1, list.Count);
+ }
+ }
+
+ [Fact]
+ public void LinqQueriesShouldSupportBitwiseAnd()
+ {
+ using (var session = new Session())
+ {
+ session.Add(new TestProduct { Name = "1", Price = 10 ,Inventory = new List<InventoryChange>() { new InventoryChange() } });
+ session.Add(new TestProduct { Name = "2", Price = 20, Inventory = new List<InventoryChange>() { new InventoryChange() } });
+ session.Add(new TestProduct { Name = "3", Price = 30,Inventory = new List<InventoryChange>() {
+ new InventoryChange(), new InventoryChange() } });
+
+ var list = session.Products.Where(x => (x.Inventory.Count() & 2) == 2).ToList();
+ Assert.Equal(1, list.Count);
+
+ list = session.Products.Where(x => (x.Inventory.Count() & 1) == 1).ToList();
+ Assert.Equal(2, list.Count);
+ }
}
[Fact]
View
502 NoRM.Tests/MongoAdminTests.cs
@@ -1,252 +1,252 @@
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Text.RegularExpressions;
-using Xunit;
-using System.Linq;
-using System;
-
-namespace Norm.Tests
-{
- public class MongoAdminTests
- {
- public MongoAdminTests()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString("strict=false")))
- {
- admin.DropDatabase();
- }
- }
-
- [Fact]
- public void Set_Profile_Level_Changes_Profile_Level_And_Reports_Change()
- {
- using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- int prev;
- if (mAdmin.SetProfileLevel(2, out prev))
- {
- mAdmin.SetProfileLevel(prev);
- }
- }
-
- }
-
-
- [Fact]
- public void Get_Assert_Info_Returns_Results()
- {
- using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var aInfo = mAdmin.AssertionInfo();
- Assert.NotNull(aInfo);
- Assert.Equal(true, aInfo.WasSuccessful);
- }
-
- }
-
- [Fact]
- public void Get_CurrentOp_Returns()
- {
- using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var currOps = mAdmin.GetCurrentOperations().ToArray();
- }
-
- }
-
- [Fact]
- public void Get_CurrentOp_Returns_Results()
- {
- using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var a = mAdmin.PreviousErrors();
- Assert.Equal(true, a.WasSuccessful);
- }
- }
-
- [Fact]
- public void Get_Current_Profile_Level()
- {
- using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var level = mAdmin.GetProfileLevel();
- if (level != 2d)
- {
- mAdmin.SetProfileLevel(2);
-
- }
- Assert.Equal(2, mAdmin.GetProfileLevel());
- mAdmin.SetProfileLevel(level);
- }
- }
-
- [Fact]
- public void Repair_Database_Returns()
- {
- using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- Assert.True(mAdmin.RepairDatabase(false, false));
- }
- }
-
-
- [Fact]
- public void Kill_Operation_Returns()
- {
- //since we don't have any long-running ops, this is all we can test without mocks.
- using (var mAdmin = new MongoAdmin("mongodb://localhost/admin"))
- {
- var x = mAdmin.KillOperation(double.MaxValue);
- Assert.Equal(false,x.WasSuccessful);
- Assert.Equal("no op number field specified?",x["err"]);
- }
- }
-
-
- [Fact]
- public void Reset_Last_Error_Returns()
- {
- using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- Assert.True(mAdmin.ResetLastError());
- }
- }
-
-
- [Fact]
- public void ListsAllDatabases()
- {
- var expected = new List<string> { "admin", "NormTests", "local" };
-
- //create another database
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- mongo.GetCollection<FakeObject>().Insert(new FakeObject());
- }
-
- using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
- {
- foreach (var db in admin.GetAllDatabases())
- {
- expected.Remove(db.Name);
- }
- }
- Assert.Equal(0, expected.Count);
- }
- [Fact]
- public void ListsAllDatabasesThrowsExceptionIfNotConnectedToAdmin()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var ex = Assert.Throws<MongoException>(() => admin.GetAllDatabases());
- Assert.Equal("This command is only valid when connected to admin", ex.Message);
- }
- }
-
- [Fact]
- public void ReturnsBuildInfo()
- {
- string gitVersion;
- string version;
- using (var process = new Process())
- {
- process.StartInfo = new ProcessStartInfo("mongod", "--version") { RedirectStandardOutput = true, UseShellExecute = false };
- process.Start();
- using (var stream = process.StandardOutput)
- {
- var data = stream.ReadToEnd();
- gitVersion = Regex.Match(data, "git version: ([a-f0-9]+)\r\n").Groups[1].Value;
- version = Regex.Match(data, "db version v([^,]+),").Groups[1].Value;
- }
- }
-
- using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
- {
- var info = admin.BuildInfo();
- Assert.Equal(true, info.WasSuccessful);
- Assert.Equal(gitVersion, info.GitVersion);
- Assert.Equal(version, info.Version);
- }
- }
- [Fact]
- public void BuildInfoThrowsExceptionIfNotConnectedToAdmin()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var ex = Assert.Throws<MongoException>(() => admin.BuildInfo());
- Assert.Equal("This command is only valid when connected to admin", ex.Message);
- }
- }
-
- [Fact]
- public void ForceSyncDoesSomethingOk()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
- {
- if (!admin.BuildInfo().SystemInformation.ToLower().Contains("windows"))
- {
- var response = admin.ForceSync(true);
- Assert.Equal(true, response.WasSuccessful);
- Assert.True(response.NumberOfFiles > 0); //don't know what this is
- }
- else
- {
- Assert.Throws(typeof(MongoException), () => admin.ForceSync(true));
- Console.WriteLine("FSync is not supported on windows version of MongoDB and will throw an exception.");
- }
- }
- }
- [Fact]
- public void ForceSyncThrowsExceptionIfNotConnectedToAdmin()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var ex = Assert.Throws<MongoException>(() => admin.ForceSync(true));
- Assert.Equal("This command is only valid when connected to admin", ex.Message);
- }
- }
-
- [Fact]
- public void DropsDatabase()
- {
- //create another database
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- mongo.GetCollection<FakeObject>().Insert(new FakeObject());
- }
- using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- admin.DropDatabase();
- }
-
- using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
- {
- foreach (var db in admin.GetAllDatabases())
- {
- Assert.NotEqual("NormTests", db.Name);
- }
- }
- }
-
- [Fact]
- public void GetsServerStatus()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- var status = admin.ServerStatus();
- Assert.Equal(true, status.WasSuccessful);
- }
- }
-
- [Fact]
- public void ReturnsEmptyInProcessResponse()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
- {
- var response = admin.GetCurrentOperations();
- Assert.Equal(0, response.Count());
- }
- }
-
- }
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Text.RegularExpressions;
+using Xunit;
+using System.Linq;
+using System;
+
+namespace Norm.Tests
+{
+ public class MongoAdminTests
+ {
+ public MongoAdminTests()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString("strict=false")))
+ {
+ admin.DropDatabase();
+ }
+ }
+
+ [Fact]
+ public void Set_Profile_Level_Changes_Profile_Level_And_Reports_Change()
+ {
+ using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ int prev;
+ if (mAdmin.SetProfileLevel(2, out prev))
+ {
+ mAdmin.SetProfileLevel(prev);
+ }
+ }
+
+ }
+
+
+ [Fact]
+ public void Get_Assert_Info_Returns_Results()
+ {
+ using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var aInfo = mAdmin.AssertionInfo();
+ Assert.NotNull(aInfo);
+ Assert.Equal(true, aInfo.WasSuccessful);
+ }
+
+ }
+
+ [Fact]
+ public void Get_CurrentOp_Returns()
+ {
+ using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var currOps = mAdmin.GetCurrentOperations().ToArray();
+ }
+
+ }
+
+ [Fact]
+ public void Get_CurrentOp_Returns_Results()
+ {
+ using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var a = mAdmin.PreviousErrors();
+ Assert.Equal(true, a.WasSuccessful);
+ }
+ }
+
+ [Fact]
+ public void Get_Current_Profile_Level()
+ {
+ using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var level = mAdmin.GetProfileLevel();
+ if (level != 2d)
+ {
+ mAdmin.SetProfileLevel(2);
+
+ }
+ Assert.Equal(2, mAdmin.GetProfileLevel());
+ mAdmin.SetProfileLevel(level);
+ }
+ }
+
+ [Fact]
+ public void Repair_Database_Returns()
+ {
+ using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ Assert.True(mAdmin.RepairDatabase(false, false));
+ }
+ }
+
+
+ [Fact]
+ public void Kill_Operation_Returns()
+ {
+ //since we don't have any long-running ops, this is all we can test without mocks.
+ using (var mAdmin = new MongoAdmin("mongodb://localhost/admin"))
+ {
+ var x = mAdmin.KillOperation(double.MaxValue);
+ Assert.Equal(false,x.WasSuccessful);
+ Assert.Equal("no op number field specified?",x["err"]);
+ }
+ }
+
+
+ [Fact]
+ public void Reset_Last_Error_Returns()
+ {
+ using (var mAdmin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ Assert.True(mAdmin.ResetLastError());
+ }
+ }
+
+
+ [Fact]
+ public void ListsAllDatabases()
+ {
+ var expected = new List<string> { "admin", "NormTests", "local" };
+
+ //create another database
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ mongo.GetCollection<FakeObject>().Insert(new FakeObject());
+ }
+
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
+ {
+ foreach (var db in admin.GetAllDatabases())
+ {
+ expected.Remove(db.Name);
+ }
+ }
+ Assert.Equal(0, expected.Count);
+ }
+ [Fact]
+ public void ListsAllDatabasesThrowsExceptionIfNotConnectedToAdmin()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var ex = Assert.Throws<MongoException>(() => admin.GetAllDatabases());
+ Assert.Equal("This command is only valid when connected to admin", ex.Message);
+ }
+ }
+
+ [Fact]
+ public void ReturnsBuildInfo()
+ {
+ string gitVersion;
+ string version;
+ using (var process = new Process())
+ {
+ process.StartInfo = new ProcessStartInfo("mongod", "--version") { RedirectStandardOutput = true, UseShellExecute = false };
+ process.Start();
+ using (var stream = process.StandardOutput)
+ {
+ var data = stream.ReadToEnd();
+ gitVersion = Regex.Match(data, "git version: ([a-f0-9]+)\r\n").Groups[1].Value;
+ version = Regex.Match(data, "db version v([^,]+),").Groups[1].Value;
+ }
+ }
+
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
+ {
+ var info = admin.BuildInfo();
+ Assert.Equal(true, info.WasSuccessful);
+ Assert.Equal(gitVersion, info.GitVersion);
+ Assert.Equal(version, info.Version);
+ }
+ }
+ [Fact]
+ public void BuildInfoThrowsExceptionIfNotConnectedToAdmin()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var ex = Assert.Throws<MongoException>(() => admin.BuildInfo());
+ Assert.Equal("This command is only valid when connected to admin", ex.Message);
+ }
+ }
+
+ [Fact]
+ public void ForceSyncDoesSomethingOk()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
+ {
+ if (!admin.BuildInfo().SystemInformation.ToLower().Contains("windows"))
+ {
+ var response = admin.ForceSync(true);
+ Assert.Equal(true, response.WasSuccessful);
+ Assert.True(response.NumberOfFiles > 0); //don't know what this is
+ }
+ else
+ {
+ Assert.Throws(typeof(MongoException), () => admin.ForceSync(true));
+ Console.WriteLine("FSync is not supported on windows version of MongoDB and will throw an exception.");
+ }
+ }
+ }
+ [Fact]
+ public void ForceSyncThrowsExceptionIfNotConnectedToAdmin()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var ex = Assert.Throws<MongoException>(() => admin.ForceSync(true));
+ Assert.Equal("This command is only valid when connected to admin", ex.Message);
+ }
+ }
+
+ [Fact]
+ public void DropsDatabase()
+ {
+ //create another database
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ mongo.GetCollection<FakeObject>().Insert(new FakeObject());
+ }
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ admin.DropDatabase();
+ }
+
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
+ {
+ foreach (var db in admin.GetAllDatabases())
+ {
+ Assert.NotEqual("NormTests", db.Name);
+ }
+ }
+ }
+
+ [Fact]
+ public void GetsServerStatus()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ var status = admin.ServerStatus();
+ Assert.Equal(true, status.WasSuccessful);
+ }
+ }
+
+ [Fact]
+ public void ReturnsEmptyInProcessResponse()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString(null, "admin", null, null)))
+ {
+ var response = admin.GetCurrentOperations();
+ Assert.Equal(0, response.Count());
+ }
+ }
+
+ }
}
View
394 NoRM.Tests/MongoDatabaseTest.cs
@@ -1,198 +1,198 @@
-using System.Collections.Generic;
-using System.Linq;
-using Xunit;
-using Norm.Collections;
-
-namespace Norm.Tests
-{
-
- public class MongoDatabaseTest
- {
- public MongoDatabaseTest()
- {
- using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
- {
- admin.DropDatabase();
- }
- }
-
- [Fact]
- public void Get_Last_Error_Returns()
- {
- using(var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var le = mongo.Database.LastError();
- Assert.Equal(true,le.WasSuccessful);
- }
- }
-
- [Fact]
- public void CreateCollectionCreatesACappedCollection()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- Assert.Equal(true, mongo.Database.CreateCollection(new CreateCollectionOptions("capped") { Capped = true, Size = 10000, Max = 3 }));
- var collection = mongo.GetCollection<FakeObject>("capped");
- collection.Insert(new FakeObject());
- collection.Insert(new FakeObject());
- collection.Insert(new FakeObject());
- collection.Insert(new FakeObject());
- Assert.Equal(3, collection.Find().Count());
- }
- }
- [Fact]
- public void CreateCollectionThrowsExceptionIfAlreadyExist()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- mongo.Database.CreateCollection(new CreateCollectionOptions("capped"));
- var ex = Assert.Throws<MongoException>(() => mongo.Database.CreateCollection(new CreateCollectionOptions("capped")));
- Assert.Equal("collection already exists", ex.Message);
- }
- }
- [Fact]
- public void CreateCollectionFailsSilentlyWithStrictModeOff()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString("strict=false")))
- {
- mongo.Database.CreateCollection(new CreateCollectionOptions("capped"));
- Assert.Equal(false, mongo.Database.CreateCollection(new CreateCollectionOptions("capped")));
- }
- }
-
-
- [Fact]
- public void GetsAllCollections()
- {
- var expected = new List<string> { "NormTests.temp", "NormTests.temp2" };
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var database = mongo.Database;
- database.CreateCollection(new CreateCollectionOptions("temp"));
- database.CreateCollection(new CreateCollectionOptions("temp2"));
- foreach (var collection in database.GetAllCollections())
- {
- Assert.Contains(collection.Name, expected);
- expected.Remove(collection.Name);
- }
- }
- Assert.Equal(0, expected.Count);
- }
- [Fact]
- public void GetCollectionsReturnsNothingIfEmpty()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- Assert.Equal(0, mongo.Database.GetAllCollections().Count());
- }
- }
-
- [Fact]
- public void DropsACollection()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var database = mongo.Database;
- database.CreateCollection(new CreateCollectionOptions("temp"));
- Assert.Equal(true, database.DropCollection("temp"));
- Assert.Equal(0, mongo.Database.GetAllCollections().Count());
- }
- }
- [Fact]
- public void ThrowsExceptionIfDropCollectionFailsWithStrictModeOn()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var ex = Assert.Throws<MongoException>(() => mongo.Database.DropCollection("temp"));
- Assert.Equal("ns not found", ex.Message);
- }
- }
- [Fact]
- public void DropCollectionFailsSilentlyWithStrictModeOff()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString("&strict=false")))
- {
- Assert.Equal(false, mongo.Database.DropCollection("temp"));
- }
- }
-
- [Fact]
- public void ReturnsTheDatabasesName()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- Assert.Equal("NormTests", mongo.Database.DatabaseName);
- }
- }
-
- [Fact]
- public void GetsACollectionsStatistics()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- mongo.Database.CreateCollection(new CreateCollectionOptions("temp"));
- var statistic = mongo.Database.GetCollectionStatistics("temp");
- }
- }
-
- [Fact]
- public void ThrowsExceptionIfGettingStatisticsFailsWithStrictModeOn()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var ex = Assert.Throws<MongoException>(() => mongo.Database.GetCollectionStatistics("temp"));
- Assert.Equal("ns not found", ex.Message);
- }
- }
- [Fact]
- public void GettingStatisticsFailsSilentlyWithStrictModeOff()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString("&strict=false")))
- {
- Assert.Equal(null, mongo.Database.GetCollectionStatistics("temp"));
- }
- }
-
- [Fact]
- public void SetProfilingLevel()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var response = mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.AllOperations);
- Assert.True((response.PreviousLevel == 0.0));
-
- response = mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.ProfilingOff);
- Assert.True((response.PreviousLevel == 2.0));
- }
- }
- [Fact]
- public void GetProfilingInformation()
- {
- //this seems to vary a lot from version to version and who knows what else
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.AllOperations);
- mongo.GetCollection<FakeObject>().Insert(new FakeObject());
- mongo.GetCollection<FakeObject>().Find();
- mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.ProfilingOff);
-
- var results = mongo.Database.GetProfilingInformation();
- var resultsInfos = results.Select(r => r.Info).ToArray();
- Assert.True(results.Any());
- Assert.True(results.All(y => y.Info != null));
- }
- }
-
- [Fact]
- public void ValidateCollection()
- {
- using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
- {
- var collection = mongo.Database.GetCollection<FakeObject>("validCollection");
- collection.Insert(new FakeObject());
- var response = mongo.Database.ValidateCollection("validCollection", false);
- Assert.Equal(collection.FullyQualifiedName, response.Ns);
- }
- }
- }
+using System.Collections.Generic;
+using System.Linq;
+using Xunit;
+using Norm.Collections;
+
+namespace Norm.Tests
+{
+
+ public class MongoDatabaseTest
+ {
+ public MongoDatabaseTest()
+ {
+ using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
+ {
+ admin.DropDatabase();
+ }
+ }
+
+ [Fact]
+ public void Get_Last_Error_Returns()
+ {
+ using(var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var le = mongo.Database.LastError();
+ Assert.Equal(true,le.WasSuccessful);
+ }
+ }
+
+ [Fact]
+ public void CreateCollectionCreatesACappedCollection()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ Assert.Equal(true, mongo.Database.CreateCollection(new CreateCollectionOptions("capped") { Capped = true, Size = 10000, Max = 3 }));
+ var collection = mongo.GetCollection<FakeObject>("capped");
+ collection.Insert(new FakeObject());
+ collection.Insert(new FakeObject());
+ collection.Insert(new FakeObject());
+ collection.Insert(new FakeObject());
+ Assert.Equal(3, collection.Find().Count());
+ }
+ }
+ [Fact]
+ public void CreateCollectionThrowsExceptionIfAlreadyExist()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ mongo.Database.CreateCollection(new CreateCollectionOptions("capped"));
+ var ex = Assert.Throws<MongoException>(() => mongo.Database.CreateCollection(new CreateCollectionOptions("capped")));
+ Assert.Equal("collection already exists", ex.Message);
+ }
+ }
+ [Fact]
+ public void CreateCollectionFailsSilentlyWithStrictModeOff()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString("strict=false")))
+ {
+ mongo.Database.CreateCollection(new CreateCollectionOptions("capped"));
+ Assert.Equal(false, mongo.Database.CreateCollection(new CreateCollectionOptions("capped")));
+ }
+ }
+
+
+ [Fact]
+ public void GetsAllCollections()
+ {
+ var expected = new List<string> { "NormTests.temp", "NormTests.temp2" };
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var database = mongo.Database;
+ database.CreateCollection(new CreateCollectionOptions("temp"));
+ database.CreateCollection(new CreateCollectionOptions("temp2"));
+ foreach (var collection in database.GetAllCollections())
+ {
+ Assert.Contains(collection.Name, expected);
+ expected.Remove(collection.Name);
+ }
+ }
+ Assert.Equal(0, expected.Count);
+ }
+ [Fact]
+ public void GetCollectionsReturnsNothingIfEmpty()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ Assert.Equal(0, mongo.Database.GetAllCollections().Count());
+ }
+ }
+
+ [Fact]
+ public void DropsACollection()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var database = mongo.Database;
+ database.CreateCollection(new CreateCollectionOptions("temp"));
+ Assert.Equal(true, database.DropCollection("temp"));
+ Assert.Equal(0, mongo.Database.GetAllCollections().Count());
+ }
+ }
+ [Fact]
+ public void ThrowsExceptionIfDropCollectionFailsWithStrictModeOn()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var ex = Assert.Throws<MongoException>(() => mongo.Database.DropCollection("temp"));
+ Assert.Equal("ns not found", ex.Message);
+ }
+ }
+ [Fact]
+ public void DropCollectionFailsSilentlyWithStrictModeOff()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString("&strict=false")))
+ {
+ Assert.Equal(false, mongo.Database.DropCollection("temp"));
+ }
+ }
+
+ [Fact]
+ public void ReturnsTheDatabasesName()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ Assert.Equal("NormTests", mongo.Database.DatabaseName);
+ }
+ }
+
+ [Fact]
+ public void GetsACollectionsStatistics()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ mongo.Database.CreateCollection(new CreateCollectionOptions("temp"));
+ var statistic = mongo.Database.GetCollectionStatistics("temp");
+ }
+ }
+
+ [Fact]
+ public void ThrowsExceptionIfGettingStatisticsFailsWithStrictModeOn()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var ex = Assert.Throws<MongoException>(() => mongo.Database.GetCollectionStatistics("temp"));
+ Assert.Equal("ns not found", ex.Message);
+ }
+ }
+ [Fact]
+ public void GettingStatisticsFailsSilentlyWithStrictModeOff()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString("&strict=false")))
+ {
+ Assert.Equal(null, mongo.Database.GetCollectionStatistics("temp"));
+ }
+ }
+
+ [Fact]
+ public void SetProfilingLevel()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var response = mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.AllOperations);
+ Assert.True((response.PreviousLevel == 0.0));
+
+ response = mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.ProfilingOff);
+ Assert.True((response.PreviousLevel == 2.0));
+ }
+ }
+ [Fact]
+ public void GetProfilingInformation()
+ {
+ //this seems to vary a lot from version to version and who knows what else
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.AllOperations);
+ mongo.GetCollection<FakeObject>().Insert(new FakeObject());
+ mongo.GetCollection<FakeObject>().Find();
+ mongo.Database.SetProfileLevel(Protocol.SystemMessages.ProfileLevel.ProfilingOff);
+
+ var results = mongo.Database.GetProfilingInformation();
+ var resultsInfos = results.Select(r => r.Info).ToArray();
+ Assert.True(results.Any());
+ Assert.True(results.All(y => y.Info != null));
+ }
+ }
+
+ [Fact]
+ public void ValidateCollection()
+ {
+ using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
+ {
+ var collection = mongo.Database.GetCollection<FakeObject>("validCollection");
+ collection.Insert(new FakeObject());
+ var response = mongo.Database.ValidateCollection("validCollection", false);
+ Assert.Equal(collection.FullyQualifiedName, response.Ns);
+ }
+ }
+ }
}
View
278 NoRM.Tests/NoRM.Tests.csproj
@@ -1,146 +1,146 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <PropertyGroup>
- <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
- <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>9.0.30729</ProductVersion>
- <SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>{505E617D-1F40-40AF-BDF0-730EFBC5BA95}</ProjectGuid>
- <OutputType>Library</OutputType>
- <AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>NoRM.Tests</RootNamespace>