Skip to content

Commit

Permalink
add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed Apr 25, 2024
1 parent 9e20402 commit 994b0e2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
5 changes: 3 additions & 2 deletions jeedomdaemon/base_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
from typing import Sequence

class BaseConfig(object):
def __init__(self):
Expand All @@ -14,9 +15,9 @@ def __init__(self):
def add_argument(self, *args, **kwargs):
return self.__parser.add_argument(*args, **kwargs)

def parse(self):
def parse(self, args: Sequence[str] | None = None):
if self._args is None:
self._args = self.__parser.parse_args()
self._args = self.__parser.parse_args(args)

@property
def callback_url(self):
Expand Down
Empty file added tests/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions tests/base_config_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sys
import os
import json

sys.path.append(os.path.realpath(os.path.dirname(__file__) + '/..'))

# now we can import the module in the parent
# directory.

import unittest

from jeedomdaemon.base_config import BaseConfig

class TestBaseConfig(unittest.TestCase):
def test_base_config_creation(self):
"""
Test that it can create a basic config parser
"""
config = BaseConfig()
config.parse([])
self.assertEqual(config.socket_host, "127.0.0.1")

def test_base_config_parse(self):
"""
Test that it can parse config
"""
config = BaseConfig()
config.parse(['--loglevel', 'info', '--socketport', '42000', '--callback', 'http://localhost/path', '--apikey', 'cnysltyql', '--pid', '123'])
self.assertEqual(config.log_level, "info")
self.assertEqual(config.socket_host, "127.0.0.1")
self.assertEqual(config.socket_port, 42000)
self.assertEqual(config.callback_url, "http://localhost/path")
self.assertEqual(config.api_key, "cnysltyql")
self.assertEqual(config.pid_filename, "123")

def test_custom_config_parse(self):
"""
Test that it can parse config
"""
class TestConfig(BaseConfig):
def __init__(self):
super().__init__()
self.add_argument("--clientId", help="my client Id", type=str)

@property
def clientId(self):
return str(self._args.clientId)

config = TestConfig()
config.parse(['--clientId', 'hfldhfsd'])
self.assertEqual(config.clientId, "hfldhfsd")



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

0 comments on commit 994b0e2

Please sign in to comment.