summaryrefslogtreecommitdiff
path: root/tests/watcher_test.py
blob: 5f92d36ac4a49643cee7fda1485d8b01f52ec709 (plain)
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
"""Tests for the watcher module."""
from os import remove
from os.path import dirname, join, realpath
from shutil import copy, move, rmtree
from tempfile import mkdtemp
import unittest

import mutagen
import six

from db.db_manager import DbManager
from models.track import Track
from watcher import LibraryWatcher


if six.PY2:
    def _u(string):
        return unicode(string, encoding="utf_8")
else:
    def _u(string):
        return string


class WatcherTestCase(unittest.TestCase):
    """Defines tests for the watcher module.

    Extends:
        unittest.Testcase

    """

    @classmethod
    def setUpClass(cls):
        """Set up fixtures for the tests."""
        cls._tempdir = mkdtemp()
        cls._tempdbdir = mkdtemp()
        cls._db_path = join(cls._tempdbdir, "test.db")
        copy(join(dirname(realpath(__file__)), "test.db"), cls._db_path)
        cls._db = DbManager(cls._db_path)
        cls._watcher = LibraryWatcher(cls._tempdir, cls._db_path)

    @classmethod
    def tearDownClass(cls):
        """Remove test fixtures."""
        cls._watcher.stop()
        rmtree(cls._tempdbdir)
        rmtree(cls._tempdir)

    def test_watcher_actions(self):
        """Test creating, moving, modifying and deleting a file."""
        new_file = join(dirname(realpath(__file__)), "testnew.ogg")
        copy(new_file, self._tempdir)

        self._watcher.check_for_events()

        found_track = Track.find_by_path(join(self._tempdir, "testnew.ogg"),
                                         self._db)
        assert found_track
        assert "testnew.ogg" in found_track.filename
        assert found_track.artists
        found_artist = False
        for artist in found_track.artists:
            if _u("Art Ist") == artist.name:
                found_artist = True
                break

        assert found_artist

        original_file = join(self._tempdir, "testnew.ogg")
        moved_file = join(self._tempdir, "testmoved.ogg")
        move(original_file, moved_file)

        self._watcher.check_for_events()

        moved_track = Track.find_by_path(moved_file, self._db)

        assert moved_track
        assert "testmoved.ogg" in moved_track.filename

        original_metadata = mutagen.File(moved_file, easy=True)

        original_metadata["title"] = [_u("New title")]
        original_metadata.save()

        self._watcher.check_for_events()

        modified_track = Track.find_by_path(moved_file, self._db)

        assert modified_track
        assert modified_track.name == "New title"

        remove(moved_file)

        self._watcher.check_for_events()

        assert Track.find_by_path(moved_file, self._db) is None