MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_cast

Method test_cast

test/sql/test_compiler.py:2912–3043  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2910 )
2911
2912 def test_cast(self):
2913 tbl = table(
2914 "casttest",
2915 column("id", Integer),
2916 column("v1", Float),
2917 column("v2", Float),
2918 column("ts", TIMESTAMP),
2919 )
2920
2921 def check_results(dialect, expected_results, literal):
2922 eq_(
2923 len(expected_results),
2924 5,
2925 "Incorrect number of expected results",
2926 )
2927 eq_(
2928 str(cast(tbl.c.v1, Numeric).compile(dialect=dialect)),
2929 "CAST(casttest.v1 AS %s)" % expected_results[0],
2930 )
2931 eq_(
2932 str(tbl.c.v1.cast(Numeric).compile(dialect=dialect)),
2933 "CAST(casttest.v1 AS %s)" % expected_results[0],
2934 )
2935 eq_(
2936 str(cast(tbl.c.v1, Numeric(12, 9)).compile(dialect=dialect)),
2937 "CAST(casttest.v1 AS %s)" % expected_results[1],
2938 )
2939 eq_(
2940 str(cast(tbl.c.ts, Date).compile(dialect=dialect)),
2941 "CAST(casttest.ts AS %s)" % expected_results[2],
2942 )
2943 eq_(
2944 str(cast(1234, Text).compile(dialect=dialect)),
2945 "CAST(%s AS %s)" % (literal, expected_results[3]),
2946 )
2947 eq_(
2948 str(cast("test", String(20)).compile(dialect=dialect)),
2949 "CAST(%s AS %s)" % (literal, expected_results[4]),
2950 )
2951
2952 # fixme: shoving all of this dialect-specific stuff in one test
2953 # is now officially completely ridiculous AND non-obviously omits
2954 # coverage on other dialects.
2955 sel = select(tbl, cast(tbl.c.v1, Numeric)).compile(dialect=dialect)
2956
2957 # TODO: another unusual result from disambiguate only:
2958 # v1__1 vs v1_1 are due to the special meaning
2959 # WrapsColumnExpression gives to the "_anon_name_label" attribute,
2960 # where it tries to default to a label name that matches that of
2961 # the column within.
2962
2963 if isinstance(dialect, type(mysql.dialect())):
2964 eq_(
2965 str(sel),
2966 "SELECT casttest.id, casttest.v1, casttest.v2, "
2967 "casttest.ts, "
2968 "CAST(casttest.v1 AS DECIMAL) AS v1__1 \n"
2969 "FROM casttest",

Callers

nothing calls this directly

Calls 8

tableFunction · 0.90
columnFunction · 0.90
castFunction · 0.90
textFunction · 0.90
nullFunction · 0.90
literal_columnFunction · 0.90
assert_compileMethod · 0.80
dialectMethod · 0.45

Tested by

no test coverage detected