-
Notifications
You must be signed in to change notification settings - Fork 16
Add unit tests for custom charm functionality #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+87
−13
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d797cb2
Add unit tests for custom charm functionality
tonyandrewmeyer 7c5f52d
chore: bump ops to get the 12-factor improvements in opst[testing]
tonyandrewmeyer df9cfc1
ci: ops[testing] is required for state transition tests
tonyandrewmeyer 6e11595
ci: combine lint and static, per current best practice
tonyandrewmeyer 9775a6e
ci: run the charm tests as well.
tonyandrewmeyer c1bcddf
test: remove unnecessary sys.path manipulation
tonyandrewmeyer 49c54c9
Apply suggestions from code review
tonyandrewmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| ops ~= 2.17 | ||
| ops ~= 3.7 | ||
| paas-charm>=1.0,<2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright 2026 Canonical Ltd. | ||
| # See LICENSE file for licensing details. | ||
|
|
||
| """Unit tests for the dashboard charm's custom functionality.""" | ||
|
|
||
| import pytest | ||
| from ops import testing | ||
|
|
||
| import charm | ||
|
|
||
| _LOADDATA_CMD = ["python3", "manage.py", "loaddata", "initial_data.yaml"] | ||
|
|
||
|
|
||
| class TestLoadSampleDataAction: | ||
| """Test the load-sample-data action.""" | ||
|
|
||
| def test_load_sample_data_runs_loaddata(self): | ||
| """load-sample-data executes Django loaddata command.""" | ||
| ctx = testing.Context(charm.DashboardCharm) | ||
| container = testing.Container( | ||
| "django-app", | ||
| can_connect=True, | ||
| execs={testing.Exec(_LOADDATA_CMD)}, | ||
| ) | ||
| state = testing.State(containers={container}, leader=True) | ||
|
|
||
| ctx.run(ctx.on.action("load-sample-data"), state) | ||
|
|
||
| exec_record = ctx.exec_history["django-app"][0] | ||
| assert exec_record.command == _LOADDATA_CMD | ||
| assert exec_record.working_dir is not None | ||
| assert ctx.action_results == {"result": "loaded sample data"} | ||
|
|
||
| def test_load_sample_data_fails_on_exec_error(self): | ||
| """load-sample-data action fails when the manage.py command fails.""" | ||
| ctx = testing.Context(charm.DashboardCharm) | ||
| container = testing.Container( | ||
| "django-app", | ||
| can_connect=True, | ||
| execs={testing.Exec(_LOADDATA_CMD, return_code=1)}, | ||
| ) | ||
| state = testing.State(containers={container}, leader=True) | ||
|
|
||
| with pytest.raises(testing.ActionFailed) as exc_info: | ||
| ctx.run(ctx.on.action("load-sample-data"), state) | ||
|
|
||
| assert "unable to load sample data" in exc_info.value.message | ||
|
|
||
| def test_load_sample_data_fails_on_api_error(self): | ||
| """load-sample-data fails when manage.py isn't available.""" | ||
| ctx = testing.Context(charm.DashboardCharm) | ||
| container = testing.Container("django-app", can_connect=True) | ||
|
tonyandrewmeyer marked this conversation as resolved.
|
||
| state = testing.State(containers={container}, leader=True) | ||
|
|
||
| with pytest.raises(testing.ActionFailed) as exc_info: | ||
| ctx.run(ctx.on.action("load-sample-data"), state) | ||
|
|
||
| assert "unable to load sample data" in exc_info.value.message | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.