@Test public void testCreateCollection() { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); db.createCollection("coll", null); assertEquals(Collections.singleton("coll"), db.getCollectionNames()); }
@Test public void testUndefinedCommand() { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); CommandResult result = db.command("undefined"); assertEquals("ok should always be defined", false, result.get("ok")); assertEquals("undefined command: { \"undefined\" : true}", result.get("err")); }
@Test public void testGetLastError() { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); DBCollection collection = db.getCollection("coll"); collection.insert(new BasicDBObject("_id", 1)); CommandResult error = db.getLastError(); assertTrue(error.ok()); }
@Test public void testGetDb() { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); assertNotNull(db); assertSame("getDB should be idempotent", db, fongo.getDB("db")); assertEquals(Arrays.asList(db), fongo.getUsedDatabases()); assertEquals(Arrays.asList("db"), fongo.getDatabaseNames()); }
@Test public void testGetCollection() { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); DBCollection collection = db.getCollection("coll"); assertNotNull(collection); assertSame("getCollection should be idempotent", collection, db.getCollection("coll")); assertSame( "getCollection should be idempotent", collection, db.getCollectionFromString("coll")); assertEquals(new HashSet<String>(Arrays.asList("coll")), db.getCollectionNames()); }
@Test public void testCountCommand() { DBObject countCmd = new BasicDBObject("count", "coll"); Fongo fongo = newFongo(); DB db = fongo.getDB("db"); DBCollection coll = db.getCollection("coll"); coll.insert(new BasicDBObject()); coll.insert(new BasicDBObject()); CommandResult result = db.command(countCmd); assertEquals("The command should have been succesful", true, result.get("ok")); assertEquals("The count should be in the result", 2L, result.get("n")); }
@Test public void testDropDatabaseFromFongoWithMultipleCollectionsDropsBothCollections() throws Exception { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); DBCollection collection1 = db.getCollection("coll1"); DBCollection collection2 = db.getCollection("coll2"); db.dropDatabase(); assertFalse("Collection 1 shouldn't exist in DB", db.collectionExists(collection1.getName())); assertFalse("Collection 2 shouldn't exist in DB", db.collectionExists(collection2.getName())); assertFalse("DB shouldn't exist in fongo", fongo.getDatabaseNames().contains("db")); }
@Test public void testDropDatabaseFromFongoDropsAllData() throws Exception { Fongo fongo = newFongo(); DBCollection collection = fongo.getDB("db").getCollection("coll"); collection.insert(new BasicDBObject()); fongo.dropDatabase("db"); assertEquals("Collection should have no data", 0, collection.count()); assertFalse( "Collection shouldn't exist in DB", collection.getDB().getCollectionNames().contains(collection.getName())); assertFalse("DB shouldn't exist in fongo", fongo.getDatabaseNames().contains("db")); }
public static void doit() { Fongo fongo = new Fongo("fongo"); for (int i = 0; i < 1; i++) { DB db = fongo.getDB("db"); DBCollection collection = db.getCollection("coll"); for (int k = 0; k < 10000; k++) { collection.insert(new BasicDBObject("_id", k).append("n", new BasicDBObject("a", 1))); collection.findOne(new BasicDBObject("_id", k)); } db.dropDatabase(); } }
@Test public void testDropCollectionsFromGetCollectionNames() { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); db.getCollection("coll1"); db.getCollection("coll2"); int dropCount = 0; for (String name : db.getCollectionNames()) { db.getCollection(name).drop(); dropCount++; } assertEquals("should drop two collections", 2, dropCount); }
/** * Test that ObjectId is getting generated even if _id is present in DBObject but it's value is * null * * @throws Exception */ @Test public void testIdGenerated() throws Exception { DBObject toSave = new BasicDBObject(); toSave.put("_id", null); toSave.put("name", "test"); Fongo fongo = newFongo(); DB fongoDB = fongo.getDB("testDB"); DBCollection collection = fongoDB.getCollection("testCollection"); collection.save(toSave); DBObject result = collection.findOne(new BasicDBObject("name", "test")); // default index in mongoDB final String ID_KEY = "_id"; assertNotNull("Expected _id to be generated" + result.get(ID_KEY)); }
private DBCollection newCollection() { Fongo fongo = newFongo(); DB db = fongo.getDB("db"); DBCollection collection = db.getCollection("coll"); return collection; }