diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6a0abc4..8ea24db 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,7 +3,7 @@ name: Ruby on: push: branches: - - master + - ci pull_request: diff --git a/Gemfile b/Gemfile index f9c9825..dfda18c 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,6 @@ source "https://rubygems.org" gemspec gem "rake", "~> 13.0" - gem "rspec", "~> 3.0" +gem "pry" +gem 'pg' diff --git a/spec/bemi_spec.rb b/spec/bemi_spec.rb index 8adcda6..20fb2fb 100644 --- a/spec/bemi_spec.rb +++ b/spec/bemi_spec.rb @@ -1,7 +1,35 @@ # frozen_string_literal: true RSpec.describe Bemi do - it "has a version number" do - expect(Bemi::VERSION).not_to eq(nil) + describe '.set_context' do + it 'sets the context' do + Bemi.set_context('test1') + expect(Bemi.context).to eq('test1') + + Bemi.set_context('test2') + expect(Bemi.context).to eq('test2') + end + end + + describe '.append_context' do + it 'appends the context to write SQL queries' do + Bemi.set_context({ foo: 'bar' }) + sql = "INSERT INTO todos (task) VALUES ('Eat')" + adapter = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new({}) + + result = Bemi.append_context.call(sql, adapter) + + expect(result).to eq("INSERT INTO todos (task) VALUES ('Eat') /*Bemi {\"foo\":\"bar\"} Bemi*/") + end + + it 'does not append the context to read SQL queries' do + Bemi.set_context({ foo: 'bar' }) + sql = "SELECT * FROM todos" + adapter = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new({}) + + result = Bemi.append_context.call(sql, adapter) + + expect(result).to eq("SELECT * FROM todos") + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f574650..214eb10 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "bemi-rails" +require "active_record/connection_adapters/postgresql_adapter" RSpec.configure do |config| # Enable flags like --only-failures and --next-failure