from django.test import TestCase
from .api import GithubApi, Commit
from .analytics import WordCounter, HourCounter
from unittest.mock import patch
# Create your tests here.


class APITestCase(TestCase):
    def mock_get_events(self, page):
        return [{'actor': {'avatar_url': 'https://avatars.githubusercontent.com/u/6829754?',
                        'display_login': 'makalaaneesh',
                        'gravatar_id': '',
                        'id': 6829754,
                        'login': 'makalaaneesh',
                        'url': 'https://api.github.com/users/makalaaneesh'},
                'created_at': '2020-02-28T08:13:04Z',
                'id': '11633422705',
                'payload': {'before': 'ba0ce1a43cc9abe55630d1d654c2bb38b0a906ce',
                          'commits': [{'author': {'email': 'makalaaneesh@yahoo.com',
                                                  'name': 'Aneesh Makala'},
                                       'distinct': True,
                                       'message': 'Update README.md',
                                       'sha': 'eb990f5b2979c2a0b4337acd2ee73891391c7944',
                                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth/commits/eb990f5b2979c2a0b4337acd2ee73891391c7944'}],
                          'distinct_size': 1,
                          'head': 'eb990f5b2979c2a0b4337acd2ee73891391c7944',
                          'push_id': 4694199900,
                          'ref': 'refs/heads/master',
                          'size': 1},
                'public': True,
                'repo': {'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                'type': 'PushEvent'},
                {'actor': {'avatar_url': 'https://avatars.githubusercontent.com/u/6829754?',
                        'display_login': 'makalaaneesh',
                        'gravatar_id': '',
                        'id': 6829754,
                        'login': 'makalaaneesh',
                        'url': 'https://api.github.com/users/makalaaneesh'},
                'created_at': '2020-02-28T08:03:19Z',
                'id': '11633358609',
                'payload': {'before': 'b7aabf47d7447f096e79372c18a1104503eeac84',
                          'commits': [{'author': {'email': 'makalaaneesh@yahoo.com',
                                                  'name': 'Aneesh Makala'},
                                       'distinct': True,
                                       'message': 'Moved requirements.txt; Minor doc',
                                       'sha': 'ba0ce1a43cc9abe55630d1d654c2bb38b0a906ce',
                                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth/commits/ba0ce1a43cc9abe55630d1d654c2bb38b0a906ce'}],
                          'distinct_size': 1,
                          'head': 'ba0ce1a43cc9abe55630d1d654c2bb38b0a906ce',
                          'push_id': 4694166370,
                          'ref': 'refs/heads/master',
                          'size': 1},
                'public': True,
                'repo': {'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                'type': 'PushEvent'},
                {'actor': {'avatar_url': 'https://avatars.githubusercontent.com/u/6829754?',
                        'display_login': 'makalaaneesh',
                        'gravatar_id': '',
                        'id': 6829754,
                        'login': 'makalaaneesh',
                        'url': 'https://api.github.com/users/makalaaneesh'},
                'created_at': '2020-02-28T08:02:13Z',
                'id': '11633351292',
                'payload': {'before': 'a986057a14c534cbb11156a95ba24970b6e21890',
                          'commits': [{'author': {'email': 'makalaaneesh@yahoo.com',
                                                  'name': 'Aneesh Makala'},
                                       'distinct': True,
                                       'message': 'Update README.md',
                                       'sha': 'b7aabf47d7447f096e79372c18a1104503eeac84',
                                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth/commits/b7aabf47d7447f096e79372c18a1104503eeac84'}],
                          'distinct_size': 1,
                          'head': 'b7aabf47d7447f096e79372c18a1104503eeac84',
                          'push_id': 4694162704,
                          'ref': 'refs/heads/master',
                          'size': 1},
                'public': True,
                'repo': {'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                'type': 'PushEvent'},
                {'actor': {'avatar_url': 'https://avatars.githubusercontent.com/u/6829754?',
                        'display_login': 'makalaaneesh',
                        'gravatar_id': '',
                        'id': 6829754,
                        'login': 'makalaaneesh',
                        'url': 'https://api.github.com/users/makalaaneesh'},
                'created_at': '2020-02-28T07:48:09Z',
                'id': '11633259876',
                'payload': {'before': 'e0b86837640ec1f224c080bc74c50ce074e23d45',
                          'commits': [{'author': {'email': 'makalaaneesh@yahoo.com',
                                                  'name': 'Aneesh Makala'},
                                       'distinct': True,
                                       'message': 'Adding requirerments.txt',
                                       'sha': 'd6f8c9c20c60978bf89d28eed74ecd30d5a284c7',
                                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth/commits/d6f8c9c20c60978bf89d28eed74ecd30d5a284c7'}],
                          'distinct_size': 1,
                          'head': 'd6f8c9c20c60978bf89d28eed74ecd30d5a284c7',
                          'push_id': 4694115464,
                          'ref': 'refs/heads/master',
                          'size': 1},
                'public': True,
                'repo': {'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                'type': 'PushEvent'},
                {'actor': {'avatar_url': 'https://avatars.githubusercontent.com/u/6829754?',
                        'display_login': 'makalaaneesh',
                        'gravatar_id': '',
                        'id': 6829754,
                        'login': 'makalaaneesh',
                        'url': 'https://api.github.com/users/makalaaneesh'},
                'created_at': '2020-02-27T14:36:50Z',
                'id': '11626472370',
                'payload': {'description': 'A simple example of a webapp using github oauth '
                                         'to fetch recent commits and compute some metrics',
                          'master_branch': 'master',
                          'pusher_type': 'user',
                          'ref': 'master',
                          'ref_type': 'branch'},
                'public': True,
                'repo': {'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                'type': 'CreateEvent'},
                {'actor': {'avatar_url': 'https://avatars.githubusercontent.com/u/6829754?',
                        'display_login': 'makalaaneesh',
                        'gravatar_id': '',
                        'id': 6829754,
                        'login': 'makalaaneesh',
                        'url': 'https://api.github.com/users/makalaaneesh'},
                'created_at': '2020-02-27T14:30:19Z',
                'id': '11626418977',
                'payload': {'description': 'A simple example of a webapp using github oauth '
                                         'to fetch recent commits and compute some metrics',
                          'master_branch': 'master',
                          'pusher_type': 'user',
                          'ref': None,
                          'ref_type': 'repository'},
                'public': True,
                'repo': {'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                'type': 'CreateEvent'},
                {'actor': {'avatar_url': 'https://avatars.githubusercontent.com/u/6829754?',
                        'display_login': 'makalaaneesh',
                        'gravatar_id': '',
                        'id': 6829754,
                        'login': 'makalaaneesh',
                        'url': 'https://api.github.com/users/makalaaneesh'},
                'created_at': '2020-02-26T11:10:49Z',
                'id': '11613683583',
                'payload': {'action': 'started'},
                'public': True,
                'repo': {'id': 138101686,
                       'name': 'lucrae/django-cheat-sheet',
                       'url': 'https://api.github.com/repos/lucrae/django-cheat-sheet'},
                'type': 'WatchEvent'}]

    @patch.object(GithubApi, '_get_events', mock_get_events)
    def test_most_recent_commits(self):
        api = GithubApi(access_token=dict(access_token="mock_access_token"))
        commits = api.get_recent_commits(2)
        self.assertEqual(len(commits), 2)
        expected_commits = [
            Commit(sha="eb990f5b2979c2a0b4337acd2ee73891391c7944",
                   author={'email': 'makalaaneesh@yahoo.com', 'name': 'Aneesh Makala'},
                   repo={'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                   text="Update README.md",
                   created_at="2020-02-28T08:13:04Z"),
            Commit(sha="ba0ce1a43cc9abe55630d1d654c2bb38b0a906ce",
                   author={'email': 'makalaaneesh@yahoo.com', 'name': 'Aneesh Makala'},
                   repo={'id': 243540324,
                         'name': 'makalaaneesh/github-oauth',
                         'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                   text="Moved requirements.txt; Minor doc",
                   created_at="2020-02-28T08:03:19Z"),
        ]
        self.assertListEqual(commits, expected_commits)


class CounterTestCase(TestCase):
    def setUp(self):
        self.commits = [
            Commit(sha="eb990f5b2979c2a0b4337acd2ee73891391c7944",
                   author={'email': 'makalaaneesh@yahoo.com', 'name': 'Aneesh Makala'},
                   repo={'id': 243540324,
                       'name': 'makalaaneesh/github-oauth',
                       'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                   text="Update README.md",
                   created_at="2020-02-28T09:13:04Z"),
            Commit(sha="ba0ce1a43cc9abe55630d1d654c2bb38b0a906ce",
                   author={'email': 'makalaaneesh@yahoo.com', 'name': 'Aneesh Makala'},
                   repo={'id': 243540324,
                         'name': 'makalaaneesh/github-oauth',
                         'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                   text="Update requirements.txt; Minor doc",
                   created_at="2020-02-28T08:03:19Z"),
            Commit(sha="ba0ce1a43cc9abe55630d1d654c2bb38b0a903nk",
                   author={'email': 'makalaaneesh@yahoo.com', 'name': 'Aneesh Makala'},
                   repo={'id': 243540324,
                         'name': 'makalaaneesh/github-oauth',
                         'url': 'https://api.github.com/repos/makalaaneesh/github-oauth'},
                   text="initial commit",
                   created_at="2020-02-28T08:00:19Z"),
        ]

    def test_word_counter(self):
        wc = WordCounter()
        wc.process_documents(self.commits)
        frequent_words = wc.get_frequent_words(2)
        self.assertListEqual(frequent_words,
                             [('update', 2), ('requirements.txt', 1)])

    def test_hour_counter(self):
        hc = HourCounter()
        hc.process_documents(self.commits)
        self.assertEqual(hc.get_most_frequent_hour(), 8)
