Skip to content

Commit

Permalink
feat(metadata): Add name validation and update migration for Metadata…
Browse files Browse the repository at this point in the history
…Template
  • Loading branch information
joshsadam committed Jan 8, 2025
1 parent 2dfbec1 commit 1179160
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/models/metadata_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion db/migrate/20250106153442_create_metadata_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions test/models/metadata_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 1179160

Please sign in to comment.