-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ gemspec | |
|
||
gem "rake", "~> 13.0" | ||
gem "rspec", "~> 3.0" | ||
gem 'sqlite3', '~> 1.4' | ||
gem "pry" | ||
gem 'pg' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters