File: test_util_json_data.py

package info (click to toggle)
quodlibet 4.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,228 kB
  • sloc: python: 89,728; sh: 381; xml: 110; makefile: 91
file content (106 lines) | stat: -rw-r--r-- 3,656 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
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import json
import os

import pytest

from quodlibet.util.json_data import JSONObjectDict, JSONObject
from . import TestCase, mkstemp
from .helper import capture_output

Field = JSONObject.Field


class TJsonData(TestCase):
    class WibbleData(JSONObject):
        """Test subclass"""

        FIELDS = {
            "name": Field("h name", "name"),
            "pattern": Field("h pattern", "pattern for stuff"),
            "wibble": Field("h wibble", "wobble"),
        }

        def __init__(self, name=None, pattern=None, wibble=False):
            JSONObject.__init__(self, name)
            self.pattern = pattern
            self.wibble = wibble
            self._dont_wibble = not wibble

    WIBBLE_JSON_STR = """{
            "foo":{"name":"foo", "pattern":"echo '<~artist~title>.mp3'"},
            "bar":{"name":"bar", "wibble":true}
    }"""

    def test_json_object(self):
        blah = JSONObject("blah")
        assert blah.name == "blah"
        assert blah.data == {"name": "blah"}
        assert blah.json == '{"name": "blah"}'

    def test_field(self):
        blah = self.WibbleData("blah")
        assert blah.field("wibble").doc == "wobble"
        assert not blah.field("not_here").doc
        assert blah.field("pattern").human_name == "h pattern"

    def test_nameless_construction(self):
        with pytest.raises(TypeError):
            JSONObject()

    def test_subclass(self):
        blah = self.WibbleData("blah")
        assert blah.name == "blah"
        exp = {"name": "blah", "pattern": None, "wibble": False}
        assert dict(blah.data) == exp
        assert json.loads(blah.json) == exp

    def test_from_invalid_json(self):
        # Invalid JSON
        with capture_output():
            jd = JSONObjectDict.from_json(JSONObject, '{"foo":{}')
            assert not jd
            # Valid but unexpected Command field
            assert not JSONObjectDict.from_json(
                JSONObject, '{"bar":{"name":"bar", "invalid":"foo"}'
            )

        def test_subclass_from_json(self):
            coms = JSONObjectDict.from_json(self.WibbleData, self.WIBBLE_JSON_STR)
            assert len(coms) == 2
            assert coms["foo"].__class__ == self.WibbleData

        def test_save_all(self):
            data = JSONObjectDict.from_json(self.WibbleData, self.WIBBLE_JSON_STR)
            fd, filename = mkstemp(suffix=".json")
            os.close(fd)
            try:
                ret = data.save(filename)
                with open(filename, "rb") as f:
                    jstr = f.read()
                # Check we also return the string as documented...
                assert jstr == ret
            finally:
                os.unlink(filename)

            jstr = jstr.decode("utf-8")

            # Check we have the right number of items
            assert len(json.loads(jstr)) == len(data)

            # Check them one by one (for convenience of debugging)
            parsed = JSONObjectDict.from_json(self.WibbleData, jstr)
            for o in data.values():
                assert parsed[o.name] == o

        def test_from_list(self):
            baz_man = JSONObject("baz man!")
            lst = [JSONObject("foo"), JSONObject("bar"), baz_man]
            data = JSONObjectDict.from_list(lst)
            assert len(data) == len(lst)
            assert "baz man!" in data
            assert baz_man == data["baz man!"]