enum.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. const { beforeAll2, expectsql, sequelize } = require('../../../support');
  3. const { DataTypes } = require('@sequelize/core');
  4. const { expect } = require('chai');
  5. const sql = sequelize.dialect.queryGenerator;
  6. describe('PostgresQueryGenerator', () => {
  7. if (sequelize.dialect.name !== 'postgres') {
  8. return;
  9. }
  10. const vars = beforeAll2(() => {
  11. const FooUser = sequelize.define(
  12. 'user',
  13. {
  14. mood: DataTypes.ENUM('happy', 'sad'),
  15. },
  16. {
  17. schema: 'foo',
  18. },
  19. );
  20. const PublicUser = sequelize.define('user', {
  21. mood: {
  22. type: DataTypes.ENUM('happy', 'sad'),
  23. field: 'theirMood',
  24. },
  25. });
  26. return { FooUser, PublicUser };
  27. });
  28. describe('pgEnumName', () => {
  29. it('does not add schema when options: { schema: false }', () => {
  30. const { FooUser, PublicUser } = vars;
  31. expect(sql.pgEnumName(PublicUser.table, 'mood', { schema: false })).to.equal(
  32. '"enum_users_mood"',
  33. );
  34. expect(sql.pgEnumName(FooUser.table, 'theirMood', { schema: false })).to.equal(
  35. '"enum_users_theirMood"',
  36. );
  37. });
  38. it('properly quotes both the schema and the enum name', () => {
  39. const { FooUser, PublicUser } = vars;
  40. expect(
  41. sql.pgEnumName(PublicUser.table, 'mood', PublicUser.getAttributes().mood.type),
  42. ).to.equal('"public"."enum_users_mood"');
  43. expect(
  44. sql.pgEnumName(FooUser.table, 'theirMood', FooUser.getAttributes().mood.type),
  45. ).to.equal('"foo"."enum_users_theirMood"');
  46. });
  47. });
  48. describe('pgEnum', () => {
  49. it('uses schema #3171', () => {
  50. const { FooUser } = vars;
  51. expectsql(sql.pgEnum(FooUser.table, 'mood', FooUser.getAttributes().mood.type), {
  52. postgres: `DO 'BEGIN CREATE TYPE "foo"."enum_users_mood" AS ENUM(''happy'', ''sad''); EXCEPTION WHEN duplicate_object THEN null; END';`,
  53. });
  54. });
  55. it('does add schema when public', () => {
  56. const { PublicUser } = vars;
  57. expectsql(sql.pgEnum(PublicUser.table, 'theirMood', PublicUser.getAttributes().mood.type), {
  58. postgres: `DO 'BEGIN CREATE TYPE "public"."enum_users_theirMood" AS ENUM(''happy'', ''sad''); EXCEPTION WHEN duplicate_object THEN null; END';`,
  59. });
  60. });
  61. });
  62. describe('pgEnumAdd', () => {
  63. it('creates alter type with exists', () => {
  64. const { PublicUser } = vars;
  65. expectsql(sql.pgEnumAdd(PublicUser.table, 'mood', 'neutral', { after: 'happy' }), {
  66. postgres:
  67. 'ALTER TYPE "public"."enum_users_mood" ADD VALUE IF NOT EXISTS \'neutral\' AFTER \'happy\'',
  68. });
  69. });
  70. });
  71. describe('pgListEnums', () => {
  72. it('works with schema #3563', () => {
  73. const { FooUser } = vars;
  74. expectsql(sql.pgListEnums(FooUser.table, 'mood'), {
  75. postgres: `SELECT t.typname enum_name, array_agg(e.enumlabel ORDER BY enumsortorder) enum_value
  76. FROM pg_type t
  77. JOIN pg_enum e ON t.oid = e.enumtypid
  78. JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
  79. WHERE n.nspname = 'foo'
  80. AND t.typname='enum_users_mood'
  81. GROUP BY 1`,
  82. });
  83. });
  84. it('uses the default schema if no options given', () => {
  85. expectsql(sql.pgListEnums(), {
  86. postgres: `SELECT t.typname enum_name, array_agg(e.enumlabel ORDER BY enumsortorder) enum_value
  87. FROM pg_type t
  88. JOIN pg_enum e ON t.oid = e.enumtypid
  89. JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
  90. WHERE n.nspname = 'public'
  91. GROUP BY 1`,
  92. });
  93. });
  94. it('is not vulnerable to sql injection', () => {
  95. expectsql(sql.pgListEnums({ tableName: `ta'"ble`, schema: `sche'"ma` }, `attri'"bute`), {
  96. postgres: `SELECT t.typname enum_name, array_agg(e.enumlabel ORDER BY enumsortorder) enum_value
  97. FROM pg_type t
  98. JOIN pg_enum e ON t.oid = e.enumtypid
  99. JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
  100. WHERE n.nspname = 'sche''"ma'
  101. AND t.typname='enum_ta''"ble_attri''"bute'
  102. GROUP BY 1`,
  103. });
  104. });
  105. });
  106. });