Skip to content

Commit ef455ae

Browse files
committed
tests: add _RowView and _row_factory unit tests
Cover __getitem__, get(), __contains__, __repr__, shared index map, read-only enforcement, empty input, single-column, and multi-row scenarios.
1 parent 0a9f1d6 commit ef455ae

2 files changed

Lines changed: 94 additions & 8 deletions

File tree

cassandra/metadata.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class _RowView(Mapping):
5858
``values``, ``items``, and ``__contains__`` for free.
5959
"""
6060

61-
__slots__ = ("_row", "_index_map")
61+
__slots__ = ("_index_map", "_row")
6262

6363
def __init__(self, row, index_map):
6464
if len(row) < max(index_map.values(), default=-1) + 1:
@@ -78,12 +78,6 @@ def __iter__(self):
7878
def __len__(self):
7979
return len(self._index_map)
8080

81-
def values(self):
82-
return (self._row[i] for i in self._index_map.values())
83-
84-
def items(self):
85-
return ((k, self._row[i]) for k, i in self._index_map.items())
86-
8781
def get(self, key, default=None):
8882
idx = self._index_map.get(key)
8983
if idx is not None:

tests/unit/test_metadata.py

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
_UnknownStrategy, ColumnMetadata, TableMetadata,
3333
IndexMetadata, Function, Aggregate,
3434
Metadata, TokenMap, ReplicationFactor,
35-
SchemaParserDSE68)
35+
SchemaParserDSE68,
36+
_RowView, _row_factory)
3637
from cassandra.policies import SimpleConvictionPolicy
3738
from cassandra.pool import Host
3839
from cassandra.protocol import QueryMessage
@@ -846,3 +847,94 @@ def test_strip_frozen(self):
846847
for argument, expected_result in argument_to_expected_results:
847848
result = strip_frozen(argument)
848849
assert result == expected_result, "strip_frozen() arg: {}".format(argument)
850+
851+
class RowViewTest(unittest.TestCase):
852+
"""Tests for the internal _RowView and _row_factory helpers."""
853+
854+
def test_getitem(self):
855+
rv = _RowView(("a_val", "b_val"), {"a": 0, "b": 1})
856+
self.assertEqual(rv["a"], "a_val")
857+
self.assertEqual(rv["b"], "b_val")
858+
859+
def test_getitem_missing_key(self):
860+
rv = _RowView(("a_val",), {"a": 0})
861+
with self.assertRaises(KeyError):
862+
rv["missing"]
863+
864+
def test_get_present(self):
865+
rv = _RowView(("a_val", "b_val"), {"a": 0, "b": 1})
866+
self.assertEqual(rv.get("a"), "a_val")
867+
self.assertEqual(rv.get("b"), "b_val")
868+
869+
def test_get_missing_returns_default(self):
870+
rv = _RowView(("a_val",), {"a": 0})
871+
self.assertIsNone(rv.get("missing"))
872+
self.assertEqual(rv.get("missing", 42), 42)
873+
874+
def test_contains(self):
875+
rv = _RowView(("a_val",), {"a": 0})
876+
self.assertIn("a", rv)
877+
self.assertNotIn("b", rv)
878+
879+
def test_repr(self):
880+
rv = _RowView(("a_val", "b_val"), {"a": 0, "b": 1})
881+
r = repr(rv)
882+
self.assertIn("'a'", r)
883+
self.assertIn("'a_val'", r)
884+
885+
def test_shared_index_map(self):
886+
"""All _RowView objects from the same _row_factory call share one index map."""
887+
rows = _row_factory(["x", "y"], [("x1", "y1"), ("x2", "y2")])
888+
self.assertIs(rows[0]._index_map, rows[1]._index_map)
889+
890+
def test_read_only(self):
891+
"""_RowView must not allow item assignment or deletion."""
892+
rv = _RowView(("val",), {"col": 0})
893+
with self.assertRaises(TypeError):
894+
rv["col"] = "new"
895+
with self.assertRaises(TypeError):
896+
del rv["col"]
897+
898+
def test_row_factory_empty(self):
899+
result = _row_factory(["a", "b"], [])
900+
self.assertEqual(result, [])
901+
902+
def test_row_factory_single_column(self):
903+
rows = _row_factory(["only"], [("v1",), ("v2",)])
904+
self.assertEqual(rows[0]["only"], "v1")
905+
self.assertEqual(rows[1]["only"], "v2")
906+
907+
def test_row_factory_values(self):
908+
rows = _row_factory(["id", "name"], [(1, "alice"), (2, "bob")])
909+
self.assertEqual(rows[0]["id"], 1)
910+
self.assertEqual(rows[0]["name"], "alice")
911+
self.assertEqual(rows[1]["id"], 2)
912+
self.assertEqual(rows[1]["name"], "bob")
913+
914+
def test_len(self):
915+
rv = _RowView(("a", "b", "c"), {"x": 0, "y": 1, "z": 2})
916+
self.assertEqual(len(rv), 3)
917+
rv2 = _RowView((), {})
918+
self.assertEqual(len(rv2), 0)
919+
920+
def test_keys(self):
921+
rv = _RowView(("a", "b"), {"x": 0, "y": 1})
922+
self.assertEqual(set(rv.keys()), {"x", "y"})
923+
924+
def test_values(self):
925+
rv = _RowView(("a", "b"), {"x": 0, "y": 1})
926+
self.assertEqual(list(rv.values()), ["a", "b"])
927+
928+
def test_items(self):
929+
rv = _RowView(("a", "b"), {"x": 0, "y": 1})
930+
self.assertEqual(set(rv.items()), {("x", "a"), ("y", "b")})
931+
932+
def test_init_raises_on_short_row(self):
933+
with self.assertRaises(ValueError):
934+
_RowView(("val",), {"a": 0, "b": 1})
935+
936+
def test_init_accepts_exact_row(self):
937+
_RowView(("a", "b"), {"a": 0, "b": 1})
938+
939+
def test_init_accepts_empty(self):
940+
_RowView((), {})

0 commit comments

Comments
 (0)