Merge branch 'master' into 4523-search-index
[arvados.git] / services / api / test / unit / arvados_model_test.rb
index 85d07a5746afce9e85c4287ed2d487c5d465663f..ca9198fd98978eefddee78811ac09835364ca250 100644 (file)
@@ -12,8 +12,7 @@ class ArvadosModelTest < ActiveSupport::TestCase
     set_user_from_auth :active_trustedclient
     want_uuid = Specimen.generate_uuid
     a = create_with_attrs(uuid: want_uuid)
-    assert_not_equal want_uuid, a.uuid, "Non-admin should not assign uuid."
-    assert a.uuid.length==27, "Auto assigned uuid length is wrong."
+    assert_nil a, "Non-admin should not assign uuid."
   end
 
   test 'admin can assign valid uuid' do
@@ -24,11 +23,23 @@ class ArvadosModelTest < ActiveSupport::TestCase
     assert a.uuid.length==27, "Auto assigned uuid length is wrong."
   end
 
+  test 'admin cannot assign uuid with wrong object type' do
+    set_user_from_auth :admin_trustedclient
+    want_uuid = Human.generate_uuid
+    a = create_with_attrs(uuid: want_uuid)
+    assert_nil a, "Admin should not be able to assign invalid uuid."
+  end
+
+  test 'admin cannot assign badly formed uuid' do
+    set_user_from_auth :admin_trustedclient
+    a = create_with_attrs(uuid: "ntoheunthaoesunhasoeuhtnsaoeunhtsth")
+    assert_nil a, "Admin should not be able to assign invalid uuid."
+  end
+
   test 'admin cannot assign empty uuid' do
     set_user_from_auth :admin_trustedclient
     a = create_with_attrs(uuid: "")
-    assert_not_equal "", a.uuid, "Admin should not assign empty uuid."
-    assert a.uuid.length==27, "Auto assigned uuid length is wrong."
+    assert_nil a, "Admin cannot assign empty uuid."
   end
 
   [ {:a => 'foo'},
@@ -75,4 +86,62 @@ class ArvadosModelTest < ActiveSupport::TestCase
                    properties: {'foo' => 'bar'}.with_indifferent_access)
     end
   end
+
+  test "unique uuid index exists on all models with the column uuid" do
+    tables = ActiveRecord::Base.connection.tables
+    tables.each do |table|
+      columns = ActiveRecord::Base.connection.columns(table)
+
+      uuid_column = columns.select do |column|
+        column.name == 'uuid'
+      end
+
+      if !uuid_column.empty?
+        indexes = ActiveRecord::Base.connection.indexes(table)
+        uuid_index = indexes.select do |index|
+          index.columns == ['uuid'] and index.unique == true
+        end
+
+        assert !uuid_index.empty?, "#{table} does not have unique uuid index"
+      end
+    end
+  end
+
+  test "owner uuid index exists on all models with the owner_uuid column" do
+    all_tables = ActiveRecord::Base.connection.tables
+
+    all_tables.each do |table|
+      columns = ActiveRecord::Base.connection.columns(table)
+
+      uuid_column = columns.select do |column|
+        column.name == 'owner_uuid'
+      end
+
+      if !uuid_column.empty?
+        indexes = ActiveRecord::Base.connection.indexes(table)
+        owner_uuid_index = indexes.select do |index|
+          index.columns == ['owner_uuid']
+        end
+        assert !owner_uuid_index.empty?, "#{table} does not have owner_uuid index"
+      end
+    end
+  end
+
+  test "search index exists on models that go into projects" do
+    all_tables =  ActiveRecord::Base.connection.tables
+    all_tables.delete 'schema_migrations'
+
+    all_tables.each do |table|
+      table_class = table.classify.constantize
+      if table_class.respond_to?('searchable_columns')
+        search_index_columns = table_class.searchable_columns('ilike')
+
+        indexes = ActiveRecord::Base.connection.indexes(table)
+        search_index = indexes.select do |index|
+          index.columns == search_index_columns
+        end
+        assert !search_index.empty?, "#{table} does not have search index with all searchable columns #{search_index_columns}"
+      end
+    end
+  end
 end