File: test_modules.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (83 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 24 09:43:53 2014

@author: pietro
"""
import sys
from fnmatch import fnmatch
from grass.gunittest.case import TestCase
from grass.gunittest.main import test

from grass.script.core import get_commands
from grass.exceptions import ParameterError
from grass.pygrass.modules.interface import Module

PY2 = sys.version_info[0] == 2
if PY2:
    from StringIO import StringIO
else:
    from io import BytesIO as StringIO


SKIP = ["g.parser", ]


# taken from six
def with_metaclass(meta, *bases):
    """Create a base class with a metaclass."""
    # This requires a bit of explanation: the basic idea is to make a dummy
    # metaclass for one level of class instantiation that replaces itself with
    # the actual metaclass.
    class metaclass(meta):

        def __new__(cls, name, this_bases, d):
            return meta(name, bases, d)
    return type.__new__(metaclass, 'temporary_class', (), {})


class ModulesMeta(type):
    def __new__(mcs, name, bases, dict):

        def gen_test(cmd):
            def test(self):
                Module(cmd)
            return test

        cmds = [c for c in sorted(list(get_commands()[0]))
                if c not in SKIP and not fnmatch(c, "g.gui.*")]
        for cmd in cmds:
            test_name = "test__%s" % cmd.replace('.', '_')
            dict[test_name] = gen_test(cmd)
        return type.__new__(mcs, name, bases, dict)


class TestModules(with_metaclass(ModulesMeta, TestCase)):
    pass


class TestModulesPickability(TestCase):
    def test_rsun(self):
        """Test if a Module instance is pickable"""
        import pickle

        out = StringIO()
        pickle.dump(Module('r.sun'), out)
        out.close()


class TestModulesCheck(TestCase):
    def test_flags_with_suppress_required(self):
        """Test if flags with suppress required are handle correctly"""
        gextension = Module('g.extension')
        # check if raise an error if required parameter are missing
        with self.assertRaises(ParameterError):
            gextension.check()

        # check if the flag suppress the required parameters
        gextension.flags.a = True
        self.assertIsNone(gextension.check())


if __name__ == '__main__':
    test()