<commit_before># Copyright (C) 2017 OpenMotics BVBA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Tests for the scheduling module.
"""
import os
import unittest
import xmlrunner
import time
import fakesleep
from threading import Lock, Semaphore

from gateway.webservice import WebInterface
from gateway.scheduling import SchedulingController


class GatewayApi(object):
    RETURN_DATA = {}

    def get_timezone(self):
        _ = self
        return 'Europe/Brussels'

    def do_group_action(self, group_action_id):
        _ = self
        GatewayApi.RETURN_DATA['do_group_action'] = group_action_id

    def do_basic_action(self, action_type, action_number):
        _ = self
        GatewayApi.RETURN_DATA['do_basic_action'] = (action_type, action_number)


class SchedulingControllerTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        fakesleep.monkey_patch()

    @classmethod
    def tearDownClass(cls):
        fakesleep.monkey_restore()

    def setUp(self):
        self._db = "test.schedule.{0}.db".format(time.time())
        GatewayApi.RETURN_DATA = {}
        if os.path.exists(self._db):
            os.remove(self._db)

    def tearDown(self):
        GatewayApi.RETURN_DATA = {}
        if os.path.exists(self._db):
            os.remove(self._db)

    def _get_controller(self):
        gateway_api = GatewayApi()
        controller = SchedulingController(self._db, Lock(), gateway_api)
        controller.set_webinterface(WebInterface(None, gateway_api, None, None, None, controller))
        return controller

    def test_base_validation(self):
        controller = self._get_controller()
        with self.assertRaises(RuntimeError):
            # Must have a name
            controller._validate(None, None, None, None, None, None, None)<commit_msg>Fixed scheduling unit tests<commit_after># Copyright (C) 2017 OpenMotics BVBA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Tests for the scheduling module.
"""
import os
import unittest
import xmlrunner
import time
import fakesleep
from threading import Lock, Semaphore
from mock import Mock

from gateway.webservice import WebInterface
from gateway.scheduling import SchedulingController


class GatewayApi(object):
    RETURN_DATA = {}

    def get_timezone(self):
        _ = self
        return 'Europe/Brussels'

    def do_group_action(self, group_action_id):
        _ = self
        GatewayApi.RETURN_DATA['do_group_action'] = group_action_id

    def do_basic_action(self, action_type, action_number):
        _ = self
        GatewayApi.RETURN_DATA['do_basic_action'] = (action_type, action_number)


class SchedulingControllerTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        fakesleep.monkey_patch()

    @classmethod
    def tearDownClass(cls):
        fakesleep.monkey_restore()

    def setUp(self):
        self._db = "test.schedule.{0}.db".format(time.time())
        GatewayApi.RETURN_DATA = {}
        if os.path.exists(self._db):
            os.remove(self._db)

    def tearDown(self):
        GatewayApi.RETURN_DATA = {}
        if os.path.exists(self._db):
            os.remove(self._db)

    def _get_controller(self):
        gateway_api = GatewayApi()
        controller = SchedulingController(self._db, Lock(), gateway_api)
        controller.set_webinterface(WebInterface(None, gateway_api, None, None, None, controller, Mock()))
        return controller

    def test_base_validation(self):
        controller = self._get_controller()
        with self.assertRaises(RuntimeError):
            # Must have a name
            controller._validate(None, None, None, None, None, None, None)
{'fraction_non_alphanumeric': 0.08394909688013136, 'fraction_numerical': 0.0024630541871921183, 'mean_word_length': 3.354781054512958, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '4038018', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1341, 'n_words': 437}