Skip to content

Commit

Permalink
Merge pull request #22 from Attumm/increase_coverage
Browse files Browse the repository at this point in the history
Increase coverage
  • Loading branch information
Attumm authored Aug 12, 2024
2 parents ad4b964 + 88a6e87 commit 9d722df
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Run Unit Tests
run: |
coverage run --omit="*test*" -m unittest discover -p "*test*.py"
coverage run --omit="*test*" -m unittest discover tests/ -p "*test*.py"
- name: Upload coverage reports to Codecov, send only once
if: matrix.python-version == '3.12'
Expand Down
10 changes: 6 additions & 4 deletions examples/example_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
"timeout": 1,
}

box = Meesee(config)

@Meesee.worker()

@box.worker()
def func_a(item, worker_id):
print('func: {}, worker_id: {}, item: {}'.format('func_a', worker_id, item))


@Meesee.worker()
@box.worker()
def func_b(item, worker_id):
print('func: {}, worker_id: {}, item: {}'.format('func_b', worker_id, item))


@Meesee.worker()
@box.worker()
def func_c(item, worker_id):
print('func: {}, worker_id: {}, item: {}'.format('func_c', worker_id, item))


if __name__ == '__main__':
workers = int(sys.argv[sys.argv.index('-w') + 1]) if '-w' in sys.argv else 10
Meesee.start_workers(workers=workers, config=config)
box.start_workers(workers=workers, config=config)
23 changes: 10 additions & 13 deletions meesee.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ def __len__(self):


class Meesee:
worker_funcs = {}

def __init__(self, workers=10, namespace="main", timeout=None, queue="main", redis_config={}):
self.workers = workers
self.namespace = namespace
self.timeout = timeout
self.queue = queue
self.redis_config = redis_config
self.worker_funcs = {}

def create_produce_config(self):
return {
Expand Down Expand Up @@ -158,28 +158,25 @@ def wrapper(*args, **kwargs):
return wrapper
return decorator

@staticmethod
def parse_func_name(func):
def parse_func_name(self, func):
return func.__name__

@classmethod
def worker(cls, queue=None):
def worker(self, queue=None):
def decorator(func):
parsed_name = queue if queue is not None else cls.parse_func_name(func)
cls.worker_funcs[parsed_name] = func
parsed_name = queue if queue is not None else self.parse_func_name(func)
self.worker_funcs[parsed_name] = func
return func
return decorator

@classmethod
def start_workers(cls, workers=10, config=config):
n_workers = len(cls.worker_funcs)
def start_workers(self, workers=10, config=config):
n_workers = len(self.worker_funcs)
if n_workers == 0:
print("No workers have been assigned with a decorator")
if n_workers > workers:
print(f"Not enough workers, increasing the workers started with: {workers} we need atleast: {n_workers}")
workers = n_workers

startapp(list(cls.worker_funcs.values()), workers=workers, config=config)
startapp(list(self.worker_funcs.values()), workers=workers, config=config)

def push_button(self, workers=None, wait=None):
if workers is not None:
Expand All @@ -189,13 +186,13 @@ def push_button(self, workers=None, wait=None):
"key": queue,
"namespace": self.namespace,
"redis_config": self.redis_config,
} for queue in self.__class__.worker_funcs.keys()
} for queue in self.worker_funcs.keys()
]
if self.timeout is not None or wait is not None:
for config in configs:
config["timeout"] = self.timeout or wait

startapp(list(self.__class__.worker_funcs.values()), workers=self.workers, config=configs)
startapp(list(self.worker_funcs.values()), workers=self.workers, config=configs)


class InitFail(Exception):
Expand Down
File renamed without changes.
File renamed without changes.
69 changes: 69 additions & 0 deletions tests/mock_function_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import json

import unittest

from unittest.mock import patch
from meesee import Meesee


class TestWorkerProducerLineCoverage(unittest.TestCase):
def setUp(self):
self.box = Meesee(workers=10, namespace="test", timeout=2)

@patch('meesee.RedisQueue')
@patch('redis.Redis')
def test_worker_producer_line_coverage(self, mock_redis, mock_redis_queue):

@self.box.worker_producer(input_queue="foo", output_queue="foobar")
def test_func_both_queues(input_data):
return input_data

@self.box.worker_producer(input_queue="bar")
def test_func_input_queue(input_data):
return input_data

@self.box.worker_producer(output_queue="baz")
def test_func_output_queue(input_data):
return input_data

@self.box.worker_producer()
def produce_to_qux(input_data):
return input_data

@self.box.worker_producer()
def test_func_list(input_data):
return [input_data, {"key": "value"}]

@self.box.worker_producer()
def test_func_dict(input_data):
return {"key": input_data}

@self.box.worker_producer()
def test_func_list_with_dict(input_data):
return input_data

@self.box.worker_producer()
def test_func_none(input_data):
return None

test_func_both_queues("test_data")
test_func_input_queue("test_data")
test_func_output_queue("test_data")
produce_to_qux("test_data")
test_func_list("test_data")
test_func_dict("test_data")
test_func_none("test_data")

test_func_list_with_dict([{"key1": "value1"}, {"key2": "value2"}])

mock_redis_queue.assert_called()
mock_redis_queue.return_value.send.assert_called()
self.assertIn("foo", self.box.worker_funcs)
self.assertIn("bar", self.box.worker_funcs)
self.assertIn("produce_to_qux", self.box.worker_funcs)

mock_redis_queue.return_value.send.assert_any_call(json.dumps({"key": "test_data"}))


if __name__ == '__main__':
unittest.main()
File renamed without changes.
File renamed without changes.

0 comments on commit 9d722df

Please sign in to comment.