Permalink
Browse files

Updates to tests

  • Loading branch information...
DanielRuf committed Jan 21, 2013
1 parent 363f4fd commit 5654fe0d26c1de40207875de5ef0e9ecfd68f119
Showing with 164 additions and 50 deletions.
  1. +164 −50 taffy-test.html
View
@@ -7,71 +7,187 @@
<script src="./taffy.js"></script>
<script>
- // recursive object compare, for future use
- Object.prototype.equals = function (x) {
- var p;
- for(p in this) {
- if(typeof(x[p])=='undefined') {return false;}
+
+
+ /*jslint browser : true, continue : true,
+ devel : true, indent : 2, maxerr : 50,
+ newcap : true, nomen : true, plusplus : true,
+ regexp : true, sloppy : true, vars : false,
+ white : true
+ */
+
+ /*global TAFFY */
+ var getVarType, runTests, getFriendList;
+
+ // Begin public utility /getVarType/
+ // Returns 'Function', 'Object', 'Array',
+ // 'String', 'Number', 'Boolean', or 'Undefined',
+ getVarType = function ( data ) {
+ if (undefined === data ){ return 'Undefined'; }
+ if (data === null ){ return 'Null'; }
+ return {}.toString.call(data).slice(8, -1);
+ };
+ // End public utility /getVarType/
+
+ // Begin recursive object compare for future use
+ Object.prototype.equals = function ( x ) {
+ var p, var_type, var_type_x, self = this;
+
+ for ( p in self ) {
+ if ( self.hasOwnProperty( p ) ) {
+ var_type = getVarType( p );
+ if ( var_type === 'Undefined') {
+ return false;
+ }
+ }
}
- for(p in this) {
- if (this[p]) {
- switch(typeof(this[p])) {
- case 'object':
- if (! this[p].equals(x[p])) { return false; }
- break;
- case 'function':
- if (typeof(x[p])=='undefined' ||
- (p != 'equals' && this[p].toString() != x[p].toString())
- ){ return false; }
+ for ( p in self ) {
+ if ( self.hasOwnProperty( p ) ) {
+ if ( self[ p ] ) {
+ var_type = getVarType( self[ p ] );
+ switch( var_type ) {
+ case 'Function':
+ var_type_x = getVarType( x[ p ] );
+ if ( var_type === 'Undefined' ||
+ ( p !== 'equals'
+ && self[ p ].toString() !== x[p].toString()
+ )){
+ return false;
+ }
+ break;
+ case 'Array' :
+ case 'Object':
+ if ( ! self[ p ].equals( x[ p ] )) {
+ return false;
+ }
break;
- default:
- if (this[p] != x[p]) { return false; }
+ default:
+ if (self[ p ] !== x[ p ] ) {
+ return false;
+ }
+ }
+ }
+ else {
+ if ( x[ p ] ){
+ return false;
+ }
}
- }
- else {
- if (x[p]){ return false; }
}
}
- for(p in x) {
- if(typeof(this[p])=='undefined') {return false;}
+ for ( p in x ) {
+ if ( x.hasOwnProperty( p ) ){
+ var_type = getVarType( self[ p ] );
+ if ( var_type === 'Undefined' ) {
+ return false;
+ }
+ }
}
return true;
};
+ // End recursive object compare for future use
+ // Begin getFriendList
-
- var key_name, data_val, friends_table, taffy_map;
- friends_table = TAFFY([
- {"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active"},
- {"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active"},
- {"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active"},
- {"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active"}
- ]);
-
- taffy_map = {
- t_by_city : friends_table({city:"Seattle, WA"}),
- t_by_id : friends_table({id:1}),
- t_by_id_f : friends_table({id:'1'}),
- t_by_name : friends_table({first:'John',last:'Smith'}),
- kelly_by_id : friends_table({id:2}).first(),
- kelly_last_name : friends_table({id:2}).first().last,
- id_list : friends_table().select('id'),
- city_list : friends_table().distinct('city'),
+ getFriendList = function () {
+ return [
+ {"id":1,"gender":"M","first":"John","last":"Smith",
+ "city":"Seattle, WA","status":"Active"},
+ {"id":2,"gender":"F","first":"Kelly","last":"Ruth",
+ "city":"Dallas, TX","status":"Active"},
+ {"id":3,"gender":"M","first":"Jeff","last":"Stevenson",
+ "city":"Washington, D.C.","status":"Active"},
+ {"id":4,"gender":"F","first":"Jennifer","last":"Gill",
+ "city":"Seattle, WA","status":"Active"}
+ ];
};
- for ( key_name in taffy_map ){
- if ( taffy_map.hasOwnProperty(key_name) ){
- data_val = taffy_map[key_name];
- console.warn(key_name, data_val);
- if ( data_val.hasOwnProperty('get') ){
- console.warn(JSON.stringify(data_val.get()));
+ // End getFriendList
+
+ // Begin runTests
+ runTests = function () {
+ var
+ friend_db, taffy_map,
+ key_name, data_val, msg_text, var_type;
+
+ friend_db = TAFFY( getFriendList() );
+
+ taffy_map = {
+ by_city : friend_db({ city : "Seattle, WA"}),
+ by_id : friend_db({ id : 1}),
+ by_id_f : friend_db({ id : '1'}),
+ by_name : friend_db({ first : 'John',last:'Smith'}),
+ kelly_by_id : friend_db({ id : 2}).first(),
+ kelly_last_name : friend_db({ id : 2}).first().last,
+ id_list : friend_db().select( 'id' ),
+ city_list : friend_db().distinct( 'city' ),
+ filter_list : friend_db().filter({ city : "Seattle, WA"})
+ };
+
+ for ( key_name in taffy_map ){
+ if ( taffy_map.hasOwnProperty( key_name ) ) {
+ data_val = taffy_map[ key_name ];
+ var_type = getVarType( data_val );
+ msg_text = key_name + ': \n ===================\n';
+
+ switch ( var_type ){
+ case 'Object' :
+ if ( data_val.hasOwnProperty( 'get' ) ){
+ msg_text += JSON.stringify( data_val.get() );
+ }
+ else {
+ msg_text += JSON.stringify( data_val );
+ }
+ break;
+ case 'Number' :
+ msg_text += String( data_val );
+ break;
+
+ default :
+ msg_text += JSON.stringify( data_val );
+ }
+
+ msg_text += '\n\n';
+
+ console.log( msg_text );
}
- console.warn('----------------');
}
- }
+
+ console.log( 'Example filter bug - rows changed without using '
+ + '.update() will filter by their original values. '
+ );
+ friend_db().each(function ( row_map, idx ) {
+ if ( row_map.city === 'Seattle, WA' ){
+ row_map.city = 'WallaWalla, WA';
+ }
+ });
+
+ console.log( 'We expect 0 rows, but get 2... ' );
+ console.log(
+ friend_db().filter({ city : 'Seattle, WA'}).get()
+ );
+ console.log( '...even though the city has changed in the collection.' );
+ console.log( friend_db().get() );
+ console.log( '' );
+
+ console.log( 'Example filter when .update() is used.');
+ friend_db = TAFFY( getFriendList() );
+
+ friend_db({ city : 'Seattle, WA' })
+ .update({ city : 'WallaWalla, WA' });
+
+ console.log( 'now we get the correct response (0 rows) ...' );
+ console.log(
+ friend_db().filter({ city : 'Seattle, WA'}).get()
+ );
+ console.log( friend_db().get() );
+ console.log( '... that reflects the taffy collection.' );
+ };
+ // End runTests
+
+ runTests();
</script>
</head>
<body>
@@ -80,5 +196,3 @@
</div>
</body>
</html>
-
-

0 comments on commit 5654fe0

Please sign in to comment.