Skip to content

Commit

Permalink
Add tests for the FeedParserSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
ogajduse committed Jul 30, 2023
1 parent e9c160e commit 5cc4cff
Show file tree
Hide file tree
Showing 24 changed files with 4,760 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:
- name: Install Dependencies
run: pip install .[dev]

- name: Collect Tests
run: pytest --collect-only tests

- name: Run Tests
run: pytest -sv tests/

- name: Analysis (git diff)
if: failure()
run: git diff
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
exclude: ^test_hass/configuration.yaml$
- id: debug-statements
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.0
Expand All @@ -21,7 +22,13 @@ repos:
rev: "v1.4.1"
hooks:
- id: mypy
additional_dependencies: [homeassistant-stubs, voluptuous-stubs, types-python-dateutil]
additional_dependencies:
[
homeassistant-stubs,
voluptuous-stubs,
types-python-dateutil,
types-PyYAML,
]
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.0.278
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ dev = [
"mypy",
"ruff",
"types-python-dateutil",
"voluptuous-stubs"
"types-PyYAML",
"voluptuous-stubs",
"pyyaml"

]
[project.urls]
Expand Down Expand Up @@ -126,3 +128,6 @@ target-version = "py311"
module = "feedparser.*"
# Workaround till https://github.com/kurtmckee/feedparser/pull/282 gets merged to the main branch
disable_error_code = ["attr-defined", "import"]

[tool.pytest.ini_options]
pythonpath = [".", "tests"]
16 changes: 16 additions & 0 deletions run-hass.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
#
# Script to test the integration in local home-assistant
#
# Install home-assistant first with `pip3 install -U homeassisant`

DIR=$(dirname $0)

mkdir -p "$DIR/test_hass"

if [ ! -L "$DIR/test_hass/custom_components" ]; then
# Create symlink from custom_components to hass subdir so it can be tested
(cd "$DIR/test_hass" && ln -s "../custom_components" "custom_components")
fi

exec python3 -m homeassistant -c test_hass
13 changes: 13 additions & 0 deletions test_hass/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

logger:
default: warning
logs:
custom_components.feedparser: debug

# Loads default set of integrations. Do not remove.
default_config:

http:
server_port: 9123

sensor: !include sensors.yaml
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the feedparser component."""
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Pytest configuration."""

import pytest
from constants import TEST_FEEDS
from feedsource import FeedSource
from pytest import FixtureRequest

from custom_components.feedparser.sensor import FeedParserSensor


def get_feeds() -> list[FeedSource]:
"""Return list of feeds represented by FeedSource objects."""
return [FeedSource(feed) for feed in TEST_FEEDS]


def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
"""Generate tests and fixtures."""
if "feed" in metafunc.fixturenames:
feeds = get_feeds()
metafunc.parametrize("feed", feeds, ids=[f.name for f in feeds], indirect=True)


@pytest.fixture
def feed(request: FixtureRequest) -> FeedSource:
"""Return feed file source."""
return request.param


@pytest.fixture
def feed_sensor(feed: FeedSource) -> FeedParserSensor:
"""Return feed sensor initialized with the local RSS feed."""
return FeedParserSensor(**feed.sensor_config_local_feed)
58 changes: 58 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Constants for tests."""
from pathlib import Path

TESTS_PATH = Path(__file__).parent
DATA_PATH = TESTS_PATH / "data"
TEST_HASS_PATH = Path(__file__).parents[1] / "test_hass"

TEST_FEEDS = [
{
"has_images": True,
"sensor_config": {
"name": "CTK",
"feed_url": "https://www.ceskenoviny.cz/sluzby/rss/cr.php",
"scan_interval": {"hours": 1, "minutes": 30},
},
},
{
"has_images": True,
"sensor_config": {
"name": "nu_nl",
"feed_url": "https://www.nu.nl/rss",
},
},
{
"has_images": True,
"sensor_config": {
"name": "nu_nl_algemeen",
"feed_url": "https://www.nu.nl/rss/Algemeen",
},
},
{
"has_images": True,
"sensor_config": {
"name": "ct24",
"feed_url": "https://ct24.ceskatelevize.cz/rss/hlavni-zpravy",
},
},
{
"has_images": False,
"sensor_config": {
"name": "bbc_europe",
"feed_url": "http://feeds.bbci.co.uk/news/world/europe/rss.xml",
"date_format": "%a, %d %b %Y %H:%M:%S %z",
},
},
{
"has_images": False,
"sensor_config": {
"name": "zive",
"feed_url": "https://www.zive.cz/rss/sc-47/",
"show_topn": 1,
},
},
]

DEFAULT_EXCLUSIONS: list[str] = []
DEFAULT_INCLUSIONS = ["image", "title", "link", "published"]
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S UTC%z"
12 changes: 12 additions & 0 deletions tests/data/CTK.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"has_images": true,
"sensor_config": {
"name": "CTK",
"feed_url": "https://www.ceskenoviny.cz/sluzby/rss/cr.php",
"scan_interval": {
"hours": 1,
"minutes": 30
}
},
"download_date": "2023-07-30T13:48:50.563494"
}
Loading

0 comments on commit 5cc4cff

Please sign in to comment.