Permalink
Browse files

Add two test for FindAndModify and fix case where operates was wrappe…

…d in $set operator.
  • Loading branch information...
1 parent f6e51ad commit 3efbbe24d3034236fe742ca8d64c4d42f015048c @lanwin lanwin committed Mar 10, 2011
Showing with 43 additions and 7 deletions.
  1. +39 −0 source/MongoDB.Tests/IntegrationTests/TestCollection_1.cs
  2. +4 −7 source/MongoDB/MongoCollection_1.cs
@@ -408,5 +408,44 @@ private class DeletesEntity
Assert.IsNotNull(result);
Assert.AreEqual(result["Existing"], 2.0);
}
+
+ public class Sequence
+ {
+ public string Name { get; set; }
+ public int Value { get; set; }
+ }
+
+ [Test]
+ public void FindAndModifyWithoutOperatorWorks()
+ {
+ var collection = DB.GetCollection<Sequence>();
+
+ collection.Remove(new Document());
+ collection.Insert(new Sequence { Name = "test", Value = 1 });
+
+ var spec = new Document().Add("Name", "test");
+ var update = new Document("Value", 10);
+
+ var document = collection.FindAndModify(update, spec, true);
+
+ Assert.AreEqual("test", document.Name);
+ Assert.AreEqual(10, document.Value);
+ }
+
+ [Test]
+ public void FindAndModifyIncOperatorWorks()
+ {
+ var collection = DB.GetCollection<Sequence>();
+
+ collection.Remove(new Document());
+ collection.Insert(new Sequence { Name = "test", Value = 1 });
+
+ var spec = new Document().Add("Name", "test");
+ var update = Mo.Inc("Value", 1);
+
+ var document = collection.FindAndModify(update, spec, true);
+
+ Assert.AreEqual(2, document.Value);
+ }
}
}
@@ -610,16 +610,13 @@ private object EnsureUpdateDocument(object document)
{
var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(typeof(T));
- var foundOp = descriptor.GetMongoPropertyNames(document)
- .Any(name => name.IndexOf('$') == 0);
+ var foundOp = document is Document && ( (Document)document ).Keys.Any(k => k.Contains("$"));
if(foundOp == false)
- {
- //wrap document in a $set.
- return new Document().Add("$set", document);
- }
+ foundOp = descriptor.GetMongoPropertyNames(document)
+ .Any(name => name.Contains('$'));
- return document;
+ return foundOp == false ? new Document().Add("$set", document) : document;
}
}
}

0 comments on commit 3efbbe2

Please sign in to comment.