Tests: enable json tests for MySQL

This commit is contained in:
Jared Beck 2021-04-09 00:54:18 -04:00
parent 4ef8a0bfed
commit 1bc511fccc
6 changed files with 23 additions and 6 deletions

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Fruit < ActiveRecord::Base
if ENV["DB"] == "postgres" || JsonVersion.table_exists?
if PaperTrail::TestEnv.json? || JsonVersion.table_exists?
has_paper_trail versions: { class_name: "JsonVersion" }
end
end

View File

@ -62,7 +62,7 @@ class SetUpTestTables < ::ActiveRecord::Migration::Current
t.timestamps null: true, limit: 6
end
if ENV["DB"] == "postgres"
if PaperTrail::TestEnv.json?
create_table :postgres_users, force: true do |t|
t.string :name
t.integer :post_ids, array: true
@ -127,7 +127,7 @@ class SetUpTestTables < ::ActiveRecord::Migration::Current
end
add_index :no_object_versions, %i[item_type item_id]
if ENV["DB"] == "postgres"
if PaperTrail::TestEnv.json?
create_table :json_versions, force: true do |t|
t.string :item_type, null: false
t.integer :item_id, null: false

View File

@ -2,7 +2,7 @@
require "spec_helper"
if ENV["DB"] == "postgres" || JsonVersion.table_exists?
if PaperTrail::TestEnv.json? || JsonVersion.table_exists?
RSpec.describe Fruit, type: :model, versioning: true do
describe "have_a_version_with_changes matcher" do
it "works with Fruit because Fruit uses JsonVersion" do

View File

@ -114,7 +114,7 @@ module PaperTrail
# way? We already have a `json_versions` table in our tests, maybe we
# could use that and add a `jsonb_versions` table?
column_overrides = [false]
if ENV["DB"] == "postgres"
if PaperTrail::TestEnv.json?
column_overrides += %w[json jsonb]
end

View File

@ -5,7 +5,7 @@ require "spec_helper"
module PaperTrail
module AttributeSerializers
::RSpec.describe ObjectAttribute do
if ENV["DB"] == "postgres"
if PaperTrail::TestEnv.json?
describe "postgres-specific column types" do
describe "#serialize" do
it "serializes a postgres array into a plain array" do

View File

@ -3,6 +3,23 @@
ENV["RAILS_ENV"] ||= "test"
ENV["DB"] ||= "sqlite"
module PaperTrail
module TestEnv
# Does the chosen database support json columns?
#
# - Postgres had json first.
# - MySQL added json support in 5.7.
# - MariaDB has only partial support, functions but not operators. PT
# currently uses operators. The devs consider the addition of operators to
# be a low priority feature. (https://jira.mariadb.org/browse/MDEV-13594) PT
# may be able to use functions like `json_extract` instead of operators like
# `->` but that work has not yet been done. PRs welcome.
def self.json?
%w[mysql postgres].include?(ENV["DB"])
end
end
end
require "byebug"
require_relative "support/pt_arel_helpers"