File: py_u_TestWith.py

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (90 lines) | stat: -rw-r--r-- 3,425 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
85
86
87
88
89
90
#
# Copyright 2009- ECMWF.
#
# This software is licensed under the terms of the Apache Licence version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#

# This test ensures the use of the `with` statement, and thus runtime context, works as expected.

from ecflow import Defs, Suite, Task, Family, Client, Variable, Edit, debug_build
import ecflow_test_util as Test
import sys, os

if __name__ == "__main__":
    Test.print_test_start(os.path.basename(__file__))

    # sys.version_info is a tuple containing (major,minor,micro,releaselevel,serial)
    version = sys.version_info;
    if (sys.version_info < (2, 7)):
        print("This test only run with python version 2.7 or greater, but found : " + str(version))
        sys.exit(0)

    # test `add_*()` using context manager -- n.b. this provides the ability to indent

    with Defs() as defs:
        with defs.add_suite("s2") as s2:
            with s2.add_family("f1") as f1:
                with f1.add_task("t1") as t1:
                    assert len(f1) == 1, "Expected 1 task"
            s2.add_family("f2")
            s2.add_task("t1")
        assert len(s2) == 3, "Expected 2 families and one task suite"
    assert len(defs) == 1, "Expected 1 suite"

    with Suite("s2") as s2:
        s2.add_family("f1")
        s2.add_task("t1")
        assert len(s2) == 2, "Expected 2 nodes family and task"

    with Family("f1") as f1:
        f1.add_family("f2")
        f1.add_task("t1")
        assert len(s2) == 2, "Expected 2 nodes family and task"

    # test `add_*()`, using the functional way

    defs1 = Defs()
    defs1.add_suite("s1").add_task("t1").add_variable("var", "v")
    defs1.add_suite("s2").add_family("f1").add_task("t1").add_variable("var", "v")
    defs1.add_suite("s3").add_family("f1").add_family("f2").add_task("t1").add_variable("var", "v")

    # test `add_*()` using context manager
    # # this is the same as above, to allow test the comparison

    with Defs() as defs2:
        with defs2.add_suite("s1") as s1:
            t1 = s1.add_task("t1")
            t1.add_variable("var", "v")
        with defs2.add_suite("s2") as s2:
            with s2.add_family("f1") as f1:
                with f1.add_task("t1") as t1:
                    t1.add_variable("var", "v")
        with defs2.add_suite("s3") as s3:
            with s3.add_family("f1") as f1:
                with f1.add_family("f2") as f2:
                    with f2.add_task("t1") as t1:
                        t1.add_variable("var", "v")

    assert defs1 == defs2, "expected defs to be the same"

    # add node using with, they should compare
    with Defs() as defs3:
        with defs3.add_suite("s1") as s1:
            s1 + Task('t1', var='v')
        with defs3.add_suite("s2") as s2:
            with s2.add_family("f1") as f1:
                with f1.add_task("t1") as t1:
                    t1 += Edit(var='v')
        with defs3.add_suite("s3") as s3:
            with s3.add_family("f1") as f1:
                with f1.add_family("f2") as f2:
                    with f2.add_task("t1") as t1:
                        t1 += Edit(var='v')

    assert defs1 == defs3, "expected defs to be the same"

    print("All tests pass")