-
Notifications
You must be signed in to change notification settings - Fork 897
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
Handle non-existant tag category id when importing a service dialog #17237
Handle non-existant tag category id when importing a service dialog #17237
Conversation
@eclarizio Please review. |
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.
Looks fine, can you add a spec for the case where we have an ID, the ID doesn't match, it looks up the category by name, but the description doesn't match? Same with having an ID, ID not matching, and also name can't find one as well? (In that case it doesn't matter if the description matches/doesn't match).
This is going to be a bit nitpicky, but, if you wanted, you could also consolidate some of the setup for those tests into a larger context block that is something like the following (you don't have to do this though, it's just a personal preference on how to organize the specs):
context "when the import file contains a category id" do
before do
@existing_category = #...
end
let(:options) do
{
:category_id => category_id,
:category_name => category_name,
:category_description => category_description
}
end
context "when the id matches an existing category" do
let(:category_id) { @existing_category.id }
context "when the category exists by name" do
let(:category_name) { "best_category" }
context "when the description does not match" do
let(:category_description) { "worst_category" }
#...
end
context "when the description does match" do
#...
end
end
end
context "when the id does not match an existing category" do
let(:category_id) { @existing_category.id + 1 }
#...
end
end
You can look at the context block that currently exists named "when the import file contains a category name with no category_id"
to see what I mean. Unfortunately there's a lot of paths to test here so we need to make sure we test them all.
The actual model code change looks fine though, only thing I could suggest is to push the whole if
statement down into another private method, maybe, but again that's nitpicking.
Also as the rubocop issue pointed out, can you change the two find_by_name
methods to find_by
? I know you technically didn't touch the other one, but it would be nice to get it consistent since we're right here anyway.
@eclarizio re: I'll work on making the other changes requested. |
@eclarizio Can you help me with adding the new tests and refactoring the test cases? Some of the test cases are failing and it is due to variables not being set, but I'm not seeing why they are not being set. The test snippet: context "when the import file contains a category_id" do
before do
@existing_category = Category.create!(:name => "best_category", :description => "best_category")
end
let(:options) do
{
:category_id => @existing_category.id,
:category_name => @existing_category.name,
:category_description => @existing_category.description
}
end
...
context "when the category_id does not match an existing category" do
let(:category_id) { 1_234_567 }
...
context "when the category_name does not match the existing category" do
let(:category_name) { "worst_category" }
it "returns nil" do
pp dialog_field
dialog_field_importer.import_field(dialog_field)
expect(DialogFieldTagControl.first.category).to eq(nil)
end
end
end
end The {"type"=>"DialogFieldTagControl",
"name"=>"Something",
"label"=>"Something else",
"resource_action"=>
{"ae_namespace"=>"Customer/Sample",
"ae_class"=>"Methods",
"ae_instance"=>"Testing"},
"options"=>
{:category_id=>6000000000151,
:category_name=>"best_category",
:category_description=>"best_category"},
"dialog_field_responders"=>["foo_that_gets_ignored"]} Any help or pointers you can give me would be greatly appreciated. |
@branic I wrote some of this on gitter but I'll put it here as well. It looks like in the options hash you passed in the The options hash I'm guessing is being used by the dialog_field in question to be imported. The The options hash should probably look like this:
Then you would adjust those three parameters with lets in each context. Sometimes they'll match the @existing_category, and sometimes they won't, depending on what you need to test, for example in the contexts you've provided above, the ID could be set to For the context of Then when |
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.
👍
@branic I think this fixes https://bugzilla.redhat.com/show_bug.cgi?id=1566266 |
@d-m-u You are correct. This PR does fix that bug. |
@gmcculloug please review 🌵 |
app/models/dialog_field_importer.rb
Outdated
elsif opts[:category_name] | ||
Category.find_by_name(opts[:category_name]) | ||
end | ||
end |
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.
@branic Thanks for the PR, this is a great start. I would advise against using error conditions as control flow. I think a simple change to use find_by
gets us what we want. The other change I am proposing below is that if the category name does not match opts[:category_name]
do the lookup by opts[:category_name]
instead of returning nil
.
Let me know what you think about changing the method to the following:
def find_category(opts)
if opts[:category_id]
cat = Category.find_by(:id => opts[:category_id])
return cat if cat.try(:name) == opts[:category_name]
end
Category.find_by_name(opts[:category_name])
end
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.
@gmcculloug I've changed the method as you suggested.
Thanks @branic. You can safely ignore the I would just ask that you squash the commits and then I think we are good. |
@gmcculloug Thanks. I've squashed the commits. |
Checked commit https://github.com/branic/manageiq/commit/04d4ba343d76e8b8eb97b062c90c59845b6ad905 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0 app/models/dialog_field_importer.rb
|
@miq-bot add_label blocker |
…ory_id Handle non-existant tag category id when importing a service dialog (cherry picked from commit 7c2ef9c) Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1568156
Gaprindashvili backport details:
|
Importing a Service Dialog with a Tag Control element will fail if the Tag Category ID has changed (e.g. when importing into a different region or when the tag category has been deleted and recreated).
There is no indication of failure in the UI, but a backtrace is generated in the logs
This PR captures the error condition and looks for the tag category using the name instead of the ID.
Steps for Testing/QA
7a. Without this PR the import fails with no error for the user
7b. With this PR the import succeeds and the Service Dialog Tag Control Element has the category assigned
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1566266