"""
Tests for go_http.metrics.

"""

import json
from unittest import TestCase

from requests_testadapter import TestAdapter, TestSession

from go_http.metrics import MetricsApiClient


class RecordingAdapter(TestAdapter):

    """ Record the request that was handled by the adapter.
    """
    request = None

    def send(self, request, *args, **kw):
        self.request = request
        return super(RecordingAdapter, self).send(request, *args, **kw)


class TestMetricApiClient(TestCase):

    def setUp(self):
        self.session = TestSession()
        self.client = MetricsApiClient(
            auth_token="auth-token",
            api_url="http://example.com/api/v1/go",
            session=self.session)

    def test_default_session(self):
        import requests
        client = MetricsApiClient(
            auth_token="auth-token")
        self.assertTrue(isinstance(client.session, requests.Session))

    def test_default_api_url(self):
        client = MetricsApiClient(
            auth_token="auth-token")
        self.assertEqual(client.api_url,
                         "https://go.vumi.org/api/v1/go")

    def check_request(self, request, method, data=None, headers=None):
        self.assertEqual(request.method, method)
        if headers is not None:
            for key, value in headers.items():
                self.assertEqual(request.headers[key], value)
        if data is None:
            self.assertEqual(request.body, None)
        else:
            self.assertEqual(json.loads(request.body), data)

    def test_get_metric(self):
        response = {u'stores.store_name.metric_name.agg':
                    [{u'x': 1413936000000,
                        u'y': 88916.0},
                     {u'x': 1414022400000,
                        u'y': 91339.0},
                     {u'x': 1414108800000,
                        u'y': 92490.0},
                     {u'x': 1414195200000,
                        u'y': 92655.0},
                     {u'x': 1414281600000,
                        u'y': 92786.0}]}
        adapter = RecordingAdapter(json.dumps(response))
        self.session.mount(
            "http://example.com/api/v1/go/"
            "metrics/?m=stores.store_name.metric_name.agg"
            "&interval=1d&from=-30d&nulls=omit", adapter)

        result = self.client.get_metric(
            "stores.store_name.metric_name.agg", "-30d", "1d", "omit")
        self.assertEqual(result, response)
        self.check_request(
            adapter.request, 'GET',
            headers={"Authorization": u'Bearer auth-token'})

    def test_fire(self):
        response = [{
            'name': 'foo.last',
            'value': 3.1415,
            'aggregator': 'last',
        }]
        adapter = RecordingAdapter(json.dumps(response))
        self.session.mount(
            "http://example.com/api/v1/go/"
            "metrics/", adapter)

        result = self.client.fire({
            "foo.last": 3.1415,
        })
        self.assertEqual(result, response)
        self.check_request(
            adapter.request, 'POST',
            data={"foo.last": 3.1415},
            headers={"Authorization": u'Bearer auth-token'})

@@ -28,14 +28,29 @@
 cs.%0A
-%0A
 %22%22%22%0A%0A
+import urlparse%0A
 impo
@@ -1160,16 +1160,29 @@
 request(
+%0A            
 self, re
@@ -1195,16 +1195,29 @@
  method,
+ params=None,
  data=No
@@ -1284,16 +1284,190 @@
 method)%0A
+        if params is not None:%0A            url = urlparse.urlparse(request.url)%0A            qs = urlparse.parse_qsl(url.query)%0A            self.assertEqual(dict(qs), params)%0A
         
@@ -2408,92 +2408,8 @@
 ics/
-?m=stores.store_name.metric_name.agg%22%0A            %22&interval=1d&from=-30d&nulls=omit
 %22, a
@@ -2632,24 +2632,187 @@
 est, 'GET',%0A
+            params=%7B%0A                %22m%22: %22stores.store_name.metric_name.agg%22,%0A                %22interval%22: %221d%22,%0A                %22from%22: %22-30d%22, %22nulls%22: %22omit%22%7D,%0A
             

Fix reliance on dict ordering.