Skip to content

Commit

Permalink
Add tests for request consumer and fix test.sh path problems (#72)
Browse files Browse the repository at this point in the history
* Add first test for request consumer

* Use python -m pytest instead of py.test

py.test doesn't add the current dir to PYTHONPATH which is why
if you try to add a test anywhere other than listenbrainz_spark/tests
you'll get an import error. this fixes that problem.

* fix definition of self

* Import create_app

* Add test for get_result
  • Loading branch information
paramsingh committed Dec 15, 2019
1 parent 9209cd1 commit 1132055
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docker/docker-compose.jenkins.yml
Expand Up @@ -25,4 +25,4 @@ services:
context: ..
dockerfile: Dockerfile
target: metabrainz-spark-dev
command: dockerize -wait tcp://hadoop-master:9000 -timeout 60s bash -c "cp listenbrainz_spark/config.py.sample listenbrainz_spark/config.py; py.test --junitxml=/data/test_report.xml --cov-report xml:/data/coverage.xml"
command: dockerize -wait tcp://hadoop-master:9000 -timeout 60s bash -c "cp listenbrainz_spark/config.py.sample listenbrainz_spark/config.py; python -m pytest --junitxml=/data/test_report.xml --cov-report xml:/data/coverage.xml"
2 changes: 1 addition & 1 deletion docker/docker-compose.test.yml
Expand Up @@ -22,6 +22,6 @@ services:
context: ..
dockerfile: Dockerfile
target: metabrainz-spark-dev
command: dockerize -wait tcp://hadoop-master:9000 -timeout 60s bash -c "py.test --junitxml=/data/test_report.xml --cov-report xml:/data/coverage.xml"
command: dockerize -wait tcp://hadoop-master:9000 -timeout 60s bash -c "python -m pytest --junitxml=/data/test_report.xml --cov-report xml:/data/coverage.xml"
volumes:
- ..:/rec:z
4 changes: 4 additions & 0 deletions listenbrainz_spark/query_map.py
Expand Up @@ -3,3 +3,7 @@
functions = {
'stats.user.all': listenbrainz_spark.stats.user.all.calculate,
}


def get_query_handler(query):
return functions[query]
2 changes: 1 addition & 1 deletion listenbrainz_spark/request_consumer/request_consumer.py
Expand Up @@ -40,7 +40,7 @@ def get_result(self, request):
return None

try:
query_handler = listenbrainz_spark.query_map.functions[query]
query_handler = listenbrainz_spark.query_map.get_query_handler(query)
except KeyError:
current_app.logger.error("Bad query sent to spark request consumer: %s", query, exc_info=True)
return None
Expand Down
44 changes: 44 additions & 0 deletions listenbrainz_spark/request_consumer/test_request_consumer.py
@@ -0,0 +1,44 @@
from unittest.mock import patch, MagicMock

from listenbrainz_spark.request_consumer.request_consumer import RequestConsumer
from listenbrainz_spark.tests import SparkTestCase
from listenbrainz_spark.utils import create_app


class RequestConsumerTestCase(SparkTestCase):

def setUp(self):
self.consumer = RequestConsumer()
self.consumer.rabbitmq = MagicMock()
self.consumer.request_channel = MagicMock()
self.consumer.result_channel = MagicMock()

@patch('listenbrainz_spark.request_consumer.request_consumer.current_app')
@patch('listenbrainz_spark.request_consumer.request_consumer.init_rabbitmq')
def test_connect_to_rabbitmq(self, mock_init_rabbitmq, mock_current_app):
self.consumer.connect_to_rabbitmq()
mock_init_rabbitmq.assert_called_once()

@patch('listenbrainz_spark.request_consumer.request_consumer.current_app')
def test_get_result_if_bad_query(self, mock_current_app):
# should return none if no query
self.assertIsNone(self.consumer.get_result({}))

# should return none if unrecognized query
self.assertIsNone(self.consumer.get_result({'query': 'idk_what_this_means'}))

@patch('listenbrainz_spark.request_consumer.request_consumer.current_app')
@patch('listenbrainz_spark.query_map.get_query_handler')
def test_get_result_if_query_recognized(self, mock_get_query_handler, mock_current_app):
# should call the returned function if query is recognized
# create the mock query handler
mock_query_handler = MagicMock()
mock_query_handler.return_value = {'result': 'ok'}

# make the get_query_handler function return the mock query handler
mock_get_query_handler.return_value = mock_query_handler

# assert that we get the correct result
self.assertEqual(self.consumer.get_result({'query': 'i_know_what_this_means'}), {'result': 'ok'})
mock_get_query_handler.assert_called_once()
mock_query_handler.assert_called_once()
2 changes: 0 additions & 2 deletions listenbrainz_spark/tests/test_utils.py
@@ -1,8 +1,6 @@
import os
import unittest
from datetime import datetime

import listenbrainz_spark
from listenbrainz_spark.tests import SparkTestCase
from listenbrainz_spark import utils, hdfs_connection, config

Expand Down

0 comments on commit 1132055

Please sign in to comment.