From e848adae5478ef6917b12f7fc05a9ba4956e7cb9 Mon Sep 17 00:00:00 2001 From: Amir Sadoughi Date: Tue, 26 Mar 2013 17:54:57 -0500 Subject: [PATCH] Implemented GET on /ports with ip_address_id filter --- quark/db/api.py | 3 +++ quark/tests/test_db_api.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 quark/tests/test_db_api.py diff --git a/quark/db/api.py b/quark/db/api.py index dd220c3..cdb5ed6 100644 --- a/quark/db/api.py +++ b/quark/db/api.py @@ -115,6 +115,9 @@ def port_find(context, **filters): query = context.session.query(models.Port).\ options(orm.joinedload(models.Port.ip_addresses)) query = _model_query(context, models.Port, filters, query=query) + if filters.get("ip_address_id"): + query = query.filter(models.Port.ip_addresses.any( + models.IPAddress.id.in_(filters["ip_address_id"]))) return query diff --git a/quark/tests/test_db_api.py b/quark/tests/test_db_api.py new file mode 100644 index 0000000..6267e40 --- /dev/null +++ b/quark/tests/test_db_api.py @@ -0,0 +1,37 @@ +# Copyright 2013 Openstack Foundation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +from oslo.config import cfg +from quantum import context +from quantum.db import api as quantum_db_api + +from quark.db import api as db_api + +from quark.tests import test_base + + +class TestDBAPI(test_base.TestBase): + def setUp(self): + cfg.CONF.set_override('sql_connection', 'sqlite://', 'DATABASE') + quantum_db_api.configure_db() + self.context = context.get_admin_context() + + def test_port_find_ip_address_id(self): + self.context.session.query = mock.Mock() + db_api.port_find(self.context, ip_address_id="fake") + query_obj = self.context.session.query.return_value + filter_fn = query_obj.options.return_value.filter + self.assertEqual(filter_fn.call_count, 1)