Skip to content

Conversation

@MutharasuArchunan13
Copy link

Fixed failing unit tests.

Verified all tests now pass locally using pytest / unittest (or relevant test runner).

google-labs-jules bot and others added 2 commits May 23, 2025 09:24
This commit refactors the Snowflake ID generation classes based on your feedback to improve code structure and type safety.

Key changes:

1.  **Unified `SnowflakeGenerator` Class:**
    *   I merged the previous `SnowflakeIDGenerator` (async) and `SyncSnowflakeIDGenerator` into a single class named `SnowflakeGenerator`.
    *   This class now handles both asynchronous ID generation via the `generate()` method (using `asyncio.Lock`) and synchronous ID generation via the `generate_sync()` method (using `threading.Lock`).
    *   Internal state like `last_timestamp` and `sequence` is shared within an instance, protected by the respective locks during the critical section of ID generation.

2.  **`SnowflakeInfo` Dataclass for Extraction Results:**
    *   I introduced a new `SnowflakeInfo` dataclass (`@dataclass(frozen=True)`).
    *   The `extract_snowflake_info` method in `SnowflakeGenerator` now returns an instance of `SnowflakeInfo` instead of a dictionary. This provides better type safety and a more explicit API contract for the extracted ID components (timestamp_ms, readable_timestamp, node_id, sequence).

3.  **Test Suite Updates:**
    *   I updated all test files (`test_32_bits.py`, `test_48_bits.py`, `test_64_bits.py`, `test_96_bits.py`) to:
        *   Use the unified `SnowflakeGenerator` class.
        *   Call `generator.generate_sync()` for synchronous test cases.
        *   Expect and assert against the `SnowflakeInfo` dataclass attributes when testing `extract_snowflake_info`.

4.  **Documentation and Cleanup:**
    *   I updated docstrings and comments throughout `snowflakeid/generator.py` and the test suite to reflect these structural changes.
    *   I performed general code cleanup to ensure consistency and remove outdated elements.

These changes address your feedback requesting a more consolidated class structure and a more type-safe return type for ID component extraction, leading to a cleaner and more maintainable codebase.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant