Skip to content

Commit 4bb365f

Browse files
test: add unit tests to this project (#43)
* test: add unit tests to this project * Improve based on feedback
1 parent a087d20 commit 4bb365f

20 files changed

Lines changed: 329 additions & 37 deletions

.github/workflows/black.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Formatting validation using black
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
77

88
jobs:
@@ -11,19 +11,19 @@ jobs:
1111
timeout-minutes: 5
1212
strategy:
1313
matrix:
14-
python-version: ['3.9']
15-
14+
python-version: ["3.13"]
15+
1616
steps:
17-
- uses: actions/checkout@v4
18-
- name: Set up Python ${{ matrix.python-version }}
19-
uses: actions/setup-python@v5
20-
with:
21-
python-version: ${{ matrix.python-version }}
22-
- name: Install dependencies
23-
run: |
24-
pip install -U pip
25-
pip install -r requirements.txt
26-
- name: Format with black
27-
run: |
28-
black .
29-
if git status --porcelain | grep .; then git --no-pager diff; exit 1; fi
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install dependencies
23+
run: |
24+
pip install -U pip
25+
pip install -r requirements.txt
26+
- name: Format with black
27+
run: |
28+
black .
29+
if git status --porcelain | grep .; then git --no-pager diff; exit 1; fi

.github/workflows/flake8.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Linting validation using flake8
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
77

88
jobs:
@@ -11,18 +11,18 @@ jobs:
1111
timeout-minutes: 5
1212
strategy:
1313
matrix:
14-
python-version: ['3.9']
15-
14+
python-version: ["3.13"]
15+
1616
steps:
17-
- uses: actions/checkout@v4
18-
- name: Set up Python ${{ matrix.python-version }}
19-
uses: actions/setup-python@v5
20-
with:
21-
python-version: ${{ matrix.python-version }}
22-
- name: Install dependencies
23-
run: |
24-
pip install -U pip
25-
pip install -r requirements.txt
26-
- name: Lint with flake8
27-
run: |
28-
flake8 *.py && flake8 listeners/
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install dependencies
23+
run: |
24+
pip install -U pip
25+
pip install -r requirements.txt
26+
- name: Lint with flake8
27+
run: |
28+
flake8 *.py && flake8 listeners/

.github/workflows/tests.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Run unit tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 5
12+
strategy:
13+
matrix:
14+
python-version: ["3.11", "3.12", "3.13"]
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install dependencies
23+
run: |
24+
pip install -U pip
25+
pip install -r requirements.txt
26+
- name: Run all tests
27+
run: |
28+
pytest . -v

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ flake8 *.py && flake8 listeners/
5252
black .
5353
```
5454

55+
#### Testing
56+
```zsh
57+
# Run pytest from root directory for unit testing
58+
pytest .
59+
```
60+
5561
## Project Structure
5662

5763
### `manifest.json`

listeners/events/app_home_opened.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from logging import Logger
22

3+
from slack_sdk import WebClient
34

4-
def app_home_opened_callback(client, event, logger: Logger):
5+
6+
def app_home_opened_callback(client: WebClient, event: dict, logger: Logger):
57
# ignore the app_home_opened event for anything but the Home tab
68
if event["tab"] != "home":
79
return

listeners/messages/sample_message.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from logging import Logger
22

33
from slack_bolt import BoltContext, Say
4-
from slack_sdk import WebClient
54

65

7-
def sample_message_callback(context: BoltContext, client: WebClient, say: Say, logger: Logger):
6+
def sample_message_callback(context: BoltContext, say: Say, logger: Logger):
87
try:
98
greeting = context["matches"][0]
109
say(f"{greeting}, how are you?")

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
slack-bolt
2-
pytest
1+
slack-bolt==1.22.0
2+
pytest==8.3.4
33
flake8==7.1.1
4-
black
4+
black==24.10.0

tests/listeners/__init__.py

Whitespace-only changes.

tests/listeners/actions/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import logging
2+
from unittest.mock import Mock
3+
4+
from slack_bolt import Ack
5+
from slack_sdk import WebClient
6+
7+
from listeners.actions.sample_action import sample_action_callback
8+
9+
test_logger = logging.getLogger(__name__)
10+
11+
12+
class TestSampleAction:
13+
def setup_method(self):
14+
self.fake_ack = Mock(Ack)
15+
self.fake_body = {"view": {"id": "test_id", "hash": "156772938.1827394"}}
16+
17+
self.fake_client = Mock(WebClient)
18+
self.fake_client.views_update = Mock(WebClient.views_update)
19+
20+
def test_sample_action_callback(self):
21+
sample_action_callback(
22+
ack=self.fake_ack,
23+
body=self.fake_body,
24+
client=self.fake_client,
25+
logger=test_logger,
26+
)
27+
28+
self.fake_ack.assert_called_once()
29+
30+
self.fake_client.views_update.assert_called_once()
31+
kwargs = self.fake_client.views_update.call_args.kwargs
32+
assert kwargs["view_id"] == self.fake_body["view"]["id"]
33+
assert kwargs["hash"] == self.fake_body["view"]["hash"]
34+
assert kwargs["view"] is not None
35+
36+
def test_ack_exception(self, caplog):
37+
self.fake_ack.side_effect = Exception("test exception")
38+
sample_action_callback(
39+
ack=self.fake_ack,
40+
body=self.fake_body,
41+
client=self.fake_client,
42+
logger=test_logger,
43+
)
44+
45+
self.fake_ack.assert_called_once()
46+
assert str(self.fake_ack.side_effect) in caplog.text

0 commit comments

Comments
 (0)