From 00add5d990975ac438f4b90fefba475455c75824 Mon Sep 17 00:00:00 2001 From: Mohamed El-Sawy Date: Mon, 22 Jul 2024 23:45:11 +0300 Subject: [PATCH] CV2-4869 investigate empty clusters (#1962) * CV2-4896: add null constrain to Cluster * CV2-4869: skip update cluster with null center * CV2-4869: fix tests * CV2-4869: fix tests --- .../20240719183518_add_null_constraints_to_cluster.rb | 5 +++++ db/schema.rb | 4 ++-- lib/sample_data.rb | 1 + lib/tasks/check_khousheh.rake | 6 +++++- test/controllers/graphql_controller_12_test.rb | 6 ++---- test/models/cluster_project_media_test.rb | 4 +++- test/models/cluster_test.rb | 9 +++++---- 7 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20240719183518_add_null_constraints_to_cluster.rb diff --git a/db/migrate/20240719183518_add_null_constraints_to_cluster.rb b/db/migrate/20240719183518_add_null_constraints_to_cluster.rb new file mode 100644 index 0000000000..6f52f646dd --- /dev/null +++ b/db/migrate/20240719183518_add_null_constraints_to_cluster.rb @@ -0,0 +1,5 @@ +class AddNullConstraintsToCluster < ActiveRecord::Migration[6.1] + def change + change_column_null(:clusters, :project_media_id, false) + end +end diff --git a/db/schema.rb b/db/schema.rb index 3620cd4a75..33471ff28f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2024_04_20_104318) do +ActiveRecord::Schema.define(version: 2024_07_19_183518) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -233,7 +233,7 @@ end create_table "clusters", force: :cascade do |t| - t.integer "project_media_id" + t.integer "project_media_id", null: false t.datetime "first_item_at" t.datetime "last_item_at" t.datetime "created_at", null: false diff --git a/lib/sample_data.rb b/lib/sample_data.rb index b1708e3e8b..37c5eca8f3 100644 --- a/lib/sample_data.rb +++ b/lib/sample_data.rb @@ -874,6 +874,7 @@ def create_tipline_request(options = {}) end def create_cluster(options = {}) + options[:project_media] = create_project_media if options[:project_media].blank? team = options[:project_media]&.team || create_team options[:feed] = options[:feed] || create_feed({ team: team }) c = Cluster.new diff --git a/lib/tasks/check_khousheh.rake b/lib/tasks/check_khousheh.rake index f56e824f19..ee1f299c65 100644 --- a/lib/tasks/check_khousheh.rake +++ b/lib/tasks/check_khousheh.rake @@ -277,7 +277,11 @@ namespace :check do cluster_title = cluster_center == pm.id ? pm.title : cluster.title updated_cluster_attributes[:title] = cluster_title # Update cluster - cluster_items[cluster.id] = updated_cluster_attributes + if updated_cluster_attributes[:project_media_id].blank? + error_logs << {Cluster: "Failed to update Cluster with id #{cluster.id}"} + else + cluster_items[cluster.id] = updated_cluster_attributes + end end end # Bulk-update Cluster diff --git a/test/controllers/graphql_controller_12_test.rb b/test/controllers/graphql_controller_12_test.rb index f5b75927ef..d80ac91d9f 100644 --- a/test/controllers/graphql_controller_12_test.rb +++ b/test/controllers/graphql_controller_12_test.rb @@ -345,8 +345,7 @@ def teardown pm2 = create_project_media team: t f = create_feed team: @t f.teams << t - c = create_cluster feed: f, team_ids: [t.id], project_media_id: pm1.id - create_cluster_project_media cluster: c, project_media: pm1 + c = create_cluster feed: f, team_ids: [t.id], project_media: pm1 create_cluster_project_media cluster: c, project_media: pm2 assert_equal 0, @t.project_medias.count @@ -365,8 +364,7 @@ def teardown pm2 = create_project_media team: t f = create_feed team: @t f.teams << t - c = create_cluster feed: f, team_ids: [t.id], project_media_id: pm1.id - create_cluster_project_media cluster: c, project_media: pm1 + c = create_cluster feed: f, team_ids: [t.id], project_media: pm1 create_cluster_project_media cluster: c, project_media: pm2 assert_equal 1, @t.project_medias.count diff --git a/test/models/cluster_project_media_test.rb b/test/models/cluster_project_media_test.rb index dd71524ee2..364fc5a82a 100644 --- a/test/models/cluster_project_media_test.rb +++ b/test/models/cluster_project_media_test.rb @@ -7,8 +7,10 @@ def setup end test "should create cluster project media" do + c = create_cluster + pm = create_project_media assert_difference 'ClusterProjectMedia.count' do - create_cluster_project_media + create_cluster_project_media cluster: c, project_media: pm end end diff --git a/test/models/cluster_test.rb b/test/models/cluster_test.rb index 2486a18726..11fc9988d4 100644 --- a/test/models/cluster_test.rb +++ b/test/models/cluster_test.rb @@ -19,10 +19,11 @@ def setup end test "should have items" do - c = create_cluster + pm = create_project_media + c = create_cluster project_media: pm pm1 = create_project_media cluster: c pm2 = create_project_media cluster: c - assert_equal [pm1, pm2].sort, c.reload.items.sort + assert_equal [pm, pm1, pm2].sort, c.reload.items.sort end test "should access cluster" do @@ -69,8 +70,8 @@ def setup test "should return size" do c = create_cluster - assert_equal 0, c.size - c.project_medias << create_project_media assert_equal 1, c.size + c.project_medias << create_project_media + assert_equal 2, c.size end end