Esempio n. 1
0
    def test__repr__(self):
        """
        This method tests the string representation of a Run row from the runs table. The expected result of this
        test is for the representation of the runs table to be returned correctly.
        """
        run = Run(create_input_pattern(), datetime.datetime.now(), self._insert_name)
        run_repr = run.__repr__()

        # Assert that the run's representation has been returned
        assert run_repr
        # Assert that the representation is a string object
        assert isinstance(run_repr, str)
Esempio n. 2
0
    def test_run__validate_time_slot(self):
        """
        This method tests the ability of the runs table to validate a time slot, making sure it is viable for using
        on the table and throwing the appropriate exception if not. The expected result of this test is for the
        given time slot to be validated as correct.
        """
        # Set a time slot to be tested
        time_slot = datetime.datetime.now().replace(minute=5) + datetime.timedelta(hours=1)

        # Assert that the validation does not call an exception, and so the time slot is correct
        try:
            Run._validate_time_slot(datetime.datetime.now(), time_slot)
        except RunSlotInvalidError:
            raise Exception("test_run__validate_time_slot has failed")
Esempio n. 3
0
    def test_json(self):
        """
        This method tests the JSON representation method of the run class. The expected result of this method is that
        a json dictionary of the run object in question is retrieved.
        """
        run = Run(create_input_pattern(), datetime.datetime.now(), self._insert_name)

        run_json = run.json()
        # Assert that a representation has been retrieved.
        assert run_json
        # Assert that the representation is a json dictionary with the correct information within.
        assert run_json.__contains__("id")
        assert run_json.__contains__("input_pattern")
        assert run_json.__contains__("time_slot")
        assert run_json.__contains__("sent")
Esempio n. 4
0
    def test_confirmation_receiver_JSON(self):
        """
        This method tests the confirmation receiver JSON view of the confirmation page. The expected result of this
        test is for the content of a session to be successfully added to the server-side database.
        """
        request = DummyRequest(route='/confirm.json')

        # Create a pattern to be saved to the database
        request.session["pattern"] = create_input_pattern()
        # Create a time and date to be saved for the pattern on the database
        time = datetime.datetime.now().replace(minute=0, second=0, microsecond=0) + datetime.timedelta(days=1)
        request.session["viewing_date"] = time.strftime("%d/%m/%Y")
        request.session["viewing_hour"] = time.strftime("%H")
        request.session["viewing_slot"] = time.strftime("%M")

        response_dict = confirmation_receiver_JSON(request)

        # Assert that a response has been retrieved.
        assert response_dict
        # Assert the data has been successfully stored to the database.
        assert response_dict["success"]

        # Assert that the data is inside the database.
        assert Run.get_run_for_time_slot(time)

        # Assert that the session has been emptied.
        assert not "pattern" in request.session
        assert not "viewing_date" in request.session
        assert not "viewing_hour" in request.session
        assert not "viewing_slot" in request.session
Esempio n. 5
0
    def test_run_get_time_slots_for_day_with_runs(self):
        """
        This method tests the ability of the runs table to retrieve the available time slots in a given day. The
        expected result of this test is for the number of retrieved time slots to be equal to the number of five
        minute time slots in a day minus two (for two runs).
        """
        # Give the method a future date and receive correct no. of time-slots for that date
        date = datetime.date.today() + datetime.timedelta(days=2)

        min_time = project_config["starting_time"]
        max_time = project_config["closing_time"]
        # Calculate the no. of hours in a given day to test
        no_mins = (max_time.hour*60 + max_time.minute) - (min_time.hour*60 + min_time.minute)
        no_time_slots = math.ceil(no_mins / 5)

        date = datetime.datetime.combine(date, datetime.time(hour=12, minute=0, second=0, microsecond=0))

        runs = [
            Run(create_input_pattern(), date, self._insert_name),
            Run(create_input_pattern(), date + datetime.timedelta(minutes=5), self._insert_name)]
        with transaction.manager:
            DBSession.add_all(runs)
            DBSession.commit

        time_slots = Run.get_time_slots_for_day(date, datetime.datetime.now())

        # Assert that the correct number of available time slots have been retrieved.
        assert no_time_slots - len(runs) == len(time_slots)
Esempio n. 6
0
    def test_run__validate_time_slot_not_five(self):
        """
        This method tests the ability of the runs table to validate a time slot, making sure it is viable for using
        on the table and throwing the appropriate exception if not. The expected result of this test is for the
        given time slot to be validated as not a multiple of five.
        """
        # Set a time slot to be tested
        time_slot = datetime.datetime.now().replace(minute=6) + datetime.timedelta(days=1)

        # Assert that the validation calls an exception because the time slot is not a multiple of five
        try:
            Run._validate_time_slot(datetime.datetime.now(), time_slot)
        except RunSlotInvalidError as e:
            # Assert that the correct error has been thrown
            assert e
            # Assert that it has been thrown for the right reason
            assert e.args[0] == "Minute value is not a multiple of 5"
Esempio n. 7
0
    def test_run_get_unsent_runs(self):
        """
        This method tests the ability of the runs table to retrieve the runs than have not yet been sent to the
        raspberry pi. The expected result of this test is that the unsent runs can be correctly retrieved.
        """

        time_slot = datetime.datetime.now() + datetime.timedelta(days=35)

        # Create some runs to go into the table
        runs = [
            Run(create_input_pattern(), time_slot, self._insert_name),
            Run(create_input_pattern(), time_slot + datetime.timedelta(minutes=5), self._insert_name),
            Run(create_input_pattern(), time_slot + datetime.timedelta(minutes=10), self._insert_name)
        ]

        # Add runs to table
        with transaction.manager:
            DBSession.add_all(runs)
            DBSession.commit()

        new_sent_runs = Run.get_unsent_runs(time_slot - datetime.timedelta(minutes=5))

        # Assert that the collected runs have been retrieved
        assert new_sent_runs
        # Assert that the list of sent runs contains the correct number of runs
        assert len(new_sent_runs) == 3

        # Create some more runs to go into the table
        new_runs = [
            Run(create_input_pattern(), time_slot + datetime.timedelta(minutes=15), self._insert_name),
            Run(create_input_pattern(), time_slot + datetime.timedelta(minutes=20), self._insert_name),
            Run(create_input_pattern(), time_slot + datetime.timedelta(minutes=25), self._insert_name)
        ]

        # Add these runs to the table
        with transaction.manager:
            DBSession.add_all(new_runs)
            DBSession.commit()

        new_sent_runs = Run.get_unsent_runs(time_slot - datetime.timedelta(minutes=5))

        # Assert that the collected runs have been retrieved
        assert new_sent_runs
        # Assert that the list of sent runs still contains the correct number of runs
        assert len(new_sent_runs) == 3
Esempio n. 8
0
    def test_run__validate_time_slot_with_start(self):
        """
        This method tests the ability of the runs table to validate a time slot, making sure it is viable for using
        on the table and throwing the appropriate exception if not. The expected result of this test is for the
        given time slot to be validated as correct.
        """
        project_config["start_date"] = datetime.date(year=2014, month=7, day=7)

        # Set a time slot to be tested
        time_slot = datetime.datetime.now().replace(year=2014, month=7, day=14, minute=5)

        # Assert that the validation does not call an exception, and so the time slot is correct
        try:
            Run._validate_time_slot(datetime.datetime.now(), time_slot)
        except RunSlotInvalidError:
            raise Exception("test_run__validate_time_slot_with_start has failed")

        project_config["start_date"] = None
Esempio n. 9
0
    def test_run_get_runs_for_day(self):
        """
        This method tests the ability of the runs table to retrieve every run set to be played on a given day. The
        expected result should be for a the call to result an empty dictionary.
        """
        # Give the method a future date and receive correct no. of time-slots for that date
        date = datetime.date.today() + datetime.timedelta(days=1)

        dayRuns = Run.get_runs_for_day(date)
        # Test that the method returns the right information
        assert dayRuns == []
Esempio n. 10
0
    def test_run_get_time_slots_for_day(self):
        """
        This method tests the ability of the runs table to retrieve the available time slots in a given day. The
        expected result of this test is for the number of retrieved time slots to be equal to the number of five minute
        time slots in a day.
        """
        # Give the method a future date and receive correct no. of time-slots for that date
        date = datetime.date.today() + datetime.timedelta(days=10)

        min_time = project_config["starting_time"]
        max_time = project_config["closing_time"]
        # Calculate the no. of hours in a given day to test
        no_mins = (max_time.hour*60 + max_time.minute) - (min_time.hour*60 + min_time.minute)
        no_time_slots = math.ceil(no_mins / 5)

        time_slots = Run.get_time_slots_for_day(date, datetime.datetime.now(), min_time, max_time)

        assert no_time_slots == len(time_slots)
Esempio n. 11
0
def run_transmitter_view(request):
    """
    This function executes the transmission of unsent server patterns to the display.

    @param request The request sent to this page of the web application.
    """

    #TODO: Write authentication logic! Quickly!
    # ...
    #TODO: Look into official authentication logic

    min_time = request.POST["min_time"]
    min_time = datetime.strptime(min_time, "%Y-%m-%dT%H:%M:%S.%f")

    unsent_runs = Run.get_unsent_runs(min_time)
    unsent_runs = [run.json() for run in unsent_runs]

    return unsent_runs
Esempio n. 12
0
    def test_run_get_runs_for_day_with_runs(self):
        """
        This method tests the ability of the runs table to retrieve every run set to be played on a given day. The
        expected result should be for the call to result in the same number of runs as previously added.
        """
        dt = datetime.datetime.now() + datetime.timedelta(days=1)
        dt = dt.replace(minute=0, second=0, microsecond=0)

        runs = [
            Run(create_input_pattern(), dt, self._insert_name),
            Run(create_input_pattern(), dt + datetime.timedelta(minutes=5), self._insert_name)]
        with transaction.manager:
            DBSession.add_all(runs)
            DBSession.commit

        date = datetime.date.today() + datetime.timedelta(days=1)
        dayRuns = Run.get_runs_for_day(date)
        # Assert that the correct number of runs have been retrieved
        assert dayRuns == runs
Esempio n. 13
0
    def test_run_get_time_slots_for_day_non_hour(self):
        """
        This method tests the ability of the runs table to retrieve the number of available of time slots in a day. The
        expected result of this this test is that the correct number of available time slots for an hour is retrieved
        when there are no runs to be played in the given hour and the day does not end 'on the hour'.
        """
        # Give the method a future date
        date = datetime.date.today() + datetime.timedelta(days=11)

        min_time = datetime.time(hour=9, minute=0)
        max_time = datetime.time(hour=17, minute=0)

        # Calculate the no. of time slots in a given day
        no_mins = (max_time.hour*60 + max_time.minute) - (min_time.hour*60 + min_time.minute)
        no_time_slots = math.ceil(no_mins / 5)

        # Assert that the correct number of available time slots have been retrieved.
        time_slots = Run.get_time_slots_for_day(date, datetime.datetime.now(), min_time, max_time)

        # Assert that the correct number of time slots has been retrieved
        assert no_time_slots == len(time_slots)