<commit_before>#!/bin/python
import json
import unittest
import datetime
import os
from multiprocessing import Process
from application import server

"""
Test Register-Publisher on an 'ad hoc' basis or automatically (pytest).
Pretend to be "System Of Record" publisher.
"""

# Set up root logger
logger = server.logger

# Basic test data.
def make_message():
    dt = str(datetime.datetime.now())
    return json.dumps(dt.split())

class Application(object):
    """ Mimic Process calls, for logging purposes. """

    def __init__(self, target=server.run):
        self.target = target
        self.process = None
        logger.debug("Target:'{}'.".format(self.target))

    def start(self):
        self.process = Process(target=self.target)

        logger.debug("Starting '{}' as a separate process.".format(self.target))
        self.process.start()

    def join(self, timeout=None):
        self.process.join(timeout=timeout)
        logger.debug("'{}' completed.".format(self.target))

    def terminate(self):
        self.process.terminate()
        logger.debug("'{}' terminated.".format(self.target))

# N.B.: these tests may reverse the default 'producer' and 'consumer' targets.
class TestRegisterPublisher(unittest.TestCase):
    #: This can be the callback applied when a message is received - i.e. "consume()" case.
    def handle_message(self, body, message):
        # Note: 'body' may have been pickled, so refer to 'payload' instead.
        logger.info('Received message: {}'.format(message.payload))
        logger.info(' properties: {}'.format(message.properties))
        logger.info(' delivery_info: {}'.format(message.delivery_info))
        message.ack()

        self.payload = message.payload

    def consume(self, cfg=server.incoming_cfg):
        """ Get message via callback mechanism """

        with server.setup_consumer(cfg=cfg, callback=self.handle_message) as consumer:

            logger.debug("cfg: {}".format(cfg))

            # 'consume' may be a misnomer here - it just initiates the consumption process, I believe.
            consumer.consume()

            # Execute 'drain_events()' loop in a time-out thread, in case it gets stuck.
            logger.info("'drain_events()' with timeout")
            try:
                consumer.connection.drain_events(timeout=5)
            except Exception as e:
                logger.error(e)
            finally:
                consumer.close()

    def reset(self):

            # """ Clear the decks. """

        logger.debug("reset")

        with server.setup_connection(server.outgoing_cfg.hostname) as outgoing_connection:

            # Need a connection to delete the queues.
            self.assertEqual(outgoing_connection.connected, True)

            outgoing_channel = outgoing_connection.channel()
            queue = server.setup_queue(outgoing_channel, cfg=server.outgoing_cfg)
            queue.purge()
            queue.delete()

        with server.setup_connection(server.incoming_cfg.hostname) as incoming_connection:

            # Need a connection to delete the queues.
            self.assertEqual(incoming_connection.connected, True)

            incoming_channel = incoming_connection.channel()
            queue = server.setup_queue(incoming_channel, cfg=server.incoming_cfg)
            queue.purge()
            queue.delete()

    def setUp(self):
        """ Establish connection and other resources; prepare """

        logger.debug("setUp")

        self.app = Application()

        # Ensure that message broker is alive, etc.
        self.reset()

        self.message = None             # Message to be sent.
        self.payload = None             # Corresponding 'payload' of message received.

        # Execute 'server.run()' as a separate process.
        self.app.start()

        test_title = self.id().split(sep='.')[-1]
        logger.info(test_title)

    def tearDown(self):

        logger.debug("setUp")

        # N.B.: app needs to be terminated before queues can be deleted!
        self.app.join(timeout=5)
        self.app.terminate()

        if os.getenv('LOG_THRESHOLD_LEVEL') != 'DEBUG':
            self.reset()

    def test_incoming_queue(self):
        """ Basic check of 'incoming' message via default direct exchange """

        # We don't need the app to be running for this test.
        self.app.terminate()

        self.message = make_message()

        producer = server.setup_producer(cfg=server.incoming_cfg)
        producer.publish(body=self.message, routing_key=server.incoming_cfg.queue)
        logger.info("Put message, exchange: {}, {}".format(self.message, producer.exchange))

        producer.close()

        self.consume()

        self.assertEqual(self.message, self.payload)

    def test_broken_connection(self):
        """ Attempt 'publish' via closed connection, which is subsequently restored. """

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        with server.setup_producer(cfg=server.incoming_cfg) as producer:

            producer.publish(body=self.message, routing_key=server.incoming_cfg.queue)

            # Kill connection to broker.
            producer.connection.close()

        # Block (wait) until app times out or terminates.
        self.app.join(timeout=5)

        # Consume message from outgoing exchange; this will establish another connection.
        self.consume(cfg=server.outgoing_cfg)

        self.assertEqual(self.message, self.payload)

    def test_stored_incoming_message(self):
        """ Store message in INCOMING queue, then consume later and check it. """

        self.app.terminate()

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        with server.setup_producer(cfg=server.incoming_cfg) as producer:
            producer.publish(body=self.message)
            logger.debug(self.message)

        self.app.start()

        # Consume message from outgoing exchange.
        self.consume(cfg=server.outgoing_cfg)

        self.assertEqual(self.message, self.payload)

    def test_stored_outgoing_message(self):
        """ Store message in OUTGOING queue, then consume later and check it. """

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        with server.setup_producer(cfg=server.incoming_cfg) as producer:
            producer.publish(body=self.message, routing_key=server.incoming_cfg.queue)
            logger.debug(self.message)

        # Kill application; wait long enough for message to be stored.
        # N.B.: 1 second may be insufficient, for a full coverage check during testing.
        self.app.join(timeout=5)
        self.app.terminate()

        # Consume message from outgoing exchange.
        self.consume(cfg=server.outgoing_cfg)

        self.assertEqual(self.message, self.payload)

    def test_topic_keys(self):
        """ Check that message with a suitable routing_key matches corresponding binding_key. """

        # We don't need the app to be running for this test.
        self.app.terminate()

        self.message = make_message()

        ROOT_KEY = 'feeder'

        # Set binding key for the queue that is created via setup_producer().
        cfg = server.outgoing_cfg._replace(binding_key=ROOT_KEY+'.*')

        with server.setup_producer(cfg=cfg) as producer:
            producer.publish(body=self.message, routing_key=ROOT_KEY+'.test_topic_keys')
            logger.debug(self.message)

        # Consume message from outgoing exchange.
        self.consume(cfg=cfg)

        self.assertEqual(self.message, self.payload)

    def test_end_to_end(self, count=1):
        """ Send message from dummy "System Of Record", then consume and check it. """

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        # import pdb; pdb.set_trace()
        with server.setup_producer(cfg=server.incoming_cfg) as producer:
            for n in range(count):

                # Message to be sent.
                self.message = make_message()

                producer.publish(body=self.message, routing_key=server.incoming_cfg.queue)
                logger.debug(self.message)

                # Wait long enough message to be processed.
                self.app.join(timeout=1)

                # Consume message from outgoing exchange, via callback.
                self.consume(cfg=server.outgoing_cfg)

                self.assertEqual(self.message, self.payload)

    def test_multiple_end_to_end(self):
        """ Check many messages. """

        self.test_end_to_end(100)

if __name__ == '__main__':
    unittest.main()
<commit_msg> Added/restored 'headers' code to tests.<commit_after>#!/bin/python
import json
import unittest
import datetime
import os
from multiprocessing import Process
from application import server

"""
Test Register-Publisher on an 'ad hoc' basis or automatically (pytest).
Pretend to be "System Of Record" publisher.
"""

# Set up root logger
logger = server.logger

# Basic test data.
def make_message():
    dt = str(datetime.datetime.now())
    return json.dumps(dt.split())

class Application(object):
    """ Mimic Process calls, for logging purposes. """

    def __init__(self, target=server.run):
        self.target = target
        self.process = None
        logger.debug("Target:'{}'.".format(self.target))

    def start(self):
        self.process = Process(target=self.target)

        logger.debug("Starting '{}' as a separate process.".format(self.target))
        self.process.start()

    def join(self, timeout=None):
        self.process.join(timeout=timeout)
        logger.debug("'{}' completed.".format(self.target))

    def terminate(self):
        self.process.terminate()
        logger.debug("'{}' terminated.".format(self.target))

