-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
151 additions
and
132 deletions.
There are no files selected for viewing
283 changes: 151 additions & 132 deletions
283
apps/dashboard/test/models/batch_connect/session_context_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,150 +1,169 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class BatchConnect::SessionContextTest < ActiveSupport::TestCase | ||
test "should use default values" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1" } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
module BatchConnect | ||
class SessionContextTest < ActiveSupport::TestCase | ||
test 'should use default values' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: 'number_field', value: '1' } }, | ||
form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
assert_equal ["", "1"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
assert_equal ['', '1'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
|
||
test "should update cache using from_json" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1" } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
test 'should update cache using from_json' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: 'number_field', value: '1' } }, | ||
form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
context.from_json({"bc_account" => "PZS0714", "num_cores" => "28"}.to_json) | ||
context.from_json({ 'bc_account' => 'PZS0714', 'num_cores' => '28' }.to_json) | ||
|
||
assert_equal ["PZS0714", "28"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
assert_equal ['PZS0714', '28'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
|
||
test "should update cache using attributes=" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1" } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
test 'should update cache using attributes=' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: 'number_field', value: '1' } }, | ||
form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
context.attributes = {"bc_account" => "PZS0714", "num_cores" => "28"} | ||
context.attributes = { 'bc_account' => 'PZS0714', 'num_cores' => '28' } | ||
|
||
assert_equal ["PZS0714", "28"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
# | ||
test "should update cache using update_with_cache" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1" } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({"bc_account" => "PZS0714", "num_cores" => "28"}) | ||
|
||
assert_equal ["PZS0714", "28"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
|
||
test "should not update cache if global cache setting disabled" do | ||
with_modified_env(OOD_BATCH_CONNECT_CACHE_ATTR_VALUES: 'FALSE') do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1" } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({"bc_account" => "PZS0714", "num_cores" => "28"}) | ||
|
||
assert_equal ["", "1"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
end | ||
|
||
test "should not update cache if per app cache disabled" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(cacheable: false, attributes: { num_cores: { widget: "number_field", value: "1" } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({"bc_account" => "PZS0714", "num_cores" => "28"}) | ||
|
||
assert_equal ["", "1"], [context["bc_account"].value,context["num_cores"].value ] | ||
end | ||
|
||
test "should not update single field if per attribute cache disabled" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1", cacheable: false } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
context.update_with_cache({"bc_account" => "PZS0714", :num_cores => 28}) | ||
assert_equal ["PZS0714", "1"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
|
||
test "should not update single field if per attribute cache disabled even if global cache setting disabled but per app cache enabled" do | ||
with_modified_env(OOD_BATCH_CONNECT_CACHE_ATTR_VALUES: 'FALSE') do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(cacheable: true, attributes: { num_cores: { widget: "number_field", value: "1", cacheable: false } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
context.update_with_cache({"bc_account" => "PZS0714", "num_cores" => "28"}) | ||
|
||
assert_equal ["PZS0714", "1"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
end | ||
|
||
test "should update single field if per attribute cache enabled even if global cache setting disabled" do | ||
with_modified_env(OOD_BATCH_CONNECT_CACHE_ATTR_VALUES: 'FALSE') do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1", cacheable: true } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({"bc_account" => "PZS0714", "num_cores" => "28"}) | ||
|
||
assert_equal ["", "28"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
end | ||
|
||
test "should update single field if per attribute cache enabled even if per app cache disabled" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(cacheable: false, attributes: { num_cores: { widget: "number_field", value: "1", cacheable: true } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({"bc_account" => "PZS0714", "num_cores" => "28"}) | ||
|
||
assert_equal ["", "28"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
|
||
test "should ignore bad cache keys when updating cache using update_with_cache" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: "number_field", value: "1" } }, form: ["bc_account", "num_cores"]) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({"bc_account" => "PZS0714", "num_cores" => "28", "bad_key_1" => "1", "bad_key_2" => "2"}) | ||
|
||
assert_equal ["PZS0714", "28"], [context["bc_account"].value, context["num_cores"].value] | ||
end | ||
|
||
test "to_openstruct throws error when using OpenStruct methods" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { table: { value: "the_table" } }, form: ["bc_account", "table"]) | ||
context = app.build_session_context | ||
|
||
assert_raises ArgumentError do | ||
context.to_openstruct | ||
assert_equal ['PZS0714', '28'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
end | ||
test 'should update cache using update_with_cache' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: 'number_field', value: '1' } }, | ||
form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
test "to_openstruct accepts empty values" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { queue: { value: "gpu" } }, form: ["bc_account", "queue"]) | ||
context = app.build_session_context | ||
context.update_with_cache({ 'bc_account' => 'PZS0714', 'num_cores' => '28' }) | ||
|
||
assert_equal context.to_openstruct.to_h, {:bc_account => "", :queue => "gpu"} | ||
end | ||
assert_equal ['PZS0714', '28'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
|
||
test "to_openstruct accepts accepts hashes with string keys" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { queue: { value: "gpu" } }, form: ["bc_account", "queue"]) | ||
context = app.build_session_context | ||
struct = context.to_openstruct(addons: {"new_thing": "some_new_thing"}) | ||
test 'should not update cache if global cache setting disabled' do | ||
with_modified_env(OOD_BATCH_CONNECT_CACHE_ATTR_VALUES: 'FALSE') do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: 'number_field', value: '1' } }, | ||
form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
assert_equal struct.to_h, {:bc_account => "", :queue => "gpu", :new_thing=>"some_new_thing"} | ||
end | ||
context.update_with_cache({ 'bc_account' => 'PZS0714', 'num_cores' => '28' }) | ||
|
||
assert_equal ['', '1'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
end | ||
|
||
test 'should not update cache if per app cache disabled' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(cacheable: false, | ||
attributes: { num_cores: { widget: 'number_field', value: '1' } }, form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({ 'bc_account' => 'PZS0714', 'num_cores' => '28' }) | ||
|
||
assert_equal ['', '1'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
|
||
test 'should not update single field if per attribute cache disabled' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns( | ||
attributes: { num_cores: { widget: 'number_field', value: '1', | ||
cacheable: false } }, form: ['bc_account', 'num_cores'] | ||
) | ||
context = app.build_session_context | ||
context.update_with_cache({ 'bc_account' => 'PZS0714', :num_cores => 28 }) | ||
assert_equal ['PZS0714', '1'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
|
||
test 'should not update single field if per attribute cache disabled even if global cache setting disabled but per app cache enabled' do | ||
with_modified_env(OOD_BATCH_CONNECT_CACHE_ATTR_VALUES: 'FALSE') do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(cacheable: true, | ||
attributes: { num_cores: { widget: 'number_field', value: '1', cacheable: false } }, form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
context.update_with_cache({ 'bc_account' => 'PZS0714', 'num_cores' => '28' }) | ||
|
||
assert_equal ['PZS0714', '1'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
end | ||
|
||
test 'should update single field if per attribute cache enabled even if global cache setting disabled' do | ||
with_modified_env(OOD_BATCH_CONNECT_CACHE_ATTR_VALUES: 'FALSE') do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns( | ||
attributes: { num_cores: { widget: 'number_field', value: '1', | ||
cacheable: true } }, form: ['bc_account', 'num_cores'] | ||
) | ||
context = app.build_session_context | ||
|
||
test "to_openstruct accepts accepts hashes with symbol keys" do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { queue: { value: "gpu" } }, form: ["bc_account", "queue"]) | ||
context = app.build_session_context | ||
struct = context.to_openstruct(addons: {:new_thing => "some_new_thing"}) | ||
context.update_with_cache({ 'bc_account' => 'PZS0714', 'num_cores' => '28' }) | ||
|
||
assert_equal struct.to_h, {:bc_account => "", :queue => "gpu", :new_thing=>"some_new_thing"} | ||
assert_equal ['', '28'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
end | ||
|
||
test 'should update single field if per attribute cache enabled even if per app cache disabled' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(cacheable: false, | ||
attributes: { num_cores: { widget: 'number_field', value: '1', cacheable: true } }, form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({ 'bc_account' => 'PZS0714', 'num_cores' => '28' }) | ||
|
||
assert_equal ['', '28'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
|
||
test 'should ignore bad cache keys when updating cache using update_with_cache' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { num_cores: { widget: 'number_field', value: '1' } }, | ||
form: ['bc_account', 'num_cores']) | ||
context = app.build_session_context | ||
|
||
context.update_with_cache({ 'bc_account' => 'PZS0714', 'num_cores' => '28', 'bad_key_1' => '1', | ||
'bad_key_2' => '2' }) | ||
|
||
assert_equal ['PZS0714', '28'], [context['bc_account'].value, context['num_cores'].value] | ||
end | ||
|
||
test 'to_openstruct throws error when using OpenStruct methods' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { table: { value: 'the_table' } }, form: ['bc_account', 'table']) | ||
context = app.build_session_context | ||
|
||
assert_raises ArgumentError do | ||
context.to_openstruct | ||
end | ||
end | ||
|
||
test 'to_openstruct accepts empty values' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { queue: { value: 'gpu' } }, form: ['bc_account', 'queue']) | ||
context = app.build_session_context | ||
|
||
assert_equal context.to_openstruct.to_h, { :bc_account => '', :queue => 'gpu' } | ||
end | ||
|
||
test 'to_openstruct accepts accepts hashes with string keys' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { queue: { value: 'gpu' } }, form: ['bc_account', 'queue']) | ||
context = app.build_session_context | ||
struct = context.to_openstruct(addons: { "new_thing": 'some_new_thing' }) | ||
|
||
assert_equal struct.to_h, { :bc_account => '', :queue => 'gpu', :new_thing => 'some_new_thing' } | ||
end | ||
|
||
test 'to_openstruct accepts accepts hashes with symbol keys' do | ||
app = BatchConnect::App.new(router: nil) | ||
app.stubs(:form_config).returns(attributes: { queue: { value: 'gpu' } }, form: ['bc_account', 'queue']) | ||
context = app.build_session_context | ||
struct = context.to_openstruct(addons: { :new_thing => 'some_new_thing' }) | ||
|
||
assert_equal struct.to_h, { :bc_account => '', :queue => 'gpu', :new_thing => 'some_new_thing' } | ||
end | ||
end | ||
end |