Skip to content

Commit

Permalink
Add more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Apr 26, 2024
1 parent 394ae52 commit 47f5df4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ gemspec

gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem 'sqlite3', '~> 1.4'
gem "pry"
gem 'pg'
27 changes: 27 additions & 0 deletions spec/bemi/change_query_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
22 changes: 22 additions & 0 deletions spec/bemi/record_query_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions spec/fixtures/models.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 47f5df4

Please sign in to comment.