diff options
author | jakefeasel <jfeasel@gmail.com> | 2012-11-11 16:29:00 -0800 |
---|---|---|
committer | jakefeasel <jfeasel@gmail.com> | 2012-11-11 16:29:00 -0800 |
commit | c04ee611a5cdbd37e85300cd39725ab7330e1f16 (patch) | |
tree | 73126945d6bf398de9cd2477729e4b41fa19a649 | |
parent | 0096d1a178507bdf3e0871ede2916ecfe4fb3245 (diff) | |
download | DDLBuilder-c04ee611a5cdbd37e85300cd39725ab7330e1f16.zip DDLBuilder-c04ee611a5cdbd37e85300cd39725ab7330e1f16.tar.gz DDLBuilder-c04ee611a5cdbd37e85300cd39725ab7330e1f16.tar.bz2 |
New tests for date parsing
Fixes for Chrome to reject things that really shouldn't be treated as
dates
-rw-r--r-- | ddl_builder/ddl_builder.js | 46 | ||||
-rw-r--r-- | ddl_builder/qunit/fixture.html | 39 | ||||
-rw-r--r-- | ddl_builder/qunit/main.js | 27 |
3 files changed, 88 insertions, 24 deletions
diff --git a/ddl_builder/ddl_builder.js b/ddl_builder/ddl_builder.js index a2797dc..df911e5 100644 --- a/ddl_builder/ddl_builder.js +++ b/ddl_builder/ddl_builder.js @@ -27,7 +27,7 @@ define( this.tableSuffix = ''; - this.dateFormatMask = "yyyy-mm-dd HH:MM:ss"; + this.dateFormatMask = "UTC:yyyy-mm-dd HH:MM:ss"; this.charType = 'varchar'; this.intType = 'int'; @@ -72,7 +72,7 @@ define( this.compiledTemplate = Handlebars.compile(this.ddlTemplate); this.setup(args); return this; - } + }; ddl_builder.prototype.setup = function (settings) { for (var opt in settings) @@ -87,7 +87,7 @@ define( this.definition.tableName = settings.tableName; return this; - } + }; ddl_builder.prototype.setupForDBType = function (type,separator) { @@ -158,7 +158,7 @@ define( } return this; - } + }; ddl_builder.prototype.populateDBTypes = function () { for (var i=0;i<this.definition.columns.length;i++) @@ -244,15 +244,22 @@ define( } return {status: true, separator: found_separator, column_count: column_count}; - } + }; ddl_builder.prototype.parse = function (raw) { - + /* + * brokenDateChecker is used to eliminate strings that for some reason pass Chrome's standard of a 'valid' date, and + * yet are not worth considering as such. Chrome will take garbage like 'ABC 1' as an input to `new Date()`, returning + * January 1st 2001 for some reason. This regex will allow a lot of fuzzy input formats to still pass, but only really + * keep meaningful entries. This isn't a problem for Firefox or Safari. I haven't checked IE. + */ + var brokenDateChecker = /^(?!Jan)(?!Feb)(?!Mar)(?!Apr)(?!May)(?!Jun)(?!Jul)(?!Aug)(?!Sep)(?!Oct)(?!Nov)(?!Dec)[A-Za-z\ \-\_]+\d+\s*$/, + result = {}, lines = [], elements = [], tmpRow = [], i = 0, j = 0, value = ""; if (!this.valueSeparator.length) { - var result = this.guessValueSeparator(raw); + result = this.guessValueSeparator(raw); if (!result.status) return "ERROR! " + result.message; else @@ -262,11 +269,11 @@ define( } } - var lines = raw.split("\n"); + lines = raw.split("\n"); - for (var i=0;i<lines.length;i++) + for (i=0;i<lines.length;i++) { - var elements = $.trim(lines[i]).split(this.valueSeparator); + elements = $.trim(lines[i]).split(this.valueSeparator); if ($.trim(lines[i]).length && ( @@ -280,9 +287,9 @@ define( { if (! this.definition.columns.length) { - for (var j = 0; j < elements.length; j++) + for (j = 0; j < elements.length; j++) { - var value = $.trim(elements[j]); + value = $.trim(elements[j]); if (value.length) this.definition.columns.push({"name": value}); else @@ -291,20 +298,19 @@ define( } else { - - var tmpRow = []; - for (var j = 0; j < elements.length; j++) + tmpRow = []; + for (j = 0; j < elements.length; j++) { if (this.definition.columns[j] !== false) { - var value = $.trim(elements[j]).replace(/'/g, "''"); + value = $.trim(elements[j]).replace(/'/g, "''"); // if the current field is not a number, or if we have previously decided that this one of the non-numeric field types... if (isNaN(value) || this.definition.columns[j].type == 'dateType' || this.definition.columns[j].type == 'charType') { // if we haven't previously decided that this is a character field, and it can be cast as a date, then declare it a date - if (this.definition.columns[j].type != 'charType' && !isNaN(Date.parse(value)) ) + if (this.definition.columns[j].type != 'charType' && !(isNaN(Date.parse(value)) || value.match(brokenDateChecker)) ) this.definition.columns[j].type = "dateType"; else this.definition.columns[j].type = "charType"; @@ -335,7 +341,7 @@ define( this.populateDBTypes(); this.populateWrappers(); return this.render(); - } + }; /* HandlebarsJS-using code below */ @@ -362,7 +368,7 @@ define( return new Handlebars.SafeString("'" + this.v.replace(/'/g, "''") + "'"); if (colType == 'dateType') - return new Handlebars.SafeString("'" + dateFormat(this.v, root.dateFormatMask) + "'"); + return new Handlebars.SafeString("'" + dateFormat("UTC:" + this.v, root.dateFormatMask) + "'"); return this.v; }); @@ -374,7 +380,7 @@ define( ddl_builder.prototype.render = function () { return this.compiledTemplate($.extend(this.definition, {"separator": this.statement_separator})); - } + }; return ddl_builder; diff --git a/ddl_builder/qunit/fixture.html b/ddl_builder/qunit/fixture.html index 8cffd89..f2a2112 100644 --- a/ddl_builder/qunit/fixture.html +++ b/ddl_builder/qunit/fixture.html @@ -86,9 +86,46 @@ customdata check service loc value 102 4 4 3 3 103 0 0 4 4 </span> - + + <span id="stepColumnKnownForDateProblems" types="varchar(6),varchar(6),datetime,varchar(7)" valueSeparator="/\s\s+/" headers="Old,New,Time Entered,Order Number" recordCount="23"> +Old New Time Entered Order Number + NULL Step 1 4/30/12 10:43 1C2014A + Step 1 Step 2 5/2/12 10:17 1C2014A + Step 2 Step 3 5/2/12 10:28 1C2014A + Step 3 Step 4 5/2/12 11:14 1C2014A + Step 4 Step 5 5/2/12 11:19 1C2014A + Step 5 Step 9 5/3/12 11:23 1C2014A + NULL Step 1 5/18/12 15:49 1C2014B + Step 1 Step 2 5/21/12 9:21 1C2014B + Step 2 Step 3 5/21/12 9:34 1C2014B + Step 3 Step 4 5/21/12 10:08 1C2014B + Step 4 Step 5 5/21/12 10:09 1C2014B + Step 5 Step 6 5/21/12 16:27 1C2014B + Step 6 Step 9 5/21/12 18:07 1C2014B + NULL Step 1 6/12/12 10:28 1C2014C + Step 1 Step 2 6/13/12 8:36 1C2014C + Step 2 Step 3 6/13/12 9:05 1C2014C + Step 3 Step 4 6/13/12 10:28 1C2014C + Step 4 Step 6 6/13/12 10:50 1C2014C + Step 6 Step 8 6/13/12 12:14 1C2014C + Step 8 Step 4 6/13/12 15:13 1C2014C + Step 4 Step 5 6/13/12 15:23 1C2014C + Step 5 Step 8 6/13/12 15:30 1C2014C + Step 8 Step 9 6/18/12 14:04 1C2014C + </span> + + <span id="dateParse" types="datetime,datetime" valueSeparator="|" headers="input,output" recordCount="5"> + input|output + 2012-03-12|2012-03-12 00:00:00 + 01/02/2003|2003-01-02 00:00:00 + 05-Mar-1995|1995-03-05 00:00:00 + April 5, 2010|2010-04-05 00:00:00 + 4/30/12 10:43|2012-04-30 10:43:00 + </span> </div> <!-- + + <span id="bracketHeaders" types="int,varchar(3),datetime" valueSeparator="/\s\s+/" headers="Serial Number,LID,Last Updated Date" recordCount="12"> [Serial Number] [LID] [Last Updated Date] -------------------------------------- diff --git a/ddl_builder/qunit/main.js b/ddl_builder/qunit/main.js index aba2ec9..d3a5ee3 100644 --- a/ddl_builder/qunit/main.js +++ b/ddl_builder/qunit/main.js @@ -5,12 +5,14 @@ define([ "./columnTypes", "./guessValueSeparators", "./headerNames", - "./recordCount" + "./recordCount", + "DDLBuilder/ddl_builder" ], function ($,QUnit,fixtureContent, columnTypes,guessValueSeparators, - headerNames,recordCount) { + headerNames,recordCount, + DDLBuilder) { $("#qunit-fixture").append(fixtureContent); @@ -24,5 +26,24 @@ define([ }); }); + QUnit.test("Various date format parsing attempts", function () { + var content = $("#qunit-fixture #dateParse").html(), + ddl_builder = new DDLBuilder({ddlTemplate: "[{{#each_with_index data}}{{#if index}},{{/if}}[{{#each_with_index r}}{{#if index}}, {{/if}}\"{{formatted_field ../..}}\"{{/each_with_index}}]{{/each_with_index}}]"}), + result = ddl_builder.parse(content), + parsedResult=null,i=0; + + try { + parsedResult = $.parseJSON(result); + } catch (err){} + + if (parsedResult) { + for (i in parsedResult) { + QUnit.equal(parsedResult[i][0], parsedResult[i][1], "Input date matches output date"); + } + } + else + QUnit.ok(false, "Reading date data failed: Unable to parse result to JSON array ("+ result +")"); + + }); } -)
\ No newline at end of file +);
\ No newline at end of file |