# N.B.: these tests may reverse the default 'producer' and 'consumer' targets.
class TestRegisterPublisher(unittest.TestCase):
    #: This can be the callback applied when a message is received - i.e. "consume()" case.
    def handle_message(self, body, message):
        # Note: 'body' may have been pickled, so refer to 'payload' instead.
        logger.info('Received message: {}'.format(message.payload))
        logger.info(' properties: {}'.format(message.properties))
        logger.info(' delivery_info: {}'.format(message.delivery_info))
        message.ack()

        self.payload = message.payload

    def consume(self, cfg=server.incoming_cfg):
        """ Get message via callback mechanism """

        with server.setup_consumer(cfg=cfg, callback=self.handle_message) as consumer:

            logger.debug("cfg: {}".format(cfg))

            # 'consume' may be a misnomer here - it just initiates the consumption process, I believe.
            consumer.consume()

            # Execute 'drain_events()' loop in a time-out thread, in case it gets stuck.
            logger.info("'drain_events()' with timeout")
            try:
                consumer.connection.drain_events(timeout=5)
            except Exception as e:
                logger.error(e)
            finally:
                consumer.close()

    def reset(self):

            # """ Clear the decks. """

        logger.debug("reset")

        with server.setup_connection(server.outgoing_cfg.hostname) as outgoing_connection:

            # Need a connection to delete the queues.
            self.assertEqual(outgoing_connection.connected, True)

            outgoing_channel = outgoing_connection.channel()
            queue = server.setup_queue(outgoing_channel, cfg=server.outgoing_cfg)
            queue.purge()
            queue.delete()

        with server.setup_connection(server.incoming_cfg.hostname) as incoming_connection:

            # Need a connection to delete the queues.
            self.assertEqual(incoming_connection.connected, True)

            incoming_channel = incoming_connection.channel()
            queue = server.setup_queue(incoming_channel, cfg=server.incoming_cfg)
            queue.purge()
            queue.delete()

    def setUp(self):
        """ Establish connection and other resources; prepare """

        logger.debug("setUp")

        self.app = Application()

        # Ensure that message broker is alive, etc.
        self.reset()

        self.message = None             # Message to be sent.
        self.payload = None             # Corresponding 'payload' of message received.

        # Execute 'server.run()' as a separate process.
        self.app.start()

        test_title = self.id().split(sep='.')[-1]
        logger.info(test_title)

    def tearDown(self):

        logger.debug("setUp")

        # N.B.: app needs to be terminated before queues can be deleted!
        self.app.join(timeout=5)
        self.app.terminate()

        if os.getenv('LOG_THRESHOLD_LEVEL') != 'DEBUG':
            self.reset()

    def test_incoming_queue(self):
        """ Basic check of 'incoming' message via default direct exchange """

        # We don't need the app to be running for this test.
        self.app.terminate()

        self.message = make_message()

        producer = server.setup_producer(cfg=server.incoming_cfg)
        producer.publish(body=self.message, routing_key=server.incoming_cfg.queue, headers={'title_number': 'DN1'})
        logger.info("Put message, exchange: {}, {}".format(self.message, producer.exchange))

        producer.close()

        self.consume()

        self.assertEqual(self.message, self.payload)

    def test_broken_connection(self):
        """ Attempt 'publish' via closed connection, which is subsequently restored. """

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        with server.setup_producer(cfg=server.incoming_cfg) as producer:

            producer.publish(body=self.message, routing_key=server.incoming_cfg.queue, headers={'title_number': 'DN1'})

            # Kill connection to broker.
            producer.connection.close()

        # Block (wait) until app times out or terminates.
        self.app.join(timeout=5)

        # Consume message from outgoing exchange; this will establish another connection.
        self.consume(cfg=server.outgoing_cfg)

        self.assertEqual(self.message, self.payload)

    def test_stored_incoming_message(self):
        """ Store message in INCOMING queue, then consume later and check it. """

        self.app.terminate()

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        with server.setup_producer(cfg=server.incoming_cfg) as producer:
            producer.publish(body=self.message, headers={'title_number': 'DN1'})
            logger.debug(self.message)

        self.app.start()

        # Consume message from outgoing exchange.
        self.consume(cfg=server.outgoing_cfg)

        self.assertEqual(self.message, self.payload)

    def test_stored_outgoing_message(self):
        """ Store message in OUTGOING queue, then consume later and check it. """

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        with server.setup_producer(cfg=server.incoming_cfg) as producer:
            producer.publish(body=self.message, routing_key=server.incoming_cfg.queue, headers={'title_number': 'DN1'})
            logger.debug(self.message)

        # Kill application; wait long enough for message to be stored.
        # N.B.: 1 second may be insufficient, for a full coverage check during testing.
        self.app.join(timeout=5)
        self.app.terminate()

        # Consume message from outgoing exchange.
        self.consume(cfg=server.outgoing_cfg)

        self.assertEqual(self.message, self.payload)

    def test_topic_keys(self):
        """ Check that message with a suitable routing_key matches corresponding binding_key. """

        # We don't need the app to be running for this test.
        self.app.terminate()

        self.message = make_message()

        ROOT_KEY = 'feeder'

        # Set binding key for the queue that is created via setup_producer().
        cfg = server.outgoing_cfg._replace(binding_key=ROOT_KEY+'.*')

        with server.setup_producer(cfg=cfg) as producer:
            producer.publish(body=self.message, routing_key=ROOT_KEY+'.test_topic_keys', headers={'title_number': 'DN1'})
            logger.debug(self.message)

        # Consume message from outgoing exchange.
        self.consume(cfg=cfg)

        self.assertEqual(self.message, self.payload)

    def test_end_to_end(self, count=1):
        """ Send message from dummy "System Of Record", then consume and check it. """

        # Send a message to 'incoming' exchange - i.e. as if from SoR.
        # import pdb; pdb.set_trace()
        with server.setup_producer(cfg=server.incoming_cfg) as producer:
            for n in range(count):

                # Message to be sent.
                self.message = make_message()

                producer.publish(body=self.message, routing_key=server.incoming_cfg.queue, headers={'title_number': 'DN1'})
                logger.debug(self.message)

                # Wait long enough message to be processed.
                self.app.join(timeout=1)

                # Consume message from outgoing exchange, via callback.
                self.consume(cfg=server.outgoing_cfg)

                self.assertEqual(self.message, self.payload)

    def test_multiple_end_to_end(self):
        """ Check many messages. """

        self.test_end_to_end(100)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>import flask_testing
import unittest
import urllib

import server

class TestIndex(unittest.TestCase):

    def test_index_returns_greeting(self):
        self.assertEquals(server.index(), 'Hello World')

class TestLiveIndex(flask_testing.LiveServerTestCase):

    def create_app(self):
        app = server.app
        app.config['TESTING'] = True
        app.config['LIVE_SERVERPORT'] = 0
        return app

    def test_server_awake(self):
        res = urllib.request.urlopen(self.get_server_url())
        self.assertEquals(res.code, 200)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Instantiate server in test.<commit_after>import flask_testing
import logging
import unittest
import urllib

import server

logging.disable(logging.CRITICAL)

class TestIndex(unittest.TestCase):

    def test_index_returns_greeting(self):
        self.assertEquals(server.index(), 'Hello World')

class TestLiveIndex(flask_testing.LiveServerTestCase):

    def create_app(self):
        app = server.Flask(server.__name__)
        app = server.app
        app.config['TESTING'] = True
        app.config['LIVE_SERVERPORT'] = 0
        return app

    def test_server_awake(self):
        res = urllib.request.urlopen(self.get_server_url())
        self.assertEquals(res.code, 200)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>"""
Testing module for bib.py
"""
import pytest
from bibtextomd.bib import main, reorder, load_bibtex, journal_article, in_proceedings

def test_single_author_good():
    names = 'Author, First A.'
    n = reorder(names, 'F.A. Author')
    assert n == '**F.A. Author**'

def test_two_authors_good():
    names = 'Author, First A. and Name, Second N.'
    n = reorder(names, 'F.A. Author')
    assert n == '**F.A. Author** and S.N. Name'

def test_three_authors_good():
    names = 'Author, First A. and Name, Second N. and Name, Unicode C.'
    n = reorder(names, 'F.A. Author')
    assert n == '**F.A. Author**, S.N. Name, and U.C. Name'

def test_unicode_good():
    names = 'Namé, Unicode C.'
    n = reorder(names, 'U.C. Namé')
    assert n == '**U.C. Namé**'

def test_missing_name():
    names = 'Author, First A.'
    with pytest.warns(UserWarning):
        reorder(names, 'Missing Author')

def test_no_highlighted_name():
    names = 'Author, First A.'
    n = reorder(names, None)
    assert n == 'F.A. Author'

def test_hyphenated_name():
    names = 'Name, Hypen-Ated'
    n = reorder(names, None)
    assert n == 'H.A. Name'

@pytest.fixture
def load_bibtex_for_test():
    return load_bibtex('tests/refs.bib')

def test_journal_article(load_bibtex_for_test):
    ref = load_bibtex_for_test["article"][0]
    reference = journal_article(ref, None)
    reference_blessed = (
        "\n{:.paper}\n"
        "A study of the best ways to make up a name{:.papertitle}  \n"
        "F.A. Author, S.B. Sécond, and T.C. Third{:.authors}  \n"
        "_Journal Of Made Up Names_, Aug. 2013{:.journal}  \n"
        "**DOI:** [10.0000/made-up-doi](https://dx.doi.org/10.0000/made-up-doi)"
        "{:.doi}  \n"
        )
    assert reference == reference_blessed

def test_in_proceedings(load_bibtex_for_test):
    ref = load_bibtex_for_test["inproceedings"][0]
    reference = in_proceedings(ref, None)
    print(reference)
    reference_blessed = (
        "\n{:.paper}\n"
        "How to properly cite media{:.papertitle}  \n"
        "S.B. Second, T.C. Third, S. Fourth-Fifth, and F.A. Author{:.authors}  \n"
        "Paper 2A12, 1st International Conference on BibTeX, University, Anytown, CA, "
        "May 2013{:.journal}  \n"
        "The files for this paper can be found at the following link{:.comment}  \n"
        )
    assert reference == reference_blessed
<commit_msg>Test phd_thesis function<commit_after>"""
Testing module for bib.py
"""
import pytest
from bibtextomd.bib import main, reorder, load_bibtex, journal_article, in_proceedings, phd_thesis

def test_single_author_good():
    names = 'Author, First A.'
    n = reorder(names, 'F.A. Author')
    assert n == '**F.A. Author**'

def test_two_authors_good():
    names = 'Author, First A. and Name, Second N.'
    n = reorder(names, 'F.A. Author')
    assert n == '**F.A. Author** and S.N. Name'

def test_three_authors_good():
    names = 'Author, First A. and Name, Second N. and Name, Unicode C.'
    n = reorder(names, 'F.A. Author')
    assert n == '**F.A. Author**, S.N. Name, and U.C. Name'

def test_unicode_good():
    names = 'Namé, Unicode C.'
    n = reorder(names, 'U.C. Namé')
    assert n == '**U.C. Namé**'

def test_missing_name():
    names = 'Author, First A.'
    with pytest.warns(UserWarning):
        reorder(names, 'Missing Author')

def test_no_highlighted_name():
    names = 'Author, First A.'
    n = reorder(names, None)
    assert n == 'F.A. Author'

def test_hyphenated_name():
    names = 'Name, Hypen-Ated'
    n = reorder(names, None)
    assert n == 'H.A. Name'

@pytest.fixture
def load_bibtex_for_test():
    return load_bibtex('tests/refs.bib')

def test_journal_article(load_bibtex_for_test):
    ref = load_bibtex_for_test["article"][0]
    reference = journal_article(ref, None)
    reference_blessed = (
        "\n{:.paper}\n"
        "A study of the best ways to make up a name{:.papertitle}  \n"
        "F.A. Author, S.B. Sécond, and T.C. Third{:.authors}  \n"
        "_Journal Of Made Up Names_, Aug. 2013{:.journal}  \n"
        "**DOI:** [10.0000/made-up-doi](https://dx.doi.org/10.0000/made-up-doi)"
        "{:.doi}  \n"
        )
    assert reference == reference_blessed

def test_in_proceedings(load_bibtex_for_test):
    ref = load_bibtex_for_test["inproceedings"][0]
    reference = in_proceedings(ref, None)
    reference_blessed = (
        "\n{:.paper}\n"
        "How to properly cite media{:.papertitle}  \n"
        "S.B. Second, T.C. Third, S. Fourth-Fifth, and F.A. Author{:.authors}  \n"
        "Paper 2A12, 1st International Conference on BibTeX, University, Anytown, CA, "
        "May 2013{:.journal}  \n"
        "The files for this paper can be found at the following link{:.comment}  \n"
        )
    assert reference == reference_blessed

def test_phd_thesis(load_bibtex_for_test):
    ref = load_bibtex_for_test["phdthesis"][0]
    reference = phd_thesis(ref, None)
    reference_blessed = (
        "\n{:.paper}\n"
        "The worst sources of name generation{:.papertitle}  \n"
        "F.A. Author{:.authors}  \n"
        "College, Aug. 2014{:.journal}  \n"
        "Files at the following link{:.comment}  \n"
        )
    assert reference == reference_blessed
<|endoftext|>


<commit_before>import os
import signal
import socket
import subprocess
import sys
import time

import pytest

from .conftest import EXAMPLE_FILE

HOST = '0.0.0.0'
FAVA = ('fava', )

if 'BEANCOUNT_FILE' in os.environ:
    del os.environ['BEANCOUNT_FILE']

def _get_port():
    sock = socket.socket()
    sock.bind((HOST, 0))
    port = sock.getsockname()[1]
    sock.close()
    return port

def _wait_for_output(process, output, timeout):
    endtime = time.time() + timeout
    while True:
        if time.time() > endtime:
            return
        if output in process.stdout.readline():
            return
        time.sleep(.1)

def _run_fava(args=None):
    proc = FAVA + args if args else FAVA
    return subprocess.Popen(
        proc,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        universal_newlines=True)

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
def test_cli_basic():
    port = _get_port()
    args = (EXAMPLE_FILE, '-p', str(port))
    process = _run_fava(args)
    _wait_for_output(process, 'Running on', 20)
    process.send_signal(signal.SIGINT)
    process.wait()

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
def test_cli_prefix():
    args = (EXAMPLE_FILE, '-p', str(_get_port()), '--prefix', '/test')
    process = _run_fava(args)
    _wait_for_output(process, 'Running on', 20)
    process.send_signal(signal.SIGINT)
    process.wait()

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
def test_cli_empty():
    process = _run_fava()
    process.wait()
    assert 'No file specified' in ''.join(process.stdout.readlines())
    assert process.returncode == 2

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
def test_cli_addrinuse():
    args = (EXAMPLE_FILE, '-p', str(_get_port()))
    process = _run_fava(args)
    _wait_for_output(process, 'Running on', 20)
    process2 = _run_fava(args)
    process2.wait()
    process.send_signal(signal.SIGINT)
    assert 'in use' in ''.join(process2.stdout.readlines())
    assert process2.returncode == 2
<commit_msg>reduce cli tests<commit_after>import os
import socket
import subprocess
import sys
import time

import pytest

from .conftest import EXAMPLE_FILE

HOST = '0.0.0.0'
FAVA = ('fava', )

if 'BEANCOUNT_FILE' in os.environ:
    del os.environ['BEANCOUNT_FILE']

def get_port():
    sock = socket.socket()
    sock.bind((HOST, 0))
    port = sock.getsockname()[1]
    sock.close()
    return port

def output_contains(process, output, timeout):
    endtime = time.time() + timeout
    while True:
        if time.time() > endtime:
            return False
        if output in process.stdout.readline():
            return True
        time.sleep(.1)

def run_fava(args=()):
    return subprocess.Popen(
        FAVA + args,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        universal_newlines=True)

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
def test_cli():
    port = str(get_port())
    args = (EXAMPLE_FILE, '-p', port)
    process = run_fava(args)
    assert output_contains(process, 'Running on', 20)
    process2 = run_fava(args)
    process2.wait()
    process.terminate()
    process.wait()
    assert 'in use' in ''.join(process2.stdout.readlines())
    assert process2.returncode == 2
<|endoftext|>


<commit_before>"""Test double linked list implementation."""

import pytest

@pytest.fixture
def test_lists():
    """Dll fixtures."""
    from src.dll import DoubleLinkedList
    empty = DoubleLinkedList()
    one = DoubleLinkedList(3)
    multi = DoubleLinkedList([1, 2, 3, 4, 5])
    return empty, one, multi

def test_node_class():
    """Test node class has data."""
    from src.dll import Node
    node = Node(5)
    assert node.data is 5

def test_list_of_none(test_lists):
    """Test list of none head and tail is none."""
    assert test_lists[0].head is None
    assert test_lists[0].tail is None

def test_list_of_one(test_lists):
    """Test list of one, head is tail."""
    assert test_lists[1].head is test_lists[1].tail

def test_list_of_five(test_lists):
    """Test list of five."""
    assert test_lists[2].head.data is 5
    assert test_lists[2].tail.data is 1

def test_prev_pointer(test_lists):
    """Test prev pointer."""
    assert test_lists[2].tail.prev.data is 2

def test_next_pointer(test_lists):
    """Test next pointer."""
    assert test_lists[2].head.next.data is 4

def test_push_increases_length(test_lists):
    """Test push increases length."""
    test_lists[0].push(2)
    assert test_lists[0]._length is 1

def test_push_updates_head(test_lists):
    """Test push updates head."""
    test_lists[1].push(6)
    assert test_lists[1].head.data is 6

def test_push_points_back(test_lists):
    """Test old head points to new with prev after a push."""
    old_head = test_lists[1].head
    test_lists[1].push(6)
    assert test_lists[1].head is old_head.prev

def test_pop_reduces_length(test_lists):
    """Test pop reduces lists."""
    old_length = test_lists[2]._length
    test_lists[2].pop()
    assert test_lists[2]._length is old_length - 1

def test_pop_removes_head(test_lists):
    """Test pop removes head."""
    new_head = test_lists[2].head.next.data
    test_lists[2].pop()
    assert test_lists[2].head.data is new_head

def test_pop_removes_prev_pointer(test_lists):
    """Test pop changes prev pointer."""
    test_lists[2].pop()
    assert test_lists[2].head.prev is None

def test_pop_list_one(test_lists):
    """Test pop decreases length."""
    test_lists[1].pop()
    assert test_lists[1]._length is 0

def test_cant_pop_on_empty_list(test_lists):
    """Test pop on an empty list raises error."""
    with pytest.raises(IndexError, message='Cannot pop from an empty list'):
        test_lists[0].pop()

def test_append_increases_length(test_lists):
    """Test append increases length."""
    test_lists[0].append(2)
    assert test_lists[0]._length is 1

def test_append_updates_tail(test_lists):
    """Test append updates tail."""
    test_lists[1].append(6)
    assert test_lists[1].tail.data is 6

def test_append_points_back(test_lists):
    """Test old tail points to new with prev after a append."""
    old_tail = test_lists[1].tail
    test_lists[1].append(6)
    assert test_lists[1].tail is old_tail.prev

def test_append_on_empty_list(test_lists):
    """Test append updates tail."""
    test_lists[0].append(6)
    assert test_lists[0].tail.data is 6
    assert test_lists[0].head.data is 6

def test_append_next_pointer_is_none(test_lists):
    """Test append next pointer is none."""
    test_lists[2].append(6)
    assert test_lists[2].tail.next is None

def test_pop_sequence(test_lists):
    """Test that entire sequence is returned by successive pops."""
    l = []
    while True:
        try:
            popped_data = test_lists[2].pop()
            l.append(popped_data.data)
        except IndexError:
            break
    assert l == [5, 4, 3, 2, 1]

def test_push_pop(test_lists):
    """Push data and pop it off."""
    test_lists[1].push(9)
    popped_data = test_lists[1].pop()
    assert popped_data.data is 9

def test_shift_reduces_length(test_lists):
    """Test shift reduces lists."""
    old_length = test_lists[2]._length
    test_lists[2].shift()
    assert test_lists[2]._length is old_length - 1

def test_shift_removes_tail(test_lists):
    """Test shift removes tail."""
    new_tail = test_lists[2].tail.prev.data
    test_lists[2].shift()
    assert test_lists[2].tail.data is new_tail

def test_shift_removes_next_pointer(test_lists):
    """Test shift changes prev pointer."""
    test_lists[2].shift()
    assert test_lists[2].tail.next is None

def test_shift_list_one(test_lists):
    """Test shift decreases length."""
    test_lists[1].shift()
    assert test_lists[1]._length is 0

def test_cant_shift_on_empty_list(test_lists):
    """Test shift on an empty list raises error."""
    with pytest.raises(IndexError, message='Cannot shift from an empty list'):
        test_lists[0].shift()

def test_shift_sequence(test_lists):
    """Test that entire sequence is returned by successive shifts."""
    l = []
    while True:
        try:
            shifted_data = test_lists[2].shift()
            l.append(shifted_data.data)
        except IndexError:
            break
    assert l == [1, 2, 3, 4, 5]

def test_shift_append(test_lists):
    """Append data and shift it off."""
    test_lists[1].append(9)
    shifted_data = test_lists[1].shift()
    assert shifted_data.data is 9

def test_remove_middle_of_list(test_lists):
    """Test remove from middle of list."""
    test_lists[2].remove(3)
    assert test_lists[2]._repr() == [5, 4, 2, 1]

def test_remove_head_of_list(test_lists):
    """Test remove from head of list."""
    test_lists[2].remove(5)
    assert test_lists[2]._repr() == [4, 3, 2, 1]

def test_remove_tail_of_list(test_lists):
    """Test remove from tail of list."""
    test_lists[2].remove(1)
    assert test_lists[2]._repr() == [5, 4, 3, 2]

def test_remove_middle_decreases_length(test_lists):
    """Test remove from middle of list decreases length."""
    test_lists[2].remove(3)
    assert test_lists[2]._length is 4

def test_remove_head_decreases_length(test_lists):
    """Test remove from head of list decreases length."""
    test_lists[2].remove(5)
    assert test_lists[2]._length is 4

def test_remove_tail_decreases_length(test_lists):
    """Test remove from tail of list decreases length."""
    test_lists[2].remove(1)
    assert test_lists[2]._length is 4

def test_remove_middle_updates_pointers(test_lists):
    """Test remove from middle of list updates pointers."""
    test_lists[2].remove(3)
    assert test_lists[2].head.next.next.data is 2

def test_remove_head_pointers(test_lists):
    """Test remove from head of list changes pointers."""
    test_lists[2].remove(5)
    assert test_lists[2].head.data is 4
    assert test_lists[2].head.prev is None

def test_remove_tail_pointers(test_lists):
    """Test remove from tail of list changes pointers."""
    test_lists[2].remove(1)
    assert test_lists[2].tail.data is 2
    assert test_lists[2].tail.next is None

def test_remove_list_of_one_length(test_lists):
    """Test remove on list of one."""
    test_lists[1].remove(3)
    assert test_lists[1]._length is 0

def test_remove_list_of_one(test_lists):
    """Test remove on list of one."""
    test_lists[1].remove(3)
    assert test_lists[1].head is None
    assert test_lists[1].tail is None

def test_remove_list_of_none(test_lists):
    """Test remove on list of none."""
    test_lists[0].remove(3)
    assert test_lists[0].head is None
    assert test_lists[0].tail is None

def test_remove_of_list_false(test_lists):
    """Test remove from middle of list."""
    test_lists[2].remove(9)
    assert test_lists[2]._repr() == [5, 4, 3, 2, 1]
<commit_msg>fixed tests to reflect new dll code<commit_after>"""Test double linked list implementation."""

import pytest

@pytest.fixture
def test_lists():
    """Dll fixtures."""
    from src.dll import DoubleLinkedList
    empty = DoubleLinkedList()
    one = DoubleLinkedList(3)
    multi = DoubleLinkedList([1, 2, 3, 4, 5])
    return empty, one, multi

def test_node_class():
    """Test node class has data."""
    from src.dll import Node
    node = Node(5)
    assert node.data is 5

def test_list_of_none(test_lists):
    """Test list of none head and tail is none."""
    assert test_lists[0].head is None
    assert test_lists[0].tail is None

def test_list_of_one(test_lists):
    """Test list of one, head is tail."""
    assert test_lists[1].head is test_lists[1].tail

def test_list_of_five(test_lists):
    """Test list of five."""
    assert test_lists[2].head.data is 5
    assert test_lists[2].tail.data is 1

def test_prev_pointer(test_lists):
    """Test prev pointer."""
    assert test_lists[2].tail.prev.data is 2

def test_next_pointer(test_lists):
    """Test next pointer."""
    assert test_lists[2].head.next.data is 4

def test_push_increases_length(test_lists):
    """Test push increases length."""
    test_lists[0].push(2)
    assert test_lists[0]._length is 1

def test_push_updates_head(test_lists):
    """Test push updates head."""
    test_lists[1].push(6)
    assert test_lists[1].head.data is 6

def test_push_points_back(test_lists):
    """Test old head points to new with prev after a push."""
    old_head = test_lists[1].head
    test_lists[1].push(6)
    assert test_lists[1].head is old_head.prev

def test_pop_reduces_length(test_lists):
    """Test pop reduces lists."""
    old_length = test_lists[2]._length
    test_lists[2].pop()
    assert test_lists[2]._length is old_length - 1

