3 class ArvadosModelTest < ActiveSupport::TestCase
6 def create_with_attrs attrs
7 a = Specimen.create({material: 'caloric'}.merge(attrs))
11 test 'non-admin cannot assign uuid' do
12 set_user_from_auth :active_trustedclient
13 want_uuid = Specimen.generate_uuid
14 a = create_with_attrs(uuid: want_uuid)
15 assert_nil a, "Non-admin should not assign uuid."
18 test 'admin can assign valid uuid' do
19 set_user_from_auth :admin_trustedclient
20 want_uuid = Specimen.generate_uuid
21 a = create_with_attrs(uuid: want_uuid)
22 assert_equal want_uuid, a.uuid, "Admin should assign valid uuid."
23 assert a.uuid.length==27, "Auto assigned uuid length is wrong."
26 test 'admin cannot assign uuid with wrong object type' do
27 set_user_from_auth :admin_trustedclient
28 want_uuid = Human.generate_uuid
29 a = create_with_attrs(uuid: want_uuid)
30 assert_nil a, "Admin should not be able to assign invalid uuid."
33 test 'admin cannot assign badly formed uuid' do
34 set_user_from_auth :admin_trustedclient
35 a = create_with_attrs(uuid: "ntoheunthaoesunhasoeuhtnsaoeunhtsth")
36 assert_nil a, "Admin should not be able to assign invalid uuid."
39 test 'admin cannot assign empty uuid' do
40 set_user_from_auth :admin_trustedclient
41 a = create_with_attrs(uuid: "")
42 assert_nil a, "Admin cannot assign empty uuid."
47 {:a => ['foo', 'bar']},
48 {'a' => [:foo, 'bar']},
49 {'a' => ['foo', :bar]},
51 {:a => {'foo' => {'bar' => 'baz'}}},
52 {'a' => {:foo => {'bar' => 'baz'}}},
53 {'a' => {'foo' => {:bar => 'baz'}}},
54 {'a' => {'foo' => {'bar' => :baz}}},
55 {'a' => {'foo' => ['bar', :baz]}},
56 {'a' => {['foo', :foo] => ['bar', 'baz']}},
58 test "refuse symbol keys in serialized attribute: #{x.inspect}" do
59 set_user_from_auth :admin_trustedclient
60 assert_nothing_raised do
61 Link.create!(link_class: 'test',
64 assert_raises ActiveRecord::RecordInvalid do
65 Link.create!(link_class: 'test',
71 test "Stringify symbols coming from serialized attribute in database" do
72 set_user_from_auth :admin_trustedclient
73 fixed = Link.find_by_uuid(links(:has_symbol_keys_in_database_somehow).uuid)
74 assert_equal(["baz", "foo"], fixed.properties.keys.sort,
75 "Hash symbol keys from DB did not get stringified.")
76 assert_equal(['waz', 'waz', 'waz', 1, nil, false, true],
77 fixed.properties['baz'],
78 "Array symbol values from DB did not get stringified.")
79 assert_equal true, fixed.save, "Failed to save fixed model back to db."
82 test "No HashWithIndifferentAccess in database" do
83 set_user_from_auth :admin_trustedclient
84 assert_raises ActiveRecord::RecordInvalid do
85 Link.create!(link_class: 'test',
86 properties: {'foo' => 'bar'}.with_indifferent_access)
90 [['uuid', {unique: true}],
91 ['owner_uuid', {}]].each do |the_column, requires|
92 test "unique index on all models with #{the_column}" do
94 ActiveRecord::Base.connection.tables.each do |table|
95 columns = ActiveRecord::Base.connection.columns(table)
97 next unless columns.collect(&:name).include? the_column
99 indexes = ActiveRecord::Base.connection.indexes(table).reject do |index|
100 requires.map do |key, val|
101 index.send(key) == val
104 assert_includes indexes.collect(&:columns), [the_column], 'no index'
107 # Sanity check: make sure we didn't just systematically miss everything.
108 assert_operator(10, :<, checked,
109 "Only #{checked} tables have a #{the_column}?!")