<commit_before>from __future__ import unicode_literals

import datetime
import threading
import time
import unittest

import mock

from mopidy_alarmclock.alarm_manager import AlarmManager


class AlarmManagerTest(unittest.TestCase):

    def test01_get_core(self):
        core = 'This core should crash on play'

        am = AlarmManager()

        self.assertTrue(am is am.get_core(core))

    def test01_get_seconds_since_midnight(self):
        am = AlarmManager()

        seconds = am.get_seconds_since_midnight()

        self.assertIsInstance(seconds, int)
        self.assertTrue(seconds >= 0 and seconds < 86400)

    def test02_is_set(self):
        playlist = 'This playlist should crash on play'

        am = AlarmManager()

        self.assertFalse(am.is_set())

        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        self.assertTrue(am.is_set())

        am.cancel()

        time.sleep(7)  # Sleep a little longer than timer-resolution (to prevent several simultaneous timers)
        # TODO Fix this issue in the code

        self.assertFalse(am.is_set())

    def test02_set_alarm__threading(self):
        playlist = 'This playlist should crash on play'
        threadcount = threading.active_count()

        am = AlarmManager()

        self.assertEqual(threading.active_count(), threadcount)

        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        self.assertEqual(threading.active_count(), threadcount + 1)

        am.cancel()

        time.sleep(7)  # Sleep a little longer than timer-resolution (to prevent several simultaneous timers)
        # TODO Fix this issue in the code

        self.assertEqual(threading.active_count(), threadcount)

    def test02_get_ring_time(self):
        playlist = 'This playlist should crash on play'

        am = AlarmManager()

        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        self.assertEqual(am.get_ring_time(), b'07:59')

        am.cancel()

    def test03_adjust_volume_100_1(self):
        core = mock.Mock()

        am = AlarmManager()
        am.get_core(core)

        am.adjust_volume(100, 1, 0)

        self.assertEqual(core.playback.volume, 50)
        time.sleep(1)
        self.assertEqual(core.playback.volume, 100)
        time.sleep(5)  # More than 3x increase step time
        self.assertEqual(core.playback.volume, 100)

    def test03_adjust_volume_100_0(self):
        core = mock.Mock()

        am = AlarmManager()
        am.get_core(core)

        am.adjust_volume(100, 0, 0)

        self.assertEqual(core.playback.volume, 100)
        time.sleep(5)  # More than 3x increase step time
        self.assertEqual(core.playback.volume, 100)

    def test03_adjust_volume_3_17(self):
        core = mock.Mock()

        am = AlarmManager()
        am.get_core(core)
<commit_msg>Improved alarm_manager tests<commit_after>from __future__ import unicode_literals

import datetime
import threading
import time
import unittest

import mock

from mopidy_alarmclock.alarm_manager import AlarmManager


class AlarmManagerTest(unittest.TestCase):

    def test01_get_core(self):
        core = 'This core should crash on play'

        am = AlarmManager()

        self.assertTrue(am is am.get_core(core))

    def test01_get_seconds_since_midnight(self):
        am = AlarmManager()

        seconds = am.get_seconds_since_midnight()

        self.assertIsInstance(seconds, int)
        self.assertTrue(seconds >= 0 and seconds < 86400)

    def test02_is_set(self):
        playlist = 'This playlist should crash on play'

        am = AlarmManager()

        self.assertFalse(am.is_set())

        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        self.assertTrue(am.is_set())

        am.cancel()

        time.sleep(7)  # Sleep a little longer than timer-resolution (to prevent several simultaneous timers)
        # TODO Fix this issue in the code

        self.assertFalse(am.is_set())

    def test02_set_alarm__threading(self):
        playlist = 'This playlist should crash on play'
        threadcount = threading.active_count()

        am = AlarmManager()

        self.assertEqual(threading.active_count(), threadcount)

        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        self.assertEqual(threading.active_count(), threadcount + 1)

        am.cancel()

        time.sleep(7)  # Sleep a little longer than timer-resolution (to prevent several simultaneous timers)
        # TODO Fix this issue in the code

        self.assertEqual(threading.active_count(), threadcount)

    def test02_get_ring_time(self):
        playlist = 'This playlist should crash on play'

        am = AlarmManager()

        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        self.assertEqual(am.get_ring_time(), b'07:59')

        am.cancel()

    def test03_adjust_volume_100_1(self):
        core = mock.Mock()
        threadcount = threading.active_count()

        am = AlarmManager()
        am.get_core(core)

        self.assertEqual(threading.active_count(), threadcount)

        am.adjust_volume(100, 1, 0)

        self.assertEqual(core.playback.volume, 50)
        self.assertEqual(threading.active_count(), threadcount + 1)
        time.sleep(1)
        self.assertEqual(core.playback.volume, 100)
        self.assertEqual(threading.active_count(), threadcount)
        time.sleep(5)  # More than 3x increase step time
        self.assertEqual(core.playback.volume, 100)
        self.assertEqual(threading.active_count(), threadcount)

    def test03_adjust_volume_100_0(self):
        core = mock.Mock()

        am = AlarmManager()
        am.get_core(core)

        am.adjust_volume(100, 0, 0)

        self.assertEqual(core.playback.volume, 100)
        time.sleep(5)  # More than 3x increase step time
        self.assertEqual(core.playback.volume, 100)

    def test03_adjust_volume_3_17(self):
        core = mock.Mock()

        am = AlarmManager()
        am.get_core(core)

{'fraction_non_alphanumeric': 0.0982359605114096, 'fraction_numerical': 0.043858229486972, 'mean_word_length': 2.7775061124694376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7458538', 'n_tokens_mistral': 2151, 'n_tokens_neox': 1865, 'n_words': 453}