1# -*- coding: utf-8 -*-
2# This file is part of beets.
3#
4# Permission is hereby granted, free of charge, to any person obtaining
5# a copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish,
8# distribute, sublicense, and/or sell copies of the Software, and to
9# permit persons to whom the Software is furnished to do so, subject to
10# the following conditions:
11#
12# The above copyright notice and this permission notice shall be
13# included in all copies or substantial portions of the Software.
14
15from __future__ import division, absolute_import, print_function
16
17from mock import patch, Mock
18
19from beets import library
20from beets.util import bytestring_path, _fsencoding
21from beetsplug.ipfs import IPFSPlugin
22
23import unittest
24import os
25
26from test import _common
27from test.helper import TestHelper
28
29
30@patch('beets.util.command_output', Mock())
31class IPFSPluginTest(unittest.TestCase, TestHelper):
32
33    def setUp(self):
34        self.setup_beets()
35        self.load_plugins('ipfs')
36        self.lib = library.Library(":memory:")
37
38    def tearDown(self):
39        self.unload_plugins()
40        self.teardown_beets()
41
42    def test_stored_hashes(self):
43        test_album = self.mk_test_album()
44        ipfs = IPFSPlugin()
45        added_albums = ipfs.ipfs_added_albums(self.lib, self.lib.path)
46        added_album = added_albums.get_album(1)
47        self.assertEqual(added_album.ipfs, test_album.ipfs)
48        found = False
49        want_item = test_album.items()[2]
50        for check_item in added_album.items():
51            try:
52                if check_item.ipfs:
53                    ipfs_item = os.path.basename(want_item.path).decode(
54                        _fsencoding(),
55                    )
56                    want_path = '/ipfs/{0}/{1}'.format(test_album.ipfs,
57                                                       ipfs_item)
58                    want_path = bytestring_path(want_path)
59                    self.assertEqual(check_item.path, want_path)
60                    self.assertEqual(check_item.ipfs, want_item.ipfs)
61                    self.assertEqual(check_item.title, want_item.title)
62                    found = True
63            except AttributeError:
64                pass
65        self.assertTrue(found)
66
67    def mk_test_album(self):
68        items = [_common.item() for _ in range(3)]
69        items[0].title = 'foo bar'
70        items[0].artist = '1one'
71        items[0].album = 'baz'
72        items[0].year = 2001
73        items[0].comp = True
74        items[1].title = 'baz qux'
75        items[1].artist = '2two'
76        items[1].album = 'baz'
77        items[1].year = 2002
78        items[1].comp = True
79        items[2].title = 'beets 4 eva'
80        items[2].artist = '3three'
81        items[2].album = 'foo'
82        items[2].year = 2003
83        items[2].comp = False
84        items[2].ipfs = 'QmfM9ic5LJj7V6ecozFx1MkSoaaiq3PXfhJoFvyqzpLXSk'
85
86        for item in items:
87            self.lib.add(item)
88
89        album = self.lib.add_album(items)
90        album.ipfs = "QmfM9ic5LJj7V6ecozFx1MkSoaaiq3PXfhJoFvyqzpLXSf"
91        album.store()
92
93        return album
94
95
96def suite():
97    return unittest.TestLoader().loadTestsFromName(__name__)
98
99if __name__ == '__main__':
100    unittest.main(defaultTest='suite')
101