Commit b12e319e authored by Kirill Smelkov's avatar Kirill Smelkov

*/tests: Use pytest.raises in modern way

Instead of

	raises(Exception, 'code')

do

	with raises(Exception):
		code

This removes lots of warnings, similar to below example:

	bigfile/tests/test_basic.py::test_basic
	  /home/kirr/src/wendelin/wendelin.core/bigfile/tests/test_basic.py:79: PytestDeprecationWarning: raises(..., 'code(as_a_string)') is deprecated, use the context manager form or use `exec()` directly

	  See https://docs.pytest.org/en/latest/deprecations.html#raises-warns-exec
	    raises(ROAttributeError, "f.blksize = 1") # RO attribute
parent 7001a495
# Wendeling.core.bigarray | Tests for ZBigArray # Wendeling.core.bigarray | Tests for ZBigArray
# Copyright (C) 2014-2015 Nexedi SA and Contributors. # Copyright (C) 2014-2019 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -512,7 +512,8 @@ def test_zbigarray_vs_conflicts(): ...@@ -512,7 +512,8 @@ def test_zbigarray_vs_conflicts():
tm1.commit() tm1.commit()
# txn2 should raise ConflictError and stay at 11 state # txn2 should raise ConflictError and stay at 11 state
raises(ConflictError, 'tm2.commit()') with raises(ConflictError):
tm2.commit()
tm2.abort() tm2.abort()
assert a2[0:1] == [11] # re-read in conn2 XXX -> [0] == 11 after BigArray can assert a2[0:1] == [11] # re-read in conn2 XXX -> [0] == 11 after BigArray can
...@@ -570,7 +571,8 @@ def test_zbigarray_vs_conflicts_metadata(): ...@@ -570,7 +571,8 @@ def test_zbigarray_vs_conflicts_metadata():
tm1.commit() tm1.commit()
# txn2 should raise ConflictError and stay at 11 state # txn2 should raise ConflictError and stay at 11 state
raises(ConflictError, 'tm2.commit()') with raises(ConflictError):
tm2.commit()
tm2.abort() tm2.abort()
assert len(a2) == 11 # re-read in conn2 assert len(a2) == 11 # re-read in conn2
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Wendeling.core.bigarray | Basic tests # Wendeling.core.bigarray | Basic tests
# Copyright (C) 2014-2018 Nexedi SA and Contributors. # Copyright (C) 2014-2019 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -109,7 +109,8 @@ def test_bigarray_noobject(testbig): ...@@ -109,7 +109,8 @@ def test_bigarray_noobject(testbig):
# it will become S0 or U0 # it will become S0 or U0
obj_dtypev = [numpy.object, 'O', 'i4, O', [('x', 'i4'), ('y', 'i4, O')]] obj_dtypev = [numpy.object, 'O', 'i4, O', [('x', 'i4'), ('y', 'i4, O')]]
for dtype_ in obj_dtypev: for dtype_ in obj_dtypev:
raises(TypeError, "BigArray((1,), dtype_, Zh)") with raises(TypeError):
BigArray((1,), dtype_, Zh)
# basic ndarray-compatibility attributes of BigArray # basic ndarray-compatibility attributes of BigArray
...@@ -118,7 +119,7 @@ def test_bigarray_basic(testbig): ...@@ -118,7 +119,7 @@ def test_bigarray_basic(testbig):
A = BigArray((10,3), int32, Zh) A = BigArray((10,3), int32, Zh)
raises(TypeError, "A.data") with raises(TypeError): A.data
assert A.strides == (12, 4) assert A.strides == (12, 4)
assert A.dtype == dtype(int32) assert A.dtype == dtype(int32)
# XXX .flags? # XXX .flags?
...@@ -137,7 +138,7 @@ def test_bigarray_basic(testbig): ...@@ -137,7 +138,7 @@ def test_bigarray_basic(testbig):
B = BigArray((10,3), int32, Zh, order='F') B = BigArray((10,3), int32, Zh, order='F')
raises(TypeError, "B.data") with raises(TypeError): B.data
assert B.strides == (4, 40) assert B.strides == (4, 40)
assert B.dtype == dtype(int32) assert B.dtype == dtype(int32)
# XXX .flags? # XXX .flags?
...@@ -191,15 +192,15 @@ def test_bigarray_indexing_1d(testbig): ...@@ -191,15 +192,15 @@ def test_bigarray_indexing_1d(testbig):
# (in numpy they create _copy_ picking up elements) # (in numpy they create _copy_ picking up elements)
A_[0:5] = range(0,10,2) A_[0:5] = range(0,10,2)
assert array_equal(A_[[0,1,2,3,4]], [0,2,4,6,8]) assert array_equal(A_[[0,1,2,3,4]], [0,2,4,6,8])
raises (TypeError, 'A[[0,1,2,3,4]]') with raises(TypeError): A[[0,1,2,3,4]]
# index out of range # index out of range
# - element access -> raises IndexError # - element access -> raises IndexError
# - slice access -> empty # - slice access -> empty
A_[-1] = 0 A_[-1] = 0
assert AA[10*PS-1] == (0,0) assert AA[10*PS-1] == (0,0)
raises(IndexError, 'A_[10*PS]') with raises(IndexError): A_[10*PS]
raises(IndexError, 'A [10*PS]') with raises(IndexError): A [10*PS]
a, _ = AA[10*PS:10*PS+1] a, _ = AA[10*PS:10*PS+1]
assert isinstance(a, ndarray) assert isinstance(a, ndarray)
assert array_equal(a, _) assert array_equal(a, _)
...@@ -329,7 +330,8 @@ def test_bigarray_indexing_1d(testbig): ...@@ -329,7 +330,8 @@ def test_bigarray_indexing_1d(testbig):
assert a[2*PS+5] == 5 assert a[2*PS+5] == 5
assert a[2*PS+6] == 5 assert a[2*PS+6] == 5
assert raises(ValueError, 'A[:4] = range(5)') with raises(ValueError):
A[:4] = range(5)
# indexing where accessed element overlaps edge between pages # indexing where accessed element overlaps edge between pages
...@@ -442,8 +444,8 @@ def test_bigarray_indexing_Nd(testbig): ...@@ -442,8 +444,8 @@ def test_bigarray_indexing_Nd(testbig):
# out-of-range element access # out-of-range element access
idx[idxpos] = shape[idxpos] # 0, 0, 0, Ni , 0 ,0, 0 idx[idxpos] = shape[idxpos] # 0, 0, 0, Ni , 0 ,0, 0
raises(IndexError, 'A [idxt()]') with raises(IndexError): A [idxt()]
raises(IndexError, 'A_[idxt()]') with raises(IndexError): A_[idxt()]
# out-of-range slice access # out-of-range slice access
idx[idxpos] = slice(shape[idxpos], # 0, 0, 0, Ni:Ni+1 , 0 ,0, 0 idx[idxpos] = slice(shape[idxpos], # 0, 0, 0, Ni:Ni+1 , 0 ,0, 0
...@@ -516,7 +518,7 @@ def test_bigarray_resize(testbig): ...@@ -516,7 +518,7 @@ def test_bigarray_resize(testbig):
assert b[10-1,-1] == 10*3-1 assert b[10-1,-1] == 10*3-1
# we cannot access old mapping beyond it's end # we cannot access old mapping beyond it's end
assert raises(IndexError, 'a[10,:]') with raises(IndexError): a[10,:]
# we can change tail # we can change tail
b[10,:] = arange(10*3, (10+1)*3) b[10,:] = arange(10*3, (10+1)*3)
...@@ -563,15 +565,15 @@ def test_bigarray_append(testbig): ...@@ -563,15 +565,15 @@ def test_bigarray_append(testbig):
assert array_equal(A[:], arange32(0,4)) assert array_equal(A[:], arange32(0,4))
# append with incorrect shape - rejected, original stays the same # append with incorrect shape - rejected, original stays the same
assert raises(ValueError, 'A.append(arange(3))') with raises(ValueError): A.append(arange(3))
assert array_equal(A[:], arange32(0,4)) assert array_equal(A[:], arange32(0,4))
assert raises(ValueError, 'A.append(arange(3*2).reshape(3,2))') with raises(ValueError): A.append(arange(3*2).reshape(3,2))
assert array_equal(A[:], arange32(0,4)) assert array_equal(A[:], arange32(0,4))
# append with correct shape, but incompatible dtype - rejected, original stays the same # append with correct shape, but incompatible dtype - rejected, original stays the same
assert raises(ValueError, with raises(ValueError):
{'C': 'A.append(asarray([[[0,1], [2,3], [4,"abc"]]], dtype=object))', if order == 'C': A.append(asarray([[[0,1], [2,3], [4,"abc"]]], dtype=object))
'F': 'A.append(asarray([[[0],[1],[2]], [[3],[4],["abc"]]], dtype=object))'} [order] ) if order == 'F': A.append(asarray([[[0],[1],[2]], [[3],[4],["abc"]]], dtype=object))
assert array_equal(A[:], arange32(0,4)) assert array_equal(A[:], arange32(0,4))
...@@ -612,8 +614,8 @@ def test_bigarray_to_ndarray(testbig): ...@@ -612,8 +614,8 @@ def test_bigarray_to_ndarray(testbig):
# converting it to ndarray is should be not possible # converting it to ndarray is should be not possible
for i in range(48,65): for i in range(48,65):
C = BigArray(((1<<i)-1,), uint8, Zh) C = BigArray(((1<<i)-1,), uint8, Zh)
raises(MemoryError, 'asarray(C)') with raises(MemoryError):
asarray(C)
...@@ -688,7 +690,7 @@ def test_arrayref(testbig): ...@@ -688,7 +690,7 @@ def test_arrayref(testbig):
assert isinstance(m_, memoryview) assert isinstance(m_, memoryview)
#assert m_ is m XXX strangely it is another object, not exactly m #assert m_ is m XXX strangely it is another object, not exactly m
# XXX however rdata.strides<0 and no rdata.base.base is enough for us here. # XXX however rdata.strides<0 and no rdata.base.base is enough for us here.
raises(AttributeError, 'm_.base') with raises(AttributeError): m_.base
a = rdata[100:140] a = rdata[100:140]
ref = ArrayRef(a) ref = ArrayRef(a)
......
...@@ -76,7 +76,7 @@ def test_basic(): ...@@ -76,7 +76,7 @@ def test_basic():
f = XBigFile(b'abcd', PS) f = XBigFile(b'abcd', PS)
assert f.blksize == PS assert f.blksize == PS
raises(ROAttributeError, "f.blksize = 1") # RO attribute with raises(ROAttributeError): f.blksize = 1 # RO attribute
assert f.head == b'abcd' assert f.head == b'abcd'
fh = f.fileh_open() # TODO + ram fh = f.fileh_open() # TODO + ram
...@@ -452,5 +452,5 @@ def test_writeout_err_notfatal(): ...@@ -452,5 +452,5 @@ def test_writeout_err_notfatal():
# call writeout 2 times as previously upon error it was leaving internal # call writeout 2 times as previously upon error it was leaving internal
# flag set that writeout was still in progress and second call to writeout # flag set that writeout was still in progress and second call to writeout
# was leading to BUG and thus process crash. # was leading to BUG and thus process crash.
raises(StoreError, "fh.dirty_writeout(WRITEOUT_STORE)") with raises(StoreError): fh.dirty_writeout(WRITEOUT_STORE)
raises(StoreError, "fh.dirty_writeout(WRITEOUT_STORE)") with raises(StoreError): fh.dirty_writeout(WRITEOUT_STORE)
...@@ -659,7 +659,8 @@ def test_bigfile_filezodb_vs_conflicts(): ...@@ -659,7 +659,8 @@ def test_bigfile_filezodb_vs_conflicts():
tm1.commit() tm1.commit()
# txn2 should raise ConflictError and stay at 11 state # txn2 should raise ConflictError and stay at 11 state
raises(ConflictError, 'tm2.commit()') with raises(ConflictError):
tm2.commit()
tm2.abort() tm2.abort()
assert Blk(vma2, 0)[0] == 11 # re-read in conn2 assert Blk(vma2, 0)[0] == 11 # re-read in conn2
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment