Permalink
Browse files

Added an "asString" function in mongoUtil that can set a value to be …

…string in mongodb
  • Loading branch information...
jye
jye committed Mar 27, 2013
1 parent d568ee5 commit 4e2f6c6fd0cb6ad06a6382f9e3815739f409dcfb
View
@@ -55,6 +55,13 @@
function newOperationalDBObject(){
return dboFactory.newInstance(variables.operationTyper);
}
/**
* Designate a value to always be evaluated as string
**/
function asString(value){
return variables.mongoFactory.getObject("net.marcesher.CFStrictTyperString").init(toString(arguments.value));
}
/**
* Create a new instance of a CFBasicDBObjectBuilder
@@ -31,7 +31,9 @@ public Object toJavaType(Object value){
return handleArray(value);
} else if( value instanceof Map ){
return handleMap(value);
}
} else if( value instanceof net.marcesher.CFStrictTyperString){
return value.toString();
}
return value;
}
@@ -0,0 +1,20 @@
package net.marcesher;
/**
*
* A simple class that does nothing but to let us know this should
* be interpreted as a string
*
*/
public class CFStrictTyperString {
private String val;
public CFStrictTyperString(String val) {
this.val = val;
}
@Override
public String toString() {
return this.val;
}
}
View
@@ -73,6 +73,7 @@
<cfargument name="count" type="numeric" required="false" default="5"/>
<cfargument name="save" type="boolean" required="false" default="true"/>
<cfargument name="name" type="string" required="false" default="unittest"/>
<cfargument name="stringTest" type="boolean" required="false" default="false" />
<cfscript>
var i = 1;
var people = [];
@@ -81,9 +82,15 @@
"name"=name,
"age"=randRange(10,100),
"now"=getTickCount(),
"networth"=randRange(10,100) * 7.25,
"counter"=i,
inprocess=false
};
if (arguments.stringTest) {
person["ageAsString"] = mongo.getMongoUtil().asString(person["age"]);
person["networthAsString"] = mongo.getMongoUtil().asString(person["networth"]);
person["counterAsString"] = mongo.getMongoUtil().asString(person["counter"]);
}
arrayAppend(people, person);
}
if(save){
View
@@ -302,6 +302,43 @@ import cfmongodb.core.*;
var doc = one.asArray()[1];
assertEquals( 3, doc.counter );
}
function find_should_handle_asString_correctly() {
var people = createPeople(5, true, "unittest", true);
var all = dbCol.find({"counterAsString": 1});
//expect no results because it's searching as a number
assertEquals(0, all.size());
var all = dbCol.find({"counterAsString": mongo.getMongoUtil().asString("1")});
//expect 1 result because it's searching as a string
assertEquals(1, all.size());
}
function find_should_handle_all_types_correctly() {
var people = createPeople(5, true, "unittest", true);
//make sure there is 5 double
all = dbCol.find( {"$and"= [{"name"="unittest"},
{"networth"={"$exists"=1}},
{"networth"={"$type"=1}}]} ); //type 1 is double
assertEquals(5, all.size());
//make sure there is 5 integer
all = dbCol.find( {"$and"= [{"name"="unittest"},
{"age"={"$exists"=1}},
{"age"={"$type"=16}}]} ); //type 16 is integer
assertEquals(5, all.size());
//make sure there is 5 string for "ageAsString"
all = dbCol.find( {"$and"= [{"name"="unittest"},
{"ageAsString"={"$exists"=1}},
{"ageAsString"={"$type"=2}}]} ); //type 2 is string
assertEquals(5, all.size());
//make sure there is 5 string for "networthAsString"
all = dbCol.find( {"$and"= [{"name"="unittest"},
{"networthAsString"={"$exists"=1}},
{"networthAsString"={"$type"=2}}]} ); //type 2 is stirng
assertEquals(5, all.size());
}
function find_should_honor_including_fields(){
var people = createPeople(5, true);

0 comments on commit 4e2f6c6

Please sign in to comment.