예제 #1
0
        public void MongoRepository_Collection_Name_Attribute()
        {
            const string connectionString = "mongodb://127.0.0.1/test";

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

            var cli = new MongoClient(connectionString);

            cli.DropDatabase("test");

            var repo = new MongoDbRepository <Order, string>(connectionString);

            // Create
            var create = new Order {
                Name = "Big sale"
            };

            repo.Add(create);

            var repoExt = new MongoDbRepository <OrderExtension, string>(connectionString);

            // Create
            var create2 = new OrderExtension {
                Name = "Big sale"
            };

            repoExt.Add(create2);

            Assert.IsTrue(cli.GetDatabase("test").ListCollections().ToList().Any(x => x["name"] == "Order"));
            Assert.IsTrue(cli.GetDatabase("test").ListCollections().ToList().Any(x => x["name"] == "OrderCustomName"));
            Assert.IsFalse(cli.GetDatabase("test").ListCollections().ToList().Any(x => x["name"] == "OrderExtended"));
        }
예제 #2
0
        public void MongoRepository_Generate_Query_With_AllowDiskSave()
        {
            const string connectionString = "mongodb://127.0.0.1/test";

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

            var repo = new MongoDbRepository <Order, string>(connectionString);

            // Create
            var create = new Order {
                Name = "Big sale"
            };

            repo.Add(create);

            var fetchStrategy = new MongoDbFetchStrategy <Order>
            {
                AllowDiskUse = true
            };

            // Read
            var read = repo.Get(create.OrderId, fetchStrategy);

            read.Name.ShouldBe(create.Name);

            var all  = repo.GetAll(fetchStrategy);
            var all2 = repo.AsQueryable(fetchStrategy);
        }
예제 #3
0
        public void MongoServer_Is_Running()
        {
            string connectionString = MongoDbConnectionStringFactory.Build("Test");

            if (MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                MongoDbRepositoryManager.DropDatabase(connectionString);
                Assert.Pass("MongoServer is running");
            }
            else
            {
                AssertIgnores.MongoServerIsNotRunning();
            }
        }
예제 #4
0
        public void MongoRepository_Supports_Basic_Crud_Operations()
        {
            const string connectionString = "mongodb://127.0.0.1/test";

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

            var repo = new MongoDbRepository <Order, string>(connectionString);

            // Create
            var create = new Order {
                Name = "Big sale"
            };

            repo.Add(create);

            // Read
            var read = repo.Get(create.OrderId);

            read.Name.ShouldBe(create.Name);

            // Update
            read.Name = "Really big sale";
            repo.Update(read);

            var all = repo.GetAll();

            var update = repo.Get(read.OrderId);

            update.OrderId.ShouldBe(read.OrderId);
            update.Name.ShouldBe(read.Name);

            // Delete
            repo.Delete(update);
            var delete = repo.Get(read.OrderId);

            delete.ShouldBeNull();
        }
