Skip to content

Commit

Permalink
Fixed formatting of CSHARP-199 fix. Standardized unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Jun 10, 2011
1 parent a8f890e commit 14b2c0a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 82 deletions.
45 changes: 19 additions & 26 deletions Driver/Builders/UpdateBuilder.cs
Expand Up @@ -400,7 +400,6 @@ T value
string oldElementName,
string newElementName
) {
// return new UpdateDocument("$rename", new BsonDocument(oldElementName, newElementName));
return new UpdateBuilder().Rename(oldElementName, newElementName);
}

Expand Down Expand Up @@ -942,6 +941,25 @@ T value
return this;
}

/// <summary>
/// Adds a $rename update modifider.
/// </summary>
/// <param name="oldElementName">The old element name.</param>
/// <param name="newElementName">The new element name.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder Rename(
string oldElementName,
string newElementName
) {
BsonElement element;
if (document.TryGetElement("$rename", out element)) {
element.Value.AsBsonDocument.Add(oldElementName, newElementName);
} else {
document.Add("$rename", new BsonDocument(oldElementName, newElementName));
}
return this;
}

/// <summary>
/// Adds a $set update modifier.
/// </summary>
Expand Down Expand Up @@ -1005,31 +1023,6 @@ string name
}
return this;
}


/// <summary>
/// Adds a $rename update modifider.
/// </summary>
/// <param name="oldElementName">The name of the element to be renamed.</param>
/// <param name="newElementName">The new name of the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder Rename(
string oldElementName,
string newElementName
)
{
BsonElement element;
if (document.TryGetElement("$rename", out element))
{
element.Value.AsBsonDocument.Add(oldElementName, newElementName);
}
else
{
document.Add("$rename", new BsonDocument(oldElementName, newElementName));
}
return this;
}

#endregion

#region protected methods
Expand Down
91 changes: 35 additions & 56 deletions DriverOnlineTests/Jira/CSharp199Tests.cs
Expand Up @@ -25,88 +25,67 @@
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace MongoDB.DriverOnlineTests.Jira.CSharp199
{
namespace MongoDB.DriverOnlineTests.Jira.CSharp199 {
[TestFixture]
public class CSharp199Tests
{

public class CSharp199Tests {
[Test]
public void TestSingleRename()
{
public void TestSingleRename() {
var server = MongoServer.Create("mongodb://localhost/?safe=true");
var database = server["onlinetests"];
var collection = database.GetCollection("CSharp199");
collection.RemoveAll();

var testDoc = new BsonDocument
{
{ "a", 1 }
};
collection.RemoveAll();
collection.Insert(new BsonDocument { { "_id", 1 }, { "a", 2 } });

var result = collection.Insert(testDoc);
var renameDoc = Update.Rename("a", "b");
var expectedDoc = new BsonDocument {
{ "$rename" , new BsonDocument { { "a", "b"} }}
};
Assert.AreEqual(expectedDoc.ToJson(), renameDoc.ToJson());
var query = Query.EQ("_id", 1);
var update = Update.Rename("a", "x");
collection.Update(query, update);
var document = collection.FindOne();

var query = Query.EQ("a", 1);
collection.Update(query, renameDoc);
var expectedUpdate = "{ '$rename' : { 'a' : 'x' } }".Replace("'", "\"");
Assert.AreEqual(expectedUpdate, update.ToJson());
var expectedDocument = "{ '_id' : 1, 'x' : 2 }".Replace("'", "\"");
Assert.AreEqual(expectedDocument, document.ToJson());
}

[Test]
public void TestMultipleRenames()
{
public void TestMultipleRenames() {
var server = MongoServer.Create("mongodb://localhost/?safe=true");
var database = server["onlinetests"];
var collection = database.GetCollection("CSharp199");
collection.RemoveAll();

var testDoc = new BsonDocument
{
{ "a", 1 },
{ "b", 2}
};
collection.RemoveAll();
collection.Insert(new BsonDocument { { "_id", 1 }, { "a", 2 }, { "b", 3 } });

var result = collection.Insert(testDoc);
var renameDoc = Update.Rename("a", "x").Rename("b", "y");
var expectedDoc = new BsonDocument {
{ "$rename" , new BsonDocument {
{ "a", "x" },
{ "b", "y" }}}
};
Assert.AreEqual(expectedDoc.ToJson(), renameDoc.ToJson());
var query = Query.EQ("_id", 1);
var update = Update.Rename("a", "x").Rename("b", "y");
collection.Update(query, update);
var document = collection.FindOne();

var query = Query.EQ("a", 1);
collection.Update(query, renameDoc);
var expectedUpdate = "{ '$rename' : { 'a' : 'x', 'b' : 'y' } }".Replace("'", "\"");
Assert.AreEqual(expectedUpdate, update.ToJson());
var expectedDocument = "{ '_id' : 1, 'x' : 2, 'y' : 3 }".Replace("'", "\"");
Assert.AreEqual(expectedDocument, document.ToJson());
}

[Test]
public void TestRenameWithSet()
{
public void TestRenameWithSet() {
var server = MongoServer.Create("mongodb://localhost/?safe=true");
var database = server["onlinetests"];
var collection = database.GetCollection("CSharp199");
collection.RemoveAll();

var testDoc = new BsonDocument
{
{ "a", 1 },
{ "x", 1 }
};
collection.RemoveAll();
collection.Insert(new BsonDocument { { "_id", 1}, { "a", 2 }, { "b", 3 } });

var result = collection.Insert(testDoc);
var renameDoc = Update.Rename("a", "b").Set("x", 2);
var expectedDoc = new BsonDocument {
{ "$rename" , new BsonDocument { {"a", "b"} }},
{ "$set", new BsonDocument { {"x", 2} }}
};
Assert.AreEqual(expectedDoc.ToJson(), renameDoc.ToJson());
var query = Query.EQ("_id", 1);
var update = Update.Rename("a", "x").Set("b", 4);
collection.Update(query, update);
var document = collection.FindOne();

var query = Query.EQ("a", 1);
collection.Update(query, renameDoc);
var expectedUpdate = "{ '$rename' : { 'a' : 'x' }, '$set' : { 'b' : 4 } }".Replace("'", "\"");
Assert.AreEqual(expectedUpdate, update.ToJson());
var expectedDocument = "{ '_id' : 1, 'b' : 4, 'x' : 2 }".Replace("'", "\""); // server rearranges elements
Assert.AreEqual(expectedDocument, document.ToJson());
}

}
}

0 comments on commit 14b2c0a

Please sign in to comment.