def test_iter(self, tmpfile): f = File(tmpfile, 'w') f.write_array('c', 10) f.write_array('a', [1, 2, 3]) f.write_json('b', {'foo': 'bar'}) f.write_json('d', 42) assert list(f) == ['a', 'b', 'c', 'd'] f.close() f = File(tmpfile) assert list(f) == ['a', 'b', 'c', 'd']
def test_invalid_key(self, tmpfile): f = File(tmpfile, 'w') pytest.raises_regexp(ValueError, 'invalid key: expected string', f.write_json, 42, 0) pytest.raises_regexp(ValueError, 'invalid key: empty string', f.write_json, '', 0) f.write_json('foo', 'bar') pytest.raises_regexp(ValueError, "key already exists: 'foo'", f.write_json, 'foo', 'baz') f.close() f = File(tmpfile) pytest.raises_regexp(ValueError, 'invalid key: expected string', f.read, 42) pytest.raises_regexp(KeyError, 'bar', f.read, 'bar')
def test_close_handle(self, tmpfile): f = File(tmpfile, 'w') f.close() pytest.raises_regexp(IOError, 'the file handle has been closed', f.read, 'foo') pytest.raises_regexp(IOError, 'the file handle has been closed', f.write_array, 'foo', [1, 2]) pytest.raises_regexp(IOError, 'the file handle has been closed', f.write_json, 'foo', [1, 2]) f.close() assert f.filename == tmpfile assert f.writable f = File(tmpfile) f.close() f.close() with File(tmpfile) as f: assert f._handle is not None assert f._handle is None pytest.raises_regexp(IOError, 'the file handle has been closed', f.read, 'foo')