@@ -1924,5 +1924,70 @@ def wait():
19241924 self .assertEqual (proc .returncode , 0 )
19251925
19261926
1927+ class RowTests (unittest .TestCase ):
1928+
1929+ def setUp (self ):
1930+ self .cx = sqlite .connect (":memory:" )
1931+ self .cx .row_factory = sqlite .Row
1932+
1933+ def tearDown (self ):
1934+ self .cx .close ()
1935+
1936+ def test_row_keys (self ):
1937+ cu = self .cx .execute ("SELECT 1 as first, 2 as second" )
1938+ row = cu .fetchone ()
1939+ self .assertEqual (row .keys (), ["first" , "second" ])
1940+
1941+ def test_row_length (self ):
1942+ cu = self .cx .execute ("SELECT 1, 2, 3" )
1943+ row = cu .fetchone ()
1944+ self .assertEqual (len (row ), 3 )
1945+
1946+ def test_row_getitem (self ):
1947+ cu = self .cx .execute ("SELECT 1 as a, 2 as b" )
1948+ row = cu .fetchone ()
1949+ self .assertEqual (row [0 ], 1 )
1950+ self .assertEqual (row [1 ], 2 )
1951+ self .assertEqual (row ["a" ], 1 )
1952+ self .assertEqual (row ["b" ], 2 )
1953+ for key in "nokey" , 4 , 1.2 :
1954+ with self .subTest (key = key ):
1955+ with self .assertRaises (IndexError ):
1956+ row [key ]
1957+
1958+ def test_row_equality (self ):
1959+ c1 = self .cx .execute ("SELECT 1 as a" )
1960+ r1 = c1 .fetchone ()
1961+
1962+ c2 = self .cx .execute ("SELECT 1 as a" )
1963+ r2 = c2 .fetchone ()
1964+
1965+ self .assertIsNot (r1 , r2 )
1966+ self .assertEqual (r1 , r2 )
1967+
1968+ c3 = self .cx .execute ("SELECT 1 as b" )
1969+ r3 = c3 .fetchone ()
1970+
1971+ self .assertNotEqual (r1 , r3 )
1972+
1973+ def test_row_no_description (self ):
1974+ cu = self .cx .cursor ()
1975+ self .assertIsNone (cu .description )
1976+
1977+ row = sqlite .Row (cu , ())
1978+ self .assertEqual (row .keys (), [])
1979+ with self .assertRaisesRegex (IndexError , "nokey" ):
1980+ row ["nokey" ]
1981+
1982+ def test_row_is_a_sequence (self ):
1983+ from collections .abc import Sequence
1984+
1985+ cu = self .cx .execute ("SELECT 1" )
1986+ row = cu .fetchone ()
1987+
1988+ self .assertIsSubclass (sqlite .Row , Sequence )
1989+ self .assertIsInstance (row , Sequence )
1990+
1991+
19271992if __name__ == "__main__" :
19281993 unittest .main ()
0 commit comments