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
Conversation
12bfff6
to
559d3ae
Compare
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
Also, does the [webui] tag make sense here? |
559d3ae
to
b061095
Compare
You could also test the
|
let!(:comment) { create(:comment) } | ||
|
||
describe "A comment" do | ||
it { expect(comment).to be_valid } |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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' } |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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') } |
There was a problem hiding this comment.
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?
b061095
to
0bbb9d9
Compare
FactoryGirl.define do | ||
factory :comment do | ||
body { Faker::Lorem.paragraph } | ||
type { Faker::Lorem.word } |
There was a problem hiding this comment.
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 comment
without 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) } |
There was a problem hiding this comment.
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.
a3ebf71
to
6cdbe43
Compare
@eduardoj what's the status of this one? |
@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 |
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. 😉 |
6cdbe43
to
736c920
Compare
So I guess you should rebase now :-) |
require "rails_helper" | ||
|
||
RSpec.shared_examples "a comment" do | ||
let(:comment_type) { described_class.name.underscore } |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 } | ||
|
There was a problem hiding this comment.
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! |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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+$/) |
There was a problem hiding this comment.
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) } |
There was a problem hiding this comment.
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') }
Please delete the last commit, shouldnt be there |
493686a
to
e6a44a0
Compare
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.
e6a44a0
to
70d3176
Compare
70d3176
to
8b5002a
Compare
|
||
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$/) |
There was a problem hiding this comment.
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+$/) |
There was a problem hiding this comment.
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+>$/) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
e42d28b
to
458eef8
Compare
|
||
describe "to_xml" do | ||
context "without parent" do | ||
let(:builder) { builder = Nokogiri::XML::Builder.new } |
There was a problem hiding this comment.
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| |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
?
458eef8
to
3387429
Compare
|
||
context "returns xml" do | ||
context "comment element" do | ||
let(:comment_element) { builder.doc.css('comment') } |
There was a problem hiding this comment.
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.
3387429
to
b17d1b5
Compare
it { is_expected.to validate_presence_of(:user) } | ||
end | ||
|
||
describe "to_xml" do |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 } |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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"
Not all the methods of the model have been tested.
b17d1b5
to
26454b9
Compare
Looks good. Let's ship this! |
No description provided.