Exemplo n.º 1
0
 def test__eq__1(self):
     """
     Both inputs should be equal.
     """
     var_1 = Variable(str, "my_var")
     var_2 = Variable(str, "my_var")
     self.assertEqual(var_1, var_2)
Exemplo n.º 2
0
 def test__eq__1(self):
     """
     Both inputs should be equal.
     """
     que_1 = Question("TestQuestion1", Variable(str, "TestVar1"))
     que_2 = Question("TestQuestion1", Variable(str, "TestVar1"))
     self.assertEqual(que_1, que_2)
Exemplo n.º 3
0
 def test__eq__2(self):
     """
     Name different.
     """
     var_1 = Variable(str, "my_var")
     var_2 = Variable(str, "my_other_var")
     self.assertNotEqual(var_1, var_2)
Exemplo n.º 4
0
 def test__eq__3(self):
     """
     Type different.
     """
     var_1 = Variable(str, "my_var")
     var_2 = Variable(int, "my_var")
     self.assertNotEqual(var_1, var_2)
Exemplo n.º 5
0
 def test__eq__3(self):
     """
     Variable different.
     """
     que_1 = Question("TestQuestion1", Variable(str, "TestVar1"))
     que_2 = Question("TestQuestion1", Variable(str, "TestVar2"))
     self.assertNotEqual(que_1, que_2)
Exemplo n.º 6
0
    def test_read_vars_from_file(self):
        read_vars_form_file("test_file.txt", self.db)

        result = self.db.variables
        expected = (Variable(int, "parrot"), Variable(float, "ant"))

        self.assertTupleEqual(result, expected)
Exemplo n.º 7
0
    def test_two_vars_present(self):
        var_1 = Variable(int, "some_int")
        var_2 = Variable(str, "some_str")

        add_var(self.conn, var_1)
        add_var(self.conn, var_2)

        result = get_all_vars(self.conn)
        self.assertEqual(len(result), 2)

        self.assertEqual(var_1, result[0])
        self.assertEqual(var_2, result[1])
Exemplo n.º 8
0
    def test_error_if_var_exists(self):
        """
        Each variable name can be added only once.
        A RuntimeError should be raised if two Variables are added with the
        same name. The type of the Variables does not matter.
        """
        var_1 = Variable(int, "some_var")
        var_2 = Variable(str, "some_var")
        add_var(self.conn, var_1)

        with self.assertRaises(RuntimeError):
            add_var(self.conn, var_2)
Exemplo n.º 9
0
    def test_get_variables_not_empty(self):
        """
        Base case: two variables in db should return both (in order).
        """
        var_1 = Variable(int, "v1")
        var_2 = Variable(str, "v2")

        self.db.create_new_var(var_1)
        self.db.create_new_var(var_2)

        expected = (var_1, var_2)
        result = self.db.variables
        self.assertTupleEqual(expected, result)
Exemplo n.º 10
0
 def test_name_correct(self):
     """
     The database should store the name of a stored Variable.
     """
     var = Variable(int, "some_var")
     add_var(self.conn, var)
     df = get_all_rows_variables(self.conn)
     self.assertEqual(df.loc[0, "var_name"], "some_var")
Exemplo n.º 11
0
 def test_get_is_executed_1(self):
     """
     After executing a command, its attribute [is_executed] should be True.
     """
     db_writer = MockDatabaseWriter()
     command = InsertCommand(db_writer, 0, Variable(int, "meh"), None)
     command.execute()
     self.assertTrue(command.is_executed)
Exemplo n.º 12
0
 def test_str_1(self):
     """
     Base case: str-typed Variable
     """
     var = Variable(str, "TestVar3")
     expected = "Var TestVar3: str"
     result = str(var)
     self.assertEqual(result, expected)
Exemplo n.º 13
0
 def test_repr_1(self):
     """
     Base case: str-typed Variable
     """
     var = Variable(str, "TestVar")
     expected = "Variable(str,'TestVar')"
     result = repr(var)
     self.assertEqual(result, expected)
Exemplo n.º 14
0
 def test_repr_2(self):
     """
     Base case: list-typed Variable
     """
     var = Variable(list, "TestVar2")
     expected = "Variable(list,'TestVar2')"
     result = repr(var)
     self.assertEqual(result, expected)
Exemplo n.º 15
0
 def test_invalid_var_type(self):
     """
     The type associated with a new variable must be a value in 
     settings.TYPE_TO_STR.
     """
     invalid_type = "rock"
     with self.assertRaises(ValueError):
         Variable(invalid_type, "test")
