Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def calculate_discount(price, percentage):
return price - (price * percentage / 100)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage: No test covers the case where percentage > 100, which causes the function to return a negative price. If callers can pass arbitrary percentages, this silently produces an invalid result (e.g. calculate_discount(50, 150)-25). A unit test asserting the return value is non-negative, or a guard clamping percentage to [0, 100], would prevent this from reaching production.

  • Mark as noise



def is_valid_email(email):
return "@" in email and "." in email.split("@")[-1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: is_valid_email accepts addresses with multiple @ signs as valid. "a@b@c.com".split("@")[-1] yields "c.com", which passes the . check — so "attacker@target@domain.com" returns True.

A minimal fix is to also assert there is exactly one @:

Suggested change
return "@" in email and "." in email.split("@")[-1]
return email.count("@") == 1 and "." in email.split("@")[-1]
  • Mark as noise

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage: The email validation logic is non-trivial and entirely uncovered. At minimum, tests should assert:

  • Valid address returns True

  • Missing @ returns False

  • Multiple @ signs return False

  • Domain with no . (e.g. "user@localhost") returns False

  • Trailing dot in domain (e.g. "user@domain.") returns False

  • Mark as noise



def format_user_name(first, last):
return f"{first.strip().capitalize()} {last.strip().capitalize()}"


def clamp(value, min_value, max_value):
if value < min_value:
return min_value
if value > max_value:
return max_value
return value


def truncate_string(text, max_length):
if len(text) <= max_length:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: When truncation occurs, the returned string is max_length + 3 characters long (the "..." suffix is appended after slicing to max_length), so the result silently exceeds the stated limit.

For example: truncate_string("hello world", 5)"hello..." (8 chars, not 5).

Callers that rely on the result fitting within max_length (e.g. for a database column or UI label) will get data that's longer than expected.

Suggested change
if len(text) <= max_length:
return text[:max(0, max_length - 3)] + "..."
  • Mark as noise

return text
return text[:max_length] + "..."


def parse_int_safe(value):
try:
return int(value)
except (ValueError, TypeError):
return None


def slugify(text):
return text.lower().strip().replace(" ", "-")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: slugify only replaces spaces — it leaves punctuation, symbols, and non-ASCII characters in the output. For example:

  • slugify("Hello, World!")"hello,-world!" (comma and ! retained)
  • slugify("café au lait")"café-au-lait" (accented character retained)
  • slugify("hello world")"hello--world" (double space → double hyphen)

Any caller using the result in a URL will silently get an invalid slug. At minimum, non-word characters should be stripped and runs of hyphens collapsed:

import re

def slugify(text):
    text = text.lower().strip()
    text = re.sub(r'[^\w\s-]', '', text)
    text = re.sub(r'[\s_]+', '-', text)
    return re.sub(r'-+', '-', text).strip('-')
  • Mark as noise



def pluralize(word, count):
return word if count == 1 else word + "s"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage: pluralize has no tests. Two cases that could regress silently:

  • count == 0 (e.g. pluralize("item", 0)) should return the plural form — confirm the intent matches the call sites.

  • Non-English words that don't pluralise with a plain +"s" (e.g. "bus""buss" instead of "buses") — if the project only uses simple English nouns this is fine, but it's worth a test to document the expected scope.

  • Mark as noise

Loading