def test_pop_removes_head(test_lists):
    """Test pop removes head."""
    new_head = test_lists[2].head.next.data
    test_lists[2].pop()
    assert test_lists[2].head.data is new_head

def test_pop_removes_prev_pointer(test_lists):
    """Test pop changes prev pointer."""
    test_lists[2].pop()
    assert test_lists[2].head.prev is None

def test_pop_list_one(test_lists):
    """Test pop decreases length."""
    test_lists[1].pop()
    assert test_lists[1]._length is 0

def test_pop_returns_data(test_lists):
    """Test pop returns data."""
    assert test_lists[2].pop() is 5

def test_cant_pop_on_empty_list(test_lists):
    """Test pop on an empty list raises error."""
    with pytest.raises(IndexError, message='Cannot pop from an empty list'):
        test_lists[0].pop()

def test_append_increases_length(test_lists):
    """Test append increases length."""
    test_lists[0].append(2)
    assert test_lists[0]._length is 1

def test_append_updates_tail(test_lists):
    """Test append updates tail."""
    test_lists[1].append(6)
    assert test_lists[1].tail.data is 6

def test_append_points_back(test_lists):
    """Test old tail points to new with prev after a append."""
    old_tail = test_lists[1].tail
    test_lists[1].append(6)
    assert test_lists[1].tail is old_tail.next

def test_append_on_empty_list(test_lists):
    """Test append updates tail."""
    test_lists[0].append(6)
    assert test_lists[0].tail.data is 6
    assert test_lists[0].head.data is 6

def test_append_next_pointer_is_none(test_lists):
    """Test append next pointer is none."""
    test_lists[2].append(6)
    assert test_lists[2].tail.next is None

def test_pop_sequence(test_lists):
    """Test that entire sequence is returned by successive pops."""
    l = []
    while True:
        try:
            popped_data = test_lists[2].pop()
            l.append(popped_data)
        except IndexError:
            break
    assert l == [5, 4, 3, 2, 1]

def test_push_pop(test_lists):
    """Push data and pop it off."""
    test_lists[1].push(9)
    popped_data = test_lists[1].pop()
    assert popped_data is 9

def test_shift_reduces_length(test_lists):
    """Test shift reduces lists."""
    old_length = test_lists[2]._length
    test_lists[2].shift()
    assert test_lists[2]._length is old_length - 1

def test_shift_removes_tail(test_lists):
    """Test shift removes tail."""
    new_tail = test_lists[2].tail.prev.data
    test_lists[2].shift()
    assert test_lists[2].tail.data is new_tail

def test_shift_removes_next_pointer(test_lists):
    """Test shift changes prev pointer."""
    test_lists[2].shift()
    assert test_lists[2].tail.next is None

def test_shift_list_one(test_lists):
    """Test shift decreases length."""
    test_lists[1].shift()
    assert test_lists[1]._length is 0

def test_cant_shift_on_empty_list(test_lists):
    """Test shift on an empty list raises error."""
    with pytest.raises(IndexError, message='Cannot shift from an empty list'):
        test_lists[0].shift()

def test_shift_sequence(test_lists):
    """Test that entire sequence is returned by successive shifts."""
    l = []
    while True:
        try:
            shifted_data = test_lists[2].shift()
            l.append(shifted_data)
        except IndexError:
            break
    assert l == [1, 2, 3, 4, 5]

def test_shift_append(test_lists):
    """Append data and shift it off."""
    test_lists[1].append(9)
    shifted_data = test_lists[1].shift()
    assert shifted_data is 9

def test_remove_middle_of_list(test_lists):
    """Test remove from middle of list."""
    test_lists[2].remove(3)
    assert test_lists[2]._repr() == [5, 4, 2, 1]

def test_remove_head_of_list(test_lists):
    """Test remove from head of list."""
    test_lists[2].remove(5)
    assert test_lists[2]._repr() == [4, 3, 2, 1]

def test_remove_tail_of_list(test_lists):
    """Test remove from tail of list."""
    test_lists[2].remove(1)
    assert test_lists[2]._repr() == [5, 4, 3, 2]

def test_remove_middle_decreases_length(test_lists):
    """Test remove from middle of list decreases length."""
    test_lists[2].remove(3)
    assert test_lists[2]._length is 4

def test_remove_head_decreases_length(test_lists):
    """Test remove from head of list decreases length."""
    test_lists[2].remove(5)
    assert test_lists[2]._length is 4

def test_remove_tail_decreases_length(test_lists):
    """Test remove from tail of list decreases length."""
    test_lists[2].remove(1)
    assert test_lists[2]._length is 4

def test_remove_middle_updates_pointers(test_lists):
    """Test remove from middle of list updates pointers."""
    test_lists[2].remove(3)
    assert test_lists[2].head.next.next.data is 2

def test_remove_head_pointers(test_lists):
    """Test remove from head of list changes pointers."""
    test_lists[2].remove(5)
    assert test_lists[2].head.data is 4
    assert test_lists[2].head.prev is None

def test_remove_tail_pointers(test_lists):
    """Test remove from tail of list changes pointers."""
    test_lists[2].remove(1)
    assert test_lists[2].tail.data is 2
    assert test_lists[2].tail.next is None

def test_remove_list_of_one_length(test_lists):
    """Test remove on list of one."""
    test_lists[1].remove(3)
    assert test_lists[1]._length is 0

def test_remove_list_of_one(test_lists):
    """Test remove on list of one."""
    test_lists[1].remove(3)
    assert test_lists[1].head is None
    assert test_lists[1].tail is None

def test_remove_list_of_none(test_lists):
    """Test remove on list of none."""
    with pytest.raises(ValueError):
        test_lists[0].remove(3)

def test_remove_of_list_false(test_lists):
    """Test remove from middle of list."""
    with pytest.raises(ValueError):
        test_lists[2].remove(9)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from . import unittest
from twisted.internet import defer
from twisted.names import dns, error

from mock import Mock

from synapse.http.endpoint import resolve_service

class DnsTestCase(unittest.TestCase):

    @defer.inlineCallbacks
    def test_resolve(self):
        dns_client_mock = Mock()

        service_name = "test_service.examle.com"
        host_name = "example.com"
        ip_address = "127.0.0.1"

        answer_srv = dns.RRHeader(
            type=dns.SRV,
            payload=dns.Record_SRV(
                target=host_name,
            )
        )

        answer_a = dns.RRHeader(
            type=dns.A,
            payload=dns.Record_A(
                address=ip_address,
            )
        )

        dns_client_mock.lookupService.return_value = ([answer_srv], None, None)
        dns_client_mock.lookupAddress.return_value = ([answer_a], None, None)

        cache = {}

        servers = yield resolve_service(
            service_name, dns_client=dns_client_mock, cache=cache
        )

        dns_client_mock.lookupService.assert_called_once_with(service_name)
        dns_client_mock.lookupAddress.assert_called_once_with(host_name)

        self.assertEquals(len(servers), 1)
        self.assertEquals(servers, cache[service_name])
        self.assertEquals(servers[0].host, ip_address)

    @defer.inlineCallbacks
    def test_from_cache(self):
        dns_client_mock = Mock()
        dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())

        service_name = "test_service.examle.com"

        entry = Mock(spec_set=["expires"])
        entry.expires = 999999999

        cache = {
            service_name: [entry]
        }

        servers = yield resolve_service(
            service_name, dns_client=dns_client_mock, cache=cache
        )

        dns_client_mock.lookupService.assert_called_once_with(service_name)

        self.assertEquals(len(servers), 1)
        self.assertEquals(servers, cache[service_name])

    @defer.inlineCallbacks
    def test_empty_cache(self):
        dns_client_mock = Mock()

        dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())

        service_name = "test_service.examle.com"

        cache = {}

        with self.assertRaises(error.DNSServerError):
            yield resolve_service(
                service_name, dns_client=dns_client_mock, cache=cache
            )

    @defer.inlineCallbacks
    def test_name_error(self):
        dns_client_mock = Mock()

        dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())

        service_name = "test_service.examle.com"

        cache = {}

        servers = yield resolve_service(
            service_name, dns_client=dns_client_mock, cache=cache
        )

        self.assertEquals(len(servers), 0)
        self.assertEquals(len(cache), 0)
<commit_msg>Tests<commit_after># -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from . import unittest
from twisted.internet import defer
from twisted.names import dns, error

from mock import Mock

from synapse.http.endpoint import resolve_service

from tests.utils import MockClock

class DnsTestCase(unittest.TestCase):

    @defer.inlineCallbacks
    def test_resolve(self):
        dns_client_mock = Mock()

        service_name = "test_service.examle.com"
        host_name = "example.com"
        ip_address = "127.0.0.1"

        answer_srv = dns.RRHeader(
            type=dns.SRV,
            payload=dns.Record_SRV(
                target=host_name,
            )
        )

        answer_a = dns.RRHeader(
            type=dns.A,
            payload=dns.Record_A(
                address=ip_address,
            )
        )

        dns_client_mock.lookupService.return_value = ([answer_srv], None, None)
        dns_client_mock.lookupAddress.return_value = ([answer_a], None, None)

        cache = {}

        servers = yield resolve_service(
            service_name, dns_client=dns_client_mock, cache=cache
        )

        dns_client_mock.lookupService.assert_called_once_with(service_name)
        dns_client_mock.lookupAddress.assert_called_once_with(host_name)

        self.assertEquals(len(servers), 1)
        self.assertEquals(servers, cache[service_name])
        self.assertEquals(servers[0].host, ip_address)

    @defer.inlineCallbacks
    def test_from_cache_expired_and_dns_fail(self):
        dns_client_mock = Mock()
        dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())

        service_name = "test_service.examle.com"

        entry = Mock(spec_set=["expires"])
        entry.expires = 0

        cache = {
            service_name: [entry]
        }

        servers = yield resolve_service(
            service_name, dns_client=dns_client_mock, cache=cache
        )

        dns_client_mock.lookupService.assert_called_once_with(service_name)

        self.assertEquals(len(servers), 1)
        self.assertEquals(servers, cache[service_name])

    @defer.inlineCallbacks
    def test_from_cache(self):
        clock = MockClock()

        dns_client_mock = Mock(spec_set=['lookupService'])
        dns_client_mock.lookupService = Mock(spec_set=[])

        service_name = "test_service.examle.com"

        entry = Mock(spec_set=["expires"])
        entry.expires = 999999999

        cache = {
            service_name: [entry]
        }

        servers = yield resolve_service(
            service_name, dns_client=dns_client_mock, cache=cache, clock=clock,
        )

        self.assertFalse(dns_client_mock.lookupService.called)

        self.assertEquals(len(servers), 1)
        self.assertEquals(servers, cache[service_name])

    @defer.inlineCallbacks
    def test_empty_cache(self):
        dns_client_mock = Mock()

        dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())

        service_name = "test_service.examle.com"

        cache = {}

        with self.assertRaises(error.DNSServerError):
            yield resolve_service(
                service_name, dns_client=dns_client_mock, cache=cache
            )

    @defer.inlineCallbacks
    def test_name_error(self):
        dns_client_mock = Mock()

        dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())

        service_name = "test_service.examle.com"

        cache = {}

        servers = yield resolve_service(
            service_name, dns_client=dns_client_mock, cache=cache
        )

        self.assertEquals(len(servers), 0)
        self.assertEquals(len(cache), 0)
<|endoftext|>


<commit_before>from __future__ import (print_function, division)
from six.moves import range
import numpy as np
from numpy import linalg
import numpy.testing as npt
import matplotlib

matplotlib.use('Agg')
from matplotlib import pyplot as plt  # noqa
import dynesty  # noqa
from dynesty import plotting as dyplot  # noqa
from dynesty import utils as dyfunc  # noqa
"""
Run a series of basic tests to check whether anything huge is broken.

"""

nlive = 500
printing = False

def bootstrap_tol(results):
    """ Compute the uncertainty of means/covs by doing bootstrapping """
    n = len(results.logz)
    niter = 50
    pos = results.samples
    wts = np.exp(results.logwt - results.logz[-1])
    means = []
    covs = []

    for i in range(niter):
        xid = np.random.randint(n, size=n)
        mean, cov = dyfunc.mean_and_cov(pos[xid], wts[xid])
        means.append(mean)
        covs.append(cov)
    return np.std(means, axis=0), np.std(covs, axis=0)

def check_results(results,
                  mean_truth,
                  cov_truth,
                  logz_truth,
                  mean_tol,
                  cov_tol,
                  logz_tol,
                  sig=5):
    """ Check if means and covariances match match expectations
    within the tolerances

    """
    pos = results.samples
    wts = np.exp(results.logwt - results.logz[-1])
    mean, cov = dyfunc.mean_and_cov(pos, wts)
    logz = results.logz[-1]
    npt.assert_array_less(np.abs(mean - mean_truth), sig * mean_tol)
    npt.assert_array_less(np.abs(cov - cov_truth), sig * cov_tol)
    npt.assert_array_less(np.abs((logz_truth - logz)), sig * logz_tol)

# GAUSSIAN TEST

ndim_gau = 3
mean_gau = np.linspace(-1, 1, ndim_gau)
cov_gau = np.identity(ndim_gau)  # set covariance to identity matrix
cov_gau[cov_gau == 0] = 0.95  # set off-diagonal terms (strongly correlated)
cov_inv_gau = linalg.inv(cov_gau)  # precision matrix
lnorm_gau = -0.5 * (np.log(2 * np.pi) * ndim_gau + np.log(linalg.det(cov_gau)))
prior_win = 10 # +/- 10 on both sides
logz_truth_gau = ndim_gau * (-np.log(2 * prior_win))

def check_results_gau(results, logz_tol, sig=5):
    mean_tol, cov_tol = bootstrap_tol(results)
    check_results(results,
                  mean_gau,
                  cov_gau,
                  logz_truth_gau,
                  mean_tol,
                  cov_tol,
                  logz_tol,
                  sig=sig)

# 3-D correlated multivariate normal log-likelihood
def loglikelihood_gau(x):
    """Multivariate normal log-likelihood."""
    return -0.5 * np.dot((x - mean_gau), np.dot(cov_inv_gau,
                                                (x - mean_gau))) + lnorm_gau

# prior transform
def prior_transform_gau(u):
    """Flat prior between -10. and 10."""
    return prior_win * (2. * u - 1.)

# gradient (no jacobian)
def grad_x_gau(x):
    """Multivariate normal log-likelihood gradient."""
    return -np.dot(cov_inv_gau, (x - mean_gau))

# gradient (with jacobian)
def grad_u_gau(x):
    """Multivariate normal log-likelihood gradient."""
    return -np.dot(cov_inv_gau, x - mean_gau) * 2 * prior_win

def test_gaussian():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive)
    sampler.run_nested(print_progress=printing)
    # check that jitter works
    dyfunc.jitter_run(sampler.results)

    # add samples
    # check continuation behavior
    sampler.run_nested(dlogz=0.1, print_progress=printing)

    # get errors
    nerr = 2
    for i in range(nerr):
        sampler.reset()
        sampler.run_nested(print_progress=False)
        results = sampler.results
        pos = results.samples
        wts = np.exp(results.logwt - results.logz[-1])
        mean, cov = dyfunc.mean_and_cov(pos, wts)
        logz = results.logz[-1]
        assert (np.abs(logz - logz_truth_gau) < logz_tol)
    # check summary
    res = sampler.results
    res.summary()

    # check plots
    dyplot.runplot(sampler.results)
    plt.close()
    dyplot.traceplot(sampler.results)
    plt.close()
    dyplot.cornerpoints(sampler.results)
    plt.close()
    dyplot.cornerplot(sampler.results)
    plt.close()
    dyplot.boundplot(sampler.results,
                     dims=(0, 1),
                     it=3000,
                     prior_transform=prior_transform_gau,
                     show_live=True,
                     span=[(-10, 10), (-10, 10)])
    plt.close()
    dyplot.cornerbound(sampler.results,
                       it=3500,
                       prior_transform=prior_transform_gau,
                       show_live=True,
                       span=[(-10, 10), (-10, 10)])
    plt.close()

def test_bounding():
    # check various bounding methods
    logz_tol = 1

    for bound in ['none', 'single', 'multi', 'balls', 'cubes']:
        sampler = dynesty.NestedSampler(loglikelihood_gau,
                                        prior_transform_gau,
                                        ndim_gau,
                                        nlive=nlive,
                                        bound=bound,
                                        sample='unif')
        sampler.run_nested(print_progress=printing)
        check_results_gau(sampler.results, logz_tol)

def test_bounding_bootstrap():
    # check various bounding methods
    logz_tol = 1

    for bound in ['single', 'multi', 'balls']:
        sampler = dynesty.NestedSampler(loglikelihood_gau,
                                        prior_transform_gau,
                                        ndim_gau,
                                        nlive=nlive,
                                        bound=bound,
                                        sample='unif',
                                        bootstrap=5)
        sampler.run_nested(print_progress=printing)
        check_results_gau(sampler.results, logz_tol)

def test_sampling():
    # check various sampling methods
    logz_tol = 1
    for sample in ['unif', 'rwalk', 'rstagger', 'slice', 'rslice']:
        sampler = dynesty.NestedSampler(loglikelihood_gau,
                                        prior_transform_gau,
                                        ndim_gau,
                                        nlive=nlive,
                                        sample=sample)
        sampler.run_nested(print_progress=printing)
        check_results_gau(sampler.results, logz_tol)

# extra checks for gradients
def test_slice_nograd():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive,
                                    sample='hslice')
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, logz_tol)

def test_slice_grad():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive,
                                    sample='hslice',
                                    gradient=grad_x_gau,
                                    compute_jac=True)
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, logz_tol)

def test_slice_grad1():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive,
                                    sample='hslice',
                                    gradient=grad_u_gau)
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, logz_tol)

def test_dynamic():
    # check dynamic nested sampling behavior
    logz_tol = 1
    dsampler = dynesty.DynamicNestedSampler(loglikelihood_gau,
                                            prior_transform_gau, ndim_gau)
    dsampler.run_nested(print_progress=printing)
    check_results_gau(dsampler.results, logz_tol)

    # check error analysis functions
    dres = dyfunc.jitter_run(dsampler.results)
    check_results_gau(dres, logz_tol)
    dres = dyfunc.resample_run(dsampler.results)
    check_results_gau(dres, logz_tol)
    dres = dyfunc.simulate_run(dsampler.results)
    check_results_gau(dres, logz_tol)
    # I bump the threshold
    # because we have the error twice
    dyfunc.kld_error(dsampler.results)
<commit_msg>add more testing for not dynamic sampler<commit_after>from __future__ import (print_function, division)
from six.moves import range
import numpy as np
from numpy import linalg
import numpy.testing as npt
import matplotlib

matplotlib.use('Agg')
from matplotlib import pyplot as plt  # noqa
import dynesty  # noqa
from dynesty import plotting as dyplot  # noqa
from dynesty import utils as dyfunc  # noqa
"""
Run a series of basic tests to check whether anything huge is broken.

"""

nlive = 500
printing = False

def bootstrap_tol(results):
    """ Compute the uncertainty of means/covs by doing bootstrapping """
    n = len(results.logz)
    niter = 50
    pos = results.samples
    wts = np.exp(results.logwt - results.logz[-1])
    means = []
    covs = []

    for i in range(niter):
        xid = np.random.randint(n, size=n)
        mean, cov = dyfunc.mean_and_cov(pos[xid], wts[xid])
        means.append(mean)
        covs.append(cov)
    return np.std(means, axis=0), np.std(covs, axis=0)

def check_results(results,
                  mean_truth,
                  cov_truth,
                  logz_truth,
                  mean_tol,
                  cov_tol,
                  logz_tol,
                  sig=5):
    """ Check if means and covariances match match expectations
    within the tolerances

    """
    pos = results.samples
    wts = np.exp(results.logwt - results.logz[-1])
    mean, cov = dyfunc.mean_and_cov(pos, wts)
    logz = results.logz[-1]
    npt.assert_array_less(np.abs(mean - mean_truth), sig * mean_tol)
    npt.assert_array_less(np.abs(cov - cov_truth), sig * cov_tol)
    npt.assert_array_less(np.abs((logz_truth - logz)), sig * logz_tol)

# GAUSSIAN TEST

ndim_gau = 3
mean_gau = np.linspace(-1, 1, ndim_gau)
cov_gau = np.identity(ndim_gau)  # set covariance to identity matrix
cov_gau[cov_gau == 0] = 0.95  # set off-diagonal terms (strongly correlated)
cov_inv_gau = linalg.inv(cov_gau)  # precision matrix
lnorm_gau = -0.5 * (np.log(2 * np.pi) * ndim_gau + np.log(linalg.det(cov_gau)))
prior_win = 10 # +/- 10 on both sides
logz_truth_gau = ndim_gau * (-np.log(2 * prior_win))

def check_results_gau(results, logz_tol, sig=5):
    mean_tol, cov_tol = bootstrap_tol(results)
    check_results(results,
                  mean_gau,
                  cov_gau,
                  logz_truth_gau,
                  mean_tol,
                  cov_tol,
                  logz_tol,
                  sig=sig)

# 3-D correlated multivariate normal log-likelihood
def loglikelihood_gau(x):
    """Multivariate normal log-likelihood."""
    return -0.5 * np.dot((x - mean_gau), np.dot(cov_inv_gau,
                                                (x - mean_gau))) + lnorm_gau

# prior transform
def prior_transform_gau(u):
    """Flat prior between -10. and 10."""
    return prior_win * (2. * u - 1.)

# gradient (no jacobian)
def grad_x_gau(x):
    """Multivariate normal log-likelihood gradient."""
    return -np.dot(cov_inv_gau, (x - mean_gau))

# gradient (with jacobian)
def grad_u_gau(x):
    """Multivariate normal log-likelihood gradient."""
    return -np.dot(cov_inv_gau, x - mean_gau) * 2 * prior_win

def test_gaussian():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive)
    sampler.run_nested(print_progress=printing)
    # check that jitter/resample/simulate_run work
    # for not dynamic sampler
    dyfunc.jitter_run(sampler.results)
    dyfunc.resample_run(sampler.results)
    dyfunc.simulate_run(sampler.results)

    # add samples
    # check continuation behavior
    sampler.run_nested(dlogz=0.1, print_progress=printing)

    # get errors
    nerr = 2
    for i in range(nerr):
        sampler.reset()
        sampler.run_nested(print_progress=False)
        results = sampler.results
        pos = results.samples
        wts = np.exp(results.logwt - results.logz[-1])
        mean, cov = dyfunc.mean_and_cov(pos, wts)
        logz = results.logz[-1]
        assert (np.abs(logz - logz_truth_gau) < logz_tol)
    # check summary
    res = sampler.results
    res.summary()

    # check plots
    dyplot.runplot(sampler.results)
    plt.close()
    dyplot.traceplot(sampler.results)
    plt.close()
    dyplot.cornerpoints(sampler.results)
    plt.close()
    dyplot.cornerplot(sampler.results)
    plt.close()
    dyplot.boundplot(sampler.results,
                     dims=(0, 1),
                     it=3000,
                     prior_transform=prior_transform_gau,
                     show_live=True,
                     span=[(-10, 10), (-10, 10)])
    plt.close()
    dyplot.cornerbound(sampler.results,
                       it=3500,
                       prior_transform=prior_transform_gau,
                       show_live=True,
                       span=[(-10, 10), (-10, 10)])
    plt.close()

def test_bounding():
    # check various bounding methods
    logz_tol = 1

    for bound in ['none', 'single', 'multi', 'balls', 'cubes']:
        sampler = dynesty.NestedSampler(loglikelihood_gau,
                                        prior_transform_gau,
                                        ndim_gau,
                                        nlive=nlive,
                                        bound=bound,
                                        sample='unif')
        sampler.run_nested(print_progress=printing)
        check_results_gau(sampler.results, logz_tol)

def test_bounding_bootstrap():
    # check various bounding methods
    logz_tol = 1

    for bound in ['single', 'multi', 'balls']:
        sampler = dynesty.NestedSampler(loglikelihood_gau,
                                        prior_transform_gau,
                                        ndim_gau,
                                        nlive=nlive,
                                        bound=bound,
                                        sample='unif',
                                        bootstrap=5)
        sampler.run_nested(print_progress=printing)
        check_results_gau(sampler.results, logz_tol)

def test_sampling():
    # check various sampling methods
    logz_tol = 1
    for sample in ['unif', 'rwalk', 'rstagger', 'slice', 'rslice']:
        sampler = dynesty.NestedSampler(loglikelihood_gau,
                                        prior_transform_gau,
                                        ndim_gau,
                                        nlive=nlive,
                                        sample=sample)
        sampler.run_nested(print_progress=printing)
        check_results_gau(sampler.results, logz_tol)

# extra checks for gradients
def test_slice_nograd():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive,
                                    sample='hslice')
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, logz_tol)

def test_slice_grad():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive,
                                    sample='hslice',
                                    gradient=grad_x_gau,
                                    compute_jac=True)
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, logz_tol)

def test_slice_grad1():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive,
                                    sample='hslice',
                                    gradient=grad_u_gau)
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, logz_tol)

def test_dynamic():
    # check dynamic nested sampling behavior
    logz_tol = 1
    dsampler = dynesty.DynamicNestedSampler(loglikelihood_gau,
                                            prior_transform_gau, ndim_gau)
    dsampler.run_nested(print_progress=printing)
    check_results_gau(dsampler.results, logz_tol)

    # check error analysis functions
    dres = dyfunc.jitter_run(dsampler.results)
    check_results_gau(dres, logz_tol)
    dres = dyfunc.resample_run(dsampler.results)
    check_results_gau(dres, logz_tol)
    dres = dyfunc.simulate_run(dsampler.results)
    check_results_gau(dres, logz_tol)
    # I bump the threshold
    # because we have the error twice
    dyfunc.kld_error(dsampler.results)
<|endoftext|>


<commit_before>import time
import psutil
import pytest
import datetime
import multiprocessing
from copy import deepcopy
from pprint import pprint

import virtool.job

class TestProcessor:

    def test(self, test_job):
        """
        Test that the processor changes the ``_id`` field to ``job_id``.

        """
        processed = virtool.job.processor(deepcopy(test_job))

        test_job["job_id"] = test_job.pop("_id")

        assert processed == test_job

class TestDispatchProcessor:

    def test(self, test_db, test_job):
        """
        Test that the dispatch processor properly formats a raw job document into a dispatchable format.

        """
        test_db.jobs.insert_one(test_job)

        document = test_db.jobs.find_one()

        assert virtool.job.dispatch_processor(document) == {
            "added": datetime.datetime(2017, 10, 6, 20, 0),
            "args": {
                "algorithm": "nuvs",
                "analysis_id": "e410429b",
                "index_id": "465428b0",
                "name": None,
                "sample_id": "1e01a382",
                "username": "igboyes"
            },
            "job_id": "4c530449",
            "mem": 16,
            "proc": 10,
            "progress": 1.0,
            "stage": "import_results",
            "state": "complete",
            "task": "rebuild_index",
            "user_id": "igboyes"
        }

class TestJob:

    def test_db_access(self, test_db, test_task_inst):
        """
        Test that the job process can write to the database.

        """
        job = test_task_inst

        job.start()

        while job.is_alive():
            time.sleep(0.1)

        assert [d for d in test_db.job_test.find({}, {"_id": False})] == [
            {"message": "hello world"}
        ]

    def test_static_calls(self, test_task_inst):
        """
        Test that the process can correctly submit static calls to the job manager through the Queue.

        """
        job = test_task_inst

        job.start()

        while job.is_alive():
            time.sleep(0.1)

        static_calls = list()

        while not job._queue.empty():
            static_calls.append(job._queue.get())

        assert static_calls == [
            ("foobar", "add_status", ("foobar", 0, "running", None, None), {}),
            ("foobar", "add_status", ("foobar", 0.33, "running", "say_message", None), {}),
            ("foobar", "pass_message", ("hello world",), {}),
            ('foobar', 'add_status', ('foobar', 0.67, 'running', 'do_db_op', None), {}),
            ('foobar', 'add_status', ('foobar', 1, 'complete', 'do_db_op', None), {})
        ]

    def test_python_error(self, test_task_inst):
        """
        Test that the process can correctly report and internal Python error and terminate as a result of it.

        """
        job = test_task_inst

        job.generate_python_error = True

        job.start()

        static_calls = list()

        while job.is_alive():
            while not job._queue.empty():
                static_call = job._queue.get()

                if static_call[2] == "error":
                    job.terminate()

                static_calls.append(static_call)

        while not job._queue.empty():
            static_calls.append(job._queue.get())

        assert static_calls[:3] == [
            ('foobar', 'add_status', ('foobar', 0, 'running', None, None), {}),
            ('foobar', 'add_status', ('foobar', 0.33, 'running', 'say_message', None), {}),
            ('foobar', 'pass_message', ('hello world',), {})
        ]

        assert static_calls[3][0] == "foobar"
        assert static_calls[3][1] == "add_status"

        args = static_calls[3][2]

        assert args[:4] == ('foobar', 1, 'error', 'say_message')

        assert args[4]["context"] == "Python Error"

        error = args[4]["message"]

        assert error["details"] == ["unsupported operand type(s) for +: 'int' and 'str'"]

        assert error["type"] == "TypeError"

class TestTermination:

    def test(self):
        """
        Test that the :class:`virtool.job.Termination` exception works properly.

        """
        with pytest.raises(virtool.job.Termination) as err:
            raise virtool.job.Termination

        assert "Termination" in str(err)

class TestJobError:

    def test(self):
        """
        Test that the :class:`virtool.job.JobError` exception works properly.

        """
        with pytest.raises(virtool.job.JobError) as err:
            raise virtool.job.JobError

        assert "JobError" in str(err)

class TestStageMethod:

    def test(self):
        """
        Test the the stage_method decorator adds a ``is_stage_method`` attribute to a function with a value of ``True``.

        """
        @virtool.job.stage_method
        def func():
            return "Hello world"

        assert func.is_stage_method is True
<commit_msg>Update test_job<commit_after>import time
import pytest
import datetime
from copy import deepcopy

import virtool.job

class TestDispatchProcessor:

    def test(self, test_db, test_job):
        """
        Test that the dispatch processor properly formats a raw job document into a dispatchable format.

        """
        test_db.jobs.insert_one(test_job)

        document = test_db.jobs.find_one()

        assert virtool.job.dispatch_processor(document) == {
            "id": "4c530449",
            "created_at": datetime.datetime(2017, 10, 6, 20, 0),
            "args": {
                "algorithm": "nuvs",
                "analysis_id": "e410429b",
                "index_id": "465428b0",
                "name": None,
                "sample_id": "1e01a382",
                "username": "igboyes"
            },
            "mem": 16,
            "proc": 10,
            "progress": 1.0,
            "stage": "import_results",
            "state": "complete",
            "task": "rebuild_index",
            "user": {
                "id": "igboyes"
            }
        }

class TestJob:

    def test_db_access(self, test_db, test_task_inst):
        """
        Test that the job process can write to the database.

        """
        job = test_task_inst

        job.start()

        while job.is_alive():
            time.sleep(0.1)

        assert [d for d in test_db.job_test.find({}, {"_id": False})] == [
            {"message": "hello world"}
        ]

    def test_static_calls(self, test_task_inst):
        """
        Test that the process can correctly submit static calls to the job manager through the Queue.

        """
        job = test_task_inst

        job.start()

        while job.is_alive():
            time.sleep(0.1)

        static_calls = list()

        while not job._queue.empty():
            static_calls.append(job._queue.get())

        assert static_calls == [
            ("foobar", "add_status", ("foobar", 0, "running", None, None), {}),
            ("foobar", "add_status", ("foobar", 0.33, "running", "say_message", None), {}),
            ("foobar", "pass_message", ("hello world",), {}),
            ('foobar', 'add_status', ('foobar', 0.67, 'running', 'do_db_op', None), {}),
            ('foobar', 'add_status', ('foobar', 1, 'complete', 'do_db_op', None), {})
        ]

    def test_python_error(self, test_task_inst):
        """
        Test that the process can correctly report and internal Python error and terminate as a result of it.

        """
        job = test_task_inst

        job.generate_python_error = True

        job.start()

        static_calls = list()

        while job.is_alive():
            while not job._queue.empty():
                static_call = job._queue.get()

                if static_call[2] == "error":
                    job.terminate()

                static_calls.append(static_call)

        while not job._queue.empty():
            static_calls.append(job._queue.get())

        assert static_calls[:3] == [
            ('foobar', 'add_status', ('foobar', 0, 'running', None, None), {}),
            ('foobar', 'add_status', ('foobar', 0.33, 'running', 'say_message', None), {}),
            ('foobar', 'pass_message', ('hello world',), {})
        ]

        assert static_calls[3][0] == "foobar"
        assert static_calls[3][1] == "add_status"

        args = static_calls[3][2]

        assert args[:4] == ('foobar', 1, 'error', 'say_message')

        assert args[4]["context"] == "Python Error"

        error = args[4]["message"]

        assert error["details"] == ["unsupported operand type(s) for +: 'int' and 'str'"]

        assert error["type"] == "TypeError"

class TestTermination:

    def test(self):
        """
        Test that the :class:`virtool.job.Termination` exception works properly.

        """
        with pytest.raises(virtool.job.Termination) as err:
            raise virtool.job.Termination

        assert "Termination" in str(err)

