def test_setup_calls_setup(self, monkeypatch):
        mock_connect = mock.Mock()
        mock_sync_table = mock.Mock()
        mock_create_keyspace = mock.Mock()
        monkeypatch.setattr(cassandra.connection, 'setup', mock_connect)
        monkeypatch.setattr(cassandra.management, 'sync_table', mock_sync_table)
        monkeypatch.setattr(cassandra.management, 'create_keyspace', mock_create_keyspace)

        manager = DatabaseManager(keyspace='test')
        manager.setup()

        assert mock_connect.called_once_with(manager.uri, manager.keyspace)
        assert mock_create_keyspace.called_once_with(manager.keyspace, replication_factor=1, strategy_class='SimpleStrategy')
    def test_register_model(self, monkeypatch):
        mock_sync = mock.Mock()
        monkeypatch.setattr(cassandra.management, 'sync_table', mock_sync)

        manager = DatabaseManager()
        manager._setup = True
        manager._models = set()

        mock_model = mock.Mock()
        assert mock_model == manager.registered_model(mock_model)
        assert mock_model == manager.register_model(mock_model)

        assert manager._models == {mock_model}
        assert mock_sync.called_once_with(mock_model)
예제 #3
0
    def test_setup_calls_setup(self, monkeypatch):
        mock_connect = mock.Mock()
        mock_sync_table = mock.Mock()
        mock_create_keyspace = mock.Mock()
        monkeypatch.setattr(cassandra.connection, 'setup', mock_connect)
        monkeypatch.setattr(cassandra.management, 'sync_table',
                            mock_sync_table)
        monkeypatch.setattr(cassandra.management, 'create_keyspace',
                            mock_create_keyspace)

        manager = DatabaseManager(keyspace='test')
        manager.setup()

        assert mock_connect.called_once_with(manager.uri, manager.keyspace)
        assert mock_create_keyspace.called_once_with(
            manager.keyspace,
            replication_factor=1,
            strategy_class='SimpleStrategy')
예제 #4
0
    def test_register_model(self, monkeypatch):
        mock_sync = mock.Mock()
        monkeypatch.setattr(cassandra.management, 'sync_table', mock_sync)

        manager = DatabaseManager()
        manager._setup = True
        manager._models = set()

        mock_model = mock.Mock()
        assert mock_model == manager.registered_model(mock_model)
        assert mock_model == manager.register_model(mock_model)

        assert manager._models == {mock_model}
        assert mock_sync.called_once_with(mock_model)
예제 #5
0
import mock
import pytest

from elasticsearch import Elasticsearch
from elasticsearch.exceptions import ConnectionError, TransportError

from cassandra.cluster import NoHostAvailable

import scrapi
from scrapi import settings
from scrapi.processing.cassandra import CassandraProcessor, DatabaseManager

settings.DEBUG = True
settings.CELERY_ALWAYS_EAGER = True
settings.CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
CassandraProcessor.manager = DatabaseManager(keyspace='test')
database = CassandraProcessor.manager

try:
    con = Elasticsearch(settings.ELASTIC_URI,
                        request_timeout=settings.ELASTIC_TIMEOUT)
    con.cluster.health(wait_for_status='yellow')
    use_es = True
except ConnectionError:
    use_es = False


@pytest.fixture(autouse=True)
def harvester(monkeypatch):
    mock_registry = mock.MagicMock()
    mock_harvester = mock.MagicMock()
예제 #6
0
    def test_respects_kwargs(self, monkeypatch):
        manager = DatabaseManager(uri='none', keyspace='test')

        assert manager.uri == 'none'
        assert manager.keyspace == 'test'
예제 #7
0
    def test_pulls_from_settings(self, monkeypatch):
        manager = DatabaseManager()

        assert manager.uri == settings.CASSANDRA_URI
        assert manager.keyspace == settings.CASSANDRA_KEYSPACE