/usr/lib/python3/dist-packages/pydap/tests/test_lib.py is in python3-pydap 3.2.2+ds1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | """Test the basic DAP functions."""
import numpy as np
from six import MAXSIZE
from pydap.model import (DatasetType, BaseType,
StructureType)
from pydap.exceptions import ConstraintExpressionError
from pydap.lib import (quote, encode, fix_slice, combine_slices, hyperslab,
walk, fix_shorthand, get_var)
import unittest
class TestQuote(unittest.TestCase):
"""Test quoting.
According to the DAP 2 specification a variable name MUST contain only
upper or lower case letters, numbers, or characters from the set
_ ! ~ * ' - "
All other characters must be escaped. This includes the period, which is
normally not quoted by ``urllib.quote``.
"""
def test_quoting(self):
"""Test a simple quoting."""
self.assertEqual(quote("White space"), "White%20space")
def test_quoting_period(self):
"""Test if period is also quoted."""
self.assertEqual(quote("Period."), "Period%2E")
class TestEncode(unittest.TestCase):
"""Test encoding.
According to the DAP 2 specification, numbers must be encoded using the C
notation "%.6g". Other objects are encoded as escaped strings.
"""
def test_integer(self):
"""Test integer encoding."""
self.assertEqual(encode(1), "1")
def test_float(self):
"""Test floating encoding."""
self.assertEqual(encode(np.pi), "3.14159")
def test_string(self):
"""Test string encoding."""
self.assertEqual(encode("test"), '"test"')
def test_string_with_quotation(self):
"""Test encoding a string with a quotation mark."""
self.assertEqual(encode('this is a "test"'), '"this is a \"test\""')
def test_unicode(self):
"""Unicode objects are encoded just like strings."""
self.assertEqual(encode(u"test"), '"test"')
def test_obj(self):
"""Other objects are encoded according to their ``repr``."""
self.assertEqual(encode({}), '"{}"')
class TestFixSlice(unittest.TestCase):
"""Test the ``fix_slice`` function."""
def test_not_tuple(self):
"""Non tuples should be converted and handled correctly."""
x = np.arange(10)
slice1 = 0
slice2 = fix_slice(slice1, x.shape)
# ``fix_slice`` will convert to a tuple
self.assertEqual(slice2, (0,))
# assert that the slice is equivalent to the original
np.testing.assert_array_equal(x[slice1], x[slice2])
def test_ellipsis(self):
"""Expand Ellipsis to occupy the missing dimensions."""
x = np.arange(6).reshape(2, 3, 1)
slice1 = Ellipsis, 0
slice2 = fix_slice(slice1, x.shape)
# an Ellipsis is expanded to slice(None)
self.assertEqual(
slice2,
((slice(0, 2, 1), slice(0, 3, 1), 0)))
np.testing.assert_array_equal(x[slice1], x[slice2])
def test_negative_int(self):
"""Negative values are converted to positive."""
x = np.arange(10)
slice1 = -5
slice2 = fix_slice(slice1, x.shape)
self.assertEqual(slice2, (5,))
np.testing.assert_array_equal(x[slice1], x[slice2])
def test_negative_start(self):
"""Test for slices with a negative start."""
x = np.arange(10)
slice1 = slice(-8, 8)
slice2 = fix_slice(slice1, x.shape)
self.assertEqual(slice2, (slice(2, 8, 1),))
np.testing.assert_array_equal(x[slice1], x[slice2])
def test_negative_stop(self):
"""Test for slices with a negative stop."""
x = np.arange(10)
slice1 = slice(2, -2)
slice2 = fix_slice(slice1, x.shape)
self.assertEqual(slice2, (slice(2, 8, 1),))
np.testing.assert_array_equal(x[slice1], x[slice2])
class TestCombineSlices(unittest.TestCase):
"""Test the ``combine_slices`` function."""
def test_not_tuple(self):
"""The function fails when one of the slices is not a tuple."""
slice1 = 0
slice2 = (0,)
with self.assertRaises(TypeError):
combine_slices(slice1, slice2)
with self.assertRaises(TypeError):
combine_slices(slice2, slice1)
def test_integer(self):
"""Test slices that are just integers."""
slice1 = (0,)
slice2 = (1,)
combined = combine_slices(slice1, slice2)
self.assertEqual(combined, (slice(1, 1, 1),))
def test_stops_none(self):
"""Test when both of the slices have ``None`` for stop."""
x = np.arange(10)
slice1 = (slice(0, None),)
slice2 = (slice(5, None),)
combined = combine_slices(slice1, slice2)
self.assertEqual(combined, (slice(5, None, 1),))
np.testing.assert_array_equal(x[combined], x[slice1][slice2])
def test_first_stop_none(self):
"""Test when the first slice has ``None`` for stop."""
x = np.arange(10)
slice1 = (slice(5, None),)
slice2 = (slice(0, 8),)
combined = combine_slices(slice1, slice2)
self.assertEqual(combined, (slice(5, 13, 1),))
np.testing.assert_array_equal(x[combined], x[slice1][slice2])
def test_second_stop_none(self):
"""Test when the second slice has ``None`` for stop."""
x = np.arange(10)
slice1 = (slice(0, 8),)
slice2 = (slice(5, None),)
combined = combine_slices(slice1, slice2)
self.assertEqual(combined, (slice(5, 8, 1),))
np.testing.assert_array_equal(x[combined], x[slice1][slice2])
def test_all_values(self):
"""Test when start and stop are all integers."""
x = np.arange(20)
slice1 = (slice(0, 8),)
slice2 = (slice(5, 6),)
combined = combine_slices(slice1, slice2)
self.assertEqual(combined, (slice(5, 6, 1),))
np.testing.assert_array_equal(x[combined], x[slice1][slice2])
class TestHyperslab(unittest.TestCase):
"""Test hyperslab generation from Python slices."""
def test_no_tuple(self):
"""Test that slices that are not tuples work."""
slice_ = slice(0)
self.assertEqual(hyperslab(slice_), "[0:1:%d]" % (MAXSIZE-1))
def test_remove(self):
"""Test that excess slices are removed."""
slice_ = (slice(0), slice(None))
self.assertEqual(hyperslab(slice_), "[0:1:%d]" % (MAXSIZE-1))
def test_ndimensional(self):
"""Test n-dimensions slices."""
slice_ = (slice(1, 10, 1), slice(2, 10, 2))
self.assertEqual(hyperslab(slice_), "[1:1:9][2:2:9]")
class TestWalk(unittest.TestCase):
"""Test the ``walk`` function to iterate over a dataset."""
def setUp(self):
"""Create a basic dataset."""
self.dataset = DatasetType("a")
self.dataset["b"] = BaseType("b")
self.dataset["c"] = StructureType("c")
def test_walk(self):
"""Test that all variables are yielded."""
self.assertEqual(
list(walk(self.dataset)),
[self.dataset, self.dataset.b, self.dataset.c])
def test_walk_type(self):
"""Test the filtering of variables yielded."""
self.assertEqual(list(walk(self.dataset, BaseType)), [self.dataset.b])
class TestFixShorthand(unittest.TestCase):
"""Test the ``fix_shorthand`` function."""
def test_fix_projection(self):
"""Test a dataset that can use the shorthand notation."""
dataset = DatasetType("a")
dataset["b"] = StructureType("b")
dataset["b"]["c"] = BaseType("c")
projection = [[("c", ())]]
self.assertEqual(
fix_shorthand(projection, dataset),
[[('b', ()), ('c', ())]])
def test_conflict(self):
"""Test a dataset with conflicting short names."""
dataset = DatasetType("a")
dataset["b"] = StructureType("b")
dataset["b"]["c"] = BaseType("c")
dataset["d"] = StructureType("d")
dataset["d"]["c"] = BaseType("c")
projection = [[("c", ())]]
with self.assertRaises(ConstraintExpressionError):
fix_shorthand(projection, dataset)
class TestGetVar(unittest.TestCase):
"""Test the ``get_var`` function."""
def test_get_var(self):
"""Test that the id is returned properly."""
dataset = DatasetType("a")
dataset["b"] = StructureType("b")
dataset["b"]["c"] = BaseType("c")
self.assertEqual(get_var(dataset, 'b.c'), dataset['b']['c'])
|