|
| 1 | +defmodule Tds.TypesTest do |
| 2 | + use ExUnit.Case, async: false |
| 3 | + |
| 4 | + alias Tds.Parameter |
| 5 | + |
| 6 | + import Tds.TestHelper |
| 7 | + |
| 8 | + require Logger |
| 9 | + |
| 10 | + @tds_data_type_decimaln 0x6A |
| 11 | + |
| 12 | + setup do |
| 13 | + {:ok, pid} = Tds.start_link(opts()) |
| 14 | + |
| 15 | + {:ok, [pid: pid]} |
| 16 | + end |
| 17 | + |
| 18 | + defp create_table(context) do |
| 19 | + precision = context[:precision] || 10 |
| 20 | + scale = context[:scale] || 4 |
| 21 | + |
| 22 | + if not is_integer(precision) do |
| 23 | + raise ArgumentError, "precision must be an integer" |
| 24 | + end |
| 25 | + |
| 26 | + if not is_integer(scale) do |
| 27 | + raise ArgumentError, "scale must be an integer" |
| 28 | + end |
| 29 | + |
| 30 | + query("DROP TABLE IF EXISTS foo", []) |
| 31 | + query("CREATE TABLE foo (col DECIMAL(#{precision}, #{scale}) NULL)", []) |
| 32 | + end |
| 33 | + |
| 34 | + @spec insert_decimal(Decimal.t() | nil, map) :: Decimal.t() |
| 35 | + defp insert_decimal(value, context) do |
| 36 | + query("TRUNCATE TABLE foo", []) |
| 37 | + |
| 38 | + :ok = |
| 39 | + query("INSERT INTO foo (col) VALUES (@1)", [ |
| 40 | + %Parameter{name: "@1", value: value, type: :decimal} |
| 41 | + ]) |
| 42 | + |
| 43 | + {:ok, result} = Tds.query(context[:pid], "SELECT col FROM foo", []) |
| 44 | + %Tds.Result{rows: [[value]]} = result |
| 45 | + value |
| 46 | + end |
| 47 | + |
| 48 | + describe "encode_data/3" do |
| 49 | + test "encodes decimal type", _context do |
| 50 | + value = Decimal.new("1000") |
| 51 | + attr = [precision: 8, scale: 4] |
| 52 | + |
| 53 | + # assert <<5, 1, 128, 150, 152, 0>> = |
| 54 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 55 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 56 | + |
| 57 | + assert byte_len == 5 |
| 58 | + assert sign == 1 |
| 59 | + assert <<232, 3, 0, 0>> = value_binary |
| 60 | + assert :binary.decode_unsigned(value_binary, :little) == 1000 |
| 61 | + end |
| 62 | + |
| 63 | + test "encodes decimal type with scientific notation", _context do |
| 64 | + value = Decimal.new("1E+3") |
| 65 | + attr = [precision: 8, scale: 4] |
| 66 | + |
| 67 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 68 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 69 | + |
| 70 | + assert byte_len == 5 |
| 71 | + assert sign == 1 |
| 72 | + assert <<232, 3, 0, 0>> = value_binary |
| 73 | + assert :binary.decode_unsigned(value_binary, :little) == 1000 |
| 74 | + end |
| 75 | + |
| 76 | + # Decimal.new("-1E+3") |
| 77 | + test "encodes negative decimal with scientific notation", _context do |
| 78 | + value = Decimal.new("-1E+3") |
| 79 | + attr = [precision: 8, scale: 4] |
| 80 | + |
| 81 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 82 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 83 | + |
| 84 | + assert byte_len == 5 |
| 85 | + assert sign == 0 |
| 86 | + assert <<232, 3, 0, 0>> = value_binary |
| 87 | + assert :binary.decode_unsigned(value_binary, :little) == 1000 |
| 88 | + end |
| 89 | + |
| 90 | + test "encodes decimal type with precision", _context do |
| 91 | + value = Decimal.new("1000.0000") |
| 92 | + attr = [precision: 8, scale: 4] |
| 93 | + |
| 94 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 95 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 96 | + |
| 97 | + assert byte_len == 5 |
| 98 | + assert sign == 1 |
| 99 | + assert <<128, 150, 152, 0>> = value_binary |
| 100 | + assert :binary.decode_unsigned(value_binary, :little) == 10_000_000 |
| 101 | + end |
| 102 | + |
| 103 | + test "encodes negative decimal", _context do |
| 104 | + value = Decimal.new("-1000.0000") |
| 105 | + attr = [precision: 8, scale: 4] |
| 106 | + |
| 107 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 108 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 109 | + |
| 110 | + assert byte_len == 5 |
| 111 | + assert sign == 0 |
| 112 | + assert <<128, 150, 152, 0>> = value_binary |
| 113 | + assert :binary.decode_unsigned(value_binary, :little) == 10_000_000 |
| 114 | + end |
| 115 | + |
| 116 | + test "encodes decimal type for 1000.1234", _context do |
| 117 | + value = Decimal.new("1000.1234") |
| 118 | + attr = [precision: 8, scale: 4] |
| 119 | + |
| 120 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 121 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 122 | + |
| 123 | + assert byte_len == 5 |
| 124 | + assert sign == 1 |
| 125 | + assert <<82, 155, 152, 0>> = value_binary |
| 126 | + assert :binary.decode_unsigned(value_binary, :little) == 10_001_234 |
| 127 | + end |
| 128 | + |
| 129 | + test "encodes very large decimal", _context do |
| 130 | + value = Decimal.new("9999999999.9999") |
| 131 | + attr = [precision: 14, scale: 4] |
| 132 | + |
| 133 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 134 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 135 | + |
| 136 | + assert byte_len == 9 |
| 137 | + assert sign == 1 |
| 138 | + assert :binary.decode_unsigned(value_binary, :little) == 99_999_999_999_999 |
| 139 | + end |
| 140 | + |
| 141 | + test "encodes very small decimal", _context do |
| 142 | + value = Decimal.new("0.0001") |
| 143 | + attr = [precision: 5, scale: 4] |
| 144 | + |
| 145 | + assert <<byte_len>> <> <<sign>> <> value_binary = |
| 146 | + Tds.Types.encode_data(@tds_data_type_decimaln, value, attr) |
| 147 | + |
| 148 | + assert byte_len == 5 |
| 149 | + assert sign == 1 |
| 150 | + assert :binary.decode_unsigned(value_binary, :little) == 1 |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + describe "inserting decimal values into the database" do |
| 155 | + setup :create_table |
| 156 | + |
| 157 | + @tag precision: 10, scale: 4 |
| 158 | + test "inserts various decimal values", context do |
| 159 | + assert insert_decimal(Decimal.new("1000"), context) == Decimal.new("1000.0000") |
| 160 | + assert insert_decimal(Decimal.new("1000.0000"), context) == Decimal.new("1000.0000") |
| 161 | + assert insert_decimal(Decimal.new("1000.1234"), context) == Decimal.new("1000.1234") |
| 162 | + assert insert_decimal(Decimal.new("-1000.0000"), context) == Decimal.new("-1000.0000") |
| 163 | + |
| 164 | + # Decimals can be scientific notation when converted from float: |
| 165 | + # iex> Decimal.from_float(1000.0) |
| 166 | + # Decimal.new("1E+3") |
| 167 | + assert insert_decimal(Decimal.new("1E+3"), context) == Decimal.new("1000.0000") |
| 168 | + assert insert_decimal(Decimal.new("-1E+3"), context) == Decimal.new("-1000.0000") |
| 169 | + end |
| 170 | + |
| 171 | + @tag precision: 5, scale: 2 |
| 172 | + test "decodes decimal type with precision 5 and scale 2", context do |
| 173 | + assert insert_decimal(Decimal.new("123.45"), context) == Decimal.new("123.45") |
| 174 | + assert insert_decimal(Decimal.new("-123.45"), context) == Decimal.new("-123.45") |
| 175 | + end |
| 176 | + |
| 177 | + @tag precision: 10, scale: 5 |
| 178 | + test "decodes decimal type to 99999.99999", context do |
| 179 | + assert insert_decimal(Decimal.new("99999.99999"), context) == Decimal.new("99999.99999") |
| 180 | + end |
| 181 | + |
| 182 | + @tag precision: 2, scale: 1 |
| 183 | + test "decodes decimal type to 9.9", context do |
| 184 | + assert insert_decimal(Decimal.new("9.9"), context) == Decimal.new("9.9") |
| 185 | + end |
| 186 | + |
| 187 | + @tag precision: 38, scale: 0 |
| 188 | + test "decodes to exact value with 0 scale", context do |
| 189 | + value = Decimal.new("99999999999999999999999999999999999999") |
| 190 | + assert insert_decimal(value, context) == value |
| 191 | + end |
| 192 | + |
| 193 | + @tag precision: 5, scale: 2 |
| 194 | + test "decodes to NULL", context do |
| 195 | + assert insert_decimal(nil, context) == nil |
| 196 | + end |
| 197 | + |
| 198 | + @tag precision: 5, scale: 2 |
| 199 | + test "rounds up fractional parts", context do |
| 200 | + assert insert_decimal(Decimal.new("123.456"), context) == Decimal.new("123.46") |
| 201 | + assert insert_decimal(Decimal.new("123.454"), context) == Decimal.new("123.45") |
| 202 | + assert insert_decimal(Decimal.new("-0.00001"), context) == Decimal.new("0.00") |
| 203 | + end |
| 204 | + |
| 205 | + @tag precision: 2, scale: 1, capture_log: true |
| 206 | + test "raises an error with truncated value (cannot round non-fractional parts)", context do |
| 207 | + value = Decimal.new("9.99") |
| 208 | + # %Tds.Error{message: nil, mssql: %{state: 8, number: 8115, line_number: |
| 209 | + # 1, msg_text: \"Arithmetic overflow error converting numeric to data type numeric.\", |
| 210 | + # server_name: \"04e0392f6c76\", class: 16, proc_name: \"\"}} |
| 211 | + message = ~r/Arithmetic overflow error converting numeric to data type numeric/ |
| 212 | + assert_raise MatchError, message, fn -> insert_decimal(value, context) end |
| 213 | + end |
| 214 | + |
| 215 | + # Maximum precision and scale for SQL Server |
| 216 | + # https://learn.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql?view=sql-server-ver16#remarks |
| 217 | + @tag precision: 38, scale: 18 |
| 218 | + test "inserts very large decimal", context do |
| 219 | + # 38 digits |
| 220 | + value = Decimal.new("99999999999999999999.999999999999999999") |
| 221 | + assert insert_decimal(value, context) == value |
| 222 | + end |
| 223 | + |
| 224 | + @tag precision: 38, scale: 18, capture_log: true |
| 225 | + test "raises an error with value larger than SQL Server maximum", context do |
| 226 | + # 39 digits |
| 227 | + value = Decimal.new("999999999999999999999.9999999999999999999") |
| 228 | + message = ~r/size \(39\) given to the type 'decimal' exceeds the maximum allowed \(38\)/ |
| 229 | + assert_raise(MatchError, message, fn -> insert_decimal(value, context) end) |
| 230 | + end |
| 231 | + |
| 232 | + test "inserts very small decimal", context do |
| 233 | + assert insert_decimal(Decimal.new("0.0001"), context) == Decimal.new("0.0001") |
| 234 | + end |
| 235 | + end |
| 236 | +end |
0 commit comments