File: test_validate.py

package info (click to toggle)
python-asdf 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,940 kB
  • sloc: python: 23,812; makefile: 123
file content (84 lines) | stat: -rw-r--r-- 2,250 bytes parent folder | download
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
import numpy as np
import pytest

import asdf
from asdf._commands import main


@pytest.fixture()
def valid_file_path(tmp_path):
    path = tmp_path / "valid_file.asdf"
    asdf.dump({"foo": 42, "arr": np.arange(42)}, path)
    return path


@pytest.fixture()
def invalid_file_path(tmp_path):
    # don't use asdf as we're intentionally making an invalid file
    contents = b"""#ASDF 1.0.0
#ASDF_STANDARD 1.6.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.1.0
arr: !core/ndarray-1.1.0
  data:
    nested: error
..."""
    path = tmp_path / "invalid_file.asdf"
    with path.open("wb") as f:
        f.write(contents)
    return path


@pytest.fixture()
def custom_schema_path(tmp_path):
    contents = """%YAML 1.1
---
id: "http://example.com/schemas/your-custom-schema"
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
type: object
properties:
  foo:
    type: string"""
    path = tmp_path / "custom_schema.yaml"
    with path.open("w") as f:
        f.write(contents)
    return path


@pytest.fixture()
def bad_blocks_file_path(tmp_path):
    to_replace = b"REPLACE"
    replacement = b"ABCDEFG"
    buff = asdf.dumps({"arr": np.frombuffer(to_replace, np.uint8)})
    buff = buff.replace(to_replace, replacement)
    path = tmp_path / "bad_block.asdf"
    with path.open("wb") as f:
        f.write(buff)
    return path


def test_valid(capsys, valid_file_path):
    assert main.main_from_args(["validate", str(valid_file_path)]) == 0

    captured = capsys.readouterr()
    assert "valid" in captured.out


def test_invalid(invalid_file_path):
    with pytest.raises(asdf.ValidationError):
        main.main_from_args(["validate", str(invalid_file_path)])


def test_custom_schema(valid_file_path, custom_schema_path):
    with pytest.raises(asdf.ValidationError):
        main.main_from_args(["validate", str(valid_file_path), "--custom-schema", str(custom_schema_path)])


def test_block_checksum(bad_blocks_file_path):
    with pytest.raises(ValueError, match="does not match given checksum"):
        main.main_from_args(["validate", str(bad_blocks_file_path)])


def test_skip_block_validation(bad_blocks_file_path):
    assert main.main_from_args(["validate", str(bad_blocks_file_path), "--skip-block-validation"]) == 0