Skip to content

Commit

Permalink
fix a bug and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krschacht committed Jan 13, 2025
1 parent 188dae0 commit 4becb15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/assistant/slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def clear_conflicting_deleted_assistant_slug
return if slug.blank?
return if !slug_changed?

conflicting_assistant = user.assistants_including_deleted.where.not(id: id, deleted_at: nil).find_by(slug: slug)
conflicting_assistant&.update_column(:slug, nil)
conflicting_assistant = user.assistants_including_deleted.where.not(deleted_at: nil).find_by(slug: slug)
conflicting_assistant&.update_column(:slug, nil) if conflicting_assistant != self
end

def set_default_slug
Expand Down
18 changes: 18 additions & 0 deletions test/models/assistant/slug_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,22 @@ class Assistant::SlugTest < ActiveSupport::TestCase
assistant.update!(name: "New Name")
assert_equal original_slug, assistant.reload.slug
end

test "fails to create a new assistant when slug collides with an existing assistant" do
existing = assistants(:samantha)
new_assistant = existing.user.assistants.new(
name: "Different Name",
slug: existing.slug,
language_model: existing.language_model
)
assert_not new_assistant.valid?
assert_includes new_assistant.errors[:slug], "has already been taken"
end

test "fails to update an assistant's slug when it collides with an existing assistant" do
existing = assistants(:samantha)
other = assistants(:keith_gpt4)
assert_not other.update(slug: existing.slug)
assert_includes other.errors[:slug], "has already been taken"
end
end

0 comments on commit 4becb15

Please sign in to comment.