from datetime import datetime
from dateutil import tz
import unittest
from unittest import mock
from freezegun import freeze_time

from zapper import Zapper, ZapTimer
from util import make_url, rand_minutes, percent_to_zap_value


class TestUtils(unittest.TestCase):

    def test_make_url(self):
        url = make_url(api_name="test_name", level=101, message="testing spaces")
        self.assertEqual(url, "https://app.pavlok.com/unlocked/remotes/test_name/zap/101?message=testing+spaces")

    def test_rand_minutes(self):
        for _ in range(1000):
            minutes = rand_minutes(100, 10)
            self.assertTrue(90 <= minutes <= 100)

    def test_percent_to_zap_value(self):
        self.assertEqual(percent_to_zap_value(45), 114)
        self.assertEqual(percent_to_zap_value(60), 153)


def make_timer() -> ZapTimer:
    return ZapTimer(
        timezone_str="US/Pacific",
        interval_minutes=30,
        interval_percent=10,
        sleep_hour_before=8,
        sleep_hour_after=18,
    )


TZ = tz.gettz("America/Los Angeles")


def parse_time(time) -> datetime:
    # work around weird bug in `freeze_time`
    dt = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
    return dt.astimezone(TZ)


class TestZapTimer(unittest.TestCase):

    def test_sleep_times(self):
        for time in ["2021-01-01 7:10:00", "2021-01-01 19:10:00"]:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.sleep_time(), msg=f"should have slept at {time}")

    def test_zap_times(self):
        for time in ["2021-01-01 8:10:00", "2021-01-01 12:10:00", "2021-01-01 17:59:00"]:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.time_to_zap(), msg=f"should have zapped at {time}")

    def test_get_next_time(self):
        with freeze_time(parse_time("2021-01-01 12:00:00")):
            for _ in range(1000):
                timer = make_timer()
                d = timer.get_next_time() - timer.now()
                self.assertTrue(27 <= (d.seconds / 60) < 30)


def make_zapper():
    timer = make_timer()
    return Zapper(timer, api_name="testing", zap_level=101, message="some space")


