Created
October 4, 2021 12:02
-
-
Save nashbridges/764f5da35d1a4f13eb98fccf647ef8b2 to your computer and use it in GitHub Desktop.
Mobility + JSONB bug
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
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "rails", "6.1.4.1" | |
gem "pg", "1.2.3" | |
gem "mobility", "1.2.2" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
require "mobility" | |
Mobility.configure do | |
plugins do | |
active_record | |
end | |
end | |
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "mobility_test") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :properties, force: true do |t| | |
t.jsonb :name_i18n, default: {}, null: false | |
t.jsonb :long_name_i18n | |
end | |
end | |
class PropertyWithoutMobility < ActiveRecord::Base | |
self.table_name = "properties" | |
end | |
class PropertyWithMobility < ActiveRecord::Base | |
self.table_name = "properties" | |
extend Mobility | |
translates :name, backend: :jsonb, column_suffix: "_i18n" | |
translates :long_name, backend: :jsonb, column_suffix: "_i18n" | |
end | |
class BugTest < Minitest::Test | |
def test_without_mobility_reading_from_not_null_column | |
property = PropertyWithoutMobility.create! | |
property.reload | |
assert_equal({}, property.name_i18n) | |
assert_equal({}, property.changes) | |
end | |
def test_without_mobility_reading_from_null_column | |
property = PropertyWithoutMobility.create! | |
property.reload | |
assert_nil property.long_name_i18n | |
assert_equal({}, property.changes) | |
end | |
def test_with_mobility_reading_from_not_null_column | |
property = PropertyWithMobility.create! | |
property.reload | |
assert_equal({}, property.name_i18n) | |
assert_equal({}, property.changes) | |
end | |
def test_with_mobility_reading_from_null_column | |
property = PropertyWithMobility.create! | |
property.reload | |
assert_nil property.long_name_i18n | |
assert_equal({}, property.changes) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment