summaryrefslogtreecommitdiff
path: root/tests/unit/test_utils.py
diff options
context:
space:
mode:
authorChristian Schwede <christian.schwede@enovance.com>2014-02-25 21:42:17 +0000
committerChmouel Boudjnah <chmouel@enovance.com>2014-05-05 11:53:37 +0200
commiteb94ac076dec7e08f4e6c4c55d7c6f60b1b1cbfd (patch)
tree07a9ccd6ff5c1382e13df5259940c0b2cd4a9c03 /tests/unit/test_utils.py
parent9ff3bad4e22fbd561d9253af2b6e38580124b450 (diff)
downloadpython-swiftclient-eb94ac076dec7e08f4e6c4c55d7c6f60b1b1cbfd.tar.gz
Add functional tests for python-swiftclient
Coverage for swiftclient.client is 71% with these tests. Unit tests have been moved into another subdirectory to separate them from functional tests. Change-Id: Ib8c4d78f7169cee893f82906f6388a5b06c45602
Diffstat (limited to 'tests/unit/test_utils.py')
-rw-r--r--tests/unit/test_utils.py160
1 files changed, 160 insertions, 0 deletions
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
new file mode 100644
index 0000000..d9d74c5
--- /dev/null
+++ b/tests/unit/test_utils.py
@@ -0,0 +1,160 @@
+# Copyright (c) 2010-2013 OpenStack, LLC.
+#
+# 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 testtools
+
+import six
+import tempfile
+
+from swiftclient import utils as u
+
+
+class TestConfigTrueValue(testtools.TestCase):
+
+ def test_TRUE_VALUES(self):
+ for v in u.TRUE_VALUES:
+ self.assertEqual(v, v.lower())
+
+ def test_config_true_value(self):
+ orig_trues = u.TRUE_VALUES
+ try:
+ u.TRUE_VALUES = 'hello world'.split()
+ for val in 'hello world HELLO WORLD'.split():
+ self.assertTrue(u.config_true_value(val) is True)
+ self.assertTrue(u.config_true_value(True) is True)
+ self.assertTrue(u.config_true_value('foo') is False)
+ self.assertTrue(u.config_true_value(False) is False)
+ finally:
+ u.TRUE_VALUES = orig_trues
+
+
+class TestPrtBytes(testtools.TestCase):
+
+ def test_zero_bytes(self):
+ bytes_ = 0
+ raw = '0'
+ human = '0'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_one_byte(self):
+ bytes_ = 1
+ raw = '1'
+ human = '1'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_less_than_one_k(self):
+ bytes_ = (2 ** 10) - 1
+ raw = '1023'
+ human = '1023'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_one_k(self):
+ bytes_ = 2 ** 10
+ raw = '1024'
+ human = '1.0K'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_a_decimal_k(self):
+ bytes_ = (3 * 2 ** 10) + 512
+ raw = '3584'
+ human = '3.5K'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_a_bit_less_than_one_meg(self):
+ bytes_ = (2 ** 20) - (2 ** 10)
+ raw = '1047552'
+ human = '1023K'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_just_a_hair_less_than_one_meg(self):
+ bytes_ = (2 ** 20) - (2 ** 10) + 1
+ raw = '1047553'
+ human = '1.0M'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_one_meg(self):
+ bytes_ = 2 ** 20
+ raw = '1048576'
+ human = '1.0M'
+ self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_ten_meg(self):
+ bytes_ = 10 * 2 ** 20
+ human = '10M'
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_bit_less_than_ten_meg(self):
+ bytes_ = (10 * 2 ** 20) - (100 * 2 ** 10)
+ human = '9.9M'
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_just_a_hair_less_than_ten_meg(self):
+ bytes_ = (10 * 2 ** 20) - 1
+ human = '10.0M'
+ self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
+
+ def test_a_yotta(self):
+ bytes_ = 42 * 2 ** 80
+ self.assertEqual('42Y', u.prt_bytes(bytes_, True).lstrip())
+
+ def test_overflow(self):
+ bytes_ = 2 ** 90
+ self.assertEqual('1024Y', u.prt_bytes(bytes_, True).lstrip())
+
+
+class TestLengthWrapper(testtools.TestCase):
+
+ def test_stringio(self):
+ contents = six.StringIO('a' * 100)
+ data = u.LengthWrapper(contents, 42)
+ self.assertEqual(42, len(data))
+ read_data = ''.join(iter(data.read, ''))
+ self.assertEqual(42, len(read_data))
+ self.assertEqual('a' * 42, read_data)
+
+ def test_tempfile(self):
+ with tempfile.NamedTemporaryFile(mode='w') as f:
+ f.write('a' * 100)
+ f.flush()
+ contents = open(f.name)
+ data = u.LengthWrapper(contents, 42)
+ self.assertEqual(42, len(data))
+ read_data = ''.join(iter(data.read, ''))
+ self.assertEqual(42, len(read_data))
+ self.assertEqual('a' * 42, read_data)
+
+ def test_segmented_file(self):
+ with tempfile.NamedTemporaryFile(mode='w') as f:
+ segment_length = 1024
+ segments = ('a', 'b', 'c', 'd')
+ for c in segments:
+ f.write(c * segment_length)
+ f.flush()
+ for i, c in enumerate(segments):
+ contents = open(f.name)
+ contents.seek(i * segment_length)
+ data = u.LengthWrapper(contents, segment_length)
+ self.assertEqual(segment_length, len(data))
+ read_data = ''.join(iter(data.read, ''))
+ self.assertEqual(segment_length, len(read_data))
+ self.assertEqual(c * segment_length, read_data)