4523: add search index
[arvados.git] / services / api / test / unit / arvados_model_test.rb
1 require 'test_helper'
2
3 class ArvadosModelTest < ActiveSupport::TestCase
4   fixtures :all
5
6   def create_with_attrs attrs
7     a = Specimen.create({material: 'caloric'}.merge(attrs))
8     a if a.valid?
9   end
10
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."
16   end
17
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."
24   end
25
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."
31   end
32
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."
37   end
38
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."
43   end
44
45   [ {:a => 'foo'},
46     {'a' => :foo},
47     {:a => ['foo', 'bar']},
48     {'a' => [:foo, 'bar']},
49     {'a' => ['foo', :bar]},
50     {: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']}},
57   ].each do |x|
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',
62                      properties: {})
63       end
64       assert_raises ActiveRecord::RecordInvalid do
65         Link.create!(link_class: 'test',
66                      properties: x)
67       end
68     end
69   end
70
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."
80   end
81
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)
87     end
88   end
89
90   test "unique uuid index exists on all models with the column uuid" do 
91     tables = ActiveRecord::Base.connection.tables
92     tables.each do |table|
93       columns = ActiveRecord::Base.connection.columns(table)
94
95       uuid_column = columns.select do |column|
96         column.name == 'uuid'
97       end
98
99       if !uuid_column.empty?
100         indexes = ActiveRecord::Base.connection.indexes(table)
101         uuid_index = indexes.select do |index|
102           index.columns == ['uuid'] and index.unique == true
103         end
104
105         assert !uuid_index.empty?, "#{table} does not have unique uuid index"
106       end
107     end
108   end
109
110   test "owner uuid index exists on all models with the owner_uuid column" do
111     all_tables = ActiveRecord::Base.connection.tables
112
113     all_tables.each do |table|
114       columns = ActiveRecord::Base.connection.columns(table)
115
116       uuid_column = columns.select do |column|
117         column.name == 'owner_uuid'
118       end
119
120       if !uuid_column.empty?
121         indexes = ActiveRecord::Base.connection.indexes(table)
122         owner_uuid_index = indexes.select do |index|
123           index.columns == ['owner_uuid']
124         end
125         assert !owner_uuid_index.empty?, "#{table} does not have owner_uuid index"
126       end
127     end
128   end
129
130   test "search index exists on models that go into projects" do
131     all_tables =  ActiveRecord::Base.connection.tables
132     all_tables.delete 'schema_migrations'
133
134     all_tables.each do |table|
135       table_class = table.classify.constantize
136       if table_class.respond_to?('searchable_columns')
137         search_index_columns = table_class.searchable_columns('ilike')
138
139         indexes = ActiveRecord::Base.connection.indexes(table)
140         search_index = indexes.select do |index|
141           index.columns == search_index_columns
142         end
143         assert !search_index.empty?, "#{table} does not have search index with all searchable columns"
144       end
145     end
146   end
147 end