class TestZapper(unittest.TestCase):
    @mock.patch("zapper.requests.get")
    def test_doesnt_zap_before_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time("2021-01-01 12:00:00")):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch("zapper.requests.get")
    def test_doesnt_zap_when_sleeping(self, mocked: mock.Mock):
        with freeze_time(parse_time("2021-01-01 5:00:00")):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch("zapper.requests.get")
    def test_zaps_when_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time("2021-01-01 12:00:00")):
            zapper = make_zapper()
            zapper.timer.next_time = zapper.timer.now()
            self.assertLess((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_called_with("https://app.pavlok.com/unlocked/remotes/testing/zap/101?message=some+space")
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)


if __name__ == '__main__':
    unittest.main()

46d12ced956d01864a5eac4c51c87ac273eafa5c
<mask token>


class TestUtils(unittest.TestCase):
    <mask token>
    <mask token>
    <mask token>


<mask token>


class TestZapTimer(unittest.TestCase):

    def test_sleep_times(self):
        for time in ['2021-01-01 7:10:00', '2021-01-01 19:10:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.sleep_time(), msg=
                    f'should have slept at {time}')

    def test_zap_times(self):
        for time in ['2021-01-01 8:10:00', '2021-01-01 12:10:00',
            '2021-01-01 17:59:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.time_to_zap(), msg=
                    f'should have zapped at {time}')

    def test_get_next_time(self):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            for _ in range(1000):
                timer = make_timer()
                d = timer.get_next_time() - timer.now()
                self.assertTrue(27 <= d.seconds / 60 < 30)


<mask token>


class TestZapper(unittest.TestCase):

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_before_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_when_sleeping(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 5:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_zaps_when_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            zapper.timer.next_time = zapper.timer.now()
            self.assertLess((zapper.timer.next_time - zapper.timer.now()).
                seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_called_with(
                'https://app.pavlok.com/unlocked/remotes/testing/zap/101?message=some+space'
                )
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)


<mask token>

<mask token>


class TestUtils(unittest.TestCase):

    def test_make_url(self):
        url = make_url(api_name='test_name', level=101, message=
            'testing spaces')
        self.assertEqual(url,
            'https://app.pavlok.com/unlocked/remotes/test_name/zap/101?message=testing+spaces'
            )

    def test_rand_minutes(self):
        for _ in range(1000):
            minutes = rand_minutes(100, 10)
            self.assertTrue(90 <= minutes <= 100)
    <mask token>


<mask token>


class TestZapTimer(unittest.TestCase):

    def test_sleep_times(self):
        for time in ['2021-01-01 7:10:00', '2021-01-01 19:10:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.sleep_time(), msg=
                    f'should have slept at {time}')

    def test_zap_times(self):
        for time in ['2021-01-01 8:10:00', '2021-01-01 12:10:00',
            '2021-01-01 17:59:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.time_to_zap(), msg=
                    f'should have zapped at {time}')

    def test_get_next_time(self):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            for _ in range(1000):
                timer = make_timer()
                d = timer.get_next_time() - timer.now()
                self.assertTrue(27 <= d.seconds / 60 < 30)


<mask token>


class TestZapper(unittest.TestCase):

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_before_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_when_sleeping(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 5:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_zaps_when_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            zapper.timer.next_time = zapper.timer.now()
            self.assertLess((zapper.timer.next_time - zapper.timer.now()).
                seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_called_with(
                'https://app.pavlok.com/unlocked/remotes/testing/zap/101?message=some+space'
                )
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)


<mask token>

<mask token>


class TestUtils(unittest.TestCase):

    def test_make_url(self):
        url = make_url(api_name='test_name', level=101, message=
            'testing spaces')
        self.assertEqual(url,
            'https://app.pavlok.com/unlocked/remotes/test_name/zap/101?message=testing+spaces'
            )

    def test_rand_minutes(self):
        for _ in range(1000):
            minutes = rand_minutes(100, 10)
            self.assertTrue(90 <= minutes <= 100)

    def test_percent_to_zap_value(self):
        self.assertEqual(percent_to_zap_value(45), 114)
        self.assertEqual(percent_to_zap_value(60), 153)


def make_timer() ->ZapTimer:
    return ZapTimer(timezone_str='US/Pacific', interval_minutes=30,
        interval_percent=10, sleep_hour_before=8, sleep_hour_after=18)


<mask token>


def parse_time(time) ->datetime:
    dt = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
    return dt.astimezone(TZ)


class TestZapTimer(unittest.TestCase):

    def test_sleep_times(self):
        for time in ['2021-01-01 7:10:00', '2021-01-01 19:10:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.sleep_time(), msg=
                    f'should have slept at {time}')

    def test_zap_times(self):
        for time in ['2021-01-01 8:10:00', '2021-01-01 12:10:00',
            '2021-01-01 17:59:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.time_to_zap(), msg=
                    f'should have zapped at {time}')

    def test_get_next_time(self):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            for _ in range(1000):
                timer = make_timer()
                d = timer.get_next_time() - timer.now()
                self.assertTrue(27 <= d.seconds / 60 < 30)


def make_zapper():
    timer = make_timer()
    return Zapper(timer, api_name='testing', zap_level=101, message=
        'some space')


class TestZapper(unittest.TestCase):

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_before_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_when_sleeping(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 5:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_zaps_when_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            zapper.timer.next_time = zapper.timer.now()
            self.assertLess((zapper.timer.next_time - zapper.timer.now()).
                seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_called_with(
                'https://app.pavlok.com/unlocked/remotes/testing/zap/101?message=some+space'
                )
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)


<mask token>

from datetime import datetime
from dateutil import tz
import unittest
from unittest import mock
from freezegun import freeze_time
from zapper import Zapper, ZapTimer
from util import make_url, rand_minutes, percent_to_zap_value


class TestUtils(unittest.TestCase):

    def test_make_url(self):
        url = make_url(api_name='test_name', level=101, message=
            'testing spaces')
        self.assertEqual(url,
            'https://app.pavlok.com/unlocked/remotes/test_name/zap/101?message=testing+spaces'
            )

    def test_rand_minutes(self):
        for _ in range(1000):
            minutes = rand_minutes(100, 10)
            self.assertTrue(90 <= minutes <= 100)

    def test_percent_to_zap_value(self):
        self.assertEqual(percent_to_zap_value(45), 114)
        self.assertEqual(percent_to_zap_value(60), 153)


def make_timer() ->ZapTimer:
    return ZapTimer(timezone_str='US/Pacific', interval_minutes=30,
        interval_percent=10, sleep_hour_before=8, sleep_hour_after=18)


TZ = tz.gettz('America/Los Angeles')


def parse_time(time) ->datetime:
    dt = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
    return dt.astimezone(TZ)


class TestZapTimer(unittest.TestCase):

    def test_sleep_times(self):
        for time in ['2021-01-01 7:10:00', '2021-01-01 19:10:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.sleep_time(), msg=
                    f'should have slept at {time}')

    def test_zap_times(self):
        for time in ['2021-01-01 8:10:00', '2021-01-01 12:10:00',
            '2021-01-01 17:59:00']:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.time_to_zap(), msg=
                    f'should have zapped at {time}')

    def test_get_next_time(self):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            for _ in range(1000):
                timer = make_timer()
                d = timer.get_next_time() - timer.now()
                self.assertTrue(27 <= d.seconds / 60 < 30)


def make_zapper():
    timer = make_timer()
    return Zapper(timer, api_name='testing', zap_level=101, message=
        'some space')


class TestZapper(unittest.TestCase):

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_before_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_doesnt_zap_when_sleeping(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 5:00:00')):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch('zapper.requests.get')
    def test_zaps_when_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time('2021-01-01 12:00:00')):
            zapper = make_zapper()
            zapper.timer.next_time = zapper.timer.now()
            self.assertLess((zapper.timer.next_time - zapper.timer.now()).
                seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_called_with(
                'https://app.pavlok.com/unlocked/remotes/testing/zap/101?message=some+space'
                )
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()
                ).seconds / 60, 10)


if __name__ == '__main__':
    unittest.main()

from datetime import datetime
from dateutil import tz
import unittest
from unittest import mock
from freezegun import freeze_time

from zapper import Zapper, ZapTimer
from util import make_url, rand_minutes, percent_to_zap_value


class TestUtils(unittest.TestCase):

    def test_make_url(self):
        url = make_url(api_name="test_name", level=101, message="testing spaces")
        self.assertEqual(url, "https://app.pavlok.com/unlocked/remotes/test_name/zap/101?message=testing+spaces")

    def test_rand_minutes(self):
        for _ in range(1000):
            minutes = rand_minutes(100, 10)
            self.assertTrue(90 <= minutes <= 100)

    def test_percent_to_zap_value(self):
        self.assertEqual(percent_to_zap_value(45), 114)
        self.assertEqual(percent_to_zap_value(60), 153)


def make_timer() -> ZapTimer:
    return ZapTimer(
        timezone_str="US/Pacific",
        interval_minutes=30,
        interval_percent=10,
        sleep_hour_before=8,
        sleep_hour_after=18,
    )


TZ = tz.gettz("America/Los Angeles")


def parse_time(time) -> datetime:
    # work around weird bug in `freeze_time`
    dt = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
    return dt.astimezone(TZ)


class TestZapTimer(unittest.TestCase):

    def test_sleep_times(self):
        for time in ["2021-01-01 7:10:00", "2021-01-01 19:10:00"]:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.sleep_time(), msg=f"should have slept at {time}")

    def test_zap_times(self):
        for time in ["2021-01-01 8:10:00", "2021-01-01 12:10:00", "2021-01-01 17:59:00"]:
            with freeze_time(parse_time(time)):
                timer = make_timer()
                timer.next_time = timer.now()
                self.assertTrue(timer.time_to_zap(), msg=f"should have zapped at {time}")

    def test_get_next_time(self):
        with freeze_time(parse_time("2021-01-01 12:00:00")):
            for _ in range(1000):
                timer = make_timer()
                d = timer.get_next_time() - timer.now()
                self.assertTrue(27 <= (d.seconds / 60) < 30)


def make_zapper():
    timer = make_timer()
    return Zapper(timer, api_name="testing", zap_level=101, message="some space")


class TestZapper(unittest.TestCase):
    @mock.patch("zapper.requests.get")
    def test_doesnt_zap_before_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time("2021-01-01 12:00:00")):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch("zapper.requests.get")
    def test_doesnt_zap_when_sleeping(self, mocked: mock.Mock):
        with freeze_time(parse_time("2021-01-01 5:00:00")):
            zapper = make_zapper()
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_not_called()

    @mock.patch("zapper.requests.get")
    def test_zaps_when_expired(self, mocked: mock.Mock):
        with freeze_time(parse_time("2021-01-01 12:00:00")):
            zapper = make_zapper()
            zapper.timer.next_time = zapper.timer.now()
            self.assertLess((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)
            zapper.attempt_zapping()
            mocked.assert_called_with("https://app.pavlok.com/unlocked/remotes/testing/zap/101?message=some+space")
            self.assertGreater((zapper.timer.next_time - zapper.timer.now()).seconds / 60, 10)


if __name__ == '__main__':
    unittest.main()
