Ejemplo n.º 1
0
    def test_time_slot_receiver_JSON_runs(self):
        """
        This method tests the time slot receiver JSON view when there are runs in the server-side database. The
        expected result of this pattern is for the correct JSON response to be retrieved.
        """
        request = DummyRequest(route='/scheduler.json')

        if project_config["start_date"]:
            input_date = datetime.datetime.combine(project_config["start_date"], datetime.time())
        else:
            input_date = datetime.datetime.today() + datetime.timedelta(hours=1)

        user_input = int(time.mktime(input_date.timetuple()) * 1000)

        request.content_type = "application/json"
        request.POST["date"] = str(user_input)

        # Insert runs that will the next hour with even minutes that are a multiple of 10
        with transaction.manager:
            runs = []
            for minutes in range(0, 60, 10):
                runs.append(Run(create_input_pattern(), datetime.datetime(input_date.year, input_date.month,
                                                                          input_date.day, input_date.hour, minutes), ""))
            DBSession.add_all(runs)
            DBSession.commit()

        response = time_slot_reciever_JSON(request)

        # Assert that a response has been received.
        assert response

        response_dict = eval(str(response))
        for min in range(0, 60, 10):
            # Assert that the given slot is not in the response (means there is a run at this point)
            assert min not in response_dict[input_date.hour]
Ejemplo n.º 2
0
 def test_insert(self):
     '''
     Test insertion of data into the runs table
     '''
     with transaction.manager:
         run = Run(create_input_pattern(), datetime.datetime.now(), self._insert_name)
         DBSession.add(run)
         DBSession.commit()
Ejemplo n.º 3
0
 def test_insert(self):
     """
     This method tests the ability of the runs table to insert a new row. The expected result of this test is for
     a row to be inserted into the table correctly.
     """
     with transaction.manager:
         run = Run(create_input_pattern(), datetime.datetime.now(), self._insert_name)
         DBSession.add(run)
         DBSession.commit()
Ejemplo n.º 4
0
    def teardown_class(self):
        """
        This method tears down the class after testing has been completed, in order to ensure no data exists after
        testing that shouldn't. In this case, closes down the database session.
        """
        with transaction.manager:
            for run in DBSession.query(Run).all():
                # Delete all runs in the database.
                DBSession.delete(run)
            DBSession.commit()

        # Close the database session.
        DBSession.remove()
        testing.tearDown()
Ejemplo n.º 5
0
    def teardown_class(self):
        """
        This method tears down the testing logic to ensure that no data remains after testing that shouldn't. In this
        case, it closes the database session.
        """
        with transaction.manager:
            for run in DBSession.query(Run).all():
                # Delete every run in the session.
                DBSession.delete(run)
            DBSession.commit()

        # Close the session.
        DBSession.remove()
        testing.tearDown()
Ejemplo n.º 6
0
 def test_delete(self):
     '''
     Test run deletion
     '''
     # Delete the run inserted by this test
     runs = DBSession.query(Run).filter(Run.user_name==self._insert_name)
     assert runs.all()
     for run in runs:
         DBSession.delete(run)
         DBSession.commit()
         
     # Ensure deletion
     runs = DBSession.query(Run).filter(Run.user_name==self._insert_name)
     assert not runs.all()
Ejemplo n.º 7
0
    def test_delete(self):
        """
        This method tests the ability of the runs table to delete data from it. The expected result of this is for the
        data to be correctly deleted from the table.
        """
        # Delete the run inserted by this test
        runs = DBSession.query(Run).filter(Run.user_name==self._insert_name)
        # Assert that the runs have been retrieved
        assert runs.all()
        for run in runs:
            DBSession.delete(run)
            DBSession.commit()

        runs = DBSession.query(Run).filter(Run.user_name==self._insert_name)
        # Assert that the selected runs have been deleted.
        assert not runs.all()
Ejemplo n.º 8
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
Ejemplo n.º 9
0
    def get_unsent_runs(cls, min_time):
        """
        This method retrieves all of the currently unsent patterns in the database that are set to occur after the
        given minimum time.

        @param min_time The time after which the database should be query.

        @return unsent_runs The runs that have not yet been sent to the Raspberry Pi.
        """
        with transaction.manager:
            unsent_runs = DBSession.query(Run).filter(and_(Run.time_slot > min_time, Run.sent == False)).all()

            for run in unsent_runs:
                run.sent = True

            DBSession.commit()

        return unsent_runs
Ejemplo n.º 10
0
def test_run_transmitter_view():
    """
    This function tests the functionality of the run_transmitter view. The expected result of this
    test is that the view functions correctly and retrieves currently unsent runs for the raspberry pi.
    """
    time_slot = datetime.datetime.now() + datetime.timedelta(days=5)

    # Add a pattern and time to the database for testing
    with transaction.manager:
        DBSession.add(Run(create_input_pattern(), time_slot, ""))
        DBSession.commit()

    # Set up request
    request = DummyRequest(route="/run_transmitter.json")
    time_slot -= datetime.timedelta(hours=1)
    request.POST["min_time"] = time_slot.isoformat()

    # Call the view
    response = run_transmitter_view(request)

    # Test that the view has returned a response
    assert response
Ejemplo n.º 11
0
    def insert_run(cls, pattern, time_slot):
        """
        Ensures that the pattern and time_slot meet validation
        and inserts the run into the run table.

        This method ensures that the pattern and time slot meet validation before inserting the run into the runs table
        of the server-side database.

        @param pattern The pattern to input into the table.
        @param time_slot The datetime at which the pattern should be run.
        """
        now = datetime.datetime.now()

        # Perform validation checks
        cls._validate_time_slot(now, time_slot)
        if cls.get_run_for_time_slot(time_slot):
            raise RunSlotTakenError("Time slots already has a run associated")

        # Insert pattern and time slot into database
        with transaction.manager:
            DBSession.add(Run(pattern, time_slot, ""))
            DBSession.commit()