Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have_db_column of_type can't compare types with activerecord-jdbc-adapter/rails 5 #944

Closed
wintersolutions opened this issue Jul 2, 2016 · 2 comments

Comments

@wintersolutions
Copy link

wintersolutions commented Jul 2, 2016

When I run the following spec in MRI everything works:

it { is_expected.to have_db_column(:example).of_type(:string) }

When I run it on JRuby 9.1.2 and with activerecord-jdbc-adapter (github: 'jruby/activerecord-jdbc-adapter', branch: 'rails-5') I get:

Expected Keyword to have db column named language_code of type string of limit 2 (FooBar has a db column named example of type #ActiveModel::Type::String:0x4c082a7d, not string.)

This seems to be the case for all types of attributes, not only strings. A possible workaround, i.e. comparing to ActiveModel::Type::String on JRuby, is not possible since were getting a instance instead of class when we call to_s i.e. ActiveModel::Type::String:0x4c082a7d.

That means shoulda-matchers will not be compatible with rails 5 on JRuby.

Relevant line:
https://github.com/thoughtbot/shoulda-matchers/blob/v3.1.1/lib/shoulda/matchers/active_record/have_db_column_matcher.rb#L152

@wintersolutions
Copy link
Author

wintersolutions commented Jul 2, 2016

Found similar issues with have_db_column(:example).with_options(limit: 42) and enum_for matchers. I wrote the following monkeypatch to get my specs running again:

if RUBY_PLATFORM == 'java'
  class Shoulda::Matchers::ActiveRecord::HaveDbColumnMatcher

    protected

    def correct_column_type?
      return true unless @options.key?(:column_type)

      if matched_column.type.class.to_s =~ /#{@options[:column_type].to_s}/i
        true
      else
        @missing = "#{model_class} has a db column named #{@column} " <<
            "of type #{matched_column.type.class.to_s} which did not match #{@options[:column_type]}."
        false
      end
    end

    def correct_limit?
      return true unless @options.key?(:limit)

      if matched_column.type.limit.to_s == @options[:limit].to_s
        true
      else
        @missing = "#{model_class} has a db column named #{@column} " <<
            "of limit #{matched_column.type.limit}, " <<
            "not #{@options[:limit]}."
        false
      end
    end
  end

  class Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher

    protected

    def column_type_is_integer?
      column.type.class.to_s =~ /Integer/
    end
  end
end

@mcmire
Copy link
Collaborator

mcmire commented Jul 25, 2017

@wintersolutions Unfortunately, we don't officially support JRuby and haven't in quite some time. I'm not sure when this will be addressed, but I will keep this here in the meantime. Sorry I can't give you more :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants