Skip to content

Commit

Permalink
MongoConfiguration.Default is generated once per App an reads configu…
Browse files Browse the repository at this point in the history
…ration search if present.
  • Loading branch information
lanwin committed May 25, 2010
1 parent d5675b6 commit faecc04
Showing 1 changed file with 29 additions and 43 deletions.
72 changes: 29 additions & 43 deletions source/MongoDB.Tests/MongoTestBase.cs
Expand Up @@ -6,75 +6,61 @@ namespace MongoDB
{
public abstract class MongoTestBase
{
public Mongo Mongo { get; set; }
public Mongo Mongo{get;set;}

/// <summary>
/// Gets the tests database.
/// </summary>
/// <value>The tests database.</value>
public IMongoDatabase TestsDatabase
{
get { return Mongo["tests"]; }
public IMongoDatabase DB{
get{
return this.Mongo["tests"];
}
}

/// <summary>
/// Gets or sets the connection string.
/// Gets or sets the connection string.
/// </summary>
/// <value>The connection string.</value>
public string ConnectionString { get; private set; }

/// <summary>
/// Comma separated list of collections to clean at startup.
/// Comma separated list of collections to clean at startup.
/// </summary>
public abstract string TestCollections { get; }

public abstract string TestCollections{get;}
/// <summary>
/// Override to add custom initialization code.
/// Override to add custom initialization code.
/// </summary>
public virtual void OnInit()
{
}

public virtual void OnInit(){}

/// <summary>
/// Override to add custom code to invoke during the test end.
/// Override to add custom code to invoke during the test end.
/// </summary>
public virtual void OnDispose()
{
}

public virtual void OnDispose(){}

/// <summary>
/// Sets up the test environment. You can either override this OnInit to add custom initialization.
/// Sets up the test environment. You can either override this OnInit to add custom initialization.
/// </summary>
[TestFixtureSetUp]
public virtual void Init()
{
public virtual void Init(){
ConnectionString = ConfigurationManager.AppSettings["tests"];
if(String.IsNullOrEmpty(ConnectionString))
throw new ArgumentNullException("Connection string not found.");
Mongo = new Mongo(ConnectionString);
Mongo.Connect();
CleanDatabase();
this.Mongo = new Mongo(ConnectionString);
this.Mongo.Connect();
CleanDB();
OnInit();
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>


[TestFixtureTearDown]
public virtual void Dispose()
{
public virtual void Dispose(){
OnDispose();
Mongo.Disconnect();
this.Mongo.Disconnect();
}

/// <summary>
/// Cleans the DB.
/// </summary>
protected void CleanDatabase()
{
foreach(var col in TestCollections.Split(','))
TestsDatabase["$cmd"].FindOne(new Document {{"drop", col.Trim()}});

protected void CleanDB(){
foreach(string col in this.TestCollections.Split(',')){
DB["$cmd"].FindOne(new Document(){{"drop", col.Trim()}});
//Console.WriteLine("Dropping " + col);
}
}
}
}

0 comments on commit faecc04

Please sign in to comment.