<commit_before># -*- coding: utf-8 -*-
"""
Presence analyzer unit tests.
"""
import os.path
import sys
import json
import datetime
import unittest

from presence_analyzer import main, utils

my_path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, my_path + '/../')

TEST_DATA_CSV = os.path.join(os.path.dirname(
    __file__), '..', '..', 'runtime', 'data', 'test_data.csv')


# pylint: disable=maybe-no-member, too-many-public-methods
class PresenceAnalyzerViewsTestCase(unittest.TestCase):
    """
    Views tests.
    """

    def setUp(self):
        """
        Before each test, set up a environment.
        """
        main.app.config.update({'DATA_CSV': TEST_DATA_CSV})
        self.client = main.app.test_client()

    def tearDown(self):
        """
        Get rid of unused objects after each test.
        """
        pass

    def test_mainpage(self):
        """
        Test main page redirect.
        """
        resp = self.client.get('/')
        self.assertEqual(resp.status_code, 302)
        assert resp.headers['Location'].endswith('/presence_weekday.html')

    def test_mainpage_header(self):
        """
        Test main page header.
        """
        resp = self.client.get('/', follow_redirects=True)
        self.assertEqual(resp.status_code, 200)
        self.assertIn(b'Presence analyzer', resp.data)

    """
    PRESENCE BY WEEKDAY
    """

    def test_presence_by_weekday_header(self):
        """
        Test presence by weekday header.
        """
        resp = self.client.get('/static/presence_weekday.html')
        self.assertEqual(resp.status_code, 200)
        self.assertIn(b'Presence by weekday', resp.data)

    def test_presence_by_weekday_api_route_parameter_is_correct(self):
        """
        Test presence by weekday api route parameter is correct.
        """
        weekdays = {
            u'Weekday': u'Presence (s)',
            u'Mon': 0,
            u'Tue': 30047,
            u'Wed': 24465,
            u'Thu': 23705,
            u'Fri': 0,
            u'Sat': 0,
            u'Sun': 0,
        }

        resp = self.client.get(
            '/api/v1/presence_weekday/10',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 200)
        data = json.loads(resp.data)
        # import ipdb; ipdb.set_trace()
        self.assertDictEqual(weekdays, dict(data))

    def test_presence_by_weekday_api_route_parameter_is_incorrect(self):
        """
        Test presence by weekday api route parameter is incorrect.
        """
        resp = self.client.get(
            '/api/v1/presence_weekday/wrong',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 404)

    """
    PRESENCE MEAN TIME
    """

    def test_presence_mean_time_header(self):
        """
        Test presence mean time header.
        """
        resp = self.client.get('/static/mean_time_weekday.html')
        self.assertEqual(resp.status_code, 200)
        self.assertIn(b'Presence mean time by weekday', resp.data)

    def test_presence_mean_time_api_route_parameter_is_correct(self):
        """
        Test presence by weekday api route parameter is correct.
        """
        weekdays = {
            u'Mon': 0,
            u'Tue': 30047.0,
            u'Wed': 24465.0,
            u'Thu': 23705.0,
            u'Fri': 0,
            u'Sat': 0,
            u'Sun': 0,
        }

        resp = self.client.get(
            '/api/v1/mean_time_weekday/10',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 200)
        data = json.loads(resp.data)
        # import ipdb; ipdb.set_trace()
        self.assertDictEqual(weekdays, dict(data))

    def test_presence_mean_tme_api_route_parameter_is_incorrect(self):
        """
        Test presence by weekday api route parameter is incorrect.
        """
        resp = self.client.get(
            '/api/v1/mean_time_weekday/wrong',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 404)

    def test_api_users(self):
        """
        Test users listing.
        """
        resp = self.client.get('/api/v1/users')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content_type, 'application/json')
        data = json.loads(resp.data)
        self.assertEqual(len(data), 2)
        self.assertDictEqual(
            data[0],
            {
                u'user_id': 10,
                u'name': u'User 10',
            },
        )

