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

Tech 4739 add unit test framework support #9

Merged
merged 38 commits into from
Sep 16, 2020

Conversation

dcaddell
Copy link

@dcaddell dcaddell commented Sep 10, 2020

1.0.0

Added

  • A Rspec test suit
  • Dummy Rails app to be used by tests
  • Test coverage reports via Coveralls/SimpleCov
  • A coverage report badge to the README
  • Unit tests being run on push with TravisCI
  • Appraisal to test Rails 4.2, 5.2, and 6.0
  • A dependency on the mysql2 gem

Changed

  • Existing tests to be run with Rspec

Removed

  • Support for the mysql gem
  • Support for Rails < 4.2

@@ -1,4 +1,17 @@
#!/usr/bin/env rake
# frozen_string_literal: true
# Add your own tasks in files placed in lib/tasks ending in .rake,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the above 2 deleted lines should stay up there.
Oh, but now I'm guessing this all came from the Rails generator? It's not worth changing then.

module Enum

class << self
def current_mysql_column_adapter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it actually helpful for testing having this in an eigenclass method like this? I was fine with the earlier version that did this part inline to initialize ActiveRecordColumnWithEnums.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I liked the way the tests were written with these eigenclass methods.

expect { ActiveRecord::Mysql::Enum.current_mysql_column_adapter }.to raise_error(RuntimeError, "could not find MySQL::Column or equivalent connection adapter")

when Symbol
value
when String
value.to_sym if value.present?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd that other values just fall out as nil. I think it would be a bug if that happened, except perhaps for nil itself. I wonder if this should have

case nil
  nil
else
  raise ArgumentError, "expected Symbol, String, or nil (got #{value.inspect})"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I hadn't thought about it before - I was just getting rid of the redundant else nil case. Do you know offhand if the non-Enum column types raise rather than falling out as nil? I'll look it up shortly, but I think this should be consistent.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was only suggesting case nil so you could else raise on surprise values. That's the important part: this code would seem out of control if, say, you passed in a Hash and it returned nil.

if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
ActiveRecord::ConnectionAdapters::Mysql2Adapter
else
raise "Could not find MySQL connection adapter"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because raise if flow-of-control, you don't need it in an else. You can always flip it:

defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) or raise "Could not find MySQL connection adapter"
ActiveRecord::ConnectionAdapters::Mysql2Adapter

I'm not sure this needs to be in a method either. It seems like it would be fine below. Especially since it doesn't really have any 'current' logic like the name implies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll drop the "current" bit and rename it as just mysql_adapter and I'll make the same change for the current_mysql_column_adapter above. I'll also flip the guard clause to the top here, good point.

Same as with the column adapter module, I like having this method because it makes testing nice so I'd prefer to leave it.

context "#type_to_sql" do
let(:expected_enum_sql_type) { "enum('a','b','c')" }

if Rails::VERSION::MAJOR < 5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be more DRY? These tests seem about the same. Could you encapsulate the differences in a function or two that did the Rails::VERSION::MAJOR < 5 test themselves?

expect(subject.default).to eq(column_options[:default])
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like your editor isn't set to put trailing newlines in the files?

Copy link

@ColinDKelley ColinDKelley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I have a slight leaning toward the else raise but I don't feel that strongly about it.

@dcaddell dcaddell merged commit 19846f8 into master Sep 16, 2020
@dcaddell dcaddell deleted the TECH-4739_add_unit_test_framework_support branch September 16, 2020 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants