| 1 |
process.env.TZ = "America/Los_Angeles"; |
| 2 |
|
| 3 |
var vows = require("vows"), |
| 4 |
assert = require("assert"); |
| 5 |
|
| 6 |
var dbf = require("../dbf"); |
| 7 |
|
| 8 |
var suite = vows.describe("dbf"); |
| 9 |
|
| 10 |
suite.addBatch({ |
| 11 |
"The header of a simple dBASE file": { |
| 12 |
topic: readHeader("./test/boolean-property.dbf"), |
| 13 |
"has the expected values": function(header) { |
| 14 |
assert.deepEqual(header, { |
| 15 |
count: 9, |
| 16 |
date: new Date(Date.UTC(1995, 6, 26, 7)), |
| 17 |
version: 3, |
| 18 |
fields: [{name: "foo", type: "L", length: 1}] |
| 19 |
}); |
| 20 |
} |
| 21 |
}, |
| 22 |
|
| 23 |
"The header of an empty dBASE file": { |
| 24 |
topic: readHeader("./test/empty.dbf"), |
| 25 |
"has the expected values": function(header) { |
| 26 |
assert.deepEqual(header, { |
| 27 |
count: 0, |
| 28 |
date: new Date(Date.UTC(1995, 6, 26, 7)), |
| 29 |
version: 3, |
| 30 |
fields: [] |
| 31 |
}); |
| 32 |
} |
| 33 |
}, |
| 34 |
|
| 35 |
"The header of a dBASE file with ISO-8859-1 property names": { |
| 36 |
topic: readHeader("./test/latin1-property.dbf"), |
| 37 |
"has the expected values": function(header) { |
| 38 |
assert.deepEqual(header, { |
| 39 |
count: 1, |
| 40 |
date: new Date(Date.UTC(1995, 6, 26, 7)), |
| 41 |
version: 3, |
| 42 |
fields: [{name: "name", type: "C", length: 80}] |
| 43 |
}); |
| 44 |
} |
| 45 |
}, |
| 46 |
|
| 47 |
"The records of a dBASE file with ISO-8859-1 property names": { |
| 48 |
topic: readRecords("./test/latin1-property.dbf"), |
| 49 |
"have the expected values": function(records) { |
| 50 |
assert.deepEqual(records, [ |
| 51 |
["México"] |
| 52 |
]); |
| 53 |
} |
| 54 |
}, |
| 55 |
|
| 56 |
"The header of a dBASE file with UTF-8 property names": { |
| 57 |
topic: readHeader("./test/utf8-property.dbf", "utf8"), |
| 58 |
"has the expected values": function(header) { |
| 59 |
assert.deepEqual(header, { |
| 60 |
count: 1, |
| 61 |
date: new Date(Date.UTC(1995, 6, 26, 7)), |
| 62 |
version: 3, |
| 63 |
fields: [{name: "☃", type: "C", length: 80}] |
| 64 |
}); |
| 65 |
} |
| 66 |
}, |
| 67 |
|
| 68 |
"The records of a dBASE file with UTF-8 property names": { |
| 69 |
topic: readRecords("./test/utf8-property.dbf", "utf8"), |
| 70 |
"have the expected values": function(records) { |
| 71 |
assert.deepEqual(records, [ |
| 72 |
["ηελλο ςορλδ"] |
| 73 |
]); |
| 74 |
} |
| 75 |
}, |
| 76 |
|
| 77 |
"The records of an empty dBASE file": { |
| 78 |
topic: readRecords("./test/empty.dbf"), |
| 79 |
"have the expected values": function(records) { |
| 80 |
assert.deepEqual(records, []); |
| 81 |
} |
| 82 |
}, |
| 83 |
|
| 84 |
"The records of a simple dBASE file": { |
| 85 |
topic: readRecords("./test/boolean-property.dbf"), |
| 86 |
"have the expected values": function(records) { |
| 87 |
assert.deepEqual(records, [ |
| 88 |
[null], |
| 89 |
[true], |
| 90 |
[true], |
| 91 |
[false], |
| 92 |
[false], |
| 93 |
[true], |
| 94 |
[true], |
| 95 |
[false], |
| 96 |
[false] |
| 97 |
]); |
| 98 |
} |
| 99 |
}, |
| 100 |
|
| 101 |
"The records of a dBASE file with a string property": { |
| 102 |
topic: readRecords("./test/string-property.dbf"), |
| 103 |
"have the expected values": function(records) { |
| 104 |
assert.deepEqual(records, [ |
| 105 |
[null], |
| 106 |
["blue"], |
| 107 |
["green"] |
| 108 |
]); |
| 109 |
} |
| 110 |
}, |
| 111 |
|
| 112 |
"The records of a dBASE file with a number property": { |
| 113 |
topic: readRecords("./test/number-property.dbf"), |
| 114 |
"have the expected values": function(records) { |
| 115 |
assert.deepEqual(records, [ |
| 116 |
[null], |
| 117 |
[42], |
| 118 |
[-4] |
| 119 |
]); |
| 120 |
} |
| 121 |
}, |
| 122 |
|
| 123 |
"The records of a dBASE file with a date property": { |
| 124 |
topic: readRecords("./test/date-property.dbf"), |
| 125 |
"have the expected values": function(records) { |
| 126 |
assert.deepEqual(records, [ |
| 127 |
[new Date(2013, 0, 2)], |
| 128 |
[new Date(2013, 1, 2)], |
| 129 |
[new Date(2013, 0, 3)] |
| 130 |
]); |
| 131 |
} |
| 132 |
}, |
| 133 |
|
| 134 |
"The records of a dBASE file with multiple properties": { |
| 135 |
topic: readRecords("./test/mixed-properties.dbf"), |
| 136 |
"have the expected values": function(records) { |
| 137 |
assert.deepEqual(records, [ |
| 138 |
[null, null], |
| 139 |
[42, null], |
| 140 |
[null, "blue"] |
| 141 |
]); |
| 142 |
} |
| 143 |
} |
| 144 |
}); |
| 145 |
|
| 146 |
function readHeader(path, encoding) { |
| 147 |
return function() { |
| 148 |
var callback = this.callback; |
| 149 |
dbf.read(path, encoding, function(error, header, records) { |
| 150 |
callback(error, header); |
| 151 |
}); |
| 152 |
}; |
| 153 |
} |
| 154 |
|
| 155 |
function readRecords(path, encoding) { |
| 156 |
return function() { |
| 157 |
var callback = this.callback; |
| 158 |
dbf.read(path, encoding, function(error, header, records) { |
| 159 |
callback(error, records); |
| 160 |
}); |
| 161 |
}; |
| 162 |
} |
| 163 |
|
| 164 |
suite.export(module); |