cast.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from textwrap import dedent
  10. from materialize.checks.actions import Testdrive
  11. from materialize.checks.checks import Check
  12. class Cast(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(
  15. dedent(
  16. """
  17. > CREATE TABLE cast_table (f1 INT);
  18. > INSERT INTO cast_table VALUES (0);
  19. """
  20. )
  21. )
  22. def manipulate(self) -> list[Testdrive]:
  23. return [
  24. Testdrive(dedent(s))
  25. for s in [
  26. """
  27. > CREATE MATERIALIZED VIEW cast_view1 AS SELECT f1::bool AS c1, f1::int AS c2, f1::float AS c3, f1::numeric AS c4, f1::real AS c5, f1::text AS c6, f1::uint2 AS c7, f1::uint4 AS c8, f1::uint8 AS c9, f1::text AS c10, cast(f1 AS bool) AS c11 FROM cast_table WHERE f1 >= 0;
  28. > INSERT INTO cast_table VALUES (1);
  29. """,
  30. """
  31. > CREATE MATERIALIZED VIEW cast_view2 AS SELECT f1::bool AS c1, f1::int AS c2, f1::float AS c3, f1::numeric AS c4, f1::real AS c5, f1::text AS c6, f1::text AS c7, cast(f1 AS bool) AS c8 FROM cast_table;
  32. > INSERT INTO cast_table VALUES (-1);
  33. """,
  34. ]
  35. ]
  36. def validate(self) -> Testdrive:
  37. return Testdrive(
  38. dedent(
  39. """
  40. > SELECT * FROM cast_view1;
  41. false 0 0 0 0 0 0 0 0 0 false
  42. true 1 1 1 1 1 1 1 1 1 true
  43. > SELECT * FROM cast_view2;
  44. false 0 0 0 0 0 0 false
  45. true 1 1 1 1 1 1 true
  46. true -1 -1 -1 -1 -1 -1 true
  47. """
  48. )
  49. )