File: test_order.py

package info (click to toggle)
eumdac 3.0.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 716 kB
  • sloc: python: 7,325; makefile: 6
file content (197 lines) | stat: -rw-r--r-- 5,686 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import pytest

from pathlib import Path
from dataclasses import asdict

from eumdac.tailor_models import Chain
from eumdac.order import (
    Order,
    new_order_filename,
    latest_order_file,
    all_order_filenames,
    highest_number_in_order_filenames,
    resolve_order,
    get_default_order_dir,
)

from tests.base import FakeProduct, FakeCollection, FakeCustomisation, FakeTailor

order_test_path = Path("test_order.yml")

fake_chain = Chain()
fake_chain.id = "id"
fake_chain.description = "description"


@pytest.fixture
def products():
    return [FakeProduct()]


@pytest.fixture
def tailor_order(tmp_path, products):
    order = Order(order_file=tmp_path / "test_tailor_order.yml")
    order.initialize(
        fake_chain,
        products=products,
        output_dir=tmp_path / "output",
        file_pattern=None,
        query=None,
    )
    yield order

    order.delete()


@pytest.fixture
def download_order(tmp_path, products):
    order = Order(order_file=tmp_path / "test_download_order.yml")
    order.initialize(
        None, products=products, output_dir=tmp_path / "output", file_pattern=None, query=None
    )
    yield order

    order.delete()


def test_order_status(tailor_order, products):
    assert tailor_order.status() == "NOT COMPLETED"

    for p in products:
        tailor_order.update(None, p._id, "FAILED", None)
    assert tailor_order.status() == "FAILED"

    for p in products:
        tailor_order.update(None, p._id, "DONE", None)
    assert tailor_order.status() == "NOT COMPLETED"

    for p in products:
        tailor_order.update(None, p._id, "DONE", {"fn": "DOWNLOADED"})
    assert tailor_order.status() == "DONE"


def test_order_collections(tailor_order, products):
    for p in products:
        assert p.collection._id in tailor_order.collections()


def test_order_pretty(tailor_order, download_order):
    for order in [tailor_order, download_order]:
        ps = order.pretty_string(True)
        assert str(order) in ps
        assert order.status() in ps
        assert str(order.collections()) in ps


def test_order_deserialization(tailor_order):
    with tailor_order.dict_from_file() as order_dict:
        assert isinstance(order_dict, dict)
        assert "output_dir" in order_dict
        assert "chain" in order_dict
        assert "products_to_process" in order_dict
        for ptp in order_dict["products_to_process"].values():
            assert ptp["col_id"] is not None
            assert ptp["server_state"] == "UNSUBMITTED"
            assert ptp["customisation"] is None


def test_order_update(tmp_path):
    p = tmp_path / order_test_path

    collection_id = "test_col_id"
    product_id = "test_prod_id"
    status = "test_status"
    download_states = {"test": "test_download_state"}
    query = {"test": "test_query_parameter"}
    products = [FakeProduct(id=product_id, collection=FakeCollection(id=collection_id))]
    order = Order(order_file=p)
    order.initialize(
        fake_chain,
        products=products,
        output_dir=tmp_path / "output",
        file_pattern=None,
        query=query,
    )
    order.update(collection_id, product_id, status, download_states)

    with order.dict_from_file() as order_dict:
        assert isinstance(order_dict, dict)
        assert order_dict["query"] == query
        for ptp in order_dict["products_to_process"].values():
            assert ptp["server_state"] == status
            assert ptp["download_states"] == download_states

    if p.exists():
        p.unlink()


def test_order_remote_delete_failed(tailor_order, products):
    with tailor_order.dict_from_file() as order_dict:
        for ptp in order_dict["products_to_process"].values():
            ptp["server_state"] = "FAILED"
    tailor_order._locked_serialize(order_dict)

    for p in products:
        tailor_order.update("test_customisation", p._id, "FAILED", {"fn": "DOWNLOADED"})

    tailor_order.remote_delete_failed(FakeTailor())


def test_order_resolve_product_num(tailor_order, products):
    cnt = 1
    for p in products:
        total_products, num = tailor_order.resolve_product_num(product_id=p._id)
        assert total_products >= num
        assert num == cnt
        cnt += 1

    with pytest.raises(KeyError):
        _, __ = tailor_order.resolve_product_num(product_id="INVALID")


def test_order_filehandling(tmp_path):
    path_order_dir = get_default_order_dir()
    assert path_order_dir.exists()

    order_filename1 = new_order_filename(path_order_dir)
    assert order_filename1.exists()

    order = Order(order_filename1)
    collection_id = "test_col_id"
    product_id = "test_prod_id"
    products = [FakeProduct(id=product_id, collection=FakeCollection(id=collection_id))]
    order.initialize(
        fake_chain,
        products=products,
        output_dir=tmp_path / "output",
        file_pattern=None,
        query=None,
    )

    assert order_filename1.exists()

    order_filename2 = new_order_filename(path_order_dir)
    assert order_filename2.exists()
    assert order_filename2 != order_filename1

    order = Order(order_filename2)
    collection_id = "test_col_id"
    product_id = "test_prod_id"
    products = [FakeProduct(id=product_id, collection=FakeCollection(id=collection_id))]
    order.initialize(
        fake_chain,
        products=products,
        output_dir=tmp_path / "output",
        file_pattern=None,
        query=None,
    )

    assert order_filename2.exists()

    o = resolve_order(path_order_dir, "latest")
    assert o._order_file == order._order_file

    if order_filename1.exists():
        order_filename1.unlink()
    if order_filename2.exists():
        order_filename2.unlink()