Skip to content

Commit e7cc812

Browse files
authored
Merge pull request #80 from ogajduse/feat/add-tests
2 parents 2623459 + 3862944 commit e7cc812

24 files changed

+4760
-2
lines changed

.github/workflows/pull_request.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ jobs:
2626
- name: Install Dependencies
2727
run: pip install .[dev]
2828

29+
- name: Collect Tests
30+
run: pytest --collect-only tests
31+
32+
- name: Run Tests
33+
run: pytest -sv tests/
34+
2935
- name: Analysis (git diff)
3036
if: failure()
3137
run: git diff

.pre-commit-config.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repos:
77
- id: trailing-whitespace
88
- id: end-of-file-fixer
99
- id: check-yaml
10+
exclude: ^test_hass/configuration.yaml$
1011
- id: debug-statements
1112
- repo: https://github.com/asottile/pyupgrade
1213
rev: v3.3.0
@@ -21,7 +22,13 @@ repos:
2122
rev: "v1.4.1"
2223
hooks:
2324
- id: mypy
24-
additional_dependencies: [homeassistant-stubs, voluptuous-stubs, types-python-dateutil, types-PyYAML]
25+
additional_dependencies:
26+
[
27+
homeassistant-stubs,
28+
voluptuous-stubs,
29+
types-python-dateutil,
30+
types-PyYAML,
31+
]
2532
- repo: https://github.com/astral-sh/ruff-pre-commit
2633
# Ruff version.
2734
rev: v0.0.278

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ dev = [
3333
"mypy",
3434
"ruff",
3535
"types-python-dateutil",
36-
"voluptuous-stubs"
36+
"types-PyYAML",
37+
"voluptuous-stubs",
38+
"pyyaml"
3739

3840
]
3941
[project.urls]
@@ -126,3 +128,6 @@ target-version = "py311"
126128
module = "feedparser.*"
127129
# Workaround till https://github.com/kurtmckee/feedparser/pull/282 gets merged to the main branch
128130
disable_error_code = ["attr-defined", "import"]
131+
132+
[tool.pytest.ini_options]
133+
pythonpath = [".", "tests"]

run-hass.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
#
3+
# Script to test the integration in local home-assistant
4+
#
5+
# Install home-assistant first with `pip3 install -U homeassisant`
6+
7+
DIR=$(dirname $0)
8+
9+
mkdir -p "$DIR/test_hass"
10+
11+
if [ ! -L "$DIR/test_hass/custom_components" ]; then
12+
# Create symlink from custom_components to hass subdir so it can be tested
13+
(cd "$DIR/test_hass" && ln -s "../custom_components" "custom_components")
14+
fi
15+
16+
exec python3 -m homeassistant -c test_hass

test_hass/configuration.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
logger:
3+
default: warning
4+
logs:
5+
custom_components.feedparser: debug
6+
7+
# Loads default set of integrations. Do not remove.
8+
default_config:
9+
10+
http:
11+
server_port: 9123
12+
13+
sensor: !include sensors.yaml

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for the feedparser component."""

tests/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Pytest configuration."""
2+
3+
import pytest
4+
from constants import TEST_FEEDS
5+
from feedsource import FeedSource
6+
from pytest import FixtureRequest
7+
8+
from custom_components.feedparser.sensor import FeedParserSensor
9+
10+
11+
def get_feeds() -> list[FeedSource]:
12+
"""Return list of feeds represented by FeedSource objects."""
13+
return [FeedSource(feed) for feed in TEST_FEEDS]
14+
15+
16+
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
17+
"""Generate tests and fixtures."""
18+
if "feed" in metafunc.fixturenames:
19+
feeds = get_feeds()
20+
metafunc.parametrize("feed", feeds, ids=[f.name for f in feeds], indirect=True)
21+
22+
23+
@pytest.fixture
24+
def feed(request: FixtureRequest) -> FeedSource:
25+
"""Return feed file source."""
26+
return request.param
27+
28+
29+
@pytest.fixture
30+
def feed_sensor(feed: FeedSource) -> FeedParserSensor:
31+
"""Return feed sensor initialized with the local RSS feed."""
32+
return FeedParserSensor(**feed.sensor_config_local_feed)

tests/constants.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Constants for tests."""
2+
from pathlib import Path
3+
4+
TESTS_PATH = Path(__file__).parent
5+
DATA_PATH = TESTS_PATH / "data"
6+
TEST_HASS_PATH = Path(__file__).parents[1] / "test_hass"
7+
8+
TEST_FEEDS = [
9+
{
10+
"has_images": True,
11+
"sensor_config": {
12+
"name": "CTK",
13+
"feed_url": "https://www.ceskenoviny.cz/sluzby/rss/cr.php",
14+
"scan_interval": {"hours": 1, "minutes": 30},
15+
},
16+
},
17+
{
18+
"has_images": True,
19+
"sensor_config": {
20+
"name": "nu_nl",
21+
"feed_url": "https://www.nu.nl/rss",
22+
},
23+
},
24+
{
25+
"has_images": True,
26+
"sensor_config": {
27+
"name": "nu_nl_algemeen",
28+
"feed_url": "https://www.nu.nl/rss/Algemeen",
29+
},
30+
},
31+
{
32+
"has_images": True,
33+
"sensor_config": {
34+
"name": "ct24",
35+
"feed_url": "https://ct24.ceskatelevize.cz/rss/hlavni-zpravy",
36+
},
37+
},
38+
{
39+
"has_images": False,
40+
"sensor_config": {
41+
"name": "bbc_europe",
42+
"feed_url": "http://feeds.bbci.co.uk/news/world/europe/rss.xml",
43+
"date_format": "%a, %d %b %Y %H:%M:%S %z",
44+
},
45+
},
46+
{
47+
"has_images": False,
48+
"sensor_config": {
49+
"name": "zive",
50+
"feed_url": "https://www.zive.cz/rss/sc-47/",
51+
"show_topn": 1,
52+
},
53+
},
54+
]
55+
56+
DEFAULT_EXCLUSIONS: list[str] = []
57+
DEFAULT_INCLUSIONS = ["image", "title", "link", "published"]
58+
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S UTC%z"

tests/data/CTK.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"has_images": true,
3+
"sensor_config": {
4+
"name": "CTK",
5+
"feed_url": "https://www.ceskenoviny.cz/sluzby/rss/cr.php",
6+
"scan_interval": {
7+
"hours": 1,
8+
"minutes": 30
9+
}
10+
},
11+
"download_date": "2023-07-30T13:48:50.563494"
12+
}

0 commit comments

Comments
 (0)