File: nodegraph_test.py

package info (click to toggle)
paleomix 1.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,156 kB
  • sloc: python: 21,423; sh: 182; makefile: 37
file content (119 lines) | stat: -rw-r--r-- 4,411 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python3
#
# Copyright (c) 2012 Mikkel Schubert <MikkelSch@gmail.com>
#
# Permission is herby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import os

from unittest.mock import Mock

from paleomix.common.fileutils import fspath
from paleomix.nodegraph import NodeGraph, FileStatusCache


_TIMESTAMP_1 = 1000190760
_TIMESTAMP_2 = 1120719000


def create_test_file(utime, *args):
    filename = os.path.join(*map(fspath, args))
    with open(filename, "wb"):
        pass

    os.utime(filename, (utime, utime))

    return filename


###############################################################################
###############################################################################
# NodeGraph: _is_done
# TODO: Avoid testing private function, mock cache


def test_nodegraph_is_done__no_output():
    cache = FileStatusCache()
    node = Mock(output_files=())
    assert NodeGraph.is_done(node, cache)


def test_nodegraph_is_done__output_changes(tmp_path):
    temp_file_1 = tmp_path / "file_1.txt"
    temp_file_2 = tmp_path / "file_2.txt"
    my_node = Mock(output_files=(str(temp_file_1), str(temp_file_2)))
    assert not NodeGraph.is_done(my_node, FileStatusCache())
    temp_file_1.write_text("foo")
    assert not NodeGraph.is_done(my_node, FileStatusCache())
    temp_file_2.write_text("bar")
    assert NodeGraph.is_done(my_node, FileStatusCache())


def test_nodegraph_is_done__subnode_not_considered(tmp_path):
    temp_file = tmp_path / "file.txt"
    subnode = Mock(output_files=(str(temp_file),))
    my_node = Mock(output_files=(), subnodes=(subnode,))
    assert NodeGraph.is_done(my_node, FileStatusCache())


def test_nodegraph_is_outdated__no_output():
    my_node = Mock(input_files=(), output_files=())
    assert not NodeGraph.is_outdated(my_node, FileStatusCache())


def test_nodegraph_is_outdated__input_but_no_output(tmp_path):
    input_file = tmp_path / "file"
    input_file.touch()

    my_node = Mock(input_files=(str(input_file),), output_files=())
    assert not NodeGraph.is_outdated(my_node, FileStatusCache())


def test_nodegraph_is_outdated__output_but_no_input(tmp_path):
    output_file = tmp_path / "file"
    output_file.touch()

    my_node = Mock(input_files=(), output_files=(str(output_file),))
    assert not NodeGraph.is_outdated(my_node, FileStatusCache())


def test_nodegraph_is_outdated__not_outdated(tmp_path):
    my_node = Mock(
        input_files=(create_test_file(_TIMESTAMP_1, tmp_path, "older_file"),),
        output_files=(create_test_file(_TIMESTAMP_2, tmp_path, "younger_file"),),
    )
    assert not NodeGraph.is_outdated(my_node, FileStatusCache())


def test_nodegraph_is_outdated__outdated(tmp_path):
    my_node = Mock(
        input_files=(create_test_file(_TIMESTAMP_2, tmp_path, "younger_file"),),
        output_files=(create_test_file(_TIMESTAMP_1, tmp_path, "older_file"),),
    )
    assert NodeGraph.is_outdated(my_node, FileStatusCache())


def test_nodegraph_is_outdated__updates(tmp_path):
    older_file = create_test_file(_TIMESTAMP_1, tmp_path, "older_file")
    younger_file = create_test_file(_TIMESTAMP_2, tmp_path, "younger_file")

    my_node = Mock(input_files=(older_file,), output_files=(younger_file,),)
    assert not NodeGraph.is_outdated(my_node, FileStatusCache())
    my_node = Mock(input_files=(younger_file,), output_files=(older_file,),)
    assert NodeGraph.is_outdated(my_node, FileStatusCache())