Skip to content

Commit

Permalink
Fix load fixture (#42)
Browse files Browse the repository at this point in the history
* add failing test

* fix load_fixture manually

* automatically modify load_fixture

* test that fixture data is loaded

* add information to README

* update CHANGELOG
  • Loading branch information
MatthewFlamm authored Feb 10, 2021
1 parent 6ecfd83 commit 6a6b534
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
This changelog only includes changes directly related to the structure of this project. Changes in testing behavior may still occur from changes in homeassistant/core.

Changes to minor version indicate a change structurally in this pacakge. Changes in patch indicate changes solely from homeassistant/core. The latter does not imply no breaking changes are introduced.

## 0.2.0
* fix `load_fixture`

## 0.1.0
* remove Python 3.7 and add Python 3.9
* remove `async_test`
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ The goal is to provide the same functionality as the tests in home-assistant/cor
* home-assistant/core native test: `from tests.common import MockConfigEntry`
* custom component test: `from pytest_homeassistant_custom_component.common import MockConfigEntry`
* If your integration is inside a `custom_components` folder, a `custom_components/__init__.py` file or changes to `sys.path` may be required.
* If using `load_fixture`, the files need to be in a `fixtures` folder colocated with the tests. For example, a test in `test_sensor.py` can load data from `some_data.json` using `load_fixture` from this structure:

```
tests/
fixtures/
some_data.json
test_sensor.py
```

## Examples:
* See [list of custom components](https://github.com/MatthewFlamm/pytest-homeassistant-custom-component/network/dependents) as examples that use this package.
Expand Down
17 changes: 17 additions & 0 deletions generate_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,20 @@
new_file.write("".join(data))

print(f"Version: {__version__}")


# modify load_fixture
with open(os.path.join(PACKAGE_DIR, "common.py"), "r") as original_file:
data = original_file.readlines()

import_time_lineno = [i for i, line in enumerate(data) if "from time" in line]
assert len(import_time_lineno) == 1
data.insert(import_time_lineno[0] + 1, "import traceback\n")

load_fixture_lineno = [i for i, line in enumerate(data) if "load_fixture" in line]
assert len(load_fixture_lineno) == 1
data.insert(load_fixture_lineno[0] + 2, " start_path = traceback.extract_stack()[-2].filename\n")
data[load_fixture_lineno[0] + 3] = data[load_fixture_lineno[0] + 3].replace("__file__", "start_path")

with open(os.path.join(PACKAGE_DIR, "common.py"), "w") as new_file:
new_file.writelines(data)
4 changes: 3 additions & 1 deletion pytest_homeassistant_custom_component/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import threading
import time
from time import monotonic
import traceback
import types
from typing import Any, Awaitable, Collection, Optional
from unittest.mock import AsyncMock, Mock, patch
Expand Down Expand Up @@ -399,7 +400,8 @@ def async_fire_service_discovered(hass, service, info):

def load_fixture(filename):
"""Load a fixture."""
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
start_path = traceback.extract_stack()[-2].filename
path = os.path.join(os.path.dirname(start_path), "fixtures", filename)
with open(path, encoding="utf-8") as fptr:
return fptr.read()

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/test_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test_key": "test_value"
}
9 changes: 9 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Tests changes to common module."""
import json

from pytest_homeassistant_custom_component.common import load_fixture


def test_load_fixture():
data = json.loads(load_fixture("test_data.json"))
assert data == {"test_key": "test_value"}

0 comments on commit 6a6b534

Please sign in to comment.