Testeando el codigo!
This commit is contained in:
217
fetcher/test.py
Normal file
217
fetcher/test.py
Normal file
@@ -0,0 +1,217 @@
|
||||
import json
|
||||
import logging
|
||||
from unittest.mock import Mock, MagicMock, patch
|
||||
from django.test import TestCase
|
||||
from fetcher import cache
|
||||
|
||||
# Parece que es molesto el logging en el output del test
|
||||
logging.disable()
|
||||
|
||||
|
||||
class CacheTest(TestCase):
|
||||
@staticmethod
|
||||
def mock_redis(mock_connection):
|
||||
mock_redis = Mock()
|
||||
mock_connection.return_value = Mock(__enter__=Mock(return_value=mock_redis),
|
||||
__exit__=Mock(return_value=False))
|
||||
|
||||
return mock_redis
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_artist_input_not_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get artist con un mbid no existente"""
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
mock_redis.get = Mock(return_value=None)
|
||||
|
||||
response = cache.get_artist('mbid')
|
||||
self.assertIsNone(response)
|
||||
|
||||
mock_redis.get.assert_called_with('artist:mbid')
|
||||
mock_jobs.load_artist_on_cache.delay.assert_called_once_with('mbid')
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_artist_input_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get artist con un mbid existente"""
|
||||
payload = {'id': 'mbid', 'name': 'name'}
|
||||
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
mock_redis.get = Mock(return_value=json.dumps(payload))
|
||||
|
||||
response = cache.get_artist('mbid')
|
||||
|
||||
self.assertEquals(payload, response)
|
||||
|
||||
mock_redis.get.assert_called_with('artist:mbid')
|
||||
mock_jobs.load_artist_on_cache.delay.assert_not_called()
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_disc_input_not_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get disc con un mbid no existente"""
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
mock_redis.get = Mock(return_value=None)
|
||||
|
||||
response = cache.get_disc('mbid')
|
||||
self.assertIsNone(response)
|
||||
|
||||
mock_redis.get.assert_called_with('release_group:mbid')
|
||||
mock_jobs.load_artist_on_cache.delay.asert_called_once_with('mbid')
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_disc_input_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get disc con un mbid existente"""
|
||||
payload = {'id': 'mbid', 'title': 'oh no'}
|
||||
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
mock_redis.get = Mock(return_value=json.dumps(payload))
|
||||
|
||||
response = cache.get_disc('mbid')
|
||||
self.assertEquals(payload, response)
|
||||
|
||||
mock_redis.get.assert_called_with('release_group:mbid')
|
||||
mock_jobs.load_artist_on_cache.delay.asert_not_called()
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_discs_of_artists_no_contains(self, mock_connection, mock_jobs):
|
||||
"""Testear get discs of artists con mbid que no esta en redis"""
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
mock_redis.__contains__ = Mock(return_value=False)
|
||||
|
||||
result, count = cache.get_discs_of_artist('mbid', 10, 0)
|
||||
self.assertEquals(result, None)
|
||||
self.assertEquals(count, 0)
|
||||
|
||||
mock_redis.__contains__.assert_called_with('artist:mbid:release_groups')
|
||||
mock_jobs.load_artist_on_cache.delay.asert_called_once_with('mbid')
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_discs_of_artists_bad_count(self, mock_connection, mock_jobs):
|
||||
"""Testear get discs of artists cuando el total no coincide con los almacenados"""
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
|
||||
mock_redis.__contains__ = Mock(return_value=True)
|
||||
mock_redis.get = Mock(return_value=10)
|
||||
mock_redis.zcard = Mock(return_value=0)
|
||||
|
||||
result, count = cache.get_discs_of_artist('mbid', 10, 0)
|
||||
self.assertEquals(result, None)
|
||||
self.assertEquals(count, 0)
|
||||
|
||||
mock_redis.__contains__.assert_called_with('artist:mbid:release_groups')
|
||||
mock_redis.get.assert_called_with('artist:mbid:release_groups:count')
|
||||
mock_redis.zcard.assert_called_with('artist:mbid:release_groups')
|
||||
mock_jobs.load_artist_on_cache.delay.asert_called_once_with('mbid')
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_discs_of_artists_not_all_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get discs of artists cuando no estan guardados todos los discos"""
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
|
||||
mock_redis.__contains__ = Mock(return_value=True)
|
||||
mock_redis.get = Mock(return_value=10)
|
||||
mock_redis.zcard = Mock(return_value=10)
|
||||
mock_redis.zrange = Mock(return_value=range(10))
|
||||
mock_redis.exists = Mock(return_value=5)
|
||||
|
||||
result, count = cache.get_discs_of_artist('mbid', 10, 0)
|
||||
self.assertEquals(result, None)
|
||||
self.assertEquals(count, 0)
|
||||
|
||||
mock_redis.__contains__.assert_called_with('artist:mbid:release_groups')
|
||||
mock_redis.get.assert_called_with('artist:mbid:release_groups:count')
|
||||
mock_redis.zcard.assert_called_with('artist:mbid:release_groups')
|
||||
mock_redis.zrange.assert_called_with('artist:mbid:release_groups', 0, 10)
|
||||
mock_redis.exists.assert_called_with(*[f'release_group:{i}' for i in range(10)])
|
||||
mock_jobs.load_artist_on_cache.delay.asert_called_once_with('mbid')
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_discs_of_artists_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get discs of artists cuando todo va bien"""
|
||||
payload = [{'id': i} for i in range(10)]
|
||||
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
|
||||
mock_redis.__contains__ = Mock(return_value=True)
|
||||
mock_redis.get = Mock(return_value=10)
|
||||
mock_redis.zcard = Mock(return_value=10)
|
||||
mock_redis.zrange = Mock(return_value=range(10))
|
||||
mock_redis.exists = Mock(return_value=10)
|
||||
mock_redis.mget = Mock(return_value=MagicMock())
|
||||
mock_redis.mget.return_value.__iter__.return_value = [json.dumps(item) for item in payload]
|
||||
|
||||
result, count = cache.get_discs_of_artist('mbid', 10, 0)
|
||||
self.assertEquals(result, payload)
|
||||
self.assertEquals(count, 10)
|
||||
|
||||
mock_redis.__contains__.assert_called_with('artist:mbid:release_groups')
|
||||
mock_redis.get.assert_called_with('artist:mbid:release_groups:count')
|
||||
mock_redis.zcard.assert_called_with('artist:mbid:release_groups')
|
||||
mock_redis.zrange.assert_called_with('artist:mbid:release_groups', 0, 10)
|
||||
mock_redis.exists.assert_called_with(*[f'release_group:{i}' for i in range(10)])
|
||||
mock_jobs.load_artist_on_cache.delay.assert_not_called()
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_artist_of_disc_input_not_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get artist of disc cuando no existe"""
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
mock_redis.get = Mock(return_value=None)
|
||||
|
||||
result = cache.get_artist_of_disc('mbid')
|
||||
self.assertEquals(result, None)
|
||||
|
||||
mock_redis.get.assert_called_with('release_group:mbid:artist')
|
||||
mock_jobs.load_entities_of_release_group.delay.assert_called_with('mbid')
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_artist_of_disc_input_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get artist of disc cuando existe"""
|
||||
payload = {'id': 'artist_id', 'title': 'ohno'}
|
||||
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
|
||||
def get(key):
|
||||
if key == 'release_group:mbid:artist':
|
||||
return payload.get('id')
|
||||
elif key == 'artist:artist_id':
|
||||
return json.dumps(payload)
|
||||
else:
|
||||
self.fail('Key inesperada')
|
||||
|
||||
mock_redis.get = get
|
||||
|
||||
result = cache.get_artist_of_disc('mbid')
|
||||
self.assertEquals(result, payload)
|
||||
|
||||
mock_jobs.load_entities_of_release_group.delay.assert_not_called()
|
||||
|
||||
@patch('fetcher.cache.jobs')
|
||||
@patch('fetcher.cache.get_redis_connection')
|
||||
def test_get_release_input_not_exists(self, mock_connection, mock_jobs):
|
||||
"""Testear get release cuando no existe"""
|
||||
payload = {'id': 'artist_id', 'title': 'ohno'}
|
||||
|
||||
mock_redis = CacheTest.mock_redis(mock_connection)
|
||||
|
||||
def get(key):
|
||||
if key == 'release_group:mbid:artist':
|
||||
return payload.get('id')
|
||||
elif key == 'artist:artist_id':
|
||||
return json.dumps(payload)
|
||||
else:
|
||||
self.fail('Key inesperada')
|
||||
|
||||
mock_redis.get = get
|
||||
|
||||
result = cache.get_artist_of_disc('mbid')
|
||||
self.assertEquals(result, payload)
|
||||
|
||||
mock_jobs.load_entities_of_release_group.delay.assert_not_called()
|
||||
Reference in New Issue
Block a user