Skip to content

Commit

Permalink
Merge pull request #3 from BottlecapDave/develop
Browse files Browse the repository at this point in the history
New bug fix
  • Loading branch information
BottlecapDave authored Aug 31, 2022
2 parents 1c5276a + b7876fc commit 67e3285
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 4 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ jobs:
uses: "hacs/action@main"
with:
category: "integration"
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: asdf_install
uses: asdf-vm/actions/install@v1
- name: Install Python modules
run: |
pip install -r requirements.test.txt
- name: Integration tests suite
run: |
python -m pytest tests/integration
release:
name: Release
if: github.ref == 'refs/heads/main'
needs: validate
needs:
- validate
- test
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
__pycache__/
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nodejs 16.13.0
nodejs 16.13.0
python 3.10.1
38 changes: 38 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


## asdf

We use `asdf` to manage plugins.

```bash
asdf plugin-add nodejs
asdf plugin-add python
sudo apt update & sudo apt upgrade
sudo apt install libffi-dev libncurses5-dev zlib1g zlib1g-dev libssl-dev libreadline-dev libbz2-dev libsqlite3-dev
```

## Python

We use pip to manage dependencies. These are needed in order to run tests

```bash
pip install -r requirements.test.txt
```

## Tests

### Unit Tests

Unit tests are written utilising `pytest`. To run them

```bash
python -m pytest tests/unit
```

### Integration Tests

Integration tests are written utilising `pytest`. To run them

```bash
python -m pytest tests/integration
```
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ Custom component built to bring you bus times for First Buses. This was built be

## How to install

To install, place the contents of `custom_components` into the `<config directory>/custom_components` folder of your Home Assistant installation.
You should take the latest [published release](https://github.com/BottlecapDave/HomeAssistant-FirstBus/releases). The current state of `develop` will be in flux and therefore possibly subject to change.

To install, place the contents of `custom_components` into the `<config directory>/custom_components` folder of your Home Assistant installation. Once installed, don't forget to restart your home assistant instance for the integration to be picked up.

### HACS

[![hacs_badge](https://img.shields.io/badge/HACS-Default-41BDF5.svg?style=for-the-badge)](https://github.com/hacs/integration)

This integration can be installed directly via HACS.

## How to setup

Expand Down
2 changes: 1 addition & 1 deletion custom_components/first_bus/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def async_get_next_bus(self, stop, buses):

if ("times" in data):
for time in data["times"]:
if (time["ServiceNumber"] in buses):
if (buses == None or len(buses) == 0 or time["ServiceNumber"] in buses):
matches = re.search(REGEX_TIME, time["Due"])
if (matches != None):
local_now = now()
Expand Down
5 changes: 5 additions & 0 deletions requirements.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pytest
pytest-socket
pytest-asyncio
mock
homeassistant>=2021.12.01
46 changes: 46 additions & 0 deletions tests/integration/api_client/test_get_next_bus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from datetime import datetime
import pytz
import pytest

from custom_components.first_bus.api_client import FirstBusApiClient

stops = ["0170SGB20077", "3800C509801", "2200YEA00934"]

@pytest.mark.asyncio
@pytest.mark.parametrize("buses",[
([]),
(None),
(["19"]),
])
async def test_when_get_next_bus_is_called_then_next_bus_is_returned(buses):
# Arrange
client = FirstBusApiClient()

# We need to check a bunch of stops as they won't all have next buses available
passes = 0
for stop in stops:
try:
# Act
next_bus = await client.async_get_next_bus(stop, buses)

# Assert
assert next_bus != None
assert "Due" in next_bus
assert next_bus["Due"].replace(tzinfo=pytz.UTC) >= datetime.utcnow().replace(tzinfo=pytz.UTC)

assert "ServiceNumber" in next_bus

assert "Destination" in next_bus

assert "IsFG" in next_bus
assert next_bus["IsFG"] == "Y" or next_bus["IsFG"] == "N"

assert "IsLive" in next_bus
assert next_bus["IsLive"] == "Y" or next_bus["IsFG"] == "N"

passes += 1
except:
# Ignore any thrown exceptions
pass

assert passes > 0

0 comments on commit 67e3285

Please sign in to comment.