From 7aefe01af72de60e4cadd223fdec31a406c51325 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Mon, 6 Aug 2012 17:03:43 -0400 Subject: [PATCH] Move results filtering to db. This patch moves exact filter and regex filter matching on the listing of instances to the database query. This is important so that we can eventually do all pagination through the database. If the db backend does not support regex matching then the SQL 'LIKE' operator will be used instead. Change-Id: If9dd644066787f0f3667992f23e5c33b1dc90286 --- nova/tests/test_db_api.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py index b2b1cf9e..89b6480d 100644 --- a/nova/tests/test_db_api.py +++ b/nova/tests/test_db_api.py @@ -51,6 +51,34 @@ class DbApiTestCase(test.TestCase): result = db.instance_get_all_by_filters(self.context, {}) self.assertEqual(2, len(result)) + def test_instance_get_all_by_filters_regex(self): + self.create_instances_with_args(display_name='test1') + self.create_instances_with_args(display_name='teeeest2') + self.create_instances_with_args(display_name='diff') + result = db.instance_get_all_by_filters(self.context, + {'display_name': 't.*st.'}) + self.assertEqual(2, len(result)) + + def test_instance_get_all_by_filters_regex_unsupported_db(self): + """Ensure that the 'LIKE' operator is used for unsupported dbs.""" + self.flags(sql_connection="notdb://") + self.create_instances_with_args(display_name='test1') + self.create_instances_with_args(display_name='test.*') + self.create_instances_with_args(display_name='diff') + result = db.instance_get_all_by_filters(self.context, + {'display_name': 'test.*'}) + self.assertEqual(1, len(result)) + result = db.instance_get_all_by_filters(self.context, + {'display_name': '%test%'}) + self.assertEqual(2, len(result)) + + def test_instance_get_all_by_filters_metadata(self): + self.create_instances_with_args(metadata={'foo': 'bar'}) + self.create_instances_with_args() + result = db.instance_get_all_by_filters(self.context, + {'metadata': {'foo': 'bar'}}) + self.assertEqual(1, len(result)) + def test_instance_get_all_by_filters_unicode_value(self): self.create_instances_with_args(display_name=u'test♥') result = db.instance_get_all_by_filters(self.context,