Sha256: 4bb6abffbcfc712f5210936c53216590c8b30e607d8d6d4f4ae40667d8f094c9

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require "spec_helper"

RSpec.describe Fx::Adapters::Postgres::Triggers, :db do
  describe ".all" do
    it "returns `Trigger` objects" do
      connection = ActiveRecord::Base.connection
      connection.execute <<~EOS
        CREATE TABLE users (
            id int PRIMARY KEY,
            name varchar(256),
            upper_name varchar(256)
        );
      EOS
      connection.execute <<~EOS
        CREATE OR REPLACE FUNCTION uppercase_users_name()
        RETURNS trigger AS $$
        BEGIN
          NEW.upper_name = UPPER(NEW.name);
          RETURN NEW;
        END;
        $$ LANGUAGE plpgsql;
      EOS
      connection.execute <<~EOS
        CREATE TRIGGER uppercase_users_name
            BEFORE INSERT ON users
            FOR EACH ROW
            EXECUTE FUNCTION uppercase_users_name();
      EOS

      triggers = Fx::Adapters::Postgres::Triggers.new(connection).all

      first = triggers.first
      expect(triggers.size).to eq(1)
      expect(first.name).to eq("uppercase_users_name")
      expect(first.definition).to include("BEFORE INSERT")
      expect(first.definition).to match(/ON [public.ser|]/)
      expect(first.definition).to include("FOR EACH ROW")
      expect(first.definition).to include("EXECUTE FUNCTION uppercase_users_name()")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fx-0.9.0 spec/fx/adapters/postgres/triggers_spec.rb