diff --git a/tests/connectors/core/test_records.py b/tests/connectors/core/test_records.py index b4e23832660ef511cabfc3f8927af5cd73de43f8..d572fde70565ccdeec7147671bf9f23a77dbd74a 100644 --- a/tests/connectors/core/test_records.py +++ b/tests/connectors/core/test_records.py @@ -780,60 +780,60 @@ class CoreRecordsTestMixin: self.assertIn(row_4, results) self.assertIn(row_5, results) - # def test__select__aggregates(self): - # """""" - # table_name = 'test_queries__select__aggregate' - # # Verify table exists. - # try: - # self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__aggregates)) - # except self.connector.errors.table_already_exists: - # # Table already exists, as we want. - # pass - # - # # Prepopulate with a few records. - # self.connector.records.insert_many( - # table_name, - # [ - # ('test one', 10, False), - # ('test two', 12, False), - # ('test three', 5, False), - # ('test four', 3, False), - # ('test five', 22, False), - # ], - # columns_clause=('test_str, test_int, test_bool'), - # ) - # - # with self.subTest('SELECT with AVG aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'AVG(test_int)') - # - # # Verify return aggregate result. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], Decimal('10.4')) - # - # with self.subTest('SELECT with MAX aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'MAX(test_int)') - # - # # Verify return aggregate result. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], 22) - # - # with self.subTest('SELECT with MIN aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'MIN(test_int)') - # - # # Verify return aggregate result. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], 3) - # - # with self.subTest('SELECT with SUM aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'SUM(test_int)') - # - # # Verify return aggregate result. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], 52) + def test__select__aggregates(self): + """""" + table_name = 'test_queries__select__aggregate' + # Verify table exists. + try: + self.connector.query.execute('CREATE TABLE {0}{1};'.format(table_name, self._columns_clause__aggregates)) + except self.connector.errors.table_already_exists: + # Table already exists, as we want. + pass + + # Prepopulate with a few records. + self.connector.records.insert_many( + table_name, + [ + ('test one', 10, False), + ('test two', 12, False), + ('test three', 5, False), + ('test four', 3, False), + ('test five', 22, False), + ], + columns_clause=('test_str, test_int, test_bool'), + ) + + with self.subTest('SELECT with AVG aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'AVG(test_int)') + + # Verify return aggregate result. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], Decimal('10.4')) + + with self.subTest('SELECT with MAX aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'MAX(test_int)') + + # Verify return aggregate result. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], 22) + + with self.subTest('SELECT with MIN aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'MIN(test_int)') + + # Verify return aggregate result. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], 3) + + with self.subTest('SELECT with SUM aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'SUM(test_int)') + + # Verify return aggregate result. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], 52) def test__insert__basic__success(self): """ diff --git a/tests/connectors/postgresql/test_records.py b/tests/connectors/postgresql/test_records.py index 2dc13d1ceb70ddd20770024a975573d614cc606d..0027928ec3ba03914ea8f859e4a5b11d262779eb 100644 --- a/tests/connectors/postgresql/test_records.py +++ b/tests/connectors/postgresql/test_records.py @@ -64,105 +64,105 @@ class TestPostgresqlRecords(TestPostgresqlDatabaseParent, CoreRecordsTestMixin): with self.assertRaises(self.connector.errors.table_already_exists): self.connector.query.execute('CREATE TABLE {0} {1};'.format(table_name, self._columns_clause__basic)) - # def test__select__aggregates(self): - # """""" - # table_name = 'test_queries__select__aggregate' - # - # # Run parent tests. - # super().test__select__aggregates() - # - # # Tests that require slightly different syntax in different database types. - # with self.subTest('SELECT with BIT_OR aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'BIT_OR(test_bool::int)') - # - # # Verify return aggregate result. - # # No records returned True, so should be False. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], False) - # - # # Upset a single record to be true, and test again. - # results = self.connector.records.update(table_name, 'test_bool = True', where_clause='WHERE id = 2') - # results = self.connector.records.select(table_name, 'BIT_OR(test_bool::int)') - # - # # Verify return aggregate result. - # # At least one record returned True so should be True. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], True) - # - # with self.subTest('SELECT with BIT_AND aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'BIT_AND(test_bool::int)') - # - # # Verify return aggregate result. - # # Not all records returned True, so should be False. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], False) - # - # # Update all records to be true, and test again. - # results = self.connector.records.update(table_name, 'test_bool = True', where_clause='') - # results = self.connector.records.select(table_name, 'bit_or(test_bool::int)') - # - # # Verify return aggregate result. - # # All records returned True so should be True. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], True) - # - # # Reset booleans to be False. - # self.connector.records.update(table_name, 'test_bool = False', where_clause='') - # - # with self.subTest('SELECT with STDDEV aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'STDDEV(test_int)') - # - # # Verify return aggregate result. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], Decimal('7.4363969770312827')) - # - # with self.subTest('SELECT with VARIANCE aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'VARIANCE(test_int)') - # - # # Verify return aggregate result. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], Decimal('55.3')) - # - # # Aggregate functions that don't exist outside of PostgreSQL. - # with self.subTest('SELECT with BOOL_OR aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'BOOL_OR(test_bool)') - # - # # Verify return aggregate result. - # # No records returned True, so should be False. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], False) - # - # # Upset a single record to be true, and test again. - # results = self.connector.records.update(table_name, 'test_bool = True', where_clause='WHERE id = 2') - # results = self.connector.records.select(table_name, 'BOOL_OR(test_bool)') - # - # # Verify return aggregate result. - # # At least one record returned True so should be True. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], True) - # - # with self.subTest('SELECT with BOOL_AND aggregation'): - # # Run test query. - # results = self.connector.records.select(table_name, 'BOOL_AND(test_bool)') - # - # # Verify return aggregate result. - # # Not all records returned True, so should be False. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], False) - # - # # Update all records to be true, and test again. - # self.connector.records.update(table_name, 'test_bool = True', where_clause='') - # results = self.connector.records.select(table_name, 'BOOL_or(test_bool)') - # - # # Verify return aggregate result. - # # All records returned True so should be True. - # self.assertEqual(len(results), 1) - # self.assertEqual(results[0][0], True) - # - # # Reset booleans to be False. - # self.connector.records.update(table_name, 'test_bool = False', where_clause='') + def test__select__aggregates(self): + """""" + table_name = 'test_queries__select__aggregate' + + # Run parent tests. + super().test__select__aggregates() + + # Tests that require slightly different syntax in different database types. + with self.subTest('SELECT with BIT_OR aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'BIT_OR(test_bool::int)') + + # Verify return aggregate result. + # No records returned True, so should be False. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], False) + + # Upset a single record to be true, and test again. + results = self.connector.records.update(table_name, 'test_bool = True', where_clause='WHERE id = 2') + results = self.connector.records.select(table_name, 'BIT_OR(test_bool::int)') + + # Verify return aggregate result. + # At least one record returned True so should be True. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], True) + + with self.subTest('SELECT with BIT_AND aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'BIT_AND(test_bool::int)') + + # Verify return aggregate result. + # Not all records returned True, so should be False. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], False) + + # Update all records to be true, and test again. + results = self.connector.records.update(table_name, 'test_bool = True', where_clause='') + results = self.connector.records.select(table_name, 'bit_or(test_bool::int)') + + # Verify return aggregate result. + # All records returned True so should be True. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], True) + + # Reset booleans to be False. + self.connector.records.update(table_name, 'test_bool = False', where_clause='') + + with self.subTest('SELECT with STDDEV aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'STDDEV(test_int)') + + # Verify return aggregate result. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], Decimal('7.4363969770312827')) + + with self.subTest('SELECT with VARIANCE aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'VARIANCE(test_int)') + + # Verify return aggregate result. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], Decimal('55.3')) + + # Aggregate functions that don't exist outside of PostgreSQL. + with self.subTest('SELECT with BOOL_OR aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'BOOL_OR(test_bool)') + + # Verify return aggregate result. + # No records returned True, so should be False. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], False) + + # Upset a single record to be true, and test again. + results = self.connector.records.update(table_name, 'test_bool = True', where_clause='WHERE id = 2') + results = self.connector.records.select(table_name, 'BOOL_OR(test_bool)') + + # Verify return aggregate result. + # At least one record returned True so should be True. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], True) + + with self.subTest('SELECT with BOOL_AND aggregation'): + # Run test query. + results = self.connector.records.select(table_name, 'BOOL_AND(test_bool)') + + # Verify return aggregate result. + # Not all records returned True, so should be False. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], False) + + # Update all records to be true, and test again. + self.connector.records.update(table_name, 'test_bool = True', where_clause='') + results = self.connector.records.select(table_name, 'BOOL_or(test_bool)') + + # Verify return aggregate result. + # All records returned True so should be True. + self.assertEqual(len(results), 1) + self.assertEqual(results[0][0], True) + + # Reset booleans to be False. + self.connector.records.update(table_name, 'test_bool = False', where_clause='')