Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit887f88a

Browse files
committed
Use pytest facilities
1 parentb2161f9 commit887f88a

File tree

1 file changed

+61
-76
lines changed

1 file changed

+61
-76
lines changed

‎lib/matplotlib/tests/test_cache.py

Lines changed: 61 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,78 @@
66
importmock
77

88
importos
9-
importshutil
10-
importstat
11-
importtempfile
12-
13-
fromnose.toolsimportraises
149

1510
frommatplotlibimportcbook
1611
frommatplotlib.testing._conversion_cacheimport_ConversionCache,_CacheError
12+
importpytest
1713

1814

19-
deftest_cache_basic():
20-
tmpdir=tempfile.mkdtemp()
21-
15+
deftest_cache_basic(tmpdir):
2216
defintmp(f):
23-
returnos.path.join(tmpdir,f)
17+
returntmpdir.join(f)
18+
deffname(f):
19+
returnstr(intmp(f))
2420
try:
25-
cache=_ConversionCache(intmp('cache'))
26-
withopen(intmp('fake.pdf'),'w')aspdf:
27-
pdf.write('this is a fake pdf file')
28-
withopen(intmp('fake.svg'),'w')assvg:
29-
svg.write('this pretends to be an svg file')
30-
31-
assertnotcache.get(intmp('fake.pdf'),intmp('fakepdf.png'))
32-
assertnotcache.get(intmp('fake.svg'),intmp('fakesvg.png'))
21+
cache=_ConversionCache(fname('cache'))
22+
intmp('fake.pdf').write_binary(b'this is a fake pdf file')
23+
intmp('fake.svg').write_binary(b'this pretends to be an svg file')
24+
assertnotcache.get(fname('fake.pdf'),fname('fakepdf.png'))
25+
assertnotcache.get(fname('fake.svg'),fname('fakesvg.png'))
3326
assertcache.report()== \
34-
{'gets': {intmp('fake.pdf'),intmp('fake.svg')},
27+
{'gets': {fname('fake.pdf'),fname('fake.svg')},
3528
'hits':set()}
3629

37-
withopen(intmp('fakepdf.png'),'w')aspng:
38-
png.write('generated from the pdf file')
39-
cache.put(intmp('fake.pdf'),intmp('fakepdf.png'))
40-
assertcache.get(intmp('fake.pdf'),intmp('copypdf.png'))
41-
withopen(intmp('copypdf.png'),'r')ascopy:
42-
assertcopy.read()=='generated from the pdf file'
30+
intmp('fakepdf.png').write_binary(b'generated from the pdf file')
31+
cache.put(fname('fake.pdf'),fname('fakepdf.png'))
32+
assertcache.get(fname('fake.pdf'),fname('copypdf.png'))
33+
assertintmp('copypdf.png').read()=='generated from the pdf file'
4334
assertcache.report()== \
44-
{'gets': {intmp('fake.pdf'),intmp('fake.svg')},
45-
'hits':set([intmp('fake.pdf')])}
46-
47-
withopen(intmp('fakesvg.png'),'w')aspng:
48-
png.write('generated from the svg file')
49-
cache.put(intmp('fake.svg'),intmp('fakesvg.png'))
50-
assertcache.get(intmp('fake.svg'),intmp('copysvg.png'))
51-
withopen(intmp('copysvg.png'),'r')ascopy:
52-
assertcopy.read()=='generated from the svg file'
35+
{'gets': {fname('fake.pdf'),fname('fake.svg')},
36+
'hits': {fname('fake.pdf')}}
37+
38+
intmp('fakesvg.png').write_binary(b'generated from the svg file')
39+
cache.put(fname('fake.svg'),fname('fakesvg.png'))
40+
assertcache.get(fname('fake.svg'),fname('copysvg.png'))
41+
assertintmp('copysvg.png').read()=='generated from the svg file'
5342
assertcache.report()== \
54-
{'gets': {intmp('fake.pdf'),intmp('fake.svg')},
55-
'hits': {intmp('fake.pdf'),intmp('fake.svg')}}
43+
{'gets': {fname('fake.pdf'),fname('fake.svg')},
44+
'hits': {fname('fake.pdf'),fname('fake.svg')}}
5645
finally:
57-
shutil.rmtree(tmpdir)
58-
46+
tmpdir.remove(rec=1)
5947

60-
deftest_cache_expire():
61-
tmpdir=tempfile.mkdtemp()
6248

49+
deftest_cache_expire(tmpdir):
6350
defintmp(*f):
64-
returnos.path.join(tmpdir,*f)
51+
returntmpdir.join(*f)
52+
deffname(*f):
53+
returnstr(intmp(*f))
6554
try:
66-
cache=_ConversionCache(intmp('cache'),10)
55+
cache=_ConversionCache(fname('cache'),10)
6756
foriinrange(5):
68-
filename=intmp('cache','file%d.png'%i)
69-
withopen(filename,'w')asf:
70-
f.write('1234')
71-
os.utime(filename, (i*1000,i*1000))
57+
pngfile=intmp('cache','file%d.png'%i)
58+
pngfile.write_binary(b'1234')
59+
os.utime(str(pngfile), (i*1000,i*1000))
7260

7361
cache.expire()
74-
assertnotos.path.exists(intmp('cache','file0.png'))
75-
assertnotos.path.exists(intmp('cache','file1.png'))
76-
assertnotos.path.exists(intmp('cache','file2.png'))
77-
assertos.path.exists(intmp('cache','file3.png'))
78-
assertos.path.exists(intmp('cache','file4.png'))
62+
assertnotos.path.exists(fname('cache','file0.png'))
63+
assertnotos.path.exists(fname('cache','file1.png'))
64+
assertnotos.path.exists(fname('cache','file2.png'))
65+
assertos.path.exists(fname('cache','file3.png'))
66+
assertos.path.exists(fname('cache','file4.png'))
7967

80-
withopen(intmp('cache','onemore.png'),'w')asf:
81-
f.write('x'*11)
82-
os.utime(intmp('cache','onemore.png'), (5000,5000))
68+
intmp('cache','onemore.png').write_binary(b'x'*11)
69+
os.utime(fname('cache','onemore.png'), (5000,5000))
8370

8471
cache.expire()
85-
assertnotos.path.exists(intmp('cache','file0.png'))
86-
assertnotos.path.exists(intmp('cache','file1.png'))
87-
assertnotos.path.exists(intmp('cache','file2.png'))
88-
assertnotos.path.exists(intmp('cache','file3.png'))
89-
assertnotos.path.exists(intmp('cache','file4.png'))
90-
assertnotos.path.exists(intmp('cache','onemore.png'))
72+
assertnotos.path.exists(fname('cache','file0.png'))
73+
assertnotos.path.exists(fname('cache','file1.png'))
74+
assertnotos.path.exists(fname('cache','file2.png'))
75+
assertnotos.path.exists(fname('cache','file3.png'))
76+
assertnotos.path.exists(fname('cache','file4.png'))
77+
assertnotos.path.exists(fname('cache','onemore.png'))
9178

9279
finally:
93-
shutil.rmtree(tmpdir)
80+
tmpdir.remove(rec=1)
9481

9582

9683
deftest_cache_default_dir():
@@ -101,25 +88,23 @@ def test_cache_default_dir():
10188
pass
10289

10390

104-
@raises(_CacheError)
10591
@mock.patch('matplotlib.testing._conversion_cache.cbook.mkdirs',
10692
side_effect=OSError)
107-
deftest_cache_mkdir_error(mkdirs):
108-
tmpdir=tempfile.mkdtemp()
109-
try:
110-
c=_ConversionCache(os.path.join(tmpdir,'cache'))
111-
finally:
112-
shutil.rmtree(tmpdir)
93+
deftest_cache_mkdir_error(mkdirs,tmpdir):
94+
withpytest.raises(_CacheError):
95+
try:
96+
c=_ConversionCache(str(tmpdir.join('cache')))
97+
finally:
98+
tmpdir.remove(rec=1)
11399

114100

115-
@raises(_CacheError)
116101
@mock.patch('matplotlib.testing._conversion_cache.os.access',
117102
side_effect=[False])
118-
deftest_cache_unwritable_error(access):
119-
tmpdir=tempfile.mkdtemp()
120-
cachedir=os.path.join(tmpdir,'test_cache')
121-
try:
122-
cbook.mkdirs(cachedir)
123-
c=_ConversionCache(cachedir)
124-
finally:
125-
shutil.rmtree(tmpdir)
103+
deftest_cache_unwritable_error(access,tmpdir):
104+
withpytest.raises(_CacheError):
105+
cachedir=tmpdir.join('cache')
106+
cachedir.ensure(dir=True)
107+
try:
108+
c=_ConversionCache(str(cachedir))
109+
finally:
110+
tmpdir.remove(rec=1)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp