Skip to content

Commit 3da6593

Browse files
committed
Allow underscores in PreviewQuery identifier classification
The production spec explicitly allows underscores in alphanumeric identifiers. PreviewQuery's :special_characters rule and SQL scope incorrectly flagged them. Updated regex from [^a-zA-Z0-9] to [^a-zA-Z0-9_] in both the FORMAT_RULES lambda and the contains_non_alphanumeric scope. Also added a comment to set_raw_identifier explaining why Arel.sql is necessary (update_all applies normalizes in this Rails version).
1 parent 91362fb commit 3da6593

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

app/services/work_packages/identifier_autofix/preview_query.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PreviewQuery
3939
[:too_long, ->(id, max) { id.length > max }],
4040
[:numerical, ->(id, _) { id.match?(/\A\d+\z/) }],
4141
[:starts_with_number, ->(id, _) { id.match?(/\A\d/) }],
42-
[:special_characters, ->(id, _) { id.match?(/[^a-zA-Z0-9]/) }],
42+
[:special_characters, ->(id, _) { id.match?(/[^a-zA-Z0-9_]/) }],
4343
[:not_uppercase, ->(id, _) { id != id.upcase }]
4444
].freeze
4545

@@ -78,7 +78,7 @@ def problematic_scope
7878
end
7979

8080
def exceeds_max_length = Project.where("length(identifier) > ?", max_identifier_length)
81-
def contains_non_alphanumeric = Project.where("identifier ~ ?", "[^a-zA-Z0-9]")
81+
def contains_non_alphanumeric = Project.where("identifier ~ ?", "[^a-zA-Z0-9_]")
8282
def starts_with_digit = Project.where("identifier ~ ?", "^[0-9]")
8383
def not_fully_uppercased = Project.where("identifier != UPPER(identifier)")
8484

spec/services/work_packages/identifier_autofix/preview_query_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
let(:display_count) { described_class::DISPLAY_COUNT }
3737

3838
# Store identifiers bypassing normalizes (which would downcase/upcase them)
39+
# Arel.sql is intentional: update_all(identifier:) applies `normalizes`,
40+
# which would upcase/downcase the value — defeating the purpose of setting
41+
# a raw identifier to simulate pre-existing data.
3942
def set_raw_identifier(project, identifier)
4043
Project.where(id: project.id).update_all(Arel.sql("identifier = #{Project.connection.quote(identifier)}"))
4144
project
@@ -58,12 +61,12 @@ def create_valid_project(name:, identifier:)
5861
end
5962
end
6063

61-
context "when a project has underscores in its identifier" do
64+
context "when a project has underscores and lowercase in its identifier" do
6265
before { create_valid_project(name: "My Project", identifier: "my_proj") }
6366

64-
it "flags it as problematic (underscores are not valid in semantic identifiers)" do
67+
it "flags it as :not_uppercase (underscores are allowed per spec)" do
6568
expect(result.total_count).to eq(1)
66-
expect(result.projects_data.first[:error_reason]).to eq(:special_characters)
69+
expect(result.projects_data.first[:error_reason]).to eq(:not_uppercase)
6770
end
6871
end
6972

0 commit comments

Comments
 (0)