예제 #1
0
        public static void Clean()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString;
            var server = new MongoClient(connectionString).GetServer();

            server.DropDatabase(GetDatabaseName(connectionString));
        }
예제 #2
0
        public void TestCleanup()
        {
            var repositoryType = Instance.GetType();

            if (repositoryType == typeof(JsonRepository))
            {
                if (Directory.Exists(JsonWorkingFolder))
                {
                    Directory.Delete(JsonWorkingFolder, recursive: true);
                }
            }
            else if (repositoryType == typeof(SqlRepository))
            {
                using (var cnn = new SqlConnection(SqlConnectionString))
                using (var cmd = new SqlCommand("EXEC sp_MSforeachtable @command1 = 'TRUNCATE TABLE ?'", cnn))
                {
                    cnn.Open();
                    cnn.ChangeDatabase(SqlDatabaseName);
                    cmd.ExecuteNonQuery();
                }
            }
            else if (repositoryType == typeof(MongoRepository))
            {
                var server = new MongoClient(MongoConnectionString).GetServer();
                server.DropDatabase(MongoDatabaseName);
            }
        }
예제 #3
0
 public static Database Create(int protocolVersion, string connectionString)
 {
     var databaseName = GetDatabaseName(connectionString);
     var server = new MongoClient(connectionString).GetServer();
     server.DropDatabase(databaseName);
     var database = new Database();
     database.mongoDatabase = server.GetDatabase(databaseName);
     database.protocolVersion = protocolVersion;
     return database;
 }
        public void DropDatabase_should_invoke_the_correct_operation(
            [Values(false, true)] bool async)
        {
            var operationExecutor = new MockOperationExecutor();
            var client = new MongoClient(operationExecutor);

            if (async)
            {
                client.DropDatabaseAsync("awesome").GetAwaiter().GetResult();
            }
            else
            {
                client.DropDatabase("awesome");
            }

            var call = operationExecutor.GetWriteCall<BsonDocument>();

            call.Operation.Should().BeOfType<DropDatabaseOperation>();
            ((DropDatabaseOperation)call.Operation).DatabaseNamespace.Should().Be(new DatabaseNamespace("awesome"));
        }
        public void DropDatabase_should_invoke_the_correct_operation(
            [Values(false, true)] bool async)
        {
            var operationExecutor = new MockOperationExecutor();
            var writeConcern = new WriteConcern(1);
            var client = new MongoClient(operationExecutor, new MongoClientSettings()).WithWriteConcern(writeConcern);

            if (async)
            {
                client.DropDatabaseAsync("awesome").GetAwaiter().GetResult();
            }
            else
            {
                client.DropDatabase("awesome");
            }

            var call = operationExecutor.GetWriteCall<BsonDocument>();

            var dropDatabaseOperation = call.Operation.Should().BeOfType<DropDatabaseOperation>().Subject;
            dropDatabaseOperation.DatabaseNamespace.Should().Be(new DatabaseNamespace("awesome"));
            dropDatabaseOperation.WriteConcern.Should().BeSameAs(writeConcern);
        }
예제 #6
0
 private void DropDatabase()
 {
     var server = new MongoClient(_connectionString).GetServer();
     server.DropDatabase(_databaseName);
 }
예제 #7
0
 private static MongoDatabase GetDatabase(bool clear)
 {
     var connectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString;
     var databaseName = GetDatabaseName(connectionString);
     var server = new MongoClient(connectionString).GetServer();
     if (clear)
         server.DropDatabase(databaseName);
     return server.GetDatabase(databaseName);
 }
        public void MongoDb_Supports_Basic_Crud_Operations()
        {
            const string connectionString = "mongodb://localhost";

            if (!MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                AssertIgnores.MongoServerIsNotRunning();
            }

            var server = new MongoClient(connectionString).GetServer();
            var databaseNames = server.GetDatabaseNames();
            foreach (var db in databaseNames)
            {
                server.DropDatabase(db);
            }

            var database = server.GetDatabase("Order");
            var orders = database.GetCollection<Order>("Order");

            Console.WriteLine("* CREATE *");

            var create = new Order { Name = "Big sale" };
            database.GetCollection<Order>("Order").Insert(create);

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            Console.WriteLine("* READ *");

            var read = orders.AsQueryable().FirstOrDefault(e => e.OrderId == create.OrderId);
            read.Name.ShouldEqual(create.Name);

            Console.WriteLine("* UPDATE *");

            read.Name = "Really big sale";
            database.GetCollection<Order>("Order").Save(read);

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            var update = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == read.OrderId);
            update.OrderId.ShouldEqual(read.OrderId);
            update.Name.ShouldEqual(read.Name);

            Console.WriteLine("* DELETE *");

            var delete = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == update.OrderId);

            database.GetCollection<Order>("Order").Remove(Query.EQ("OrderId", delete.OrderId));

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            database.GetCollection<Order>("Order").RemoveAll();

            Console.WriteLine("* DELETE ALL *");

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            server.DropDatabase("Order");
        }
 public static void DropDatabase(string connectionString)
 {
     var server = new MongoClient(connectionString).GetServer();
     var db = DatabaseName(connectionString);
     server.DropDatabase(db);
 }