Skip to content

Instantly share code, notes, and snippets.

@emiaj
Created June 17, 2011 14:32
Show Gist options
  • Select an option

  • Save emiaj/1031528 to your computer and use it in GitHub Desktop.

Select an option

Save emiaj/1031528 to your computer and use it in GitHub Desktop.
[TestFixture]
public class MongoPersistenceTester
{
[Test]
public void smoke()
{
const string connectionString = "mongodb://localhost";
MongoServer server = null;
MongoDatabase database = null;
Assert.IsTrue(Process.GetProcessesByName("mongod").Length >= 1, "mongod service is not running");
try
{
server = MongoServer.Create(connectionString);
database = server.GetDatabase("test");
BsonClassMap.RegisterClassMap<Book>(x =>
{
x.AutoMap();
x.SetIgnoreExtraElements(true);
});
BsonClassMap.RegisterClassMap<Restriction>(x =>
{
x.AutoMap();
x.SetIgnoreExtraElements(true);
});
var books = database.GetCollection<Book>("Books");
var collection = (MongoCollection) books;
var query = Query.EQ("Author", "Jaime Febres");
// Inserting
{
var book = new Book { Author = "Jaime Febres", Name = "ZG In Deep" };
collection.Insert(book);
}
// Appending metadata
{
var builder = new UpdateBuilder();
builder.Set("Published", true);
collection.Update(query, builder);
}
// Fetching using generic collection
{
var book = books.FindOne(query);
Assert.NotNull(book);
Assert.AreEqual("Jaime Febres", book.Author);
}
// Fetching using regular collection
{
var book = collection.FindOneAs<Book>(query);
Assert.AreEqual("Jaime Febres", book.Author);
}
// Fetching Restriction
{
var book = collection.FindOneAs<Book>(query);
var restriction = collection.FindOneAs<Restriction>(query); // we would use the entity id here
Assert.NotNull(restriction);
Assert.AreEqual(book.Id, restriction.Id);
Assert.True(restriction.Published);
}
}
finally
{
if (database != null)
{
//database.Drop();
}
if (server != null)
{
server.Disconnect();
}
}
}
}
public class Book
{
public Guid Id { get; protected set; }
public string Name { get; set; }
public string Author { get; set; }
}
public class Restriction
{
public object Id { get; set; }
public bool Published { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment