From 47f5df40b4322b088fd2a4b68c8d9c02bbb4e083 Mon Sep 17 00:00:00 2001 From: exAspArk Date: Fri, 26 Apr 2024 14:24:33 -0400 Subject: [PATCH] Add more specs --- Gemfile | 1 + spec/bemi/change_query_helpers_spec.rb | 27 +++++++++++++++++++ spec/{bemi_spec.rb => bemi/context_spec.rb} | 0 spec/bemi/record_query_helpers_spec.rb | 22 ++++++++++++++++ spec/fixtures/models.rb | 29 +++++++++++++++++++++ spec/spec_helper.rb | 2 ++ 6 files changed, 81 insertions(+) create mode 100644 spec/bemi/change_query_helpers_spec.rb rename spec/{bemi_spec.rb => bemi/context_spec.rb} (100%) create mode 100644 spec/bemi/record_query_helpers_spec.rb create mode 100644 spec/fixtures/models.rb diff --git a/Gemfile b/Gemfile index dfda18c..58b5537 100644 --- a/Gemfile +++ b/Gemfile @@ -7,5 +7,6 @@ gemspec gem "rake", "~> 13.0" gem "rspec", "~> 3.0" +gem 'sqlite3', '~> 1.4' gem "pry" gem 'pg' diff --git a/spec/bemi/change_query_helpers_spec.rb b/spec/bemi/change_query_helpers_spec.rb new file mode 100644 index 0000000..2938086 --- /dev/null +++ b/spec/bemi/change_query_helpers_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +RSpec.describe Bemi::ChangeQueryHelpers do + after { BemiChange.delete_all } + + describe '#diff' do + it 'returns the diff between before and after' do + change = BemiChange.new(before: { name: 'Alice' }, after: { name: 'Bob' }) + expect(change.diff).to eq({ 'name' => ['Alice', 'Bob'] }) + end + end + + describe '.created' do + it 'returns changes with CREATE operation' do + change = BemiChange.create!(operation: 'CREATE') + expect(BemiChange.created).to include(change) + end + end + + describe '.asc' do + it 'returns changes in ascending order' do + change1 = BemiChange.create!(committed_at: Time.current) + change2 = BemiChange.create!(committed_at: 1.day.ago) + expect(BemiChange.asc).to eq([change2, change1]) + end + end +end diff --git a/spec/bemi_spec.rb b/spec/bemi/context_spec.rb similarity index 100% rename from spec/bemi_spec.rb rename to spec/bemi/context_spec.rb diff --git a/spec/bemi/record_query_helpers_spec.rb b/spec/bemi/record_query_helpers_spec.rb new file mode 100644 index 0000000..d4d3eaa --- /dev/null +++ b/spec/bemi/record_query_helpers_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +RSpec.describe Bemi::RecordQueryHelpers do + after { BemiChange.delete_all } + + describe '#bemi_changes' do + it 'returns the changes for the record' do + todo = Todo.create!(task: 'Buy milk', completed: false) + change = BemiChange.create!( + table: 'todos', + primary_key: todo.id, + committed_at: Time.current, + operation: 'CREATE', + before: {}, + after: { task: 'Buy milk', completed: true }, + context: { user_id: 1 }, + ) + + expect(todo.bemi_changes).to eq([change]) + end + end +end diff --git a/spec/fixtures/models.rb b/spec/fixtures/models.rb new file mode 100644 index 0000000..037c39d --- /dev/null +++ b/spec/fixtures/models.rb @@ -0,0 +1,29 @@ +require 'sqlite3' + +ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ':memory:') +ActiveRecord::Schema.define do + create_table :todos, force: true do |t| + t.string :task + t.boolean :completed + end + + create_table :changes, force: true do |t| + t.string :table + t.string :primary_key + t.datetime :committed_at + t.string :operation + t.json :before + t.json :after + t.json :context + end +end + +class BemiChange < ActiveRecord::Base + include Bemi::ChangeQueryHelpers + self.table_name = 'changes' +end + +class Todo < ActiveRecord::Base + include Bemi::RecordQueryHelpers + bemi_change_class 'BemiChange' +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 214eb10..a478156 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,8 @@ require "bemi-rails" require "active_record/connection_adapters/postgresql_adapter" +require_relative "fixtures/models" + RSpec.configure do |config| # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status"