From 21f926586b92e320001a6c42ed75ad283162d9d0 Mon Sep 17 00:00:00 2001 From: ZhiQiang Fan Date: Tue, 28 Oct 2014 00:09:42 +0800 Subject: [PATCH] Add __repr__ method for sample.Sample sample.Sample has no string format method, which will cause useless message when print log. Change-Id: I8b9863a4ac6ee3373e18d36681fbfc4c89732111 Closes-Bug: #1386187 --- ceilometer/sample.py | 4 ++++ ceilometer/tests/test_sample.py | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 ceilometer/tests/test_sample.py diff --git a/ceilometer/sample.py b/ceilometer/sample.py index a9cd607f51..afc2b30f9f 100644 --- a/ceilometer/sample.py +++ b/ceilometer/sample.py @@ -73,6 +73,10 @@ class Sample(object): def as_dict(self): return copy.copy(self.__dict__) + def __repr__(self): + return '' % ( + self.name, self.volume, self.resource_id, self.timestamp) + @classmethod def from_notification(cls, name, type, volume, unit, user_id, project_id, resource_id, diff --git a/ceilometer/tests/test_sample.py b/ceilometer/tests/test_sample.py new file mode 100644 index 0000000000..72639ab408 --- /dev/null +++ b/ceilometer/tests/test_sample.py @@ -0,0 +1,38 @@ +# 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. + +"""Tests for ceilometer/sample.py""" + +import datetime + +from ceilometer import sample +from ceilometer.tests import base + + +class TestSample(base.BaseTestCase): + SAMPLE = sample.Sample( + name='cpu', + type=sample.TYPE_CUMULATIVE, + unit='ns', + volume='1234567', + user_id='56c5692032f34041900342503fecab30', + project_id='ac9494df2d9d4e709bac378cceabaf23', + resource_id='1ca738a1-c49c-4401-8346-5c60ebdb03f4', + timestamp=datetime.datetime(2014, 10, 29, 14, 12, 15, 485877), + resource_metadata={} + ) + + def test_sample_string_format(self): + expected = ('') + self.assertEqual(expected, str(self.SAMPLE))