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

Update specs to use modern style #321

Merged
merged 5 commits into from
Mar 17, 2017

Conversation

dshvimer
Copy link
Contributor

Updated the specs to reflect a cleaner style of rspec testing. For info
about spec style visit http://betterspecs.org/

David Shvimer added 2 commits March 14, 2017 22:27
Updated the specs to reflect a cleaner style of rspec testing. For info
about spec style visit http://betterspecs.org/
@dshvimer
Copy link
Contributor Author

dshvimer commented Mar 15, 2017

Currently having an issue with Rubocop:

Metrics/BlockLength: Block has too many lines.

Rubocop is complaining about the length of the Rspec.describe block. I can't imagine being able to reduce it's size. Just figured I'd would mention that.

Otherwise this file is ready for review. Let me know if the style is suitable and I will proceed to work on more specs.

@dshvimer dshvimer mentioned this pull request Mar 15, 2017
Copy link
Collaborator

@zverok zverok left a comment

Choose a reason for hiding this comment

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

Good start! 👍

Daru::IO::SqlDataSource.make_dataframe(Object.new, query)
}.to raise_error(ArgumentError)
end
subject(:df) { Daru::IO::SqlDataSource.make_dataframe(active_record_connection, query) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be written once, at the very beginning, as subject(:df) { described_class.make_dataframe(source, query) },
and then you just let(:source) to different values in different contexts.

end
subject(:df) { Daru::IO::SqlDataSource.make_dataframe(active_record_connection, query) }
it { is_expected.to be_a(Daru::DataFrame) }
it { expect(df.nrows).to eq 2 }
Copy link
Collaborator

@zverok zverok Mar 15, 2017

Choose a reason for hiding this comment

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

its(:nrows) { is_expected.to eq 2 }

it { is_expected.to be_a(Daru::DataFrame) }
it { expect(df.nrows).to eq 2 }
it { expect(df.row[0][:id]).to eq 1 }
it { expect(df.row[0][:name]).to eq 'Homer' }
Copy link
Collaborator

Choose a reason for hiding this comment

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

it { expect(df.row[0]).to have_attributes ...
Better communicates that we are testing the same object (first row).

it {
expect {
Daru::IO::SqlDataSource.make_dataframe(Object.new, query)
}.to raise_error(ArgumentError)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you'd follow my suggestion about common subject, this context could be turned into

let(:source) { Object.new }
it { expect { subject }.to raise_error(ArgumentError) }

And I'd suggest to check error message too.

@zverok
Copy link
Collaborator

zverok commented Mar 15, 2017

Rubocop is complaining about the length of the Rspec.describe block. I can't imagine being able to reduce it's size. Just figured I'd would mention that.

And yes, block length limitation has no value for specs. It could be disabled in local specs/.rubocop.yml

To keep the spec dry, we reuse the subject and set a new source or query
in every context
@dshvimer
Copy link
Contributor Author

dshvimer commented Mar 16, 2017

@zverok I made the requested changes. I had an issue testing the :name property.

df.row[0].name #returns 0
df.row[0][:name] #returns "Homer"

It seems that the have_attributes method tests the first variation. I opted to test the :age column instead.

Copy link
Collaborator

@zverok zverok left a comment

Choose a reason for hiding this comment

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

Some small-ish things left.

@@ -14,70 +14,58 @@
ActiveRecord::Base.connection
end

let(:dat_file) do
'spec/fixtures/bank2.dat'
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is used only in one context -- therefore should be initialized there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

And same about all others (dbi_handle, active_record_connection). In fact, you can just do in an appropriate context:

let(:source) { DBI.connect("DBI:SQLite3:#{db_name}") }

...and that's it.

let(:source) { dbi_handle }
it { is_expected.to be_a(Daru::DataFrame) }
it { expect(df.row[0]).to have_attributes(id: 1) }
it { expect(df.row[0]).to have_attributes(age: 20) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Join those two: it { expect(df.row[0]).to have_attributes(id: 1, age: 20) }, it is logically just one test "we have right row here".

@dshvimer
Copy link
Contributor Author

@zverok Thank you for the feedback. I moved the declarations into the contexts. The ActiveRecord connection is the default source since it is used twice. So far we have gone from 83 lines to 56 .

Copy link
Collaborator

@zverok zverok left a comment

Choose a reason for hiding this comment

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

+27 −54 The best kind of PRs :)
Thank you!

@zverok zverok merged commit 46b0362 into SciRuby:master Mar 17, 2017
@dshvimer dshvimer deleted the enahncement/specs-style-sqldatasource branch March 17, 2017 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants