Skip to content

Commit

Permalink
Merge pull request #37 from rucarrol-goog/rebase
Browse files Browse the repository at this point in the history
Add tests for app.py, and add gitignore entry for bazel
  • Loading branch information
awlx authored Apr 29, 2021
2 parents ee57fb8 + ca17534 commit 69455d5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Bazel local cache/files ignore.
bazel-*
3 changes: 2 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Load python specific bazel rules.
http_archive(
name = "rules_python",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.1.0/rules_python-0.1.0.tar.gz",
sha256 = "b6d46438523a3ec0f3cead544190ee13223a52f6a6765a29eae7b7cc24cc83a0",
)


load("@rules_python//python:pip.bzl", "pip_install")

pip_install(
Expand Down
18 changes: 18 additions & 0 deletions wgkex/worker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ py_test(
requirement("mock"),
],
)

py_binary(
name = "app",
srcs = ["app.py"],
deps = [
":mqtt",
"//wgkex/config:config",
],
)

py_test(
name = "app_test",
srcs = ["app_test.py"],
deps = [
":app",
requirement("mock"),
],
)
24 changes: 18 additions & 6 deletions wgkex/worker/app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#!/usr/bin/env python3
"""Initialises the MQTT worker."""

import wgkex.config.config as config
from wgkex.worker import mqtt

from wgkex.config import load_config
from wgkex.worker.mqtt import connect as mqtt

config = load_config()
class Error(Exception):
"""Base Exception handling class."""


def main():
class DomainsNotInConfig(Error):
"""If no domains exist in configuration file."""


mqtt(config.get("domains"))
def main():
"""Starts MQTT listener.
Raises:
DomainsNotInConfig: If no domains were found in configuration file.
"""
domains = config.load_config().get("domains")
if not domains:
raise DomainsNotInConfig("Could not locate domains in configuration.")
mqtt.connect(domains)


if __name__ == "__main__":
Expand Down
28 changes: 28 additions & 0 deletions wgkex/worker/app_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Unit tests for app.py"""
import unittest
import mock
import app


class AppTest(unittest.TestCase):
@mock.patch.object(app.config, "load_config")
@mock.patch.object(app.mqtt, "connect", autospec=True)
def test_main_success(self, connect_mock, config_mock):
"""Ensure we can execute main."""
connect_mock.return_value = None
config_mock.return_value = dict(domains=["domain.one"])
app.main()
connect_mock.assert_called_with(["domain.one"])

@mock.patch.object(app.config, "load_config")
@mock.patch.object(app.mqtt, "connect", autospec=True)
def test_main_fails_no_domain(self, connect_mock, config_mock):
"""Ensure we fail when domains are not configured."""
connect_mock.return_value = None
config_mock.return_value = dict(domains=None)
with self.assertRaises(app.DomainsNotInConfig):
app.main()


if __name__ == "__main__":
unittest.main()

0 comments on commit 69455d5

Please sign in to comment.