Skip to content

Add unit tests for SQS functionality#483

Merged
jan-janssen merged 2 commits intosqs-improve-interfacefrom
sqs-improve-tests-14985907204539575987
Apr 10, 2026
Merged

Add unit tests for SQS functionality#483
jan-janssen merged 2 commits intosqs-improve-interfacefrom
sqs-improve-tests-14985907204539575987

Conversation

@jan-janssen
Copy link
Copy Markdown
Member

Added comprehensive unit tests for the SQS (Special Quasirandom Structures) module to increase test coverage to 97%. The new tests cover:

  • Parameters: atol, rtol, shell_radii, log_level, num_threads, and arbitrary kwargs.
  • Error handling: Invalid log levels, ParseError from sqsgenerator, AttributeError in result proxy, and RuntimeError on optimization failure.
  • Graceful stopping: Simulated KeyboardInterrupt during the optimization loop.
  • Verified that the full test suite passes and coverage is significantly improved.

PR created automatically by Jules for task 14985907204539575987 started by @jan-janssen

Added several test cases to tests/test_sqs.py to increase coverage of the
sqs_structures function and its helper classes. Covered parameters include
atol, rtol, shell_radii, log_level, and num_threads. Also added tests for
error handling (ParseError, AttributeError, RuntimeError) and graceful
stopping via KeyboardInterrupt. Coverage for _interface.py increased to 97%.

Co-authored-by: jan-janssen <3854739+jan-janssen@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings April 10, 2026 08:26
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 10, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f226ff66-53f0-4cd8-a926-83d86c7569f9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sqs-improve-tests-14985907204539575987

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.13%. Comparing base (618be26) to head (2526895).
⚠️ Report is 1 commits behind head on sqs-improve-interface.

Additional details and impacted files
@@                    Coverage Diff                    @@
##           sqs-improve-interface     #483      +/-   ##
=========================================================
+ Coverage                  83.03%   84.13%   +1.10%     
=========================================================
  Files                         27       27              
  Lines                       1904     1904              
=========================================================
+ Hits                        1581     1602      +21     
+ Misses                       323      302      -21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds additional unit test coverage for the SQS (stk.build.sqs_structures) interface, focusing on optional parameters and failure/interrupt paths in the sqsgenerator-backed implementation.

Changes:

  • Added tests covering tolerances (atol, rtol) and shell_radii handling for both interact and split sublattice modes.
  • Added tests for log-level handling, passthrough kwargs, and num_threads.
  • Added tests for error paths (parse failures, proxy attribute behavior, simulated interrupt behavior, and optimization returning no result).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +213 to +218
with patch("structuretoolkit.build.sqs._interface.Thread.is_alive", side_effect=[True, KeyboardInterrupt, False]):
stk.build.sqs_structures(
structure=bulk("Au", cubic=True).repeat([2, 2, 2]),
composition=dict(Cu=16, Au=16),
iterations=10,
)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

In patch(..., side_effect=[True, KeyboardInterrupt, False]), the iterable side_effect will return the KeyboardInterrupt class on the second call rather than raising it (mock raises only exception instances in an iterable). As written, this likely never exercises the except (KeyboardInterrupt, EOFError) branch in sqs_structures. Use KeyboardInterrupt() (or a callable side_effect that raises) to actually simulate the interrupt.

Copilot uses AI. Check for mistakes.
Comment on lines +200 to +212
# We mock time.sleep to raise KeyboardInterrupt to simulate it during the wait loop
# However sqs_structures uses stop_event.wait(timeout=1.0)
# Let's mock stop_event.wait instead, but carefully.

# We need to make sure we only mock the wait call inside sqs_structures loop
# but since we are mocking the class Event in the module, it might be safer to mock the instance
# but we don't have access to the instance easily.

# Alternatively, we can mock Thread.is_alive to raise it.
# However, stk.build.sqs_structures catches KeyboardInterrupt and sets stop_gracefully = True
# but it DOES NOT re-raise it if a result is already available or if it finishes.
# Actually it should probably re-raise it or return what it has.
# In the current implementation, it catches it and proceeds to join the thread and return results.
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The comment block in test_sqs_keyboard_interrupt is misleading/overly speculative (it discusses mocking time.sleep/stop_event.wait and critiques behavior, but the test actually patches Thread.is_alive). Consider trimming to a short explanation of what is being mocked and why, to keep the test focused and maintainable.

Suggested change
# We mock time.sleep to raise KeyboardInterrupt to simulate it during the wait loop
# However sqs_structures uses stop_event.wait(timeout=1.0)
# Let's mock stop_event.wait instead, but carefully.
# We need to make sure we only mock the wait call inside sqs_structures loop
# but since we are mocking the class Event in the module, it might be safer to mock the instance
# but we don't have access to the instance easily.
# Alternatively, we can mock Thread.is_alive to raise it.
# However, stk.build.sqs_structures catches KeyboardInterrupt and sets stop_gracefully = True
# but it DOES NOT re-raise it if a result is already available or if it finishes.
# Actually it should probably re-raise it or return what it has.
# In the current implementation, it catches it and proceeds to join the thread and return results.
# Simulate a KeyboardInterrupt while sqs_structures is polling the worker thread.
# Patching Thread.is_alive keeps the test focused on interrupt handling in that loop.

Copilot uses AI. Check for mistakes.
@jan-janssen jan-janssen requested a review from dgehringer April 10, 2026 08:39
@jan-janssen
Copy link
Copy Markdown
Member Author

@dgehringer I asked Googles Jules to create a couple of tests for your recent changes. This improves the test coverage, so I would like to include these changes in your pull request.

@google-labs-jules
Copy link
Copy Markdown

@dgehringer I asked Googles Jules to create a couple of tests for your recent changes. This improves the test coverage, so I would like to include these changes in your pull request.

I have added comprehensive unit tests for the new SQS functionality. These tests cover various parameters (atol, rtol, shell_radii, log_level, num_threads), error cases (ParseError, AttributeError, RuntimeError), and edge cases (KeyboardInterrupt), increasing the coverage of _interface.py to 97%.

- Added tests for atol, rtol, and shell_radii in both interact and split modes.
- Added tests for different log levels (info, debug, error, trace).
- Added tests for kwargs passing (e.g., chunk_size).
- Added error handling tests for invalid compositions and non-split result sublattices access.
- Added simulation of KeyboardInterrupt to test graceful stopping.
- Added test for num_threads parameter.
- Added test for optimization failure handling.
- Coverage for _interface.py increased to 97%.

Co-authored-by: jan-janssen <3854739+jan-janssen@users.noreply.github.com>
@jan-janssen jan-janssen merged commit 2f99f20 into sqs-improve-interface Apr 10, 2026
19 checks passed
@jan-janssen jan-janssen deleted the sqs-improve-tests-14985907204539575987 branch April 10, 2026 19:41
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.

3 participants