Beispiel #1
0
def test_get_df(mocker):
    class DatabaseMock:
        def __init__(self, collection):
            self.collections = {collection: pymongo.collection.Collection}

        def __getitem__(self, col):
            return self.collections[col]

        def list_collection_names(self):
            return self.collections.keys()

    class MongoMock:
        def __init__(self, database, collection):
            self.data = {database: DatabaseMock(collection)}

        def __getitem__(self, row):
            return self.data[row]

        def close(self):
            pass

        def list_database_names(self):
            return self.data.keys()

    snock = mocker.patch('pymongo.MongoClient')
    snock.return_value = MongoMock('toucan', 'test_col')
    aggregate = mocker.patch('pymongo.collection.Collection.aggregate')

    mongo_connector = MongoConnector(name='mycon',
                                     host='localhost',
                                     port=22,
                                     username='******',
                                     password='******')

    datasource = MongoDataSource(name='mycon',
                                 domain='mydomain',
                                 database='toucan',
                                 collection='test_col',
                                 query={'domain': 'domain1'})
    mongo_connector.get_df(datasource)

    datasource = MongoDataSource(name='mycon',
                                 domain='mydomain',
                                 database='toucan',
                                 collection='test_col',
                                 query=[{
                                     '$match': {
                                         'domain': 'domain1'
                                     }
                                 }])
    mongo_connector.get_df(datasource)

    snock.assert_called_with('mongodb://*****:*****@localhost:22',
                             ssl=False)
    assert snock.call_count == 2

    aggregate.assert_called_with([{'$match': {'domain': 'domain1'}}])
    assert aggregate.call_count == 2
Beispiel #2
0
def test_get_form_empty_query(mongo_connector):
    """It should give suggestions of the databases without changing the rest"""
    current_config = {}
    form = MongoDataSource.get_form(mongo_connector, current_config)
    assert form['required'] == ['domain', 'name', 'database', 'collection']
    assert form['properties']['database'] == {
        'title': 'Database',
        'type': 'string',
        'enum': ['admin', 'config', 'local', 'toucan']
    }
    assert form['properties']['collection'] == {
        'title': 'Collection',
        'type': 'string'
    }
Beispiel #3
0
def test_get_form_query_with_good_database(mongo_connector):
    """It should give suggestions of the collections"""
    current_config = {'database': 'toucan'}
    form = MongoDataSource.get_form(mongo_connector, current_config)
    assert form['required'] == ['domain', 'name', 'database', 'collection']
    assert form['properties']['database'] == {
        'title': 'Database',
        'type': 'string',
        'enum': ['admin', 'config', 'local', 'toucan'],
    }
    assert form['properties']['collection'] == {
        'title': 'Collection',
        'type': 'string',
        'enum': ['test_col'],
    }
Beispiel #4
0
def test_get_form_empty_query(mongo_connector):
    """It should give suggestions of the databases without changing the rest"""
    current_config = {}
    form = MongoDataSource.get_form(mongo_connector, current_config)
    assert form['required'] == ['domain', 'name', 'database', 'collection']
    assert form['properties']['database'] == {'$ref': '#/definitions/database'}
    assert form['definitions']['database'] == {
        'title': 'database',
        'description': 'An enumeration.',
        'type': 'string',
        'enum': ['admin', 'config', 'local', 'toucan'],
    }
    assert form['properties']['collection'] == {
        'type': 'string',
        'title': 'Collection',
        'description': 'The name of the collection you want to query',
    }
Beispiel #5
0
def test_get_form_query_with_bad_database(mongo_connector):
    """It should raise an error"""
    current_config = {'database': 'qweqwe'}
    with pytest.raises(UnkwownMongoDatabase):
        MongoDataSource.get_form(mongo_connector, current_config)
Beispiel #6
0
 def f(**kwargs):
     params = {'name': 'mycon', 'domain': 'mydomain', 'database': 'toucan'}
     params.update(kwargs)
     return MongoDataSource(**params)
Beispiel #7
0
 def f(collection, query):
     return MongoDataSource(name='mycon',
                            domain='mydomain',
                            collection=collection,
                            query=query)