File: test_utils.py

package info (click to toggle)
metview-python 1.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,896 kB
  • sloc: python: 11,306; makefile: 84; ansic: 51; sh: 7
file content (104 lines) | stat: -rw-r--r-- 2,813 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
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
# (C) Copyright 2017- 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.
#

import datetime

from metview.metviewpy import utils


def test_utils_date():

    d = utils.date_from_str("20210204")
    assert d == datetime.datetime(2021, 2, 4)

    d = utils.date_from_str("2021-02-04")
    assert d == datetime.datetime(2021, 2, 4)

    d = utils.date_from_str("2021-02-04 06")
    assert d == datetime.datetime(2021, 2, 4, 6, 0, 0)

    d = utils.date_from_str("2021-02-04 06:14")
    assert d == datetime.datetime(2021, 2, 4, 6, 14, 0)

    d = utils.date_from_str("2021-02-04 06:14:32")
    assert d == datetime.datetime(2021, 2, 4, 6, 14, 32)

    d = utils.date_from_str("20210204.25")
    assert d == datetime.datetime(2021, 2, 4, 6, 0, 0)

    d = utils.date_from_str("402")
    assert d == (4, 2)

    d = utils.date_from_str("0402")
    assert d == (4, 2)

    try:
        d = utils.date_from_str("0432")
        assert False
    except:
        pass

    d = utils.date_from_str("apr-02")
    assert d == (4, 2)

    d = utils.date_from_str("Apr-02")
    assert d == (4, 2)

    try:
        d = utils.date_from_str("afr-02")
        assert False
    except:
        pass


def test_utils_time():

    d = utils.time_from_str("6")
    assert d == datetime.time(6, 0, 0)

    d = utils.time_from_str("06")
    assert d == datetime.time(6, 0, 0)

    d = utils.time_from_str("14")
    assert d == datetime.time(14, 0, 0)

    d = utils.time_from_str("600")
    assert d == datetime.time(6, 0, 0)

    d = utils.time_from_str("612")
    assert d == datetime.time(6, 12, 0)

    d = utils.time_from_str("1100")
    assert d == datetime.time(11, 0, 0)

    d = utils.time_from_str("1457")
    assert d == datetime.time(14, 57, 0)

    d = utils.time_from_str("6:12")
    assert d == datetime.time(6, 12, 0)

    d = utils.time_from_str("06:12")
    assert d == datetime.time(6, 12, 0)

    d = utils.time_from_str("14:57")
    assert d == datetime.time(14, 57, 0)


def test_has_globbing():

    assert utils.has_globbing("a*") == True
    assert utils.has_globbing("a?") == True
    assert utils.has_globbing("my_path/a*.grib") == True
    assert utils.has_globbing("my_path/[Aa].grib") == True
    assert utils.has_globbing("my_path/[a-m].grib") == True
    assert utils.has_globbing("my_path/test.grib") == False
    assert utils.has_globbing("my_path/te[st.grib") == False
    assert utils.has_globbing("my_path/tes]t.grib") == False
    # assert utils.has_globbing("my_path/t]e[st.grib") == False