예제 #5
0
        public void MongoDb_Supports_Basic_Crud_Operations()
        {
            const string connectionString = "mongodb://localhost/Order";

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

            var cli           = new MongoClient(connectionString);
            var databaseNames = cli.ListDatabases().ToList();

            foreach (var db in databaseNames)
            {
                if (db["name"].AsString != "admin")
                {
                    cli.DropDatabase(db["name"].AsString);
                }
            }

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

            Console.WriteLine("* CREATE *");

            var create = new Order {
                Name = "Big sale"
            };

            database.GetCollection <Order>("Order").InsertOne(create);

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

            Console.WriteLine("* READ *");

            var filter = Builders <Order> .Filter.Eq(o => o.OrderId, create.OrderId);

            var read = orders.Find(filter).ToList().FirstOrDefault();

            read.Name.ShouldBe(create.Name);

            Console.WriteLine("* UPDATE *");

            var update = Builders <Order> .Update.Set(o => o.Name, "Really big sale");

            orders.UpdateOne(filter, update);

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

            var read_updated = orders.Find(filter).ToList().FirstOrDefault();

            read_updated.OrderId.ShouldBe(read.OrderId);
            read_updated.Name.ShouldBe("Really big sale");

            Console.WriteLine("* DELETE *");

            orders.DeleteOne(filter);

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

            orders.Count(filter).ShouldBe(0);

            Console.WriteLine("* DELETE ALL *");
            orders.DeleteMany(new BsonDocument());

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

            orders.AsQueryable().Count().ShouldBe(0);

            cli.DropDatabase("Order");
        }
        public static IEnumerable <TestCaseData> Build(RepositoryType[] includeType)
        {
            if (includeType.Contains(RepositoryType.InMemory))
            {
                yield return(new TestCaseData(new InMemoryRepository <Contact, string>()).SetName("InMemoryRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Xml))
            {
                var xmlDataDirectoryPath = XmlDataDirectoryFactory.Build("Contact");
                yield return
                    (new TestCaseData(new XmlRepository <Contact, string>(xmlDataDirectoryPath)).SetName("XmlRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Ef5) || includeType.Contains(RepositoryType.Ef))
            {
                var dbPath = EfDataDirectoryFactory.Build();
                Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
                yield return
                    (new TestCaseData(new EfRepository <Contact, string>(new TestObjectEntities("Data Source=" + dbPath))).SetName("EfRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Dbo4))
            {
                var dbPath = Db4oDataDirectoryFactory.Build("Contact");
                yield return(new TestCaseData(new Db4oRepository <Contact, string>(dbPath)).SetName("Db4oRepository Test"));
            }

            if (includeType.Contains(RepositoryType.MongoDb))
            {
                string connectionString = MongoDbConnectionStringFactory.Build("Contact");

                if (MongoDbRepositoryManager.ServerIsRunning(connectionString))
                {
                    MongoDbRepositoryManager.DropDatabase(connectionString); // Pre-test cleanup
                    yield return(new TestCaseData(new MongoDbRepository <Contact, string>(connectionString)).SetName("MongoDb Test"));
                }
            }

            if (includeType.Contains(RepositoryType.RavenDb))
            {
                var documentStore = new EmbeddableDocumentStore
                {
                    RunInMemory = true,
                    Conventions = { DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites }
                };
                yield return(new TestCaseData(new RavenDbRepository <Contact, string>(documentStore)).SetName("RavenDbRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Cache))
            {
                yield return(new TestCaseData(new CacheRepository <Contact, string>(CachePrefixFactory.Build())).SetName("CacheRepository Test"));
            }

            if (includeType.Contains(RepositoryType.CouchDb))
            {
                if (CouchDbRepositoryManager.ServerIsRunning(CouchDbUrl.Host, CouchDbUrl.Port))
                {
                    var databaseName = CouchDbDatabaseNameFactory.Build("Contact");
                    CouchDbRepositoryManager.DropDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName);
                    CouchDbRepositoryManager.CreateDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName);

                    yield return(new TestCaseData(new CouchDbRepository <Contact>(CouchDbUrl.Host, CouchDbUrl.Port, databaseName)).SetName("CouchDbRepository Test"));
                }
            }
        }
        public static IEnumerable <TestCaseData> Build(RepositoryType[] includeType)
        {
            if (includeType.Contains(RepositoryType.InMemory))
            {
                yield return(new TestCaseData(new InMemoryRepository <Contact, string>()).SetName("InMemoryRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Xml))
            {
                var xmlDataDirectoryPath = XmlDataDirectoryFactory.Build("Contact");
                yield return
                    (new TestCaseData(new XmlRepository <Contact, string>(xmlDataDirectoryPath)).SetName("XmlRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Ef))
            {
                var dbPath = EfDataDirectoryFactory.Build();
                yield return
                    (new TestCaseData(new EfRepository <Contact, string>(new TestObjectEntities("Data Source=" + dbPath))).SetName("EfRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Dbo4))
            {
                var dbPath = Db4oDataDirectoryFactory.Build("Contact");
                yield return(new TestCaseData(new Db4oRepository <Contact, string>(dbPath)).SetName("Db4oRepository Test"));
            }

            if (includeType.Contains(RepositoryType.MongoDb))
            {
                string connectionString = MongoDbConnectionStringFactory.Build("Contact");

                if (MongoDbRepositoryManager.ServerIsRunning(connectionString))
                {
                    MongoDbRepositoryManager.DropDatabase(connectionString); // Pre-test cleanup
                    yield return(new TestCaseData(new MongoDbRepository <Contact, string>(connectionString)).SetName("MongoDb Test"));
                }
            }

            if (includeType.Contains(RepositoryType.RavenDb))
            {
                var documentStore = new EmbeddableDocumentStore
                {
                    RunInMemory   = true,
                    DataDirectory = "~\\Data\\RavenDb"
                };
                if (IntPtr.Size == 4)
                {
                    documentStore.Configuration.Storage.Voron.AllowOn32Bits = true;
                }

                yield return(new TestCaseData(new RavenDbRepository <Contact, string>(documentStore)).SetName("RavenDbRepository Test"));
            }

            if (includeType.Contains(RepositoryType.Cache))
            {
                yield return(new TestCaseData(new CacheRepository <Contact, string>(CachePrefixFactory.Build())).SetName("CacheRepository Test"));
            }

            if (includeType.Contains(RepositoryType.CouchDb))
            {
                if (CouchDbRepositoryManager.ServerIsRunning(CouchDbUrl.Host, CouchDbUrl.Port))
                {
                    var databaseName = CouchDbDatabaseNameFactory.Build("Contact");
                    CouchDbRepositoryManager.DropDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName);
                    CouchDbRepositoryManager.CreateDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName);

                    yield return(new TestCaseData(new CouchDbRepository <Contact>(CouchDbUrl.Host, CouchDbUrl.Port, databaseName)).SetName("CouchDbRepository Test"));
                }
            }
        }
예제 #8
0
        public static IEnumerable <TestCaseData> Build(RepositoryType[] includeType, string testName = "Test")
        {
            if (includeType.Contains(RepositoryType.InMemory))
            {
                yield return(new TestCaseData(new InMemoryRepository <Contact, string>()).SetName("InMemoryRepository " + testName));
            }

            if (includeType.Contains(RepositoryType.Xml))
            {
                var xmlDataDirectoryPath = XmlDataDirectoryFactory.Build("Contact");
                yield return
                    (new TestCaseData(new XmlRepository <Contact, string>(xmlDataDirectoryPath)).SetName("XmlRepository" + testName));
            }

            if (includeType.Contains(RepositoryType.Ef))
            {
                var dbPath = EfDataDirectoryFactory.Build();
                yield return
                    (new TestCaseData(new EfRepository <Contact, string>(new TestObjectContext("Data Source=" + dbPath))).SetName("EfRepository" + testName));
            }

            if (includeType.Contains(RepositoryType.EfCore))
            {
                var connection = new SqliteConnection("DataSource=:memory:");
                connection.Open();

                var options = new DbContextOptionsBuilder <TestObjectContextCore>()
                              .UseSqlite(connection)
                              .Options;

                // Create the schema in the database
                var context = new TestObjectContextCore(options);
                context.Database.EnsureCreated();
                yield return(new TestCaseData(new EfCoreRepository <Contact, string>(context)).SetName("EfCoreRepository " + testName));
            }

            if (includeType.Contains(RepositoryType.Dbo4))
            {
                var dbPath = Db4oDataDirectoryFactory.Build("Contact");
                yield return(new TestCaseData(new Db4oRepository <Contact, string>(dbPath)).SetName("Db4oRepository " + testName));
            }

            if (includeType.Contains(RepositoryType.MongoDb))
            {
                string connectionString = MongoDbConnectionStringFactory.Build("Contact");

                if (MongoDbRepositoryManager.ServerIsRunning(connectionString))
                {
                    MongoDbRepositoryManager.DropDatabase(connectionString); // Pre-test cleanup
                    yield return(new TestCaseData(new MongoDbRepository <Contact, string>(connectionString)).SetName("MongoDb " + testName));
                }
            }

            if (includeType.Contains(RepositoryType.RavenDb))
            {
                var documentStore = new EmbeddableDocumentStore
                {
                    RunInMemory   = true,
                    DataDirectory = "~\\Data\\RavenDb"
                };
                if (IntPtr.Size == 4)
                {
                    documentStore.Configuration.Storage.Voron.AllowOn32Bits = true;
                }

                IDocumentStore x = new EmbeddableDocumentStore();
                yield return(new TestCaseData(new RavenDbRepository <Contact, string>(documentStore: documentStore)).SetName("RavenDbRepository " + testName));
            }

            if (includeType.Contains(RepositoryType.Cache))
            {
                var cachingProvider = new InMemoryCachingProvider(new MemoryCache(new MemoryCacheOptions()));
                yield return(new TestCaseData(new CacheRepository <Contact, string>(CachePrefixFactory.Build(), cachingProvider)).SetName("CacheRepository " + testName));
            }

            if (includeType.Contains(RepositoryType.CouchDb))
            {
                if (CouchDbRepositoryManager.ServerIsRunning(CouchDbUrl.Host, CouchDbUrl.Port))
                {
                    var databaseName = CouchDbDatabaseNameFactory.Build("Contact");
                    CouchDbRepositoryManager.DropDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName);
                    CouchDbRepositoryManager.CreateDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName);

                    yield return(new TestCaseData(new CouchDbRepository <Contact, string>(CouchDbUrl.Host, CouchDbUrl.Port, databaseName)).SetName("CouchDbRepository " + testName));
                }
            }
        }
        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");
        }