Set None explicitly to filter options.

When user set None in query string value
'None' was set instead of None.

Note: This fix is for mongodb only. I will
add another patch for other database.

Partial-Bug: #1388680
Change-Id: I2f42013b9c8fb9a3af9d6366f10dd8bb3906d73a
This commit is contained in:
ananya23d 2015-12-22 10:47:36 +05:30 committed by Ananya Chatterjee
parent a7f41eeeb6
commit bbaabaf887
2 changed files with 75 additions and 3 deletions

View File

@ -66,11 +66,17 @@ class Connection(base.Connection):
metaquery = pymongo_utils.improve_keys(metaquery, metaquery=True) or {}
q = {}
if user is not None:
if user == 'None':
q['user_id'] = None
elif user is not None:
q['user_id'] = user
if project is not None:
if project == 'None':
q['project_id'] = None
elif project is not None:
q['project_id'] = project
if resource is not None:
if resource == 'None':
q['_id'] = None
elif resource is not None:
q['_id'] = resource
if source is not None:
q['source'] = source

View File

@ -78,3 +78,69 @@ class CompatibilityTest(test_storage_scenarios.DBTestBase):
def test_counter_unit(self):
meters = list(self.conn.get_meters())
self.assertEqual(1, len(meters))
# TODO(ananya) same test should be done for other databse
@tests_db.run_with('mongodb', 'db2')
class FilterQueryTestForMeters(test_storage_scenarios.DBTestBase):
def prepare_data(self):
def old_record_metering_data(self, data):
received_timestamp = datetime.datetime.utcnow()
self.db.resource.update(
{'_id': data['resource_id']},
{'$set': {'project_id': data['project_id'],
'user_id': data['user_id'],
# Current metadata being used and when it was
# last updated.
'timestamp': data['timestamp'],
'received_timestamp': received_timestamp,
'metadata': data['resource_metadata'],
'source': data['source'],
},
'$addToSet': {'meter': {'counter_name': data['counter_name'],
'counter_type': data['counter_type'],
},
},
},
upsert=True,
)
record = copy.copy(data)
self.db.meter.insert(record)
# Stubout with the old version DB schema, the one w/o 'counter_unit'
with mock.patch.object(self.conn, 'record_metering_data',
side_effect=old_record_metering_data):
self.counters = []
c = sample.Sample(
'volume.size',
'gauge',
'GiB',
5,
None,
None,
None,
timestamp=datetime.datetime(2012, 9, 25, 10, 30),
resource_metadata={'display_name': 'test-volume',
'tag': 'self.counter',
},
source='test',
)
self.counters.append(c)
msg = utils.meter_message_from_counter(
c,
secret='not-so-secret')
self.conn.record_metering_data(self.conn, msg)
def test_get_meters_by_user(self):
meters = list(self.conn.get_meters(user='None'))
self.assertEqual(1, len(meters))
def test_get_meters_by_resource(self):
meters = list(self.conn.get_meters(resource='None'))
self.assertEqual(1, len(meters))
def test_get_meters_by_project(self):
meters = list(self.conn.get_meters(project='None'))
self.assertEqual(1, len(meters))