From 11791600d60dafc62abbfe8f9f81db104e88b81d Mon Sep 17 00:00:00 2001 From: Josh Adam Date: Tue, 7 Jan 2025 07:24:16 -0600 Subject: [PATCH] feat(metadata): Add name validation and update migration for MetadataTemplate --- app/models/metadata_template.rb | 3 +++ ...0250106153442_create_metadata_templates.rb | 3 ++- test/models/metadata_template_test.rb | 21 +++++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index c308462e71..2e74f07506 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -8,6 +8,9 @@ class MetadataTemplate < ApplicationRecord acts_as_paranoid broadcasts_refreshes + # Validations + validates :name, presence: true + # Associations belongs_to :namespace belongs_to :created_by, class_name: 'User' diff --git a/db/migrate/20250106153442_create_metadata_templates.rb b/db/migrate/20250106153442_create_metadata_templates.rb index 4edc8534c1..aed0b12931 100644 --- a/db/migrate/20250106153442_create_metadata_templates.rb +++ b/db/migrate/20250106153442_create_metadata_templates.rb @@ -6,9 +6,10 @@ def change create_table :metadata_templates, id: :uuid do |t| t.references :namespace, type: :uuid, null: false, foreign_key: true t.references :created_by, type: :uuid, null: false, foreign_key: { to_table: :users } - t.string :name, null: false + t.string :name t.string :description t.jsonb :fields, null: false, default: [] + t.datetime :deleted_at t.timestamps end end diff --git a/test/models/metadata_template_test.rb b/test/models/metadata_template_test.rb index ee21d13567..0b9143d465 100644 --- a/test/models/metadata_template_test.rb +++ b/test/models/metadata_template_test.rb @@ -7,7 +7,7 @@ class MetadataTemplateTest < ActiveSupport::TestCase @namespace = namespaces_user_namespaces(:john_doe_namespace) @user = users(:john_doe) @valid_metadata_template = metadata_templates(:valid_metadata_template) - @invalid_metadata_template = metadata_templates(:invalid_metadata_template) + @invalid_metadata_template = nil end # Validation Tests @@ -16,9 +16,16 @@ class MetadataTemplateTest < ActiveSupport::TestCase assert_not_nil @valid_metadata_template.name end + test 'invalid without namespace' do + @valid_metadata_template.namespace = nil + assert_not @valid_metadata_template.valid? + assert_not_nil metadata_template.errors[:namespace] + end + test 'invalid without name' do - assert_not @invalid_metadata_template.valid? - # assert_not_nil @invalid_metadata_template.errors[:name] + @valid_metadata_template.name = nil + assert_not @valid_metadata_template.valid? + assert_not_nil @valid_metadata_template.errors[:name] end test 'invalid with duplicate name in same namespace' do @@ -85,9 +92,11 @@ class MetadataTemplateTest < ActiveSupport::TestCase # Logidze Tests test 'tracks history changes' do original_name = @valid_metadata_template.name - assert_difference '@valid_metadata_template.log_size' do - @valid_metadata_template.update!(name: 'Updated Name') + travel 1.minute do + assert_difference -> { @valid_metadata_template.reload.log_size } do + @valid_metadata_template.update!(name: 'Updated Name') + end end - assert_equal original_name, @valid_metadata_template.log_data.versions.first['changes']['name'] + assert_equal original_name, @valid_metadata_template.reload.log_data.versions.first['changes']['name'] end end