import uuid
import couchdb
import pytest

from io import StringIO
from cdbcli.environment import Environment


INTEGRATION_TEST_COUCHDB_URL = 'http://admin:password@localhost:5984/'

INTEGRATION_TEST_NON_ADMIN_COUCHDB_URL = 'http://localhost:5984/'


@pytest.fixture
def environment():
    output_stream = StringIO()
    return Environment(None, output_stream)


@pytest.fixture
def couch_server():
    couch_server = couchdb.Server(INTEGRATION_TEST_COUCHDB_URL)
    for db in couch_server:
        if db not in ['_replicator', '_users']:
            del couch_server[db]

    return couch_server


@pytest.fixture
def non_admin_couch_server():
    return couchdb.Server(INTEGRATION_TEST_NON_ADMIN_COUCHDB_URL)


def get_user_doc(first_name=None, last_name=None):
    first_name = first_name or uuid.uuid4().hex
    last_name = last_name or uuid.uuid4().hex

    return dict(
        _id='{}.{}'.format(first_name, last_name),
        first_name=first_name,
        last_name=last_name,
    )


def get_user_design_doc():
    return {
        '_id': '_design/users',
        'language': 'python',
        'views': {
            'by_lastname': {
                'map': 'def map(doc): yield doc.get("last_name"), doc'
            },
            'by_firstname': {
                'map': 'def map(doc): yield doc.get("first_name"), doc'
            }
        }
    }


def get_empty_design_doc():
    return {
        '_id': '_design/empty',
        'language': 'python',
    }

import uuid
import couchdb
import pytest

from io import StringIO
from cdbcli.environment import Environment


INTEGRATION_TEST_COUCHDB_URL = 'http://admin:password@localhost:5984/'

INTEGRATION_TEST_NON_ADMIN_COUCHDB_URL = 'http://localhost:5984/'


@pytest.fixture
def environment():
    output_stream = StringIO()
    return Environment(None, output_stream)


@pytest.fixture
def couch_server():
    couch_server = couchdb.Server(INTEGRATION_TEST_COUCHDB_URL)
    for db in couch_server:
        if db not in ['_replicator', '_users']:
            del couch_server[db]

    return couch_server


@pytest.fixture
def non_admin_couch_server():
    return couchdb.Server(INTEGRATION_TEST_NON_ADMIN_COUCHDB_URL)


def get_user_doc(first_name=None, last_name=None):
    first_name = first_name or uuid.uuid4().hex
    last_name = last_name or uuid.uuid4().hex

    return dict(
        _id='{}.{}'.format(first_name, last_name),
        first_name=first_name,
        last_name=last_name,
    )


def get_user_design_doc():
    return {
        '_id': '_design/users',
        'language': 'python',
        'views': {
            'by_lastname': {
                'map': 'def map(doc): yield doc.get("last_name"), doc'
            },
            'by_firstname': {
                'map': 'def map(doc): yield doc.get("first_name"), doc'
            }
        }
    }


def get_empty_design_doc():
    return {
        '_id': '_design/empty',
        'language': 'python',
    }
