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

[webui][ci] Initial tests for model Comment #2247

Merged
merged 1 commit into from Nov 14, 2016

Conversation

eduardoj
Copy link
Member

No description provided.

@eduardoj eduardoj added the Frontend Things related to the OBS RoR app label Oct 20, 2016
@eduardoj eduardoj force-pushed the tests_for_model_comment branch 5 times, most recently from 12bfff6 to 559d3ae Compare October 21, 2016 07:33
@Ana06
Copy link
Member

Ana06 commented Oct 21, 2016

I think that the commit message is not very clear. If what you meant is that you haven't tested all the method what about something like

[ci] Create test for Comment model

Not all the methods of the model have been tested.

Also, does the [webui] tag make sense here?

@Ana06
Copy link
Member

Ana06 commented Oct 21, 2016

You could also test the belong_to and has_many relations. For example for the first one:

it { should belong_to(:bs_request).inverse_of(:comments) }

let!(:comment) { create(:comment) }

describe "A comment" do
it { expect(comment).to be_valid }
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to check if an object that has already been created is valid?

I think it should be like:

it 'has a valid factory' do
  expect(build(:comment)).to be_valid
end

end

describe "blank_or_destroy" do
context "without children" do
Copy link
Member

Choose a reason for hiding this comment

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

Why hasn't you tested the with children case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

end

describe "to_xml" do
context "without parents" do
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't it be without parent? in singular I mean.

Copy link
Member

Choose a reason for hiding this comment

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

Do we really need a context for that? I would directly test the with parent case. We could also keep both cases, with and without parent.

}

context "returns xml" do
it { expect(builder.class.name).to eq 'Nokogiri::XML::Builder' }
Copy link
Member

Choose a reason for hiding this comment

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

why do we want to test that?


describe "to_xml" do
context "without parents" do
let(:builder) {
Copy link
Member

Choose a reason for hiding this comment

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

this let is strange. Why not just

let(:builder) { Nokogiri::XML::Builder.new }
let(:comment_to_xml) { comment.to_xml(builder) }

And then testing that the content of the comment is what is expected

it { expect(builder.class.name).to eq 'Nokogiri::XML::Builder' }

context "comment element" do
let(:comment_element) { builder.doc.css('comment') }
Copy link
Member

Choose a reason for hiding this comment

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

why do you need that?

FactoryGirl.define do
factory :comment do
body { Faker::Lorem.paragraph }
type { Faker::Lorem.word }
Copy link
Member

@Ana06 Ana06 Oct 21, 2016

Choose a reason for hiding this comment

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

You shouldn't use a random word as a type as it is reserved for storing the the inheritance class. Because of the same reason and taking into account that it is not possible to create a commentwithout type (because of the presence validation) it doesn't make sense to test comment. You should test the comment children. I think that for some function it is enough with testing it for one child (for example comment_package), although for some of them it may be needed to test it for more.

Also, I added a validation for that in #2252 and this should fail in the tests after it is merged (it already fails outside the tests).

require "rails_helper"

RSpec.describe Comment do
let!(:comment) { create(:comment) }
Copy link
Member

Choose a reason for hiding this comment

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

You don't need the !

In case that you need the object to be created (like happens in the blank_or_destroy test) you can do

  describe "blank_or_destroy" do
    before do
      comment
    end

That way you don't create it for all tests.

@eduardoj eduardoj force-pushed the tests_for_model_comment branch 3 times, most recently from a3ebf71 to 6cdbe43 Compare October 28, 2016 08:40
@hennevogel
Copy link
Member

@eduardoj what's the status of this one?

@eduardoj
Copy link
Member Author

eduardoj commented Nov 3, 2016

@hennevogel: @Ana06, @bgeuken and me have been working on it, and still are. We are facing problems when we execute the whole test suite, but not when we execute the test alone. We have created a branch with some changes: https://github.com/eduardoj/open-build-service/tree/test-comment

@Ana06
Copy link
Member

Ana06 commented Nov 3, 2016

Yes, how the database is cleaned after every feature is wrong as it is deleting the users in the seeds and this needs to be solved before writing this test. 😉

@eduardoj eduardoj added the DO NOT MERGE ⚠️ Explain yourself if you add/remove this label to a PR label Nov 3, 2016
@hennevogel
Copy link
Member

So I guess you should rebase now :-)

require "rails_helper"

RSpec.shared_examples "a comment" do
let(:comment_type) { described_class.name.underscore }
Copy link
Member

Choose a reason for hiding this comment

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

what about calling it comment_factory? It is the name of the factory what we get here described_class.name.underscore, so it may be more clear.

end

describe "to_xml" do
context "without parent" do
Copy link
Member

Choose a reason for hiding this comment

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

As I said:

Do we really need a context for that? I would directly test the with parent case. We could also keep both cases, with and without parent.

I would keep either both cases or the with parent case that is more general, but not only the without parent .case

create(comment_type, parent: comment)
end

it 'should be destroyed' do
Copy link
Member

Choose a reason for hiding this comment

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

This should be it 'shouldn't be destroyed' do


it 'should be destroyed' do
expect { comment.blank_or_destroy }.to_not change { Comment.count }

Copy link
Member

Choose a reason for hiding this comment

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

why is it this blank line here?

expect { comment.blank_or_destroy }.to_not change { Comment.count }

expect(comment.body).to eq 'This comment has been deleted'
expect(comment.user).to eq User.find_nobody!
Copy link
Member

Choose a reason for hiding this comment

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

I would test check the user login instead of the whole User to ensure that the find_nobody method is working properly:

expect(comment.user.login).to eq '_nobody_'


