Permalink
Browse files

tweaked basic tests

  • Loading branch information...
billy
billy committed Oct 19, 2009
1 parent 026d714 commit 4892084554558c0dd535d7e5371337221f096266
Showing with 80 additions and 97 deletions.
  1. +21 −45 MongoBlogTest.cfc
  2. +7 −1 MongoDB.cfc
  3. +26 −49 MongoDBTest.cfc
  4. +24 −0 README
  5. +2 −2 demo.cfm
View
@@ -1,10 +1,10 @@
<cfcomponent output="false" extends="mxunit.framework.TestCase">
<cfscript>
- mongo = createObject('component', 'MongoDB');
-
+ mongo = createObject('component', 'MongoDB');
blog = createObject('component', 'Blog');
- blog.title = 'Title 1';
+
+ blog.title = 'Title 1';
blog.text = 'Text 1';
blog.author = 'bill-1';
mongo.put(blog);
@@ -17,54 +17,30 @@
blog.title = 'Title 3';
blog.text = 'Text 3';
blog.author = 'bill-3';
- mongo.put(blog);
-
- /* */
+ mongo.put(blog);
+
+ /* weird. setUp appears to not be called in Railo? */
+
+ function tearDown(){
+ items = mongo.find();
+ for(item in items){
+ o = items[item];
+ mongo.deleteById( o['_id'].toString() );
+ debug( o['_id'].toString() );
+ }
+ }
- function testFindIterator(){
+ function testFindIterator(){
items = mongo.find();
- debug(items)
- for(item in items){
- post = items[item];
- debug(post["_id"].toString());
- }
-
-
+ assertEquals(3,items.size());
}
-
- function testBlog() {
-
- blog = createObject('component', 'Blog');
- blog.title = 'Look Ma. No SQL ...';
- blog.text = 'Rapid development with MongoDB';
- blog.author = 'bill';
- /*
- blog.author = 'billy';
- blog.title = 'blog test';
- blog.tags = ['politics','food','satire'];
- blog.body = '<h1>testing ...</h1>';
- blog.comments[1].author = 'Foo';
- blog.comments[1].comment_text = 'whatever';
- blog.comments[2].author = 'Bar';
- blog.comments[2].comment_www = 'http://google.com';
- blog.comments[2].comment_text = 'cool';
- */
-
- mongo.put(blog);
-
- // debug(blog);
-
- // blog.author = 'ed';
- // mongo.update(blog);
-
- mongo.delete(blog);
-
- bb = mongo.findOne();
- debug(bb);
- }
+
+ function setUp(){
+
+ }
View
@@ -9,7 +9,7 @@ collection_name = 'default_collection';
//maybe this goes in super class?
mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
-db = mongo.getDb(db_name);
+db = mongo.getDb(db_name);
collection = db.getCollection(collection_name);
@@ -37,6 +37,12 @@ function get(field,value){
} //end function
+//when only a string id is available
+function getById(id){
+ var objId = createObject('java','com.mongodb.ObjectId').init(id);
+ return get("_id", objId);
+ } //en
+
function count(){
return collection.getCount();
View
@@ -28,56 +28,49 @@
function testSerializeJSONThis(){
- var p = createObject('component','Person');
+ var p = createObject('component','Blog');
var j = serializeJSON(p);
debug(j);
- mongo = createObject('component','Mongo');
+ mongo = createObject('component','MongoDB');
id = mongo.put(p);
o = mongo.get('_id',id);
debug(o);
+ mongo.delete(p);
}
function $endToEndSyntax(){
- mongo = createObject('component','Mongo');
+ mongo = createObject('component','MongoDB');
id = mongo.put(person); //name/value or struct
- id2 = mongo.put('asdasd','xcvxcvxc');
the_guy = mongo.get('_id',id);
debug(id.toString());
- debug(id2.toString());
- the = mongo.get('_id',id2);
- //debug(the);
-
debug( mongo.count() );
+ assert(1==mongo.count());
+ mongo.delete(person);
- return;
- mongo.delete('NAME','bill');
- mongo.delete('asdasd','xcvxcvxc');
- debug( mongo.count() );
}
function $testFindSame(){
return;
- mongo = createObject('component','Mongo');
+ mongo = createObject('component','MongoDB');
//id = mongo.put(person); //name/value or struct
person.name = 'ed';
newperson = mongo.findSame(person);
debug(person);
debug(newperson);
+ mongo.delete(person);
+ assertEquals(newperson.name,'ed');
}
function $updatePerson(){
- mongo = createObject('component','Mongo');
- id = mongo.put(person); //name/value or struct
+ mongo = createObject('component','MongoDB');
+ mongo.put(person); //name/value or struct
person.name = 'ed';
- newperson = mongo.update(id,person);
+ newperson = mongo.update(person);
//debug(newperson.hashCode());
debug(newperson);
- person._id = id.toString();
- debug(person);
- return;
+
+ mongo.delete(person);
- mongo.delete('NAME','bill');
- mongo.delete('NAME','ed');
}
@@ -88,38 +81,22 @@ function $updatePerson(){
email='bill@bill.com'
};
-
- function basicDbTest() {
- db = mongo.getDB("mydb");
- assertEquals("com.mongodb.DBTCP", db.getClass().getName() );
+ function getByStringIdTest(){
+ var localp ={
+ name='shmoopy',
+ email='shmoopy@bshmoopy.com'
+ };
+ var id = mongo.put(localp);
+ var fetched_from_db_person = mongo.getById( id.toString() );
+ debug(fetched_from_db_person);
+ mongo.deleteById(id.toString());
+ assertEquals(fetched_from_db_person.name,'shmoopy');
- coll = db.getCollection("testCollection");
-
- doc = createObject('java', 'com.mongodb.BasicDBObject').init();
- doc.putAll(person);
-
- coll.insert(doc);
-
- }
-
-
- function smokeServerPort(){
- debug("these are defaults");
- assert( 'localhost'== mongo.getServer() );
- assert( '27017'== mongo.getPort() );
- assert( 'default_db'== mongo.getDBName() );
- }
-
- function mongoShouldBeCool() {
- assertSame(mongo, mongo);
- assertEquals(mongo, mongo);
- //debug(mongo);
}
-
-
+
function setUp(){
- mongo = createObject('component','MongoDB').init();
+ mongo = createObject('component','MongoDB');
}
function tearDown(){
View
24 README
@@ -5,6 +5,30 @@ This is simple proof of concept of using ColdFusion and MongoDB.
Conclusion: sweet
+Description: Inserts CFML structs into MongoDB
+Basic Usage:
+
+mongo = createObject('component','MongoDB');
+
+person = {
+ name="bill",
+ email = 'bill@mxunit.org',
+ address = {
+ street = '123 main st',
+ city = 'anytown',
+ state = 'here',
+ zip = '12345',
+ country = 'usa'
+ },
+ };
+
+id = mongo.put(person);
+person.city = 'some other town';
+mongo.update(person);
+fetched_person = mongo.get
+
+mongo.delete(person);
+
Installation:
1. Download and install MongoDB: http://www.mongodb.org/
View
@@ -1,6 +1,6 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
-<title>MongoDB/ColdFusion Ajax Demo</title>
+<title>MongoDB/ColdFusion Blog Demo</title>
<link type="text/css" rel="stylesheet" href="http://jqueryui.com/themes/base/ui.all.css" />
<script src="http://www.google.com/jsapi"></script>
@@ -44,7 +44,7 @@
</script>
</head>
<body>
-<h2>MongoDB/ColdFusion Ajax Demo</h2>
+<h2>MongoDB/ColdFusion Blog Demo</h2>
<form>
Blog Title: <input type="text" name="title"><br />
Author: <input type="text" name="author"><br />

0 comments on commit 4892084

Please sign in to comment.