File: test_unicode.py

package info (click to toggle)
organize 3.3.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,540 kB
  • sloc: python: 5,837; makefile: 4
file content (108 lines) | stat: -rw-r--r-- 2,659 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
from pathlib import Path

import pytest
from conftest import make_files, read_files

from organize import Config
from organize.utils import normalize_unicode


def test_startswith_issue74(fs):
    # test for issue https://github.com/tfeldmann/organize/issues/74
    make_files(
        {
            "Cálculo_1.pdf": "",
            "Cálculo_2.pdf": "",
            "Calculo.pdf": "",
        },
        "test",
    )
    config = r"""
        # Cálculo PDF
        rules:
            - locations: /test
              filters:
                - extension:
                    - pdf
                - name:
                    startswith: Cálculo
              actions:
                - move: "/test/Cálculo Integral/Periodo #6/PDF's/"
        """
    Config.from_string(config).execute(simulate=False)
    assert read_files("test") == {
        "Cálculo Integral": {
            "Periodo #6": {
                "PDF's": {
                    "Cálculo_1.pdf": "",
                    "Cálculo_2.pdf": "",
                }
            }
        },
        "Calculo.pdf": "",
    }


def test_folder_umlauts(fs):
    make_files(["file1", "file2"], "Erträge")

    conf = Path("config.yaml")
    conf.write_text(
        """
    rules:
      - locations: "Erträge"
        actions:
          - delete
    """,
        encoding="utf-8",
    )
    Config.from_path(conf).execute(simulate=False)
    assert read_files("Erträge") == {}


CONFUSABLES = (
    (
        b"Ertr\xc3\xa4gnisaufstellung".decode("utf-8"),
        b"Ertra\xcc\x88gnisaufstellung".decode("utf-8"),
    ),
    (
        b"Ertra\xcc\x88gnisaufstellung".decode("utf-8"),
        b"Ertr\xc3\xa4gnisaufstellung".decode("utf-8"),
    ),
)


@pytest.mark.parametrize("a, b", CONFUSABLES)
def test_normalize(a, b):
    assert a != b
    assert normalize_unicode(a) == normalize_unicode(b)


@pytest.mark.parametrize("a, b", CONFUSABLES)
def test_normalization_regex(fs, a, b):
    make_files({f"{a}.txt": ""}, "test")
    config = f"""
    rules:
      - locations: /test
        filters:
          - regex: {b}
        actions:
          - rename: "found-regex.txt"
    """
    Config.from_string(config).execute(simulate=False)
    assert read_files("test") == {"found-regex.txt": ""}


@pytest.mark.parametrize("a, b", CONFUSABLES)
def test_normalization_filename(fs, a, b):
    make_files({f"{a}.txt": ""}, "test")
    config = f"""
    rules:
      - locations: /test
        filters:
          - name: {a}
        actions:
          - rename: "found-regex.txt"
    """
    Config.from_string(config).execute(simulate=False)
    assert read_files("test") == {"found-regex.txt": ""}