1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
# Copyright (c) 2016, Science and Technology Facilities Council
# This software is distributed under a BSD licence. See LICENSE.txt.
"""
Tests for mrcfile validation functions.
"""
# Import Python 3 features for future-proofing
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import os
import shutil
import sys
import tempfile
import unittest
import warnings
# Avoid str/unicode issues when traceback.print_exc tries to write to StringIO
# (see https://stackoverflow.com/a/34872005)
try:
# Python 2
from cStringIO import StringIO
except ImportError:
# Python 3
from io import StringIO
import numpy as np
import mrcfile
from mrcfile.validator import validate_all
from . import helpers
class ValidationTest(helpers.AssertRaisesRegexMixin, unittest.TestCase):
"""Unit tests for MRC validation functions.
"""
def setUp(self):
super(ValidationTest, self).setUp()
# Set up test files and names to be used
self.test_data = helpers.get_test_data_path()
self.test_output = tempfile.mkdtemp()
self.temp_mrc_name = os.path.join(self.test_output, 'test_mrcfile.mrc')
self.example_mrc_name = os.path.join(self.test_data, 'EMD-3197.map')
self.gzip_mrc_name = os.path.join(self.test_data, 'emd_3197.map.gz')
self.bzip2_mrc_name = os.path.join(self.test_data, 'EMD-3197.map.bz2')
self.ext_header_mrc_name = os.path.join(self.test_data, 'EMD-3001.map')
self.fei1_ext_header_mrc_name = os.path.join(self.test_data, 'fei-extended.mrc')
self.fei2_ext_header_mrc_name = os.path.join(self.test_data, 'epu2.9_example.mrc')
self.not_an_mrc_name = os.path.join(self.test_data, 'README.txt')
# Set up stream to catch print output from validate()
self.print_stream = StringIO()
# Replace stdout and stderr to capture output for checking
self.orig_stdout = sys.stdout
self.orig_stderr = sys.stderr
sys.stdout = StringIO()
sys.stderr = StringIO()
def tearDown(self):
# Restore stdout and stderr
sys.stdout = self.orig_stdout
sys.stderr = self.orig_stderr
self.print_stream.close()
if os.path.exists(self.test_output):
shutil.rmtree(self.test_output)
super(ValidationTest, self).tearDown()
def test_good_file(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(np.arange(36, dtype=np.float32).reshape(3, 3, 4))
mrc.voxel_size = 2.3
result = mrcfile.validate(self.temp_mrc_name, self.print_stream)
assert result == True
print_output = self.print_stream.getvalue()
assert print_output == (
"Checking if " + self.temp_mrc_name + " is a valid MRC2014 file...\n"
"File appears to be valid.\n"
)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_emdb_file(self):
result = mrcfile.validate(self.example_mrc_name, self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert print_output.strip() == (
"Checking if " + self.example_mrc_name + " is a valid MRC2014 file...\n"
"File does not declare MRC format version 20140 or 20141: nversion = 0"
)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_gzip_emdb_file(self):
result = mrcfile.validate(self.gzip_mrc_name, self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert print_output.strip() == (
"Checking if " + self.gzip_mrc_name + " is a valid MRC2014 file...\n"
"File does not declare MRC format version 20140 or 20141: nversion = 0"
)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_bzip2_emdb_file(self):
result = mrcfile.validate(self.bzip2_mrc_name, self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert print_output.strip() == (
"Checking if " + self.bzip2_mrc_name + " is a valid MRC2014 file...\n"
"File does not declare MRC format version 20140 or 20141: nversion = 0"
)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_emdb_cryst_file(self):
result = mrcfile.validate(self.ext_header_mrc_name, self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert print_output.strip() == (
"Checking if " + self.ext_header_mrc_name + " is a valid MRC2014 file...\n"
"File does not declare MRC format version 20140 or 20141: nversion = 0\n"
"Extended header type is undefined or unrecognised: exttyp = ''"
)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def check_temp_mrc_invalid_with_warning(self, message):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert message.lower() in print_output.lower()
assert len(w) == 1
assert issubclass(w[0].category, RuntimeWarning)
assert message in str(w[0].message)
def test_incorrect_map_id(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.map = b'fake'
self.check_temp_mrc_invalid_with_warning("Map ID string")
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_short_map_id(self):
"""This tests the case of files where the map ID is almost correct.
For example, MotionCor2 writes files with ID 'MAP\0', which is not
valid according to the MRC2014 spec on the CCP-EM website, but could
be considered valid according to the MRC2014 paper (which just
specifies 'MAP', i.e. without the final byte). We should read such
files without errors or warnings, but they should fail a strict
validation check."""
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.map = b'MAP\0'
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Map ID string is incorrect" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_incorrect_machine_stamp(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.machst = bytearray(b' ')
self.check_temp_mrc_invalid_with_warning("machine stamp")
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_invalid_mode(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(np.arange(12, dtype=np.float32).reshape(1, 3, 4))
mrc.header.mode = 8
self.check_temp_mrc_invalid_with_warning("mode")
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_file_too_small(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(np.arange(12, dtype=np.float32).reshape(1, 3, 4))
mrc.header.nz = 2
self.check_temp_mrc_invalid_with_warning("data block")
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_file_too_large(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(np.arange(36, dtype=np.float32).reshape(3, 3, 4))
mrc.header.nz = 2
self.check_temp_mrc_invalid_with_warning("larger than expected")
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_negative_mx(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.mx = -10
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Header field 'mx' is negative" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_negative_my(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.my = -10
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Header field 'my' is negative" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_negative_mz(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.mz = -10
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Header field 'mz' is negative" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_negative_ispg(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.ispg = -10
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Header field 'ispg' is negative" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_negative_nlabl(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.nlabl = -3
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Header field 'nlabl' is negative" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_negative_cella_x(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.cella.x = -10
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Cell dimension 'x' is negative" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_invalid_axis_mapping(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.mapc = 3
mrc.header.mapr = 4
mrc.header.maps = -200
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Invalid axis mapping: found [-200, 3, 4]" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_mz_correct_for_volume_stack(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(np.arange(120, dtype=np.float32).reshape(3, 2, 4, 5))
with warnings.catch_warnings(record=True):
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == True
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_mz_incorrect_for_volume_stack(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(np.arange(120, dtype=np.float32).reshape(3, 2, 4, 5))
mrc.header.mz = 5
with warnings.catch_warnings(record=True):
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Error in dimensions for volume stack: nz should be "
"divisible by mz. Found nz = 6, mz = 5" in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_nlabl_too_large(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.label[1] = 'test label'
mrc.header.nlabl = 3
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Error in header labels: "
"nlabl is 3 but 2 labels contain text" in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_nlabl_too_small(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.label[1] = 'test label'
mrc.header.nlabl = 1
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Error in header labels: "
"nlabl is 1 but 2 labels contain text" in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_empty_labels_in_list(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.label[2] = 'test label'
mrc.header.nlabl = 2
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Error in header labels: empty labels appear between "
"text-containing labels" in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_old_format_version(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.nversion = 20140
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == True
print_output = self.print_stream.getvalue()
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_incorrect_format_version(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.header.nversion = 20139
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "File does not declare MRC format version 20140 or 20141" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_missing_exttyp(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_extended_header(np.arange(10))
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Extended header type is undefined or unrecognised" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_unknown_exttyp(self):
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_extended_header(np.arange(10))
mrc.header.exttyp = 'Fake'
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert "Extended header type is undefined or unrecognised" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_incorrect_rms(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.rms = 9.0
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Data statistics appear to be inaccurate: RMS deviation is {0} but the"
" value in the header is 9.0".format(data.std()) in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_rms_undetermined(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.rms = -15
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == True
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_incorrect_dmin(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.dmin = -11
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Data statistics appear to be inaccurate: minimum is {0} but the value"
" in the header is -11".format(data.min()) in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_incorrect_dmax(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.dmax = 15
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Data statistics appear to be inaccurate: maximum is {0} but the value"
" in the header is 15".format(data.max()) in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_min_and_max_undetermined(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.dmin = 30.1
mrc.header.dmax = 30.0
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == True
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_incorrect_dmean(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.dmean = -2.5
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Data statistics appear to be inaccurate: mean is {0} but the value in"
" the header is -2.5".format(data.mean(dtype=np.float64))
in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_incorrect_dmean_with_undetermined_dmin_and_dmax(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.dmin = 20
mrc.header.dmax = -30.1
mrc.header.dmean = -2.5
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert ("Data statistics appear to be inaccurate: mean is {0} but the value in"
" the header is -2.5".format(data.mean(dtype=np.float64))
in print_output)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_mean_undetermined(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.dmean = -11
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == True
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_min_max_and_mean_undetermined(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.header.dmin = 30.1
mrc.header.dmax = 30.0
mrc.header.dmean = 29.9
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == True
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_many_problems_simultaneously(self):
data = np.arange(-10, 20, dtype=np.float32).reshape(3, 2, 5)
with mrcfile.new(self.temp_mrc_name) as mrc:
mrc.set_data(data)
mrc.set_extended_header(data)
mrc.header.nz = 2
mrc.header.my = -1000
mrc.header.mz = -5
mrc.header.cella.y = -12.1
mrc.header.mapc = 5
mrc.header.dmin = 10
mrc.header.dmax = 11
mrc.header.dmean = 19.0
mrc.header.ispg = -20
mrc.header.exttyp = 'fake'
mrc.header.nversion = 0
mrc.header.rms = 0.0
mrc.header.nlabl = 4
mrc.header.label[9] = 'test label'
with warnings.catch_warnings(record=True):
result = mrcfile.validate(self.temp_mrc_name,
print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert len(print_output.split('\n')) == 16
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def create_good_files(self):
"""Create some files known to be valid and return their names."""
good_mrc_name_1 = os.path.join(self.test_output, 'good_file_1.mrc')
good_mrc_name_2 = os.path.join(self.test_output, 'good_file_2.mrc')
# Make good files which will pass validation
with mrcfile.new(good_mrc_name_1) as mrc:
mrc.set_data(np.arange(36, dtype=np.float32).reshape(3, 3, 4))
with mrcfile.new(good_mrc_name_2) as mrc:
mrc.set_data(np.arange(36, dtype=np.uint16).reshape(3, 3, 4))
return [
good_mrc_name_1,
good_mrc_name_2,
self.fei1_ext_header_mrc_name,
self.fei2_ext_header_mrc_name
]
def test_validate_good_files(self):
good_files = self.create_good_files()
result = validate_all(good_files, print_file=self.print_stream)
assert result == True
print_output = self.print_stream.getvalue()
assert print_output == (
"Checking if " + good_files[0] + " is a valid MRC2014 file...\n"
"File appears to be valid.\n"
"Checking if " + good_files[1] + " is a valid MRC2014 file...\n"
"File appears to be valid.\n"
"Checking if " + good_files[2] + " is a valid MRC2014 file...\n"
"File appears to be valid.\n"
"Checking if " + good_files[3] + " is a valid MRC2014 file...\n"
"File appears to be valid.\n"
)
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_validate_bad_files(self):
bad_files = [
self.not_an_mrc_name,
self.example_mrc_name,
self.ext_header_mrc_name,
self.gzip_mrc_name
]
result = validate_all(bad_files, print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert len(print_output) > 0
assert "Checking if " + bad_files[0] + " is a valid MRC2014 file..." in print_output
assert "Checking if " + bad_files[1] + " is a valid MRC2014 file..." in print_output
assert "Checking if " + bad_files[2] + " is a valid MRC2014 file..." in print_output
assert "Checking if " + bad_files[3] + " is a valid MRC2014 file..." in print_output
assert "ValueError:" in print_output
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
def test_validate_good_and_bad_files(self):
files = self.create_good_files() + [
self.not_an_mrc_name,
self.example_mrc_name,
self.ext_header_mrc_name,
self.gzip_mrc_name
]
result = validate_all(files, print_file=self.print_stream)
assert result == False
print_output = self.print_stream.getvalue()
assert len(print_output) > 0
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0
if __name__ == '__main__':
unittest.main()
|