# Path: hoplite/client/status_updater.py
# class StatusUpdater(object):
#     """
#     Used to update the status of the job running on the daemon.
#     This is passed in to the job's run() function when it is invoked.
#     """
#     def __init__(self, addr, uuid, api_key):
#         self.addr = addr
#         self._uuid = uuid
#         self._daemon_addr = 'http://{0}'.format(self.addr)
#         self._api_key = api_key
#         self.status = {}
# 
#     def update(self, status):
#         """
#         Updates the job's status on the server that created it.
# 
#         :param dict status: The new status of the job
#         """
#         self.status = status
#         body = hoplite_dumps({
#             "api_key": self._api_key,
#             "status": status
#         })
#         url = self._daemon_addr + '/jobs/{0}'.format(self._uuid)
#         headers = {'content-type': 'application/json'}
#         r = requests.put(url, data=body, headers=headers)
#         if r.status_code == 404:
#             raise JobDoesNotExistError
# 
# Path: hoplite/exceptions.py
# class JobDoesNotExistError(HopliteError):
#     def __init__(self, message="Job does not exist"):
#         self.message = message
# 
#     def __str__(self):
#         return self.message


import json
import unittest2
from httmock import urlmatch, HTTMock, response
from hoplite.client.status_updater import StatusUpdater
from hoplite.exceptions import JobDoesNotExistError



@urlmatch(path='\/jobs\/someuuid$')
def update_status(url, request):
    if json.loads(request.body)["api_key"] == "apikeyhere":
        return response(200)
    return response(404)

class TestStatusUpdater(unittest2.TestCase):
    def test_update(self):
        with HTTMock(update_status):
            status = StatusUpdater('localhost:5001', "someuuid", "apikeyhere")
            status.update({"some": "status"})

    def test_update_raises(self):
        with HTTMock(update_status):

# Path: hoplite/client/status_updater.py
# class StatusUpdater(object):
#     """
#     Used to update the status of the job running on the daemon.
#     This is passed in to the job's run() function when it is invoked.
#     """
#     def __init__(self, addr, uuid, api_key):
#         self.addr = addr
#         self._uuid = uuid
#         self._daemon_addr = 'http://{0}'.format(self.addr)
#         self._api_key = api_key
#         self.status = {}
# 
#     def update(self, status):
#         """
#         Updates the job's status on the server that created it.
# 
#         :param dict status: The new status of the job
#         """
#         self.status = status
#         body = hoplite_dumps({
#             "api_key": self._api_key,
#             "status": status
#         })
#         url = self._daemon_addr + '/jobs/{0}'.format(self._uuid)
#         headers = {'content-type': 'application/json'}
#         r = requests.put(url, data=body, headers=headers)
#         if r.status_code == 404:
#             raise JobDoesNotExistError
# 
# Path: hoplite/exceptions.py
# class JobDoesNotExistError(HopliteError):
#     def __init__(self, message="Job does not exist"):
#         self.message = message
# 
#     def __str__(self):
#         return self.message


# Path: tests/server/jobs/test_status_updater.py
import json
import unittest2
from httmock import urlmatch, HTTMock, response
from hoplite.client.status_updater import StatusUpdater
from hoplite.exceptions import JobDoesNotExistError




@urlmatch(path='\/jobs\/someuuid$')
def update_status(url, request):
    if json.loads(request.body)["api_key"] == "apikeyhere":
        return response(200)
    return response(404)

class TestStatusUpdater(unittest2.TestCase):
    def test_update(self):
        with HTTMock(update_status):
            status = StatusUpdater('localhost:5001', "someuuid", "apikeyhere")
            status.update({"some": "status"})

    def test_update_raises(self):
        with HTTMock(update_status):

            status = StatusUpdater('localhost:5001', "someuuid", "wrongapikey")