describe "to_xml" do
context "without parent" do
let(:builder) {
Copy link
Member

Choose a reason for hiding this comment

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

This is still strange. Why not doing

let(:builder) { builder = Nokogiri::XML::Builder.new }

and then

before do
  comment.to_xml(builder)
end

that should be enough

let(:comment_element) { builder.doc.css('comment') }

it "comment attributes and content" do
expect(comment_element.attribute('id').value).to match(/^\d+$/)
Copy link
Member

Choose a reason for hiding this comment

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

why don't you check the exactly value?

it { is_expected.to belong_to(:package).inverse_of(:comments) }
it { is_expected.to belong_to(:user).inverse_of(:comments) }

it { is_expected.to have_many(:children) }
Copy link
Member

Choose a reason for hiding this comment

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

why not

it { is_expected.to have_many(:children).dependent(:destroy).class_name('Comment').with_foreign_key('parent_id') }

http://www.rubydoc.info/github/thoughtbot/shoulda-matchers/Shoulda%2FMatchers%2FActiveRecord%3Ahave_many

@Ana06
Copy link
Member

Ana06 commented Nov 4, 2016

Please delete the last commit, shouldnt be there

eduardoj pushed a commit to eduardoj/open-build-service that referenced this pull request Nov 4, 2016
We are catching the nobody user in the User#find_nobody method and it is
only used here. It is confusing and  the Rails query cache will cache
this too, so we will only do one more query per thread.

Also, the openSUSE#2247 PR is
failing because of this way of caching users and how the database is
cleaned, and it was difficult to get to know what the error was.
@Ana06
Copy link
Member

Ana06 commented Nov 4, 2016

Squash 8b5002a into 28d1740 😉

@eduardoj eduardoj removed the DO NOT MERGE ⚠️ Explain yourself if you add/remove this label to a PR label Nov 4, 2016

it "comment attributes and content" do
expect(comment_element.attribute('id').value).to eq(comment.id.to_s)
expect(comment_element.attribute('when').value).to match(/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d UTC$/)
Copy link
Member

Choose a reason for hiding this comment

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

why don't you check the exactly value?

it "comment attributes and content" do
expect(comment_element.attribute('id').value).to eq(comment.id.to_s)
expect(comment_element.attribute('when').value).to match(/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d UTC$/)
expect(comment_element.attribute('who').value).to match(/^user_\d+$/)
Copy link
Member

Choose a reason for hiding this comment

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

why don't you check the exactly value?

expect(comment_element.attribute('when').value).to match(/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d UTC$/)
expect(comment_element.attribute('who').value).to match(/^user_\d+$/)

expect(comment_element.text).to match(/^#<Nokogiri::XML::Builder::NodeBuilder:0x\h+>$/)
Copy link
Member

Choose a reason for hiding this comment

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

why don't you check the exactly value?

end
end

context "with parent" do
Copy link
Member

Choose a reason for hiding this comment

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

This tests is exactly the same that the one above but with one more attribute. Why have you left it empty? I would prefer to have the with parent case as it is "more general".

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@eduardoj eduardoj force-pushed the tests_for_model_comment branch 3 times, most recently from e42d28b to 458eef8 Compare November 9, 2016 09:55

describe "to_xml" do
context "without parent" do
let(:builder) { builder = Nokogiri::XML::Builder.new }
Copy link
Member

Choose a reason for hiding this comment

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

This variable is defined several times, so you could put it up and define it only once.

@@ -60,8 +60,8 @@ def to_xml(builder)
attrs = { who: user, when: created_at, id: id }
attrs[:parent] = parent_id if parent_id

builder.comment_(attrs) do
builder.text(body)
builder.comment_(attrs) do |xml|
Copy link
Member

Choose a reason for hiding this comment

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

Why did you change that?

let(:comment_element) { builder.doc.css('comment') }

it "comment attributes and content" do
expect(comment_element.attribute('id').value).to eq(comment.id.to_s)
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it possible to do that using the result of the .to_xml ?


context "returns xml" do
context "comment element" do
let(:comment_element) { builder.doc.css('comment') }
Copy link
Member

Choose a reason for hiding this comment

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

let(:comment_element) { builder.doc.css('comment') } is also several time, please move it up so you don't need to have it twice.

it { is_expected.to validate_presence_of(:user) }
end

describe "to_xml" do
Copy link
Member

Choose a reason for hiding this comment

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

I am not pretty sure if there is a better way to test this function. @bgeuken can you take a look?

Copy link
Member

Choose a reason for hiding this comment

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

I am also not sure. Let's not invest more time into this without changing the to_xml implenentation for comments.

end

describe "to_xml" do
let(:builder) { builder = Nokogiri::XML::Builder.new }
Copy link
Member

Choose a reason for hiding this comment

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

The assignment is not necessary, is it?

create(comment_factory, parent: comment)
end

it 'shouldn\'t be destroyed' do
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer double quotes instead of escaping the single quote!
"shouldn't be destroyed"

@ChrisBr
Copy link
Member

ChrisBr commented Nov 14, 2016

@eduardoj Is this still dependent on #2297 or ready to merge?

@Ana06
Copy link
Member

Ana06 commented Nov 14, 2016

@ChrisBr it doesn't depends on #2297

Not all the methods of the model have been tested.
@bgeuken
Copy link
Member

bgeuken commented Nov 14, 2016

Looks good. Let's ship this!

@bgeuken bgeuken merged commit d906c10 into openSUSE:master Nov 14, 2016
@eduardoj eduardoj deleted the tests_for_model_comment branch March 23, 2017 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frontend Things related to the OBS RoR app
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants