예제 #1
0
    def test_01_construct(self):
        """HistoriaRecord: __init__"""

        searcher = core_data_objects.HistoriaDatabaseSearch(self.db)

        self.assertIsInstance(
            searcher, core_data_objects.HistoriaDatabaseSearch,
            "Yeah, so this should really never come up since an exception should have happened by now"
        )
        self.assertIs(searcher.database, self.db,
                      "Database isn't the one we sent over")
        self.assertIsInstance(searcher.database,
                              core_data_objects.HistoriaDatabase,
                              "Database object isn't the right type (oops)")
        self.assertIsNone(searcher.search_scope, "Default scope is not None")
        self.assertEqual(searcher._return_fields, {},
                         "Default field list is not blank")
        self.assertEqual(searcher._joins, {}, "Default join dict not empty")
        self.assertEqual(searcher._conditions, [],
                         "Default conditions list not empty")
        self.assertEqual(searcher._sorts, [], "Default sort list is empty")
        self.assertIsNone(searcher._limit, "Initial list is not None")
        self.assertIsNone(searcher.saved_results,
                          "Default saved results not None")

        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")
        self.assertIs(searcher.search_scope, "User",
                      "Search scope not assigned correctly during setup")
예제 #2
0
    def authenticate_user(self, user_name, password):
        """Check the user_name and password. If it is a validate user, return
        user object."""
        if self.database is None:
            raise ControllerNotReady(
                "The controller is not fully configured, not database object.")

        if not self.database.connected:
            self.database.connect()
            if not self.database.connected:
                raise DatabaseNotReady('Cannot connect to database')

        search = core_data_objects.HistoriaDatabaseSearch(
            self.database, user.HistoriaUser.machine_type)

        search.add_field('id')
        search.add_condition('name', user_name)
        search.add_condition('email', user_name, '=', 'OR')
        search.add_limit(1)
        test_user = user.HistoriaUser(self.database)
        try:
            results = search.execute_search()
            test_user.load(results[0]['id'])
            if (test_user.checkPassword(password)):
                return test_user
            else:
                return False
        except:
            return False
예제 #3
0
    def test_80_generate_sql(self):
        """HistoriaRecord: _generate_sql(self)"""
        #def _generate_sql(self):
        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")

        searcher.add_field('id')
        searcher.add_field('name', "Real_Name")
        searcher.add_expression('COUNT[{0}]', ('email'), 'count_mail')
        searcher.add_join('id', 'user_database', 'uid')
        searcher.add_condition('id', '1')
        searcher.add_condition('name', 'John')
        searcher.add_sort('id')
        searcher.add_limit(100)

        sql = searcher._generate_sql()

        # we can't really test this without a database (See next test), but we
        # can check a couple small things.
        self.assertIsInstance(
            sql, tuple,
            "_generate_sql didn't return a tuple of code and values")
        self.assertIsInstance(sql[0], str,
                              "_generate_sql didn't return a string of code")
        self.assertNotEqual('', sql[0], "SQL is empty")
        self.assertEqual('SELECT', sql[0][:6], "SQL doesn't start with SELECT")
예제 #4
0
    def test_30_add_expression(self):
        """HistoriaRecord: add_expression(self, expression, values, alias=None)"""
        #def add_expression(self, expression, values, alias=None):
        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")

        searcher.add_expression('COUNT[{0}]', ('email'), 'count_mail')
        self.assertIn('count_mail', searcher._return_fields,
                      "expression not added to field list")
        self.assertEqual(searcher._return_fields['count_mail'],
                         ('COUNT[{0}]', ('email'), 'count_mail'),
                         "Tuple values not in place")
예제 #5
0
    def test_40_add_join(self):
        """HistoriaRecord: add_join(self, main_field, table, table_field, operation = "=")"""
        #def add_join(self, main_field, table, table_field, operation = "="):

        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")

        searcher.add_join('id', 'user_database', 'uid')

        self.assertIn('user_database', searcher._joins,
                      "join missing from join list.")
        self.assertEqual(searcher._joins['user_database'], ('id', 'uid', '='),
                         "Expression not saved as expected")
예제 #6
0
    def test_70_add_limit(self):
        """HistoriaRecord: add_limit(self, limit=None)"""
        #def add_limit(self, limit=None):
        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")

        searcher.add_limit(100)

        self.assertIs(searcher._limit, 100,
                      "Limit did not set correctly to value")

        searcher.add_limit()
        self.assertIsNone(searcher._limit,
                          "Limit did not set correctly to None")
예제 #7
0
    def test_20_add_field(self):
        """HistoriaRecord: add_field(self, field_name)"""
        #def add_field(self, field_name):
        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")

        searcher.add_field('name')
        self.assertIn('name', searcher._return_fields,
                      "field not added to field list")
        self.assertEqual(searcher._return_fields['name'], 'name',
                         "Default alias wasn't the same as the field name")

        searcher.add_field('email', 'mail')
        self.assertIn('email', searcher._return_fields,
                      "field not added to field list when alias given")
        self.assertEqual(searcher._return_fields['email'], 'mail',
                         "Assigned alias wasn't the same as the one provided")
예제 #8
0
    def test_60_add_sort(self):
        """HistoriaRecord: add_sort(self, field, direction="ASC")"""
        #def add_sort(self, field, direction="ASC"):

        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")

        searcher.add_sort('id')

        self.assertEqual(searcher._sorts[0], ('id', 'ASC'),
                         "Adding ID sort failed to save as expected")

        searcher.add_sort('email', 'DESC')

        self.assertEqual(searcher._sorts[1], ('email', 'DESC'),
                         "Adding email sort failed to save as expected")

        self.assertRaises(ValueError, searcher.add_sort, 'email', "NOPE")
예제 #9
0
    def test_50_add_condition(self):
        """HistoriaRecord: add_condition(self, field, value, comparison = "=", scope="AND")"""
        #def add_condition(self, field, value, comparison = "=", scope="AND"):

        searcher = core_data_objects.HistoriaDatabaseSearch(self.db, "User")

        searcher.add_condition('id', '1')

        self.assertEqual(searcher._conditions[0], ('id', '1', '=', 'AND'))

        searcher.add_condition('email', 'sir.robin@camelot.%', 'LIKE', "OR")

        self.assertEqual(searcher._conditions[1],
                         ('email', 'sir.robin@camelot.%', 'LIKE', 'OR'))

        self.assertRaises(ValueError, searcher.add_condition, 'email',
                          'sir.robin@camelot.%', 'LIKE', "NOPE")
예제 #10
0
    def test_90_execute_search(self):
        """HistoriaRecord: execute_search(self)"""
        #def execute_search(self):
        value1 = "First"
        value2 = "Nope"
        searcher = core_data_objects.HistoriaDatabaseSearch(
            self.db, core_data_objects.HistoriaRecord.machine_type)
        self.database_setup(withTables=True)

        hr = core_data_objects.HistoriaRecord(self.db)
        hr2 = core_data_objects.HistoriaRecord(self.db)
        hr.value = value1
        hr.save()
        hr2.value = value2
        hr2.save()

        # Get me the second record
        searcher.add_field('id')
        searcher.add_field('value')
        searcher.add_condition('value', value2)

        results = searcher.execute_search()

        self.assertIsNotNone(results, "None returned from valid search")
        self.assertEqual(len(results), 1, "Wrong number of results")
        self.assertEqual(results[0]['id'], hr2.id, "Wrong ID value returned")
        self.assertEqual(results[0]['value'], value2, "Wrong value found")

        searcher = core_data_objects.HistoriaDatabaseSearch(
            self.db, core_data_objects.HistoriaRecord.machine_type)
        searcher.add_field('id')
        searcher.add_field('value')
        searcher.add_condition('id', '0', ">")
        results = searcher.execute_search()
        self.assertIsNotNone(results, "None returned from valid search")
        self.assertEqual(len(results), 2, "Wrong number of results")

        searcher = core_data_objects.HistoriaDatabaseSearch(
            self.db, core_data_objects.HistoriaRecord.machine_type)
        searcher.add_field('id')
        searcher.add_field('value')
        searcher.add_condition('id', '1')
        searcher.add_condition('id', '2', '=', 'OR')
        results = searcher.execute_search()
        self.assertIsNotNone(results, "None returned from valid search")
        self.assertEqual(
            len(results), 2,
            "Wrong number of results sql: {0} values: {1}".format(
                searcher._generate_sql()[0],
                searcher._generate_sql()[1]))

        searcher = core_data_objects.HistoriaDatabaseSearch(
            self.db, core_data_objects.HistoriaRecord.machine_type)
        searcher.add_field('id')
        searcher.add_field('value')
        searcher.add_expression('COUNT(`id`)')
        searcher.add_condition('id', '0', ">")
        results = searcher.execute_search()
        self.assertIsNotNone(results, "None returned from valid search")
        self.assertEqual(len(results), 1, "Wrong number of results")
        self.assertIsNotNone(results[0]['expression_2'],
                             "Expression didn't return")