File: test_requires.py

package info (click to toggle)
mdanalysis 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,696 kB
  • sloc: python: 92,135; ansic: 8,156; makefile: 215; sh: 138
file content (62 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download | duplicates (2)
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
"""Tests for core.groups.requires decorator

"""

import numpy as np

import pytest
from MDAnalysis.core.groups import requires
from MDAnalysis import NoDataError

from MDAnalysisTests import make_Universe


class TestRequires(object):
    def test_requires_failure_singular(self):
        @requires("masses")
        def mass_multiplier(ag1, ag2, scalar):
            return (ag1.masses + ag2.masses) * scalar

        u = make_Universe(("charges",))
        with pytest.raises(NoDataError):
            mass_multiplier(u.atoms[:10], u.atoms[20:30], 4.0)

    def test_requires_failure_multiple(self):
        @requires("masses", "charges")
        def mass_multiplier(ag1, ag2, scalar):
            return (ag1.masses + ag2.charges) * scalar

        u = make_Universe(("masses", "types"))
        with pytest.raises(NoDataError):
            mass_multiplier(u.atoms[:10], u.atoms[20:30], 4.0)

    def test_requires_success(self):
        @requires("masses")
        def mass_multiplier(ag1, ag2, scalar):
            return (ag1.masses + ag2.masses) * scalar

        u = make_Universe(("masses",))

        result = mass_multiplier(u.atoms[:10], u.atoms[20:30], 4.0)

        assert isinstance(result, np.ndarray)

    def test_failure_errormessage(self):
        # failures should list all required attributes not
        # just the first one
        @requires("cats", "dogs", "frogs")
        def animal_print(ag):
            return len(ag.cats), len(ag.dogs), len(ag.frogs)

        u = make_Universe()
        try:
            animal_print(u.atoms)
        except NoDataError as e:
            message = e.args[0]
            # Test function name gets returned (for debug)
            assert "animal_print" in message
            assert "cats" in message
            assert "dogs" in message
            assert "frogs" in message
        else:
            pytest.fail(msg="Should raise NoDataError")