diff --git a/cbpi/cli.py b/cbpi/cli.py index 61a43aa..815e30e 100644 --- a/cbpi/cli.py +++ b/cbpi/cli.py @@ -1,15 +1,12 @@ import logging import requests -import yaml from cbpi.configFolder import ConfigFolder from cbpi.utils.utils import load_config from zipfile import ZipFile from cbpi.craftbeerpi import CraftBeerPi import os -import pathlib import pkgutil import shutil -import yaml import click from subprocess import call from colorama import Fore, Back, Style @@ -18,120 +15,211 @@ from importlib_metadata import metadata from tabulate import tabulate from PyInquirer import prompt, print_json -def create_home_folder_structure(configFolder): - pathlib.Path(os.path.join(".", 'logs/sensors')).mkdir(parents=True, exist_ok=True) - - configFolder.create_folders() - print("Folder created") +class CraftBeerPiCli(): + def __init__(self, config) -> None: + self.config = config + pass -def setup_one_wire(): - print("Setting up 1Wire") - with open('/boot/config.txt', 'w') as f: - f.write("dtoverlay=w1-gpio,gpiopin=4,pullup=on") - print("/boot/config.txt created") + def setup(self): + print("Setting up CraftBeerPi") + self.config.create_home_folder_structure() + self.config.create_config_file() -def list_one_wire(): - print("List 1Wire") - call(["modprobe", "w1-gpio"]) - call(["modprobe", "w1-therm"]) - try: - for dirname in os.listdir('/sys/bus/w1/devices'): - if (dirname.startswith("28") or dirname.startswith("10")): - print(dirname) - except Exception as e: - print(e) + def start(self): + if self.config.check_for_setup() is False: + return + print("START") + cbpi = CraftBeerPi(self.config) + cbpi.start() + def setup_one_wire(self): + print("Setting up 1Wire") + with open('/boot/config.txt', 'w') as f: + f.write("dtoverlay=w1-gpio,gpiopin=4,pullup=on") + print("/boot/config.txt created") -def plugins_list(): - result = [] - print("") - print(Fore.LIGHTYELLOW_EX,"List of active plugins", Style.RESET_ALL) - print("") - discovered_plugins = { - name: importlib.import_module(name) - for finder, name, ispkg - in pkgutil.iter_modules() - if name.startswith('cbpi') and len(name) > 4 - } - for key, module in discovered_plugins.items(): - + def list_one_wire(self): + print("List 1Wire") + call(["modprobe", "w1-gpio"]) + call(["modprobe", "w1-therm"]) try: - meta = metadata(key) - result.append(dict(Name=meta["Name"], Version=meta["Version"], Author=meta["Author"], Homepage=meta["Home-page"], Summary=meta["Summary"])) - + for dirname in os.listdir('/sys/bus/w1/devices'): + if (dirname.startswith("28") or dirname.startswith("10")): + print(dirname) except Exception as e: print(e) - print(Fore.LIGHTGREEN_EX, tabulate(result, headers="keys"), Style.RESET_ALL) - -def plugin_create(): - print("Plugin Creation") - print("") - - questions = [ - { - 'type': 'input', - 'name': 'name', - 'message': 'Plugin Name:', + def plugins_list(self): + result = [] + print("") + print(Fore.LIGHTYELLOW_EX,"List of active plugins", Style.RESET_ALL) + print("") + discovered_plugins = { + name: importlib.import_module(name) + for finder, name, ispkg + in pkgutil.iter_modules() + if name.startswith('cbpi') and len(name) > 4 } - ] + for key, module in discovered_plugins.items(): + try: + meta = metadata(key) + result.append(dict(Name=meta["Name"], Version=meta["Version"], Author=meta["Author"], Homepage=meta["Home-page"], Summary=meta["Summary"])) + + except Exception as e: + print(e) + print(Fore.LIGHTGREEN_EX, tabulate(result, headers="keys"), Style.RESET_ALL) - answers = prompt(questions) - name = "cbpi4_" + answers["name"] - if os.path.exists(os.path.join(".", name)) is True: - print("Cant create Plugin. Folder {} already exists ".format(name)) - return + def plugin_create(self): + print("Plugin Creation") + print("") - url = 'https://github.com/Manuel83/craftbeerpi4-plugin-template/archive/main.zip' - r = requests.get(url) - with open('temp.zip', 'wb') as f: - f.write(r.content) + questions = [ + { + 'type': 'input', + 'name': 'name', + 'message': 'Plugin Name:', + } + ] - with ZipFile('temp.zip', 'r') as repo_zip: - repo_zip.extractall() + answers = prompt(questions) - os.rename("./craftbeerpi4-plugin-template-main", os.path.join(".", name)) - os.rename(os.path.join(".", name, "src"), os.path.join(".", name, name)) + name = "cbpi4_" + answers["name"] + if os.path.exists(os.path.join(".", name)) is True: + print("Cant create Plugin. Folder {} already exists ".format(name)) + return - import jinja2 + url = 'https://github.com/Manuel83/craftbeerpi4-plugin-template/archive/main.zip' + r = requests.get(url) + with open('temp.zip', 'wb') as f: + f.write(r.content) - templateLoader = jinja2.FileSystemLoader(searchpath=os.path.join(".", name)) - templateEnv = jinja2.Environment(loader=templateLoader) - TEMPLATE_FILE = "setup.py" - template = templateEnv.get_template(TEMPLATE_FILE) - outputText = template.render(name=name) + with ZipFile('temp.zip', 'r') as repo_zip: + repo_zip.extractall() - with open(os.path.join(".", name, "setup.py"), "w") as fh: - fh.write(outputText) + os.rename("./craftbeerpi4-plugin-template-main", os.path.join(".", name)) + os.rename(os.path.join(".", name, "src"), os.path.join(".", name, name)) - TEMPLATE_FILE = "MANIFEST.in" - template = templateEnv.get_template(TEMPLATE_FILE) - outputText = template.render(name=name) - with open(os.path.join(".", name, "MANIFEST.in"), "w") as fh: - fh.write(outputText) + import jinja2 - TEMPLATE_FILE = os.path.join("/", name, "config.yaml") - operatingsystem = str(platform.system()).lower() - if operatingsystem.startswith("win"): - TEMPLATE_FILE=str(TEMPLATE_FILE).replace('\\','/') - - template = templateEnv.get_template(TEMPLATE_FILE) - outputText = template.render(name=name) + templateLoader = jinja2.FileSystemLoader(searchpath=os.path.join(".", name)) + templateEnv = jinja2.Environment(loader=templateLoader) + TEMPLATE_FILE = "setup.py" + template = templateEnv.get_template(TEMPLATE_FILE) + outputText = template.render(name=name) - with open(os.path.join(".", name, name, "config.yaml"), "w") as fh: - fh.write(outputText) + with open(os.path.join(".", name, "setup.py"), "w") as fh: + fh.write(outputText) - print("") - print("") - print("Plugin {}{}{} created! ".format(Fore.LIGHTGREEN_EX, name, Style.RESET_ALL) ) - print("") - print("Developer Documentation: https://openbrewing.gitbook.io/craftbeerpi4_support/readme/development") - print("") - print("Happy developing! Cheers") - print("") - print("") + TEMPLATE_FILE = "MANIFEST.in" + template = templateEnv.get_template(TEMPLATE_FILE) + outputText = template.render(name=name) + with open(os.path.join(".", name, "MANIFEST.in"), "w") as fh: + fh.write(outputText) + + TEMPLATE_FILE = os.path.join("/", name, "config.yaml") + operatingsystem = str(platform.system()).lower() + if operatingsystem.startswith("win"): + TEMPLATE_FILE=str(TEMPLATE_FILE).replace('\\','/') + + template = templateEnv.get_template(TEMPLATE_FILE) + outputText = template.render(name=name) + + with open(os.path.join(".", name, name, "config.yaml"), "w") as fh: + fh.write(outputText) + + print("") + print("") + print("Plugin {}{}{} created! ".format(Fore.LIGHTGREEN_EX, name, Style.RESET_ALL) ) + print("") + print("Developer Documentation: https://openbrewing.gitbook.io/craftbeerpi4_support/readme/development") + print("") + print("Happy developing! Cheers") + print("") + print("") + + def autostart(self, name): + '''Enable or disable autostart''' + if(name == "status"): + if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is True: + print("CraftBeerPi Autostart is {}ON{}".format(Fore.LIGHTGREEN_EX,Style.RESET_ALL)) + else: + print("CraftBeerPi Autostart is {}OFF{}".format(Fore.RED,Style.RESET_ALL)) + elif(name == "on"): + print("Add craftbeerpi.service to systemd") + try: + if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is False: + srcfile = self.config.get_file_path("craftbeerpi.service") + destfile = os.path.join("/etc/systemd/system") + shutil.copy(srcfile, destfile) + print("Copied craftbeerpi.service to /etc/systemd/system") + os.system('systemctl enable craftbeerpi.service') + print('Enabled craftbeerpi service') + os.system('systemctl start craftbeerpi.service') + print('Started craftbeerpi.service') + else: + print("craftbeerpi.service is already located in /etc/systemd/system") + except Exception as e: + print(e) + return + return + elif(name == "off"): + print("Remove craftbeerpi.service from systemd") + try: + status = os.popen('systemctl list-units --type=service --state=running | grep craftbeerpi.service').read() + if status.find("craftbeerpi.service") != -1: + os.system('systemctl stop craftbeerpi.service') + print('Stopped craftbeerpi service') + os.system('systemctl disable craftbeerpi.service') + print('Removed craftbeerpi.service as service') + else: + print('craftbeerpi.service service is not running') + + if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is True: + os.remove(os.path.join("/etc/systemd/system","craftbeerpi.service")) + print("Deleted craftbeerpi.service from /etc/systemd/system") + else: + print("craftbeerpi.service is not located in /etc/systemd/system") + except Exception as e: + print(e) + return + return + + + def chromium(self, name): + '''Enable or disable autostart''' + if(name == "status"): + if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is True: + print("CraftBeerPi Chromium Desktop is {}ON{}".format(Fore.LIGHTGREEN_EX,Style.RESET_ALL)) + else: + print("CraftBeerPi Chromium Desktop is {}OFF{}".format(Fore.RED,Style.RESET_ALL)) + elif(name == "on"): + print("Add chromium.desktop to /etc/xdg/autostart/") + try: + if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is False: + srcfile = self.config.get_file_path("chromium.desktop") + destfile = os.path.join("/etc/xdg/autostart/") + shutil.copy(srcfile, destfile) + print("Copied chromium.desktop to /etc/xdg/autostart/") + else: + print("chromium.desktop is already located in /etc/xdg/autostart/") + except Exception as e: + print(e) + return + return + elif(name == "off"): + print("Remove chromium.desktop from /etc/xdg/autostart/") + try: + if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is True: + os.remove(os.path.join("/etc/xdg/autostart/","chromium.desktop")) + print("Deleted chromium.desktop from /etc/xdg/autostart/") + else: + print("chromium.desktop is not located in /etc/xdg/autostart/") + except Exception as e: + print(e) + return + return @click.group() @@ -143,139 +231,55 @@ def main(context, config_folder_path): print("---------------------") level = logging.INFO logging.basicConfig(level=level, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') - context.obj = ConfigFolder(config_folder_path) + cbpi_cli = CraftBeerPiCli(ConfigFolder(config_folder_path)) + context.obj = cbpi_cli pass - @main.command() @click.pass_context def setup(context): '''Create Config folder''' - print("Setting up CraftBeerPi") - create_home_folder_structure(context.obj) - context.obj.create_config_file() - + context.obj.setup() @main.command() +@click.pass_context @click.option('--list', is_flag=True, help="List all 1Wire Devices") @click.option('--setup', is_flag=True, help="Setup 1Wire on Raspberry Pi") -def onewire(list, setup): +def onewire(context, list, setup): '''Setup 1wire on Raspberry Pi''' if setup is True: - setup_one_wire() + context.obj.setup_one_wire() if list is True: - list_one_wire() - + context.obj.list_one_wire() @main.command() @click.pass_context def start(context): - '''Lets go brewing''' - if context.obj.check_for_setup() is False: - return - print("Starting up CraftBeerPi ...") - cbpi = CraftBeerPi(context.obj) - cbpi.start() - + context.obj.start() @main.command() @click.pass_context def plugins(context): '''List active plugins''' - plugins_list() - return - + context.obj.plugins_list() @click.command() -def create(): +@click.pass_context +def create(context): '''Create New Plugin''' - plugin_create() - + context.obj.plugin_create() @main.command() -@click.argument('name') @click.pass_context +@click.argument('name') def autostart(context, name): '''(on|off|status) Enable or disable autostart''' - if(name == "status"): - if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is True: - print("CraftBeerPi Autostart is {}ON{}".format(Fore.LIGHTGREEN_EX,Style.RESET_ALL)) - else: - print("CraftBeerPi Autostart is {}OFF{}".format(Fore.RED,Style.RESET_ALL)) - elif(name == "on"): - print("Add craftbeerpi.service to systemd") - try: - if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is False: - srcfile = context.obj.get_file_path("craftbeerpi.service") - destfile = os.path.join("/etc/systemd/system") - shutil.copy(srcfile, destfile) - print("Copied craftbeerpi.service to /etc/systemd/system") - os.system('systemctl enable craftbeerpi.service') - print('Enabled craftbeerpi service') - os.system('systemctl start craftbeerpi.service') - print('Started craftbeerpi.service') - else: - print("craftbeerpi.service is already located in /etc/systemd/system") - except Exception as e: - print(e) - return - return - elif(name == "off"): - print("Remove craftbeerpi.service from systemd") - try: - status = os.popen('systemctl list-units --type=service --state=running | grep craftbeerpi.service').read() - if status.find("craftbeerpi.service") != -1: - os.system('systemctl stop craftbeerpi.service') - print('Stopped craftbeerpi service') - os.system('systemctl disable craftbeerpi.service') - print('Removed craftbeerpi.service as service') - else: - print('craftbeerpi.service service is not running') - - if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is True: - os.remove(os.path.join("/etc/systemd/system","craftbeerpi.service")) - print("Deleted craftbeerpi.service from /etc/systemd/system") - else: - print("craftbeerpi.service is not located in /etc/systemd/system") - except Exception as e: - print(e) - return - return + context.obj.autostart(name) @main.command() -@click.argument('name') @click.pass_context +@click.argument('name') def chromium(context, name): '''(on|off|status) Enable or disable Kiosk mode''' - if(name == "status"): - if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is True: - print("CraftBeerPi Chromium Desktop is {}ON{}".format(Fore.LIGHTGREEN_EX,Style.RESET_ALL)) - else: - print("CraftBeerPi Chromium Desktop is {}OFF{}".format(Fore.RED,Style.RESET_ALL)) - elif(name == "on"): - print("Add chromium.desktop to /etc/xdg/autostart/") - try: - if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is False: - srcfile = context.obj.get_file_path("chromium.desktop") - destfile = os.path.join("/etc/xdg/autostart/") - shutil.copy(srcfile, destfile) - print("Copied chromium.desktop to /etc/xdg/autostart/") - else: - print("chromium.desktop is already located in /etc/xdg/autostart/") - except Exception as e: - print(e) - return - return - elif(name == "off"): - print("Remove chromium.desktop from /etc/xdg/autostart/") - try: - if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is True: - os.remove(os.path.join("/etc/xdg/autostart/","chromium.desktop")) - print("Deleted chromium.desktop from /etc/xdg/autostart/") - else: - print("chromium.desktop is not located in /etc/xdg/autostart/") - except Exception as e: - print(e) - return - return \ No newline at end of file + context.obj.chromium(name) diff --git a/cbpi/configFolder.py b/cbpi/configFolder.py index 699f6c4..5641c92 100644 --- a/cbpi/configFolder.py +++ b/cbpi/configFolder.py @@ -103,6 +103,11 @@ class ConfigFolder: print("Config Folder created") + def create_home_folder_structure(configFolder): + pathlib.Path(os.path.join(".", 'logs/sensors')).mkdir(parents=True, exist_ok=True) + + configFolder.create_folders() + print("Folder created") def create_folders(self): pathlib.Path(self._rawPath).mkdir(parents=True, exist_ok=True) diff --git a/cbpi/http_endpoints/http_actor.py b/cbpi/http_endpoints/http_actor.py index 0572206..8a6ea3b 100644 --- a/cbpi/http_endpoints/http_actor.py +++ b/cbpi/http_endpoints/http_actor.py @@ -23,6 +23,32 @@ class ActorHttpEndpoints(): description: successful operation """ return web.json_response(data=self.controller.get_state()) + + @request_mapping(path="/{id:\w+}", auth_required=False) + async def http_get_one(self, request): + """ + --- + description: Get one Actor + tags: + - Actor + parameters: + - name: "id" + in: "path" + description: "Actor ID" + required: true + type: "integer" + format: "int64" + responses: + "200": + description: successful operation + "404": + description: Actor not found + """ + actor = self.controller.find_by_id(request.match_info['id']) + if (actor is None): + return web.json_response(status=404) + + return web.json_response(data=actor.to_dict(), status=200) @request_mapping(path="/", method="POST", auth_required=False) diff --git a/tests/cbpi-test-config/actor.json b/tests/cbpi-test-config/actor.json new file mode 100644 index 0000000..9e413ee --- /dev/null +++ b/tests/cbpi-test-config/actor.json @@ -0,0 +1,12 @@ +{ + "data": [ + { + "id": "3CUJte4bkxDMFCtLX8eqsX", + "name": "SomeActor", + "power": 100, + "props": {}, + "state": false, + "type": "DummyActor" + } + ] +} \ No newline at end of file diff --git a/tests/cbpi-test-config/chromium.desktop b/tests/cbpi-test-config/chromium.desktop new file mode 100644 index 0000000..a112515 --- /dev/null +++ b/tests/cbpi-test-config/chromium.desktop @@ -0,0 +1,6 @@ +[Desktop Entry] +Type=Application +Name=Chromium +Comment=Chromium Webbrowser +NoDisplay=false +Exec=chromium-browser --noerrordialogs --disable-session-crashed-bubble --disable-infobars --force-device-scale-factor=1.00 --start-fullscreen "http://localhost:8000" diff --git a/tests/cbpi-test-config/config.json b/tests/cbpi-test-config/config.json new file mode 100644 index 0000000..56f98c5 --- /dev/null +++ b/tests/cbpi-test-config/config.json @@ -0,0 +1,148 @@ +{ + "AUTHOR": { + "description": "Author", + "name": "AUTHOR", + "options": null, + "type": "string", + "value": "John Doe" + }, + "BREWERY_NAME": { + "description": "Brewery Name", + "name": "BREWERY_NAME", + "options": null, + "type": "string", + "value": "CraftBeerPi Brewery" + }, + "MASH_TUN": { + "description": "Default Mash Tun", + "name": "MASH_TUN", + "options": null, + "type": "kettle", + "value": "" + }, + "AddMashInStep": { + "description": "Add MashIn Step automatically if not defined in recipe", + "name": "AddMashInStep", + "options": [ + { + "label": "Yes", + "value": "Yes" + }, + { + "label": "No", + "value": "No" + } + ], + "type": "select", + "value": "Yes" + }, + "RECIPE_CREATION_PATH": { + "description": "API path to creation plugin. Default: empty", + "name": "RECIPE_CREATION_PATH", + "options": null, + "type": "string", + "value": "" + }, + "brewfather_api_key": { + "description": "Brewfather API Kay", + "name": "brewfather_api_key", + "options": null, + "type": "string", + "value": "" + }, + "brewfather_user_id": { + "description": "Brewfather User ID", + "name": "brewfather_user_id", + "options": null, + "type": "string", + "value": "" + }, + "TEMP_UNIT": { + "description": "Temperature Unit", + "name": "TEMP_UNIT", + "options": [ + { + "label": "C", + "value": "C" + }, + { + "label": "F", + "value": "F" + } + ], + "type": "select", + "value": "C" + }, + "AutoMode": { + "description": "Use AutoMode in steps", + "name": "AutoMode", + "options": [ + { + "label": "Yes", + "value": "Yes" + }, + { + "label": "No", + "value": "No" + } + ], + "type": "select", + "value": "Yes" + }, + "steps_boil": { + "description": "Boil step type", + "name": "steps_boil", + "options": null, + "type": "step", + "value": "BoilStep" + }, + "steps_boil_temp": { + "description": "Default Boil Temperature for Recipe Creation", + "name": "steps_boil_temp", + "options": null, + "type": "number", + "value": "99" + }, + "steps_cooldown": { + "description": "Cooldown step type", + "name": "steps_cooldown", + "options": null, + "type": "step", + "value": "CooldownStep" + }, + "steps_cooldown_sensor": { + "description": "Alternative Sensor to monitor temperature durring cooldown (if not selected, Kettle Sensor will be used)", + "name": "steps_cooldown_sensor", + "options": null, + "type": "sensor", + "value": "" + }, + "steps_cooldown_temp": { + "description": "Cooldown temp will send notification when this temeprature is reached", + "name": "steps_cooldown_temp", + "options": null, + "type": "number", + "value": "20" + }, + "steps_mash": { + "description": "Mash step type", + "name": "steps_mash", + "options": null, + "type": "step", + "value": "MashStep" + }, + "steps_mashin": { + "description": "MashIn step type", + "name": "steps_mashin", + "options": null, + "type": "step", + "value": "MashInStep" + }, + "steps_mashout": { + "description": "MashOut step type", + "name": "steps_mashout", + "options": null, + "type": "step", + "value": "NotificationStep" + } +} diff --git a/tests/cbpi-test-config/config.yaml b/tests/cbpi-test-config/config.yaml new file mode 100644 index 0000000..f7a5abe --- /dev/null +++ b/tests/cbpi-test-config/config.yaml @@ -0,0 +1,20 @@ + +name: CraftBeerPi +version: 4.0.8 + +index_url: /cbpi_ui/static/index.html + +port: 8000 + +mqtt: false +mqtt_host: localhost +mqtt_port: 1883 +mqtt_username: "" +mqtt_password: "" + +username: cbpi +password: 123 + +plugins: +- cbpi4ui + diff --git a/tests/cbpi-test-config/craftbeerpi.service b/tests/cbpi-test-config/craftbeerpi.service new file mode 100644 index 0000000..cd02dce --- /dev/null +++ b/tests/cbpi-test-config/craftbeerpi.service @@ -0,0 +1,9 @@ +[Unit] +Description=Craftbeer Pi + +[Service] +WorkingDirectory=/home/pi +ExecStart=/usr/local/bin/cbpi start + +[Install] +WantedBy=multi-user.target diff --git a/tests/cbpi-test-config/craftbeerpiboot b/tests/cbpi-test-config/craftbeerpiboot new file mode 100644 index 0000000..9c4ca4b --- /dev/null +++ b/tests/cbpi-test-config/craftbeerpiboot @@ -0,0 +1,62 @@ +#!/bin/sh + +### BEGIN INIT INFO +# Provides: craftbeerpi +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Put a short description of the service here +# Description: Put a long description of the service here +### END INIT INFO + +# Change the next 3 lines to suit where you install your script and what you want to call it +DIR=#DIR# +DAEMON=$DIR/cbpi +DAEMON_NAME=CraftBeerPI + +# Add any command line options for your daemon here +DAEMON_OPTS="" + +# This next line determines what user the script runs as. +# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python. +DAEMON_USER=root + +# The process ID of the script when it runs is stored here: +PIDFILE=/var/run/$DAEMON_NAME.pid + +. /lib/lsb/init-functions + +do_start () { + log_daemon_msg "Starting system $DAEMON_NAME daemon" + start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --chdir $DIR --startas $DAEMON -- $DAEMON_OPTS + log_end_msg $? +} +do_stop () { + log_daemon_msg "Stopping system $DAEMON_NAME daemon" + start-stop-daemon --stop --pidfile $PIDFILE --retry 10 + log_end_msg $? +} + +case "$1" in + + start|stop) + do_${1} + ;; + + restart|reload|force-reload) + do_stop + do_start + ;; + + status) + status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $? + ;; + + *) + echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}" + exit 1 + ;; + +esac +exit 0 \ No newline at end of file diff --git a/tests/cbpi-test-config/dashboard/cbpi_dashboard_1.json b/tests/cbpi-test-config/dashboard/cbpi_dashboard_1.json new file mode 100644 index 0000000..92079a0 --- /dev/null +++ b/tests/cbpi-test-config/dashboard/cbpi_dashboard_1.json @@ -0,0 +1,3 @@ +{ + "elements": [] +} \ No newline at end of file diff --git a/tests/cbpi-test-config/dashboard/widgets/.empty b/tests/cbpi-test-config/dashboard/widgets/.empty new file mode 100644 index 0000000..e69de29 diff --git a/tests/cbpi-test-config/fermenter_data.json b/tests/cbpi-test-config/fermenter_data.json new file mode 100644 index 0000000..f788313 --- /dev/null +++ b/tests/cbpi-test-config/fermenter_data.json @@ -0,0 +1,5 @@ +{ + "data": [ + + ] +} \ No newline at end of file diff --git a/tests/cbpi-test-config/kettle.json b/tests/cbpi-test-config/kettle.json new file mode 100644 index 0000000..f788313 --- /dev/null +++ b/tests/cbpi-test-config/kettle.json @@ -0,0 +1,5 @@ +{ + "data": [ + + ] +} \ No newline at end of file diff --git a/tests/cbpi-test-config/plugin_list.txt b/tests/cbpi-test-config/plugin_list.txt new file mode 100644 index 0000000..da44c91 --- /dev/null +++ b/tests/cbpi-test-config/plugin_list.txt @@ -0,0 +1,3 @@ +cbpi4-ui: + installation_date: '2021-01-06 16:03:31' + version: '0.0.1' \ No newline at end of file diff --git a/tests/cbpi-test-config/recipes/.empty b/tests/cbpi-test-config/recipes/.empty new file mode 100644 index 0000000..e69de29 diff --git a/tests/cbpi-test-config/sensor.json b/tests/cbpi-test-config/sensor.json new file mode 100644 index 0000000..6346c5f --- /dev/null +++ b/tests/cbpi-test-config/sensor.json @@ -0,0 +1,5 @@ +{ + "data": [ + + ] +} \ No newline at end of file diff --git a/tests/cbpi-test-config/splash.png b/tests/cbpi-test-config/splash.png new file mode 100644 index 0000000..68086b5 Binary files /dev/null and b/tests/cbpi-test-config/splash.png differ diff --git a/tests/cbpi-test-config/step_data.json b/tests/cbpi-test-config/step_data.json new file mode 100644 index 0000000..60a4e28 --- /dev/null +++ b/tests/cbpi-test-config/step_data.json @@ -0,0 +1,8 @@ +{ + "basic": { + "name": "" + }, + "steps": [ + + ] +} \ No newline at end of file diff --git a/tests/cbpi-test-config/upload/.empty b/tests/cbpi-test-config/upload/.empty new file mode 100644 index 0000000..e69de29 diff --git a/tests/cbpi_config_fixture.py b/tests/cbpi_config_fixture.py new file mode 100644 index 0000000..b9824ba --- /dev/null +++ b/tests/cbpi_config_fixture.py @@ -0,0 +1,23 @@ +# content of conftest.py +from codecs import ignore_errors +from distutils.command.config import config +import os +from cbpi.configFolder import ConfigFolder +from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import AioHTTPTestCase +from distutils.dir_util import copy_tree + + +class CraftBeerPiTestCase(AioHTTPTestCase): + + async def get_application(self): + self.config_folder = self.configuration() + self.cbpi = CraftBeerPi(self.config_folder) + await self.cbpi.init_serivces() + return self.cbpi.app + + def configuration(self): + test_directory = os.path.dirname(__file__) + test_config_directory = os.path.join(test_directory, 'cbpi-test-config') + configFolder = ConfigFolder(test_config_directory) + return configFolder diff --git a/tests/test_actor.py b/tests/test_actor.py index d808fe4..69ecc82 100644 --- a/tests/test_actor.py +++ b/tests/test_actor.py @@ -1,66 +1,39 @@ import logging from unittest import mock -from unittest.mock import MagicMock, Mock -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop -from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') -class ActorTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app - - @unittest_run_loop - async def test_actor_mock(self): - with mock.patch.object(self.cbpi.bus, 'fire', wraps=self.cbpi.bus.fire) as mock_obj: - # Send HTTP POST - resp = await self.client.request("POST", "/actor/1/on") - # Check Result - assert resp.status == 204 - # Check if Event are fired - assert mock_obj.call_count == 2 - +class ActorTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_actor_switch(self): resp = await self.client.post(path="/login", data={"username": "cbpi", "password": "123"}) - assert resp.status == 200 + assert resp.status == 200, "login should be successful" - resp = await self.client.request("POST", "/actor/1/on") - assert resp.status == 204 - i = await self.cbpi.actor.get_one(1) + resp = await self.client.request("POST", "/actor/3CUJte4bkxDMFCtLX8eqsX/on") + assert resp.status == 204, "switching actor on should work" + i = self.cbpi.actor.find_by_id("3CUJte4bkxDMFCtLX8eqsX") assert i.instance.state is True - resp = await self.client.request("POST", "/actor/1/off") + resp = await self.client.request("POST", "/actor/3CUJte4bkxDMFCtLX8eqsX/off") assert resp.status == 204 - i = await self.cbpi.actor.get_one(1) - assert i.instance.state is False - - resp = await self.client.request("POST", "/actor/1/toggle") - - assert resp.status == 204 - i = await self.cbpi.actor.get_one(1) - assert i.instance.state is True - - resp = await self.client.request("POST", "/actor/1/toggle") - assert resp.status == 204 - i = await self.cbpi.actor.get_one(1) + i = self.cbpi.actor.find_by_id("3CUJte4bkxDMFCtLX8eqsX") assert i.instance.state is False @unittest_run_loop async def test_crud(self): data = { - "name": "CustomActor", - "type": "CustomActor", - "config": { - "interval": 5 - } + "name": "SomeActor", + "power": 100, + "props": { + }, + "state": False, + "type": "DummyActor" } # Add new sensor diff --git a/tests/test_cli.py b/tests/test_cli.py index 1e41833..0a9f705 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,20 +1,16 @@ import logging import unittest +from cbpi.cli import CraftBeerPiCli -from cli import add, remove, list_plugins +from cbpi.configFolder import ConfigFolder logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') - class CLITest(unittest.TestCase): - def test_install(self): - assert add("cbpi4-ui-plugin") == True - assert add("cbpi4-ui-plugin") == False - assert remove("cbpi4-ui-plugin") == True - def test_list(self): - list_plugins() + cli = CraftBeerPiCli(ConfigFolder("./cbpi-test-config")) + cli.plugins_list() if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/tests/test_config.py b/tests/test_config.py index 90c7770..ffe7cd0 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,44 +1,31 @@ import time -import aiosqlite -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop -from cbpi.api.config import ConfigType - -from cbpi.craftbeerpi import CraftBeerPi - - -class ConfigTestCase(AioHTTPTestCase): - - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase +class ConfigTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_get(self): - assert self.cbpi.config.get("CBPI_TEST_1", 1) == "22" + assert self.cbpi.config.get("steps_boil_temp", 1) == "99" @unittest_run_loop async def test_set_get(self): - value = str(time.time()) + value = 35 - await self.cbpi.config.set("CBPI_TEST_2", value) - - assert self.cbpi.config.get("CBPI_TEST_2", 1) == value + await self.cbpi.config.set("steps_cooldown_temp", value) + assert self.cbpi.config.get("steps_cooldown_temp", 1) == value @unittest_run_loop async def test_http_set(self): - value = str(time.time()) - key = "CBPI_TEST_3" - await self.cbpi.config.set(key, value) - assert self.cbpi.config.get(key, 1) == value + value = "Some New Brewery Name" + key = "BREWERY_NAME" - resp = await self.client.request("PUT", "/config/%s/" % key, json={'value': '1'}) + resp = await self.client.request("PUT", "/config/%s/" % key, json={'value': value}) assert resp.status == 204 - assert self.cbpi.config.get(key, -1) == "1" + + assert self.cbpi.config.get(key, -1) == value @unittest_run_loop async def test_http_get(self): @@ -47,5 +34,5 @@ class ConfigTestCase(AioHTTPTestCase): @unittest_run_loop async def test_get_default(self): - value = self.cbpi.config.get("HELLO_WORLD", None) - assert value == None \ No newline at end of file + value = self.cbpi.config.get("HELLO_WORLD", "DefaultValue") + assert value == "DefaultValue" \ No newline at end of file diff --git a/tests/test_dashboard.py b/tests/test_dashboard.py index 6066130..ea7bbee 100644 --- a/tests/test_dashboard.py +++ b/tests/test_dashboard.py @@ -1,18 +1,10 @@ -import aiohttp -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase from cbpi.craftbeerpi import CraftBeerPi -class DashboardTestCase(AioHTTPTestCase): - - - - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +class DashboardTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_crud(self): @@ -28,56 +20,12 @@ class DashboardTestCase(AioHTTPTestCase): "config": {} } - resp = await self.client.get(path="/dashboard") + resp = await self.client.get(path="/dashboard/current") assert resp.status == 200 - # Add new dashboard - resp = await self.client.post(path="/dashboard/", json=data) - assert resp.status == 200 + dashboard_id = await resp.json() - m = await resp.json() - dashboard_id = m["id"] - - # Get dashboard - resp = await self.client.get(path="/dashboard/%s" % dashboard_id) - assert resp.status == 200 - - m2 = await resp.json() - dashboard_id = m2["id"] - - # Update dashboard - resp = await self.client.put(path="/dashboard/%s" % dashboard_id, json=m) - assert resp.status == 200 - - # Add dashboard content + # Add dashboard content dashboard_content["dbid"] = dashboard_id resp = await self.client.post(path="/dashboard/%s/content" % dashboard_id, json=dashboard_content) - assert resp.status == 200 - m_content = await resp.json() - print("CONTENT", m_content) - content_id = m_content["id"] - # Get dashboard - resp = await self.client.get(path="/dashboard/%s/content" % (dashboard_id)) - assert resp.status == 200 - - - resp = await self.client.post(path="/dashboard/%s/content/%s/move" % (dashboard_id, content_id), json=dict(x=1,y=1)) - assert resp.status == 200 - - resp = await self.client.delete(path="/dashboard/%s/content/%s" % (dashboard_id, content_id)) - assert resp.status == 204 - - # Delete dashboard - resp = await self.client.delete(path="/dashboard/%s" % dashboard_id) - assert resp.status == 204 - - @unittest_run_loop - async def test_dashboard_controller(self): - result = await self.cbpi.dashboard.get_all() - print(result) - - await self.cbpi.dashboard.add(**{"name":"Tewst"}) - print(await self.cbpi.dashboard.get_one(1)) - - await self.cbpi.dashboard.add_content(dict(dbid=1,element_id=1,type="test",config={"name":"Manue"})) - await self.cbpi.dashboard.move_content(1,2,3) \ No newline at end of file + assert resp.status == 204 \ No newline at end of file diff --git a/tests/test_fermenter.py b/tests/test_fermenter.py deleted file mode 100644 index 96ccfc0..0000000 --- a/tests/test_fermenter.py +++ /dev/null @@ -1,116 +0,0 @@ -import asyncio -from cbpi.api.dataclasses import Fermenter, FermenterStep, Props, Step -import logging -from unittest import mock -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop -from cbpi.craftbeerpi import CraftBeerPi -from cbpi.controller.fermentation_controller import FermenationController -import unittest -import json -from aiohttp import web -from unittest.mock import MagicMock, Mock -logging.basicConfig(level=logging.INFO, - format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') - - -class FermenterTest(AioHTTPTestCase): - - async def get_application(self): - app = web.Application() - return app - - def create_file(self): - - data = [ - { - "id": "f1", - "name": "Fermenter1", - "props": {}, - "steps": [ - { - "id": "f1s1", - "name": "Step1", - "props": {}, - "state_text": "", - "status": "I", - "type": "T2" - }, - { - "id": "f1s2", - "name": "Step2", - "props": {}, - "state_text": "", - "status": "I", - "type": "T1" - }, - ], - "target_temp": 0 - }, - { - "id": "f2", - "name": "Fermenter2", - "props": {}, - "steps": [ - { - "id": "f2s1", - "name": "Step1", - "props": {}, - "state_text": "", - "status": "I", - "type": "T1" - }, - { - "id": "f2s2", - "name": "Step2", - "props": {}, - "state_text": "", - "status": "I", - "type": "T2" - }, - ], - "target_temp": 0 - } - ] - - with open("./config/fermenter_data.json", "w") as file: - json.dump(data, file, indent=4, sort_keys=True) - - - @unittest_run_loop - async def test_actor_mock(self): - self.create_file() - mock = Mock() - f = FermenationController(mock) - - f.types = { - "T1": {"name": "T2", "class": FermenterStep, "properties": [], "actions": []}, - "T2": {"name": "T2", "class": FermenterStep, "properties": [], "actions": []} - } - await f.load() - #ferm = Fermenter(name="Maneul") - # item = await f.create(ferm) - # await f.create_step(item.id, Step(name="Manuel")) - # await f.delete(item.id) - - item = await f.get("f1") - - await f.start("f1") - await f.start("f2") - await asyncio.sleep(3) - # await f.create_step(item.id, Step(name="MANUEL", props=Props())) - - #await f.start(item.id) - #await asyncio.sleep(1) - #await f.next(item.id) - #await asyncio.sleep(1) - #await f.next(item.id) - #await asyncio.sleep(1) - #await f.next(item.id) - #await asyncio.sleep(1) - #await f.move_step("f1", "f1s1", 1) - # await f.reset(item.id) - await f.shutdown() - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_gpio.py b/tests/test_gpio.py index d5279c0..c5cba85 100644 --- a/tests/test_gpio.py +++ b/tests/test_gpio.py @@ -15,13 +15,6 @@ except Exception: patcher.start() import RPi.GPIO as GPIO - -class HelloWorld(object): - - def test(self, a): - return a - - class TestSwitch(unittest.TestCase): GPIO_NUM = 22 @@ -35,15 +28,4 @@ class TestSwitch(unittest.TestCase): @patch("RPi.GPIO.output") def test_switch_without_scheduler_starts_disabled(self, patched_output): GPIO.output(self.GPIO_NUM, GPIO.LOW) - patched_output.assert_called_once_with(self.GPIO_NUM, GPIO.LOW) - - - def test_hello_world(self): - h = HelloWorld() - with mock.patch.object(HelloWorld, 'test', wraps=h.test) as fake_increment: - #print(h.test("HALLO")) - print(h.test("ABC")) - print(fake_increment.call_args) - print(h.test("HALLO")) - print(fake_increment.call_args_list) - + patched_output.assert_called_once_with(self.GPIO_NUM, GPIO.LOW) \ No newline at end of file diff --git a/tests/test_index.py b/tests/test_index.py index 06d8b20..c548113 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -1,14 +1,8 @@ -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop - -from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase -class IndexTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +class IndexTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_index(self): diff --git a/tests/test_kettle.py b/tests/test_kettle.py index 402af9e..b481d6a 100644 --- a/tests/test_kettle.py +++ b/tests/test_kettle.py @@ -1,52 +1,16 @@ -import asyncio -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop -from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase -class KettleTestCase(AioHTTPTestCase): - - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +class KettleTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_get(self): resp = await self.client.request("GET", "/kettle") assert resp.status == 200 - print(await resp.json()) - - @unittest_run_loop - async def test_heater(self): - resp = await self.client.get("/kettle/1/heater/on") - assert resp.status == 204 - - resp = await self.client.get("/kettle/1/heater/off") - assert resp.status == 204 - - @unittest_run_loop - async def test_agitator(self): - resp = await self.client.get("/kettle/1/agitator/on") - assert resp.status == 204 - - resp = await self.client.get("/kettle/1/agitator/off") - assert resp.status == 204 - - @unittest_run_loop - async def test_temp(self): - resp = await self.client.get("/kettle/1/temp") - assert resp.status == 204 - - resp = await self.client.get("/kettle/1/targettemp") - assert resp.status == 200 - - @unittest_run_loop - async def test_automatic(self): - resp = await self.client.post("/kettle/1/automatic") - assert resp.status == 204 - + kettle = resp.json() + assert kettle != None @unittest_run_loop async def test_crud(self): @@ -69,21 +33,21 @@ class KettleTestCase(AioHTTPTestCase): m = await resp.json() - sensor_id = m["id"] + kettle_id = m["id"] print("KETTLE", m["id"], m) - # Get sensor - resp = await self.client.get(path="/kettle/%s" % sensor_id) + + # Update Kettle + resp = await self.client.put(path="/kettle/%s" % kettle_id, json=m) assert resp.status == 200 - m2 = await resp.json() - sensor_id = m2["id"] - - # Update Sensor - resp = await self.client.put(path="/kettle/%s" % sensor_id, json=m) - assert resp.status == 200 + # Set Kettle target temp + resp = await self.client.post(path="/kettle/%s/target_temp" % kettle_id, json={"temp":75}) + assert resp.status == 204 # # Delete Sensor - resp = await self.client.delete(path="/kettle/%s" % sensor_id) + resp = await self.client.delete(path="/kettle/%s" % kettle_id) assert resp.status == 204 + + diff --git a/tests/test_logger.py b/tests/test_logger.py index cc9e7f3..d626ab0 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -1,21 +1,16 @@ import asyncio import glob -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase +import os -from cbpi.craftbeerpi import CraftBeerPi, load_config - - -class UtilsTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +class LoggerTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_log_data(self): + os.makedirs("./logs", exist_ok=True) log_name = "test" #clear all logs self.cbpi.log.clear_log(log_name) diff --git a/tests/test_notification_controller.py b/tests/test_notification_controller.py index b69bfc0..9de65ff 100644 --- a/tests/test_notification_controller.py +++ b/tests/test_notification_controller.py @@ -1,16 +1,8 @@ -import aiohttp -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop - -from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase -class NotificationTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app - +class NotificationTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_actor_switch(self): diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 7473469..250c4b9 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -1,14 +1,8 @@ -import asyncio -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop -from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase -class SensorTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +class SensorTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_crud(self): @@ -28,12 +22,11 @@ class SensorTestCase(AioHTTPTestCase): m = await resp.json() sensor_id = m["id"] - # Get sensor + # Get sensor value resp = await self.client.get(path="/sensor/%s"% sensor_id) assert resp.status == 200 m2 = await resp.json() - sensor_id = m2["id"] # Update Sensor resp = await self.client.put(path="/sensor/%s" % sensor_id, json=m) diff --git a/tests/test_step.py b/tests/test_step.py index e2ccb9d..a4ce794 100644 --- a/tests/test_step.py +++ b/tests/test_step.py @@ -1,23 +1,13 @@ import asyncio -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop -from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase - -class StepTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +class StepTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_get(self): - resp = await self.client.request("GET", "/step") - print(resp) - assert resp.status == 200 - - resp = await self.client.request("GET", "/step/types") + resp = await self.client.request("GET", "/step2") print(resp) assert resp.status == 200 @@ -31,26 +21,19 @@ class StepTestCase(AioHTTPTestCase): } # Add new step - resp = await self.client.post(path="/step/", json=data) + resp = await self.client.post(path="/step2/", json=data) assert resp.status == 200 m = await resp.json() print("Step", m) sensor_id = m["id"] - # Get sensor - resp = await self.client.get(path="/step/%s" % sensor_id) + # Update step + resp = await self.client.put(path="/step2/%s" % sensor_id, json=m) assert resp.status == 200 - m2 = await resp.json() - sensor_id = m2["id"] - - # Update Sensor - resp = await self.client.put(path="/step/%s" % sensor_id, json=m) - assert resp.status == 200 - - # # Delete Sensor - resp = await self.client.delete(path="/step/%s" % sensor_id) + # # Delete step + resp = await self.client.delete(path="/step2/%s" % sensor_id) assert resp.status == 204 def create_wait_callback(self, topic): @@ -66,34 +49,4 @@ class StepTestCase(AioHTTPTestCase): done, pending = await asyncio.wait({future}) if future in done: - pass - - @unittest_run_loop - async def test_process(self): - - step_ctlr = self.cbpi.step - - await step_ctlr.clear_all() - await step_ctlr.add(**{"name": "Kettle1", "type": "CustomStepCBPi", "config": {"name1": "1", "temp": 99}}) - await step_ctlr.add(**{"name": "Kettle1", "type": "CustomStepCBPi", "config": {"name1": "1", "temp": 99}}) - await step_ctlr.add(**{"name": "Kettle1", "type": "CustomStepCBPi", "config": {"name1": "1", "temp": 99}}) - - await step_ctlr.stop_all() - - future = self.create_wait_callback("step/+/started") - await step_ctlr.start() - await self.wait(future) - - for i in range(len(step_ctlr.cache)-1): - future = self.create_wait_callback("step/+/started") - await step_ctlr.next() - await self.wait(future) - - await self.print_steps() - - async def print_steps(self): - - s = await self.cbpi.step.get_all() - print(s) - for k, v in s.items(): - print(k, v.to_json()) + pass \ No newline at end of file diff --git a/tests/test_step_ng.py b/tests/test_step_ng.py deleted file mode 100644 index 7bc8383..0000000 --- a/tests/test_step_ng.py +++ /dev/null @@ -1,46 +0,0 @@ -import logging -from unittest import mock -import unittest -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop -from cbpi.craftbeerpi import CraftBeerPi -import pprint -import asyncio -logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') -pp = pprint.PrettyPrinter(indent=4) - -class ActorTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app - ''' - @unittest_run_loop - async def test_get_all(self): - resp = await self.client.get(path="/step2") - assert resp.status == 200 - - @unittest_run_loop - async def test_add_step(self): - resp = await self.client.post(path="/step2", json=dict(name="Manuel")) - data = await resp.json() - assert resp.status == 200 - - @unittest_run_loop - async def test_delete(self): - - resp = await self.client.post(path="/step2", json=dict(name="Manuel")) - data = await resp.json() - assert resp.status == 200 - resp = await self.client.delete(path="/step2/%s" % data["id"]) - assert resp.status == 204 - ''' - - @unittest_run_loop - async def test_move(self): - await self.cbpi.step2.resume() - - - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/tests/test_system.py b/tests/test_system.py index dd4de41..3cbbc7b 100644 --- a/tests/test_system.py +++ b/tests/test_system.py @@ -1,19 +1,11 @@ -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop - -from cbpi.craftbeerpi import CraftBeerPi +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase -class IndexTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +class IndexTestCase(CraftBeerPiTestCase): @unittest_run_loop async def test_endpoints(self): - - # Test Index Page resp = await self.client.post(path="/system/restart") assert resp.status == 200 @@ -21,9 +13,3 @@ class IndexTestCase(AioHTTPTestCase): resp = await self.client.post(path="/system/shutdown") assert resp.status == 200 - resp = await self.client.get(path="/system/jobs") - assert resp.status == 200 - - resp = await self.client.get(path="/system/events") - assert resp.status == 200 - diff --git a/tests/test_utils.py b/tests/test_utils.py deleted file mode 100644 index fdf4ba8..0000000 --- a/tests/test_utils.py +++ /dev/null @@ -1,16 +0,0 @@ -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop - -from cbpi.craftbeerpi import CraftBeerPi, load_config - - -class UtilsTestCase(AioHTTPTestCase): - - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app - - @unittest_run_loop - async def test_load_file(self): - assert load_config("") is None - diff --git a/tests/test_ws.py b/tests/test_ws.py index c0ebce8..862cfaf 100644 --- a/tests/test_ws.py +++ b/tests/test_ws.py @@ -1,56 +1,46 @@ -import asyncio - import aiohttp -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop +from aiohttp.test_utils import unittest_run_loop +from tests.cbpi_config_fixture import CraftBeerPiTestCase -from cbpi.craftbeerpi import CraftBeerPi +# class WebSocketTestCase(CraftBeerPiTestCase): +# @unittest_run_loop +# async def test_brewing_process(self): -class WebSocketTestCase(AioHTTPTestCase): +# count_step_done = 0 +# async with self.client.ws_connect('/ws') as ws: +# await ws.send_json(data=dict(topic="step/stop")) +# await ws.send_json(data=dict(topic="step/start")) +# async for msg in ws: +# if msg.type == aiohttp.WSMsgType.TEXT: +# try: +# msg_obj = msg.json() +# topic = msg_obj.get("topic") +# if topic == "job/step/done": +# count_step_done = count_step_done + 1 +# if topic == "step/brewing/finished": +# await ws.send_json(data=dict(topic="close")) +# except Exception as e: +# print(e) +# break +# elif msg.type == aiohttp.WSMsgType.ERROR: +# break - async def get_application(self): - self.cbpi = CraftBeerPi() - await self.cbpi.init_serivces() - return self.cbpi.app +# assert count_step_done == 4 +# @unittest_run_loop +# async def test_wrong_format(self): - @unittest_run_loop - async def test_brewing_process(self): +# async with self.client.ws_connect('/ws') as ws: +# await ws.send_json(data=dict(a="close")) +# async for msg in ws: +# print("MSG TYP", msg.type, msg.data) +# if msg.type == aiohttp.WSMsgType.TEXT: +# msg_obj = msg.json() +# if msg_obj["topic"] != "connection/success": +# print(msg.data) +# raise Exception() - count_step_done = 0 - async with self.client.ws_connect('/ws') as ws: - await ws.send_json(data=dict(topic="step/stop")) - await ws.send_json(data=dict(topic="step/start")) - async for msg in ws: - if msg.type == aiohttp.WSMsgType.TEXT: - try: - msg_obj = msg.json() - topic = msg_obj.get("topic") - if topic == "job/step/done": - count_step_done = count_step_done + 1 - if topic == "step/brewing/finished": - await ws.send_json(data=dict(topic="close")) - except Exception as e: - print(e) - break - elif msg.type == aiohttp.WSMsgType.ERROR: - break - - assert count_step_done == 4 - - @unittest_run_loop - async def test_wrong_format(self): - - async with self.client.ws_connect('/ws') as ws: - await ws.send_json(data=dict(a="close")) - async for msg in ws: - print("MSG TYP", msg.type, msg.data) - if msg.type == aiohttp.WSMsgType.TEXT: - msg_obj = msg.json() - if msg_obj["topic"] != "connection/success": - print(msg.data) - raise Exception() - - else: - raise Exception() +# else: +# raise Exception()