class TestJobError:

    def test(self):
        """
        Test that the :class:`virtool.job.JobError` exception works properly.

        """
        with pytest.raises(virtool.job.JobError) as err:
            raise virtool.job.JobError

        assert "JobError" in str(err)

class TestStageMethod:

    def test(self):
        """
        Test the the stage_method decorator adds a ``is_stage_method`` attribute to a function with a value of ``True``.

        """
        @virtool.job.stage_method
        def func():
            return "Hello world"

        assert func.is_stage_method is True
<|endoftext|>


<commit_before>from unittest import TestCase
from dropbot.map import Map, ship_class_to_range
import pkgutil

class MapTestCase(TestCase):

    def setUp(self):
        self.map = Map.from_json(pkgutil.get_data('dropbot', 'data/map.json'))

    def test_load_from_package_data(self):
        m = Map.from_json(pkgutil.get_data('dropbot', 'data/map.json'))
        self.assertIsNotNone(m)

    def test_get_system_name(self):
        self.assertEquals(self.map.get_system_name(123), None)
        self.assertEqual(self.map.get_system_name(30000142), 'Jita')

    def test_get_system_id(self):
        self.assertEqual(self.map.get_system_id('Llamatron'), None)
        self.assertEqual(self.map.get_system_id('Jita'), 30000142)

    def test_get_systems(self):
        self.assertEquals(len(self.map.get_systems('Jita')), 1)
        self.assertEquals(len(self.map.get_systems('Ji')), 7)
        self.assertEquals(len(self.map.get_systems('J')), 2660)
        self.assertEquals(len(self.map.get_systems('123435345345')), 0)
        self.assertEquals(len(self.map.get_systems('jita')), 1)
        self.assertEquals(len(self.map.get_systems('JITA')), 1)
        self.assertEquals(len(self.map.get_systems('JiTa')), 1)

    def test_system_distance(self):
        self.assertEqual(self.map.system_distance(30000142, 30000144), 2.10268108033618)
        self.assertEqual(self.map.system_distance(30000142, 30000222), 9.334275248404591)
        self.assertEqual(self.map.system_distance(30000142, 30000536), 39.15289747780095)
        self.assertRaises(Exception, self.map.system_distance, (1, 2))

    def test_route_gate(self):
        r = self.map.route_gate(30001161, 30001198)
        self.assertEqual(len(r), 9)
        self.assertListEqual(r, [30001161, 30001158, 30001160, 30001154, 30001157, 30001155, 30001156, 30001162, 30001198])

    def test_route_jump(self):
        pass

    def test_route_jump_distance(self):
        pass

    def test_route_jump_isotopes(self):
        pass

    def test_neighbors_gate(self):
        pass

    def test_neighbors_jump(self):
        pass

    def test_jump_bridge_addition(self):
        # HED-GP to GE-8
        self.assertGreater(len(self.map.route_gate(30001161, 30001198)), 2)
        self.map.add_jumpbridge(30001161, 30001198)
        r = self.map.route_gate(30001161, 30001198)
        self.assertEqual(len(r), 2)
        self.assertListEqual(r, [30001161, 30001198])

    # Jump ranges taken from devblog:
    # http://community.eveonline.com/news/dev-blogs/phoebe-travel-change-update/

    def test_jump_distance_titan(self):
        """
        The maximum jump range of a titan is 5LY.
        """
        self.assertEquals(ship_class_to_range('titan', 5), 5)

    def test_jump_distance_supercarrier(self):
        """
        The maximum jump range of a supercarrier is 5LY.
        """
        self.assertEquals(ship_class_to_range('supercarrier', 5), 5)

    def test_jump_distance_carrier(self):
        """
        The maximum jump range of a carrier is 5LY.
        """
        self.assertEquals(ship_class_to_range('carrier', 5), 5)

    def test_jump_distance_dreadnought(self):
        """
        The maximum jump range of a dread is 5LY.
        """
        self.assertEquals(ship_class_to_range('dreadnought', 5), 5)

    def test_jump_distance_industrial(self):
        """
        The maximum jump range of a rorqual is 5LY.
        """
        self.assertEquals(ship_class_to_range('industrial', 5), 5)

    def test_jump_distance_jumpfreighter(self):
        """
        The maximum jump range of a JF is 10LY.
        """
        self.assertEquals(ship_class_to_range('jumpfreighter', 5), 10)

    def test_jump_distance_blackops(self):
        """
        The maximum jump range of a bloops is 8LY.
        """
        self.assertEquals(ship_class_to_range('blackops', 5), 8)
<commit_msg>Fix test failing due to the new SDE (100 new WH systems).<commit_after>from unittest import TestCase
from dropbot.map import Map, ship_class_to_range
import pkgutil

class MapTestCase(TestCase):

    def setUp(self):
        self.map = Map.from_json(pkgutil.get_data('dropbot', 'data/map.json'))

    def test_load_from_package_data(self):
        m = Map.from_json(pkgutil.get_data('dropbot', 'data/map.json'))
        self.assertIsNotNone(m)

    def test_get_system_name(self):
        self.assertEquals(self.map.get_system_name(123), None)
        self.assertEqual(self.map.get_system_name(30000142), 'Jita')

    def test_get_system_id(self):
        self.assertEqual(self.map.get_system_id('Llamatron'), None)
        self.assertEqual(self.map.get_system_id('Jita'), 30000142)

    def test_get_systems(self):
        self.assertEquals(len(self.map.get_systems('Jita')), 1)
        self.assertEquals(len(self.map.get_systems('Ji')), 7)
        self.assertEquals(len(self.map.get_systems('J')), 2760)
        self.assertEquals(len(self.map.get_systems('123435345345')), 0)
        self.assertEquals(len(self.map.get_systems('jita')), 1)
        self.assertEquals(len(self.map.get_systems('JITA')), 1)
        self.assertEquals(len(self.map.get_systems('JiTa')), 1)

    def test_system_distance(self):
        self.assertEqual(self.map.system_distance(30000142, 30000144), 2.10268108033618)
        self.assertEqual(self.map.system_distance(30000142, 30000222), 9.334275248404591)
        self.assertEqual(self.map.system_distance(30000142, 30000536), 39.15289747780095)
        self.assertRaises(Exception, self.map.system_distance, (1, 2))

    def test_route_gate(self):
        r = self.map.route_gate(30001161, 30001198)
        self.assertEqual(len(r), 9)
        self.assertListEqual(r, [30001161, 30001158, 30001160, 30001154, 30001157, 30001155, 30001156, 30001162, 30001198])

    def test_route_jump(self):
        pass

    def test_route_jump_distance(self):
        pass

    def test_route_jump_isotopes(self):
        pass

    def test_neighbors_gate(self):
        pass

    def test_neighbors_jump(self):
        pass

    def test_jump_bridge_addition(self):
        # HED-GP to GE-8
        self.assertGreater(len(self.map.route_gate(30001161, 30001198)), 2)
        self.map.add_jumpbridge(30001161, 30001198)
        r = self.map.route_gate(30001161, 30001198)
        self.assertEqual(len(r), 2)
        self.assertListEqual(r, [30001161, 30001198])

    # Jump ranges taken from devblog:
    # http://community.eveonline.com/news/dev-blogs/phoebe-travel-change-update/

    def test_jump_distance_titan(self):
        """
        The maximum jump range of a titan is 5LY.
        """
        self.assertEquals(ship_class_to_range('titan', 5), 5)

    def test_jump_distance_supercarrier(self):
        """
        The maximum jump range of a supercarrier is 5LY.
        """
        self.assertEquals(ship_class_to_range('supercarrier', 5), 5)

    def test_jump_distance_carrier(self):
        """
        The maximum jump range of a carrier is 5LY.
        """
        self.assertEquals(ship_class_to_range('carrier', 5), 5)

    def test_jump_distance_dreadnought(self):
        """
        The maximum jump range of a dread is 5LY.
        """
        self.assertEquals(ship_class_to_range('dreadnought', 5), 5)

    def test_jump_distance_industrial(self):
        """
        The maximum jump range of a rorqual is 5LY.
        """
        self.assertEquals(ship_class_to_range('industrial', 5), 5)

    def test_jump_distance_jumpfreighter(self):
        """
        The maximum jump range of a JF is 10LY.
        """
        self.assertEquals(ship_class_to_range('jumpfreighter', 5), 10)

    def test_jump_distance_blackops(self):
        """
        The maximum jump range of a bloops is 8LY.
        """
        self.assertEquals(ship_class_to_range('blackops', 5), 8)
<|endoftext|>


<commit_before>'''Test the low-level client up to the request/response level. That is, verify
a request is made to the expected URL and the response is as provided. Unless
specifically needed (e.g., JSON format), the response content should not
matter'''
from planet import api
import requests_mock

client = api.Client()
client.api_key = 'foobar'

def mockget(path, data, status_code=200):
    def outer(func):
        @requests_mock.mock()
        def inner(m):
            m.register_uri('GET', client.base_url + path,
                           text=data, status_code=status_code)
            func()
        return inner
    return outer

def assert_client_exc(clz, msg, status=None):
    try:
        client._get('whatevs')
    except clz, ex:
        assert msg == ex.message
    else:
        raise AssertionError('expected %s' % clz.__name__)

@mockget('whatevs', 'test', status_code=200)
def test_assert_client_exc_success():
    '''make sure our test works'''
    try:
        assert_client_exc(Exception, '')
        assert False
    except AssertionError, ae:
        assert ae.message == 'expected Exception'

@mockget('whatevs', 'test', status_code=400)
def test_assert_client_exc_fail():
    '''make sure our test works'''
    try:
        assert_client_exc(api.BadQuery, 'not test')
        assert False
    except AssertionError, ae:
        assert "'not test' == 'test'" in ae.message

def test_missing_api_key():
    client = api.Client()
    # make sure any detected key is cleared
    client.api_key = None

    def assert_missing():
        try:
            client._get('whatevs')
            assert False
        except api.InvalidAPIKey, ex:
            assert ex.message == 'No API key provided'
    assert_missing()
    client.api_key = ''
    assert_missing()

@mockget('whatevs', 'not exist', status_code=404)
def test_status_code_404():
    assert_client_exc(api.MissingResource, 'not exist')

@mockget('whatevs', 'emergency', status_code=911)
def test_status_code_other():
    assert_client_exc(api.APIException, '911: emergency')

@mockget('scenes', 'oranges')
def test_list_all_scene_types():
    assert client.list_all_scene_types().get_raw() == 'oranges'

@mockget('scenes/ortho/x22', 'bananas')
def test_fetch_scene_info_scene_id():
    assert client.fetch_scene_info('x22').get_raw() == 'bananas'
<commit_msg>update method name<commit_after>'''Test the low-level client up to the request/response level. That is, verify
a request is made to the expected URL and the response is as provided. Unless
specifically needed (e.g., JSON format), the response content should not
matter'''
from planet import api
import requests_mock

client = api.Client()
client.api_key = 'foobar'

def mockget(path, data, status_code=200):
    def outer(func):
        @requests_mock.mock()
        def inner(m):
            m.register_uri('GET', client.base_url + path,
                           text=data, status_code=status_code)
            func()
        return inner
    return outer

def assert_client_exc(clz, msg, status=None):
    try:
        client._get('whatevs')
    except clz, ex:
        assert msg == ex.message
    else:
        raise AssertionError('expected %s' % clz.__name__)

