Skip to content
Merged
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- python-version: "3.8"
os: ubuntu-latest
- python-version: "3.9"
os: ubuntu-latest
- python-version: "3.10"
os: ubuntu-latest
- python-version: "3.11"
os: ubuntu-latest
- python-version: "3.12"
os: ubuntu-latest
- python-version: "3.13"
os: ubuntu-latest
- python-version: "3.14"
os: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov

- name: Run tests with coverage
run: pytest tests/ --cov=matterhook --cov-report=term-missing
23 changes: 22 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@ Advanced usage
'''
message['text'] = markdown_msg
attachments.append(message)
mwh.send(attachments=attachments)
mwh.send(attachments=attachments)

Testing
=======

Install the test dependencies first:

.. code-block:: bash

pip install pytest pytest-cov

Run the tests:

.. code-block:: bash

pytest tests/

Run the tests with code coverage:

.. code-block:: bash

pytest tests/ --cov=matterhook --cov-report=term-missing
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
25 changes: 25 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -e

VERSIONS="3.8 3.9 3.10 3.11 3.12 3.13 3.14"
FAILED=""

for v in $VERSIONS; do
echo "=== Python $v ==="
if uv run --python "$v" --with pytest,pytest-cov,requests \
pytest tests/ --cov=matterhook --cov-report=term-missing; then
echo "=== Python $v: OK ==="
else
echo "=== Python $v: FAILED ==="
FAILED="$FAILED $v"
fi
echo
done

if [ -n "$FAILED" ]; then
echo "FAILED versions:$FAILED"
exit 1
else
echo "All versions passed."
fi
35 changes: 6 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
"""

import os
import sys

from pkg_resources import get_distribution, parse_version
from setuptools import find_packages, setup
from setuptools.command.test import test as TestCommand


def read(fname: str):
Expand All @@ -25,29 +22,6 @@ def read(fname: str):
return open(os.path.join(os.path.dirname(__file__), fname)).read()


class PyTest(TestCommand):
user_options = [("pytest-args=", "a", "Arguments to pass to py.test")]

def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = []

def finalize_options(self):
TestCommand.finalize_options(self)

# https://bitbucket.org/pypa/setuptools/commits/cf565b6
if get_distribution("setuptools").parsed_version < parse_version("18.4"):
self.test_args = []
self.test_suite = True

def run_tests(self):
# import here, cause outside the eggs aren't loaded
import pytest

errno = pytest.main(self.pytest_args)
sys.exit(errno)


setup(
author="numberly",
classifiers=[
Expand All @@ -60,9 +34,13 @@ def run_tests(self):
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Topic :: Software Development :: Libraries :: Python Modules",
],
cmdclass={"test": PyTest},
description="Interact with Mattermost incoming webhooks easily.",
download_url="https://github.com/numberly/matterhook/tags",
include_package_data=True,
Expand All @@ -72,8 +50,7 @@ def run_tests(self):
name="matterhook",
packages=find_packages(),
platforms="any",
tests_require=["pytest"],
url="https://github.com/numberly/matterhook",
version="0.2",
version="0.3",
zip_safe=True,
)
Empty file added tests/__init__.py
Empty file.
137 changes: 137 additions & 0 deletions tests/test_attachments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
from matterhook.attachments import Attachment


class TestAttachmentInit:
def test_minimal(self):
att = Attachment(fallback="fallback text")
assert att.fallback == "fallback text"
assert att.color is None
assert att.pretext is None
assert att.text is None
assert att.author_name is None
assert att.author_link is None
assert att.author_icon is None
assert att.title is None
assert att.title_link is None
assert att.fields is None
assert att.image_url is None
assert att.thumb_url is None

def test_all_params(self):
fields = [{"short": True, "title": "F", "value": "V"}]
att = Attachment(
fallback="fb",
color="#FF0000",
pretext="pre",
text="body",
author_name="author",
author_link="https://author.com",
author_icon="https://author.com/icon.png",
title="Title",
title_link="https://title.com",
fields=fields,
image_url="https://image.png",
thumb_url="https://thumb.png",
)
assert att.fallback == "fb"
assert att.color == "#FF0000"
assert att.pretext == "pre"
assert att.text == "body"
assert att.author_name == "author"
assert att.author_link == "https://author.com"
assert att.author_icon == "https://author.com/icon.png"
assert att.title == "Title"
assert att.title_link == "https://title.com"
assert att.fields == fields
assert att.image_url == "https://image.png"
assert att.thumb_url == "https://thumb.png"


class TestAttachmentPayload:
def test_minimal_payload(self):
att = Attachment(fallback="fb")
assert att.payload == {"fallback": "fb"}

def test_full_payload(self):
fields = [{"short": True, "title": "F", "value": "V"}]
att = Attachment(
fallback="fb",
color="#FF0000",
pretext="pre",
text="body",
author_name="author",
author_link="https://author.com",
author_icon="https://author.com/icon.png",
title="Title",
title_link="https://title.com",
fields=fields,
image_url="https://image.png",
thumb_url="https://thumb.png",
)
assert att.payload == {
"fallback": "fb",
"color": "#FF0000",
"pretext": "pre",
"text": "body",
"author_name": "author",
"author_link": "https://author.com",
"author_icon": "https://author.com/icon.png",
"title": "Title",
"title_link": "https://title.com",
"fields": fields,
"image_url": "https://image.png",
"thumb_url": "https://thumb.png",
}

def test_partial_payload_only_color(self):
att = Attachment(fallback="fb", color="#00FF00")
assert att.payload == {"fallback": "fb", "color": "#00FF00"}

def test_partial_payload_only_text(self):
att = Attachment(fallback="fb", text="hello")
assert att.payload == {"fallback": "fb", "text": "hello"}

def test_partial_payload_author_fields(self):
att = Attachment(
fallback="fb",
author_name="a",
author_link="https://a.com",
author_icon="https://a.com/i.png",
)
assert att.payload == {
"fallback": "fb",
"author_name": "a",
"author_link": "https://a.com",
"author_icon": "https://a.com/i.png",
}

def test_partial_payload_title_fields(self):
att = Attachment(
fallback="fb", title="T", title_link="https://t.com"
)
assert att.payload == {
"fallback": "fb",
"title": "T",
"title_link": "https://t.com",
}

def test_partial_payload_image_fields(self):
att = Attachment(
fallback="fb",
image_url="https://img.png",
thumb_url="https://thumb.png",
)
assert att.payload == {
"fallback": "fb",
"image_url": "https://img.png",
"thumb_url": "https://thumb.png",
}

def test_partial_payload_pretext(self):
att = Attachment(fallback="fb", pretext="before")
assert att.payload == {"fallback": "fb", "pretext": "before"}

def test_partial_payload_fields(self):
fields = [{"short": False, "title": "X", "value": "Y"}]
att = Attachment(fallback="fb", fields=fields)
assert att.payload == {"fallback": "fb", "fields": fields}
Loading
Loading