1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
# frozen_string_literal: true
# encoding: ascii-8bit
require 'spec_helper'
RSpec.describe DBF::Column do
let(:table) { DBF::Table.new fixture('dbase_30.dbf') }
context 'when initialized' do
let(:column) { DBF::Column.new table, 'ColumnName', 'N', 1, 0 }
it 'sets :name accessor' do
expect(column.name).to eq 'ColumnName'
end
it 'sets :type accessor' do
expect(column.type).to eq 'N'
end
it 'sets the #length accessor' do
expect(column.length).to eq 1
end
it 'sets the #decimal accessor' do
expect(column.decimal).to eq 0
end
it 'accepts length of 0' do
column = DBF::Column.new table, 'ColumnName', 'N', 0, 0
expect(column.length).to eq 0
end
describe 'with length less than 0' do
it 'raises DBF::Column::LengthError' do
expect { DBF::Column.new table, 'ColumnName', 'N', -1, 0 }.to raise_error(DBF::Column::LengthError)
end
end
describe 'with empty column name' do
it 'raises DBF::Column::NameError' do
expect { DBF::Column.new table, '', 'N', 1, 0 }.to raise_error(DBF::Column::NameError)
end
end
end
describe '#type_cast' do
context 'with type N (number)' do
context 'when value is empty' do
it 'returns nil' do
value = ''
column = DBF::Column.new table, 'ColumnName', 'N', 5, 2
expect(column.type_cast(value)).to be_nil
end
end
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'N', 0, 0
expect(column.type_cast('')).to be_nil
end
end
context 'with 0 decimals' do
it 'casts value to Integer' do
value = '135'
column = DBF::Column.new table, 'ColumnName', 'N', 3, 0
expect(column.type_cast(value)).to eq 135
end
it 'supports negative Integer' do
value = '-135'
column = DBF::Column.new table, 'ColumnName', 'N', 3, 0
expect(column.type_cast(value)).to eq(-135)
end
end
context 'with more than 0 decimals' do
it 'casts value to Float' do
value = '13.5'
column = DBF::Column.new table, 'ColumnName', 'N', 2, 1
expect(column.type_cast(value)).to eq 13.5
end
it 'casts negative value to Float' do
value = '-13.5'
column = DBF::Column.new table, 'ColumnName', 'N', 2, 1
expect(column.type_cast(value)).to eq(-13.5)
end
end
end
context 'with type F (float)' do
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'F', 0, 0
expect(column.type_cast('')).to be_nil
end
end
it 'casts value to Float' do
value = '135'
column = DBF::Column.new table, 'ColumnName', 'F', 3, 0
expect(column.type_cast(value)).to eq 135.0
end
it 'casts negative value to Float' do
value = '-135'
column = DBF::Column.new table, 'ColumnName', 'F', 3, 0
expect(column.type_cast(value)).to eq(-135.0)
end
end
context 'with type B (binary)' do
context 'with Foxpro dbf' do
it 'casts to float' do
column = DBF::Column.new table, 'ColumnName', 'B', 1, 2
expect(column.type_cast("\xEC\x51\xB8\x1E\x85\x6B\x31\x40")).to be_a(Float)
expect(column.type_cast("\xEC\x51\xB8\x1E\x85\x6B\x31\x40")).to eq 17.42
end
it 'stores original precision' do
column = DBF::Column.new table, 'ColumnName', 'B', 1, 0
expect(column.type_cast("\xEC\x51\xB8\x1E\x85\x6B\x31\x40")).to be_a(Float)
expect(column.type_cast("\xEC\x51\xB8\x1E\x85\x6B\x31\x40")).to eq 17.42
end
it 'supports negative binary' do
column = DBF::Column.new table, 'ColumnName', 'B', 1, 2
expect(column.type_cast("\x00\x00\x00\x00\x00\xC0\x65\xC0")).to be_a(Float)
expect(column.type_cast("\x00\x00\x00\x00\x00\xC0\x65\xC0")).to eq(-174.0)
end
end
end
context 'with type I (integer)' do
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'I', 0, 0
expect(column.type_cast('')).to be_nil
end
end
it 'casts value to Integer' do
value = "\203\171\001\000"
column = DBF::Column.new table, 'ColumnName', 'I', 3, 0
expect(column.type_cast(value)).to eq 96_643
end
it 'supports negative Integer' do
value = "\x24\xE1\xFF\xFF"
column = DBF::Column.new table, 'ColumnName', 'I', 3, 0
expect(column.type_cast(value)).to eq(-7900)
end
end
context 'with type L (logical/boolean)' do
let(:column) { DBF::Column.new table, 'ColumnName', 'L', 1, 0 }
it "casts 'y' to true" do
expect(column.type_cast('y')).to be true
end
it "casts 't' to true" do
expect(column.type_cast('t')).to be true
end
it "casts value other than 't' or 'y' to false" do
expect(column.type_cast('n')).to be false
end
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'L', 0, 0
expect(column.type_cast('')).to be_nil
end
end
end
context 'with type T (datetime)' do
let(:column) { DBF::Column.new table, 'ColumnName', 'T', 16, 0 }
context 'with valid datetime' do
it 'casts to DateTime' do
expect(column.type_cast("Nl%\000\300Z\252\003")).to eq Time.parse('2002-10-10T17:04:56+00:00')
end
end
context 'with invalid datetime' do
it 'casts to nil' do
expect(column.type_cast("Nl%\000\000A\000\999")).to be_nil
end
end
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'T', 0, 0
expect(column.type_cast('')).to be_nil
end
end
end
context 'with type D (date)' do
let(:column) { DBF::Column.new table, 'ColumnName', 'D', 8, 0 }
context 'with valid date' do
it 'casts to Date' do
expect(column.type_cast('20050712')).to eq Date.new(2005, 7, 12)
end
end
context 'with invalid date' do
it 'casts to nil' do
expect(column.type_cast('000000000')).to be_nil
end
end
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'D', 0, 0
expect(column.type_cast('')).to be_nil
end
end
end
context 'with type M (memo)' do
it 'casts to string' do
column = DBF::Column.new table, 'ColumnName', 'M', 3, 0
expect(column.type_cast('abc')).to eq 'abc'
end
it 'casts nil to nil' do
column = DBF::Column.new table, 'ColumnName', 'M', 3, 0
expect(column.type_cast(nil)).to be_nil
end
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'M', 0, 0
expect(column.type_cast('')).to be_nil
end
end
end
context 'with type G (memo)' do
it 'returns binary data' do
column = DBF::Column.new table, 'ColumnName', 'G', 3, 0
expect(column.type_cast("\000\013\120")).to eq "\000\013\120"
expect(column.type_cast("\000\013\120").encoding).to eq Encoding::ASCII_8BIT
end
it 'casts nil to nil' do
column = DBF::Column.new table, 'ColumnName', 'G', 3, 0
expect(column.type_cast(nil)).to be_nil
end
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'G', 0, 0
expect(column.type_cast('')).to be_nil
end
end
end
end
context 'with type Y (currency)' do
let(:column) { DBF::Column.new table, 'ColumnName', 'Y', 8, 4 }
it 'casts to float' do
expect(column.type_cast(" \xBF\x02\x00\x00\x00\x00\x00")).to eq 18.0
end
it 'supports negative currency' do
expect(column.type_cast("\xFC\xF0\xF0\xFE\xFF\xFF\xFF\xFF")).to eq(-1776.41)
end
it 'supports 64bit negative currency' do
expect(column.type_cast("pN'9\xFF\xFF\xFF\xFF")).to eq(-333_609.0)
end
context 'with 0 length' do
it 'returns nil' do
column = DBF::Column.new table, 'ColumnName', 'Y', 0, 0
expect(column.type_cast('')).to be_nil
end
end
end
end
|