@mockget('whatevs', 'test', status_code=200)
def test_assert_client_exc_success():
    '''make sure our test works'''
    try:
        assert_client_exc(Exception, '')
        assert False
    except AssertionError, ae:
        assert ae.message == 'expected Exception'

@mockget('whatevs', 'test', status_code=400)
def test_assert_client_exc_fail():
    '''make sure our test works'''
    try:
        assert_client_exc(api.BadQuery, 'not test')
        assert False
    except AssertionError, ae:
        assert "'not test' == 'test'" in ae.message

def test_missing_api_key():
    client = api.Client()
    # make sure any detected key is cleared
    client.api_key = None

    def assert_missing():
        try:
            client._get('whatevs')
            assert False
        except api.InvalidAPIKey, ex:
            assert ex.message == 'No API key provided'
    assert_missing()
    client.api_key = ''
    assert_missing()

@mockget('whatevs', 'not exist', status_code=404)
def test_status_code_404():
    assert_client_exc(api.MissingResource, 'not exist')

@mockget('whatevs', 'emergency', status_code=911)
def test_status_code_other():
    assert_client_exc(api.APIException, '911: emergency')

@mockget('scenes', 'oranges')
def test_list_all_scene_types():
    assert client.list_scene_types().get_raw() == 'oranges'

@mockget('scenes/ortho/x22', 'bananas')
def test_fetch_scene_info_scene_id():
    assert client.get_scene_metadata('x22').get_raw() == 'bananas'
<|endoftext|>


<commit_before>""" run with

nosetests -v --nocapture

or

nosetests -v

individual tests can be run with

nosetests -v  --nocapture test_cm_compute.py:Test.test_06

"""

from sh import head
from sh import fgrep
import string
import os
import time

from sh import ssh
import json
from pprint import pprint

from cloudmesh.pbs.pbs import PBS
from cloudmesh.config.cm_config import cm_config
from cloudmesh.util.util import HEADING, banner

class Test:

    hosts = ["india.futuregrid.org",
             "sierra.futuregrid.org",
             "hotel.futuregrid.org",
             "alamo.futuregrid.org"]

    def setup(self):
        # self.configuration = cm_config()
        # pprint (self.configuration.__dict__)
        self.user = "gvonlasz"
        self.host = "india.futuregrid.org"

        # pprint (_create_pbsinfo_dict(data))

    def tearDown(self):
        pass

    def get_qstat(self, host):
        self.pbs = PBS(self.user, host)
        results = self.pbs.qstat()
        for name in results:
            element = results[name]
            pprint (element)

    def test_all(self):
        for host in self.hosts:
            banner(host)
            self.get_qstat(host)

    def test_info(self):
        HEADING()

        for host in self.hosts:
            self.pbs = PBS(self.user, host)
            results = self.pbs.qstat()

            print host, " =", len(results), "jobs"

<commit_msg>format<commit_after>""" run with

nosetests -v --nocapture

or

nosetests -v

individual tests can be run with

nosetests -v  --nocapture test_cm_compute.py:Test.test_06

"""

from sh import head
from sh import fgrep
import string
import os
import time

from sh import ssh
import json
from pprint import pprint

from cloudmesh.pbs.pbs import PBS
from cloudmesh.config.cm_config import cm_config
from cloudmesh.util.util import HEADING, banner

class Test:

    hosts = ["india.futuregrid.org",
             "sierra.futuregrid.org",
             "hotel.futuregrid.org",
             "alamo.futuregrid.org"]

    def setup(self):
        # self.configuration = cm_config()
        # pprint (self.configuration.__dict__)
        self.user = "gvonlasz"
        self.host = "india.futuregrid.org"

        # pprint (_create_pbsinfo_dict(data))

    def tearDown(self):
        pass

    def get_qstat(self, host):
        self.pbs = PBS(self.user, host)
        results = self.pbs.qstat()
        for name in results:
            element = results[name]
            pprint (element)

    def test_all(self):
        for host in self.hosts:
            banner(host)
            self.get_qstat(host)

    def test_info(self):
        HEADING()

        for host in self.hosts:
            self.pbs = PBS(self.user, host)
            results = self.pbs.qstat()

            print host, " =", len(results), "jobs"

<|endoftext|>


<commit_before>'''
PCA testing ideas:
 * Check max_pc
 * calc_n_pc to max_pc
 * Results sets and shapes
 * Check zero variance test
 * Check that res is a new copy each time (unique id?)
 * Calculation with missing data
'''
import os.path as osp

import numpy as np

import pytest

from hoggorm import nipalsPCA as PCA

# If the following equation is element-wise True, then allclose returns True.
# absolute(a - b) <= (atol + rtol * absolute(b))
# default: rtol=1e-05, atol=1e-08
rtol = 1e-05
atol = 1e-08

ATTRS = [
    'modelSettings',
    'X_means',
    'X_scores',
    'X_loadings',
    'X_corrLoadings',
    'X_residuals',
    'X_calExplVar',
    'X_cumCalExplVar_indVar',
    'X_cumCalExplVar',
    'X_predCal',
    'X_PRESSE_indVar',
    'X_PRESSE',
    'X_MSEE_indVar',
    'X_MSEE',
    'X_RMSEE_indVar',
    'X_RMSEE',
    'X_valExplVar',
    'X_cumValExplVar_indVar',
    'X_cumValExplVar',
    'X_predVal',
    'X_PRESSCV_indVar',
    'X_PRESSCV',
    'X_MSECV_indVar',
    'X_MSECV',
    'X_RMSECV_indVar',
    'X_RMSECV',
    # 'X_scores_predict',
    'cvTrainAndTestData',
    'corrLoadingsEllipses',
]

def test_api_verify(pcacached, ldat):

    for fn in ATTRS:
        res = getattr(pcacached, fn)()
        print(fn, type(res))
        if isinstance(res, np.ndarray):
            print(res.shape)
    res = pcacached.X_scores_predict(ldat)
    print('X_scores_predict', type(res))
    print(res.shape)

def test_constructor_api_variants(ldat):
    print("\n")
    pca1 = PCA(ldat)
    print("pca1")
    pca2 = PCA(ldat, numComp=200, Xstand=False)
    print("pca2")
    # FIXME: This will hang for given dataset
    # pca3 = PCA(ldat, Xstand=True, cvType=["loo"])
    # print("pca3")
    pca4 = PCA(ldat, numComp=2, cvType=["loo"])
    print("pca4")
    pca5 = PCA(ldat, numComp=2, Xstand=False, cvType=["loo"])
    print("pca5")

def test_compare_reference(pcaref, pcacached):
    rname, refdat = pcaref
    res = getattr(pcacached, rname)()
    if refdat is None:
        dump_res(rname, res)
        assert False, "Missing reference data for {}, data is dumpet".format(rname)
    elif not np.allclose(res, refdat, rtol=rtol, atol=atol):
        dump_res(rname, res)
        assert False, "Difference in {}, data is dumpet".format(rname)
    else:
        assert True

@pytest.fixture(params=["X_scores", "X_loadings"])
def pcaref(request, datafolder):
    rname = request.param
    refn = "ref_{}.tsv".format(rname.lower())
    try:
        refdat = np.loadtxt(osp.join(datafolder, refn))
    except FileNotFoundError:
        refdat = None

    return (rname, refdat)

@pytest.fixture(scope="module")
def pcacached(ldat):
    return PCA(ldat, cvType=["loo"])

def dump_res(rname, dat):
    dumpfolder = osp.realpath(osp.dirname(__file__))
    dumpfn = "dump_{}.tsv".format(rname.lower())
    np.savetxt(osp.join(dumpfolder, dumpfn), dat, fmt='%.9e', delimiter='\t')
<commit_msg>Update ideas comment<commit_after>'''
FIXME: PCA testing ideas:
 * Well known datasets (iris)
 * Combinations of input parameters
 * Edge case datasets
 * Big matrix for performance testing / profiling
 * Illegale data and error handling (zero variance)
 * Integer and float type matrix
'''
import os.path as osp

import numpy as np

import pytest

from hoggorm import nipalsPCA as PCA

# If the following equation is element-wise True, then allclose returns True.
# absolute(a - b) <= (atol + rtol * absolute(b))
# default: rtol=1e-05, atol=1e-08
rtol = 1e-05
atol = 1e-08

ATTRS = [
    'modelSettings',
    'X_means',
    'X_scores',
    'X_loadings',
    'X_corrLoadings',
    'X_residuals',
    'X_calExplVar',
    'X_cumCalExplVar_indVar',
    'X_cumCalExplVar',
    'X_predCal',
    'X_PRESSE_indVar',
    'X_PRESSE',
    'X_MSEE_indVar',
    'X_MSEE',
    'X_RMSEE_indVar',
    'X_RMSEE',
    'X_valExplVar',
    'X_cumValExplVar_indVar',
    'X_cumValExplVar',
    'X_predVal',
    'X_PRESSCV_indVar',
    'X_PRESSCV',
    'X_MSECV_indVar',
    'X_MSECV',
    'X_RMSECV_indVar',
    'X_RMSECV',
    # 'X_scores_predict',
    'cvTrainAndTestData',
    'corrLoadingsEllipses',
]

def test_api_verify(pcacached, ldat):

    for fn in ATTRS:
        res = getattr(pcacached, fn)()
        print(fn, type(res))
        if isinstance(res, np.ndarray):
            print(res.shape)
    res = pcacached.X_scores_predict(ldat)
    print('X_scores_predict', type(res))
    print(res.shape)

def test_constructor_api_variants(ldat):
    print("\n")
    pca1 = PCA(ldat)
    print("pca1")
    pca2 = PCA(ldat, numComp=200, Xstand=False)
    print("pca2")
    # FIXME: This will hang for given dataset
    # pca3 = PCA(ldat, Xstand=True, cvType=["loo"])
    # print("pca3")
    pca4 = PCA(ldat, numComp=2, cvType=["loo"])
    print("pca4")
    pca5 = PCA(ldat, numComp=2, Xstand=False, cvType=["loo"])
    print("pca5")

def test_compare_reference(pcaref, pcacached):
    rname, refdat = pcaref
    res = getattr(pcacached, rname)()
    if refdat is None:
        dump_res(rname, res)
        assert False, "Missing reference data for {}, data is dumpet".format(rname)
    elif not np.allclose(res, refdat, rtol=rtol, atol=atol):
        dump_res(rname, res)
        assert False, "Difference in {}, data is dumpet".format(rname)
    else:
        assert True

@pytest.fixture(params=["X_scores", "X_loadings"])
def pcaref(request, datafolder):
    rname = request.param
    refn = "ref_{}.tsv".format(rname.lower())
    try:
        refdat = np.loadtxt(osp.join(datafolder, refn))
    except FileNotFoundError:
        refdat = None

    return (rname, refdat)

@pytest.fixture(scope="module")
def pcacached(ldat):
    return PCA(ldat, cvType=["loo"])

def dump_res(rname, dat):
    dumpfolder = osp.realpath(osp.dirname(__file__))
    dumpfn = "dump_{}.tsv".format(rname.lower())
    np.savetxt(osp.join(dumpfolder, dumpfn), dat, fmt='%.9e', delimiter='\t')
<|endoftext|>