<commit_msg>Tests style fixes<commit_after># -*- coding: utf-8 -*-
"""
Presence analyzer unit tests.
"""
import os.path
import sys
import json
import datetime
import unittest

from presence_analyzer import main, utils

MY_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, MY_PATH + '/../')

TEST_DATA_CSV = os.path.join(os.path.dirname(
    __file__), '..', '..', 'runtime', 'data', 'test_data.csv')


# pylint: disable=maybe-no-member, too-many-public-methods
class PresenceAnalyzerViewsTestCase(unittest.TestCase):
    """
    Views tests.
    """

    def setUp(self):
        """
        Before each test, set up a environment.
        """
        main.app.config.update({'DATA_CSV': TEST_DATA_CSV})
        self.client = main.app.test_client()

    def tearDown(self):
        """
        Get rid of unused objects after each test.
        """
        pass

    def test_mainpage(self):
        """
        Test main page redirect.
        """
        resp = self.client.get('/')
        self.assertEqual(resp.status_code, 302)
        assert resp.headers['Location'].endswith('/presence_weekday.html')

    def test_mainpage_header(self):
        """
        Test main page header.
        """
        resp = self.client.get('/', follow_redirects=True)
        self.assertEqual(resp.status_code, 200)
        self.assertIn(b'Presence analyzer', resp.data)

    def test_presence_by_weekday_header(self):
        """
        Test presence by weekday header.
        """
        resp = self.client.get('/static/presence_weekday.html')
        self.assertEqual(resp.status_code, 200)
        self.assertIn(b'Presence by weekday', resp.data)

    def weekday_parameter_correct(self):
        """
        Test presence by weekday api route parameter is correct.
        """
        weekdays = {
            u'Weekday': u'Presence (s)',
            u'Mon': 0,
            u'Tue': 30047,
            u'Wed': 24465,
            u'Thu': 23705,
            u'Fri': 0,
            u'Sat': 0,
            u'Sun': 0,
        }

        resp = self.client.get(
            '/api/v1/presence_weekday/10',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 200)
        data = json.loads(resp.data)
        # import ipdb; ipdb.set_trace()
        self.assertDictEqual(weekdays, dict(data))

    def weekday_parameter_incorrect(self):
        """
        Test presence by weekday api route parameter is incorrect.
        """
        resp = self.client.get(
            '/api/v1/presence_weekday/wrong',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 404)

    def test_presence_mean_time_header(self):
        """
        Test presence mean time header.
        """
        resp = self.client.get('/static/mean_time_weekday.html')
        self.assertEqual(resp.status_code, 200)
        self.assertIn(b'Presence mean time by weekday', resp.data)

    def mean_time_parameter_correct(self):
        """
        Test presence by weekday api route parameter is correct.
        """
        weekdays = {
            u'Mon': 0,
            u'Tue': 30047.0,
            u'Wed': 24465.0,
            u'Thu': 23705.0,
            u'Fri': 0,
            u'Sat': 0,
            u'Sun': 0,
        }

        resp = self.client.get(
            '/api/v1/mean_time_weekday/10',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 200)
        data = json.loads(resp.data)
        # import ipdb; ipdb.set_trace()
        self.assertDictEqual(weekdays, dict(data))

    def mean_time_parameter_incorrect(self):
        """
        Test presence by weekday api route parameter is incorrect.
        """
        resp = self.client.get(
            '/api/v1/mean_time_weekday/wrong',
            content_type='application/json',
        )
        self.assertEqual(resp.status_code, 404)

    def test_api_users(self):
        """
        Test users listing.
        """
        resp = self.client.get('/api/v1/users')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content_type, 'application/json')
        data = json.loads(resp.data)
        self.assertEqual(len(data), 2)
        self.assertDictEqual(
            data[0],
            {
                u'user_id': 10,
                u'name': u'User 10',
            },
        )


{'fraction_non_alphanumeric': 0.12508361204013377, 'fraction_numerical': 0.0189520624303233, 'mean_word_length': 1.9422761561167596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1857901', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2711, 'n_words': 524}