22052: Improve trigram index tests
authorBrett Smith <brett.smith@curii.com>
Tue, 20 Aug 2024 20:12:13 +0000 (16:12 -0400)
committerBrett Smith <brett.smith@curii.com>
Tue, 20 Aug 2024 20:19:07 +0000 (16:19 -0400)
Rather than defining tests from strings and reflecting the classes from
them, just start from the classes directly and use their methods to get
additional information we need. This is fewer hoops to jump through.

Arvados-DCO-1.1-Signed-off-by: Brett Smith <brett.smith@curii.com>

services/api/test/unit/arvados_model_test.rb

index 82a3b9d9b544804eaae21516b621a989769dc96a..88670ee172f9717b1042048dcd4f96bd624be53b 100644 (file)
@@ -151,32 +151,26 @@ class ArvadosModelTest < ActiveSupport::TestCase
     end
   end
 
-  [
-    %w[collections collections_trgm_text_search_idx],
-    %w[container_requests container_requests_trgm_text_search_idx],
-    %w[groups groups_trgm_text_search_idx],
-    %w[workflows workflows_trgm_text_search_idx]
-  ].each do |model|
-    table = model[0]
-    indexname = model[1]
-    test "trigram index exists on #{table} model" do
-      table_class = table.classify.constantize
-      expect = table_class.full_text_searchable_columns
-      ok = false
+  [Collection, ContainerRequest, Group, Workflow].each do |model|
+    test "trigram index exists on #{model} model" do
+      expect = model.full_text_searchable_columns
       conn = ActiveRecord::Base.connection
-      conn.exec_query("SELECT indexdef FROM pg_indexes WHERE tablename = '#{table}' AND indexname = '#{indexname}'").each do |res|
+      index_name = "#{model.table_name}_trgm_text_search_idx"
+      indexes = conn.exec_query("SELECT indexdef FROM pg_indexes WHERE tablename = '#{model.table_name}' AND indexname = '#{index_name}'")
+      assert_not_equal(indexes.length, 0)
+      indexes.each do |res|
         searchable = res['indexdef'].scan(/COALESCE\(+([A-Za-z_]+)/).flatten
-        ok = (expect == searchable)
-        assert ok, "Invalid or no trigram index on #{table} named #{indexname}\nexpect: #{expect.inspect}\nfound: #{searchable}"
+        assert_equal(
+          searchable, expect,
+          "Invalid or no trigram index for #{model} named #{index_name}\nexpect: #{expect.inspect}\nfound: #{searchable}",
+        )
       end
     end
 
-    test "UUID and hash columns are excluded from #{table} full text index" do
-      class_name = model.first.classify
-      actual = class_name.constantize.full_text_searchable_columns
+    test "UUID and hash columns are excluded from #{model} full text index" do
       assert_equal(
-        actual & full_text_excluded_columns, [],
-        "UUID/hash columns returned by #{class_name}.full_text_searchable_columns",
+        model.full_text_searchable_columns & full_text_excluded_columns, [],
+        "UUID/hash columns returned by #{model}.full_text_searchable_columns",
       )
     end
   end