Exemplo n.º 16
0
    def test_repr_2(self):
        """
        Base-case: str-typed Question
        """

        question = Question("TestQuestion1", Variable(str, "TestVar1"))
        expected = "Question(Variable(str,'TestVar1')):TestQuestion1"
        result = str(question)
        self.assertEqual(result, expected)
Exemplo n.º 17
0
 def test_type_correct(self):
     """
     The database should store a string representation of the type
     of a Variable.
     """
     var = Variable(int, "some_var")
     add_var(self.conn, var)
     df = get_all_rows_variables(self.conn)
     self.assertEqual(df.loc[0, "var_type"], settings.TYPE_TO_STR[int])
Exemplo n.º 18
0
    def test_repr_1(self):
        """
        Base case: str-typed Question
        """

        question = Question("TestQuestion", Variable(str, "TestVar"))
        expected = "Question('TestQuestion',Variable(str,'TestVar'))"
        result = repr(question)
        self.assertEqual(result, expected)
Exemplo n.º 19
0
 def test_table_not_exits(self):
     """
     Cannot insert into the table of nonexisting variables!
     Should raise a RuntimeError.
     """
     another_var = Variable(int, "another_var")
     new_value = 10
     with self.assertRaises(RuntimeError):
         insert_new_var_value(self.conn, another_var, new_value)
Exemplo n.º 20
0
 def test_insert_invalid_var_2(self):
     """
     Corner case: giving a Variable instance with the same name as the one
     of the table, but a different type. Even if the type of the actual
     new value is correct, this should still raise a RuntimeError.
     """
     invalid_value = 13
     fake_var = Variable(float, "test_var")
     with self.assertRaises(RuntimeError):
         insert_new_var_value(self.conn, fake_var, invalid_value)
Exemplo n.º 21
0
    def test_get_rows_of_vars(self):
        """
        Test get_rows_of_vars(), given as input a sequence of strings.
        """
        var_foo = Variable(int, "foo")
        var_bar = Variable(float, "bar")
        self.db.create_new_var(var_foo)
        self.db.create_new_var(var_bar)
        self.db.insert_new_value_of_var(var_foo, 10)
        self.db.insert_new_value_of_var(var_bar, 10.5)
        # Pretend this happens a second later!
        self.db.insert_new_value_of_var(var_foo, 12, int(time.time()) + 1)
        self.db.insert_new_value_of_var(var_bar, 12.5, int(time.time()) + 1)

        result = self.db.get_rows_of_vars(("foo", "bar"))

        expected = {"foo": (10, 12), "bar": (10.5, 12.5)}

        for key in expected.keys():
            np.testing.assert_allclose(expected[key], result[key])
Exemplo n.º 22
0
    def setUp(self) -> None:
        """
        Scheduled time: Tuesday 7:30
        Start time: Monday 15:22, 22 March
        """
        start_time = 1616422958

        self.recurring_question = RecurringQuestionSet(
            {WeekDay(1)}, 7, 30,
            {Question("test", Variable(str, "my_var"))},
            start_time)
Exemplo n.º 23
0
    def test_read_vars_from_file_if_already_in(self):
        """
        Corner case:
        if a Variable already exists, it should be ignored.
        If it is not ignored an error will be raised...
        So this testcase passes if no error occurred.
        """

        self.db.create_new_var(Variable(int, "parrot"))

        read_vars_form_file("test_file.txt", self.db)
Exemplo n.º 24
0
 def test_insert_invalid_var_1(self):
     """
     Corner case: giving a Variable instance with the same name as the one
     of the table, but a different type. The input value corresponds to
     this wrong type as well.
     This should still raise an error! (a RuntimeError)
     """
     invalid_value = 13.5
     fake_var = Variable(float, "test_var")
     with self.assertRaises(RuntimeError):
         insert_new_var_value(self.conn, fake_var, invalid_value)
Exemplo n.º 25
0
 def test_timestamp_correct(self):
     """
     In the database, the timestamp of the moment that the Variable
     was created should be stored.
     It needs to be an epoch time in seconds.
     """
     var = Variable(int, "some_var")
     add_var(self.conn, var)
     df = get_all_rows_variables(self.conn)
     timestamp_now = time.time()
     self.assertLess(timestamp_now - df.loc[0, "timestamp"], 2)
Exemplo n.º 26
0
    def test_insert_valid_with_timestamp(self):
        var = Variable(int, "test_var")
        self.db.create_new_var(var)

        new_value = 13
        timestamp = 12345
        self.db.insert_new_value_of_var(var, new_value, timestamp)

        df = self.db.get_rows_of_var(var)

        self.assertEqual(df.loc[0, "value"], new_value)
        self.assertEqual(df.loc[0, "timestamp"], timestamp)
Exemplo n.º 27
0
    def setUp(self):
        remove_database(TEST_DB_NAME)
        self.db = DatabaseWriter(TEST_DB_NAME)

        self.var_1 = Variable(int, "foo")
        self.db.create_new_var(self.var_1)
        self.db.insert_new_value_of_var(self.var_1, 10, 1)
        self.db.insert_new_value_of_var(self.var_1, 11, 2)
        self.db.insert_new_value_of_var(self.var_1, 12, 3)
        self.db.insert_new_value_of_var(self.var_1, 13, 4)
        self.db.insert_new_value_of_var(self.var_1, 14, 5)

        self.var_2 = Variable(float, "bar")
        self.db.create_new_var(self.var_2)
        self.db.insert_new_value_of_var(self.var_2, 10.1, 1)
        self.db.insert_new_value_of_var(self.var_2, 11.1, 2)
        self.db.insert_new_value_of_var(self.var_2, 12.1, 3)
        self.db.insert_new_value_of_var(self.var_2, 13.1, 4)
        self.db.insert_new_value_of_var(self.var_2, 14.1, 5)

        # Rule #1 is fireable.
        trig_1 = TriggerExpression(
            "mean(vars_dict['foo'][-2:]) > mean(vars_dict['bar'][-3:])",
            {"foo", "bar"})
        mess_1 = MessageExpression("vars_dict['bar'][-1:]", {"foo"})
        eval_1 = EvaluationExpression(
            "tanh(vars_dict['bar'][-1:] - vars_dict['foo'][-1:])",
            {"foo", "bar"})
        self.rule_1 = Rule(trig_1, mess_1, eval_1)

        # Rule #2 is not fireable.
        trig_2 = TriggerExpression(
            "vars_dict['foo'][:1] > mean(vars_dict['bar'][:2])",
            {"foo", "bar"})
        mess_2 = MessageExpression("vars_dict['foo'][-1:]", {"bar"})
        eval_2 = EvaluationExpression(
            "tanh(vars_dict['bar'][:1] - vars_dict['foo'][-1:])",
            {"foo", "bar"})
        self.rule_2 = Rule(trig_2, mess_2, eval_2)
Exemplo n.º 28
0
def read_vars_form_file(filename: str, db: DatabaseWriter):
    """
    Read all Variable definitions from a file,
    and store any new Variables in the database.
    All cases are converted to lowercase.
    """
    var_lines = extract_lines_with_prefix_from_file(filename, "VAR")
    existing_var_names = tuple(map(lambda x: x.name, db.variables))
    for var_line in var_lines:
        var_line = var_line.lower().split()
        new_var = Variable(eval(var_line[2]), var_line[1])
        if new_var.name not in existing_var_names:
            db.create_new_var(new_var)
Exemplo n.º 29
0
    def test_execute(self):
        var = Variable(int, "meh")
        timestamp = 123
        new_value = 321
        id = 999

        db_writer = MockDatabaseWriter()
        command = InsertCommand(db_writer, id, var, new_value, timestamp)
        command.execute()

        observation = db_writer.input
        self.assertEqual(observation[0], var)
        self.assertEqual(observation[1], new_value)
        self.assertEqual(observation[2], timestamp)
Exemplo n.º 30
0
    def test_get_rows_of_var(self):
        """
        Test insert_new_value_of_var() and get_rows_of_var().
        """
        some_var = Variable(int, "some_var")
        self.db.create_new_var(some_var)
        self.db.insert_new_value_of_var(some_var, 10)
        # Pretend this happens a second later!
        self.db.insert_new_value_of_var(some_var, 12, int(time.time()) + 1)
        df = self.db.get_rows_of_var(some_var)
        timestamp = time.time()

        self.assertEqual(df.loc[0, "value"], 10)
        self.assertEqual(df.loc[1, "value"], 12)
        self.assertLess(timestamp - df.loc[0, "timestamp"], 2)
        self.assertLess(timestamp + 1 - df.loc[1, "timestamp"], 2)
        self.assertEqual(len(df), 2)