#!/usr/bin/env python
#
# Copyright (c) 2014 Martina Kollarova
#
# 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 __future__ import absolute_import, print_function, unicode_literals
import mock
from nose.tools import raises, assert_equals

from launchpadstats.table import GroupMetricsTable, PRETTY_NAME, REVIEWS_FORMAT
from launchpadstats.configuration import ConfigurationError
import fakes


def fake_request(url, params):
    """Simulate `requests.get()` on the stackalytics API."""
    return fakes.GOOD_RESPONSE


class TestGroupMetricsTable():
    def setup(self):
        self.patch = \
            mock.patch('launchpadstats.table.stackalytics.requests.get',
                       side_effect=fake_request)
        self.mock_request = self.patch.start()

    def teardown(self):
        self.patch.stop()

    @raises(ConfigurationError)
    def test_empty_query(self):
        table = GroupMetricsTable(people='', releases='', metrics='')
        table.generate()

    @raises(ConfigurationError)
    def test_partial_query(self):
        table = GroupMetricsTable(people='user1', releases='',
                                  metrics='loc')
        table.generate()

    def test_simple_query(self):
        table = GroupMetricsTable(people='user1',
                                  releases='Icehouse',
                                  metrics='loc')
        fake_response = fakes.GOOD_RESPONSE.json()['contribution']
        expected_result = [
            [table.header_info, 'Icehouse'],
            [PRETTY_NAME['loc'], str(fake_response['loc'])],
            ['sum', '0']  # because LOC is in `SKIP_FROM_SUM`
        ]
        table.generate()
        matrix = table.matrix()
        assert_equals(matrix, expected_result)

    def test_query(self):
        table = GroupMetricsTable(people='user1,user2,user3',
                                  releases='Havana,Icehouse,Juno',
                                  metrics='loc')
        fake_loc = str(fakes.GOOD_RESPONSE.json()['contribution']['loc'])
        expected_result = [
            [table.header_info, 'Havana', 'Icehouse', 'Juno'],
            [PRETTY_NAME['loc'], fake_loc, fake_loc, fake_loc],
            ['sum', '0', '0', '0']  # because LOC is in `SKIP_FROM_SUM`
        ]
        table.generate()
        assert_equals(table.matrix(), expected_result)

    def test_release_order(self):
        table = GroupMetricsTable(people='user1',
                                  releases='Havana,Juno,Icehouse',
                                  metrics='loc')
        table.generate()
        assert_equals(table.matrix()[0],
                      [table.header_info, 'Havana', 'Juno', 'Icehouse'])

    def test_metrics(self):
        # test all metrics except reviews and the sum
        metrics = ('loc', 'email_count', 'commit_count',
                   'drafted_blueprint_count',
                   'completed_blueprint_count', 'filed_bug_count',
                   'resolved_bug_count', 'patch_set_count')
        table = GroupMetricsTable(people='user1,user2,user3',
                                  releases='Havana', metrics=','.join(metrics))
        table.generate()
        matrix = table.matrix()
        assert_equals(_matrix_size(matrix), (len(metrics) + 2, 2))
        assert_equals(matrix[0], [table.header_info, 'Havana'])
        fake_response = fakes.GOOD_RESPONSE.json()['contribution']
        for index, metric in enumerate(metrics):
            assert_equals(matrix[index + 1][0], metric)
            assert_equals(matrix[index + 1][1], str(fake_response[metric]))
        assert_equals(matrix[-1][0], 'sum')

    def test_reviews(self):
        table = GroupMetricsTable(people='user1,user2,user3',
                                  releases='Havana', metrics='reviews')
        table.generate()
        assert_equals(table.matrix()[1][0], PRETTY_NAME['reviews'])
        reviews = table.matrix()[1][1].strip('()').split(',')
        assert_equals(len(reviews), len(REVIEWS_FORMAT))
        fake_response = fakes.GOOD_RESPONSE.json()['contribution']
        for index, mark in enumerate(REVIEWS_FORMAT):
            assert_equals(reviews[index].strip(),
                          str(fake_response['marks'][mark]))


def _matrix_size(matrix):
    rows = len(matrix)
    cols = list(map(len, matrix))
    assert len(set(cols)) == 1, "Matrix columns are not of the same length."
    return rows, cols[0]

@@ -1807,17 +1807,17 @@
 leases='
-I
+i
 cehouse'
@@ -1995,17 +1995,17 @@
 _info, '
-I
+i
 cehouse'
@@ -2381,24 +2381,24 @@
 es='
-H
+h
 avana,
-I
+i
 cehouse,
 Juno
@@ -2393,17 +2393,17 @@
 cehouse,
-J
+j
 uno',%0A  
@@ -2576,33 +2576,33 @@
 e.header_info, '
-H
+h
 avana', 'Icehous
@@ -2590,25 +2590,25 @@
  'havana', '
-I
+i
 cehouse', 'J
@@ -2602,25 +2602,25 @@
 icehouse', '
-J
+j
 uno'%5D,%0A     
@@ -2973,21 +2973,21 @@
 es='
-H
+h
 avana,
-J
+j
 uno,
-I
+i
 ceho
@@ -3146,25 +3146,25 @@
 ader_info, '
-H
+h
 avana', 'Jun
@@ -3164,17 +3164,17 @@
 ', '
-J
+j
 uno', '
-I
+i
 ceho
@@ -3591,33 +3591,33 @@
       releases='
-H
+h
 avana', metrics=
@@ -3813,17 +3813,17 @@
 _info, '
-H
+h
 avana'%5D)
@@ -4250,17 +4250,17 @@
 leases='
-H
+h
 avana', 

Use lower-case names of releases