string.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 String(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(
  15. dedent(
  16. """
  17. > CREATE TABLE string_table (f1 STRING, f2 STRING, f3 STRING, f4 INT, f5 INT, f6 INT[]);
  18. > INSERT INTO string_table VALUES (' foobar ', ' abc ', ' xyz ', 2, 3, '{1,NULL,3}');
  19. """
  20. )
  21. )
  22. def manipulate(self) -> list[Testdrive]:
  23. return [
  24. Testdrive(dedent(s))
  25. for s in [
  26. """
  27. > CREATE MATERIALIZED VIEW string_view1 AS SELECT
  28. f1 BETWEEN f2 AND f3 AS c1,
  29. 'foo' BETWEEN 'abc' AND 'xyz' AS c2,
  30. f1 NOT BETWEEN f2 AND f3 AS c3,
  31. substring(f1 FROM f4 FOR f5) AS c4,
  32. substring(f1 FROM f4) AS c5,
  33. substring(f1 FOR f5) AS c6,
  34. trim(BOTH f1) AS c7,
  35. trim(LEADING f1) AS c8,
  36. trim(TRAILING f1) AS c9,
  37. trim(LEADING ' f' FROM f1) AS c10,
  38. trim(' fr' FROM f1) AS c11,
  39. array_to_string(f6, ',', 'NULL') AS c12
  40. FROM string_table;
  41. > INSERT INTO string_table VALUES (' foo ', ' abc ', ' xyz ', 2, 3, '{1,2,3,4,NULL,NULL}');
  42. """,
  43. """
  44. > CREATE MATERIALIZED VIEW string_view2 AS SELECT
  45. f1 BETWEEN f2 AND f3 AS c1,
  46. 'foo' BETWEEN 'abc' AND 'xyz' AS c2,
  47. f1 NOT BETWEEN f2 AND f3 AS c3,
  48. substring(f1 FROM f4 FOR f5) AS c4,
  49. substring(f1 FROM f4) AS c5,
  50. substring(f1 FOR f5) AS c6,
  51. trim(BOTH f1) AS c7,
  52. trim(LEADING f1) AS c8,
  53. trim(TRAILING f1) AS c9,
  54. trim(LEADING ' f' FROM f1) AS c10,
  55. trim(' fr' FROM f1) AS c11,
  56. array_to_string(f6, ',', 'NULL') AS c12
  57. FROM string_table;
  58. > INSERT INTO string_table VALUES (' bar ', 'abc', 'xyz', 2, 3, '{NULL}');
  59. """,
  60. ]
  61. ]
  62. def validate(self) -> Testdrive:
  63. return Testdrive(
  64. dedent(
  65. """
  66. > SELECT * FROM string_view1;
  67. true true false foo "foobar " " fo" "foobar" "foobar " " foobar" "oobar " "ooba" 1,NULL,3
  68. true true false foo "foo " " fo" foo "foo " " foo" "oo " oo 1,2,3,4,NULL,NULL
  69. false true true bar "bar " " ba" bar "bar " " bar" "bar " ba NULL
  70. > SELECT * FROM string_view2;
  71. true true false foo "foobar " " fo" "foobar" "foobar " " foobar" "oobar " "ooba" 1,NULL,3
  72. true true false foo "foo " " fo" foo "foo " " foo" "oo " oo 1,2,3,4,NULL,NULL
  73. false true true bar "bar " " ba" bar "bar " " bar" "bar " ba NULL
  74. """
  75. )
  76. )