Skip to content

Commit

Permalink
Spec tests for plugin role seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Feb 28, 2024
1 parent 7b05020 commit e8605bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/server_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.seed
server_role_paths = [fixture_path] + Vmdb::Plugins.server_role_paths

csv_rows = server_role_paths.flat_map do |path|
CSV.foreach(path, :headers => true, :skip_lines => /^#/).map(&:to_hash)
CSV.foreach(path.to_s, :headers => true, :skip_lines => /^#/).map(&:to_hash)
end

csv_rows.each do |action|
Expand Down
26 changes: 24 additions & 2 deletions spec/models/server_role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@
allow(File).to receive(:exist?).with(a_string_matching(/content\/server_roles.csv/)).and_return(false)

allow(File).to receive(:open).and_call_original
allow(File).to receive(:open).with(ServerRole.fixture_path, "r", any_args).and_return(StringIO.new(@csv))
ServerRole.seed
allow(File).to receive(:open).with(ServerRole.fixture_path.to_s, "r", any_args).and_return(StringIO.new(@csv))
end

it "should create proper number of rows" do
ServerRole.seed

expect(@csv.split("\n").length - 1).to eq(ServerRole.count)
end

it "should import rows properly" do
ServerRole.seed

roles = @csv.split("\n")
roles.shift
roles.each do |role|
Expand All @@ -90,5 +93,24 @@
end
end
end

context "with a plugin" do
before do
@plugin_csv = <<-CSV.gsub(/^\s+/, "")
name,description,max_concurrent,external_failover,role_scope
special_api_role,Special API Role,0,false,region
CSV

allow(File).to receive(:exist?).with(a_string_matching(/manageiq-api\/content\/server_roles.csv/)).and_return(true)
allow(File).to receive(:open).with(a_string_matching(/manageiq-api\/content\/server_roles.csv/), "r", any_args).and_return(StringIO.new(@plugin_csv))
end

it "should create proper number of rows" do
ServerRole.seed

expected_role_count = @csv.split("\n").length + @plugin_csv.split("\n").length - 2
expect(expected_role_count).to eq(ServerRole.count)
end
end
end
end

0 comments on commit e8605bb

Please sign in to comment.