| 1002 | @testing.fixture |
| 1003 | def do_numeric_test(self, metadata, connection): |
| 1004 | def run(type_, input_, output, filter_=None, check_scale=False): |
| 1005 | t = Table("t", metadata, Column("x", type_)) |
| 1006 | t.create(connection) |
| 1007 | connection.execute(t.insert(), [{"x": x} for x in input_]) |
| 1008 | |
| 1009 | result = {row[0] for row in connection.execute(t.select())} |
| 1010 | output = set(output) |
| 1011 | if filter_: |
| 1012 | result = {filter_(x) for x in result} |
| 1013 | output = {filter_(x) for x in output} |
| 1014 | eq_(result, output) |
| 1015 | if check_scale: |
| 1016 | eq_([str(x) for x in result], [str(x) for x in output]) |
| 1017 | |
| 1018 | connection.execute(t.delete()) |
| 1019 | |
| 1020 | # test that this is actually a number! |
| 1021 | # note we have tiny scale here as we have tests with very |
| 1022 | # small scale Numeric types. PostgreSQL will raise an error |
| 1023 | # if you use values outside the available scale. |
| 1024 | if type_.asdecimal: |
| 1025 | test_value = decimal.Decimal("2.9") |
| 1026 | add_value = decimal.Decimal("37.12") |
| 1027 | else: |
| 1028 | test_value = 2.9 |
| 1029 | add_value = 37.12 |
| 1030 | |
| 1031 | connection.execute(t.insert(), {"x": test_value}) |
| 1032 | assert_we_are_a_number = connection.scalar( |
| 1033 | select(type_coerce(t.c.x + add_value, type_)) |
| 1034 | ) |
| 1035 | eq_( |
| 1036 | round(assert_we_are_a_number, 3), |
| 1037 | round(test_value + add_value, 3), |
| 1038 | ) |
| 1039 | |
| 1040 | return run |
| 1041 | |