File: test_loaders_data.py

package info (click to toggle)
python-moderngl-window 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,096 kB
  • sloc: python: 12,076; makefile: 21
file content (58 lines) | stat: -rw-r--r-- 2,523 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
from pathlib import Path
from unittest import TestCase

from moderngl_window import resources
from moderngl_window.exceptions import ImproperlyConfigured
from moderngl_window.meta import DataDescription

resources.register_dir((Path(__file__).parent / 'fixtures' / 'resources').resolve())


class DataLoaderTestcase(TestCase):

    def test_txt(self):
        """Ensure correct loader is selected by looking at file extension (txt)"""
        text = resources.data.load(DataDescription(path='data/data.txt'))
        self.assertEqual(text, "Hello")

    def test_json(self):
        """Ensure correct loader is selected by looking at file extension (json)"""
        json = resources.data.load(DataDescription(path='data/data.json'))
        self.assertEqual(json, {"test": "Hello"})

    def test_binary_kind(self):
        """Loading a binary file"""
        data = resources.data.load(DataDescription(path='data/data.bin', kind="binary"))
        self.assertEqual(data, b'Hello')

    def test_text_kind(self):
        """Load a e textfile"""
        text = resources.data.load(DataDescription(path='data/data.txt', kind="text"))
        self.assertEqual(text, "Hello")

    def test_json_kind(self):
        """Load a json file"""
        json = resources.data.load(DataDescription(path='data/data.json', kind="json"))
        self.assertEqual(json, {"test": "Hello"})

    def test_txt_not_found(self):
        """Ensure ImproperlyConfigured is raised if file is not found"""
        with self.assertRaises(ImproperlyConfigured):
            resources.data.load(DataDescription(path='data/notfound.txt'))

    def test_json_not_found(self):
        """Ensure ImproperlyConfigured is raised if file is not found"""
        with self.assertRaises(ImproperlyConfigured):
            resources.data.load(DataDescription(path='data/notfound.json'))

    def test_binary_not_found(self):
        """Ensure ImproperlyConfigured is raised if file is not found"""
        with self.assertRaises(ImproperlyConfigured):
            resources.data.load(DataDescription(path='data/notfound.bin', kind="binary"))

    def test_binary_abspath(self):
        """Strip search directories and use absolute path"""
        path = (Path(__file__).parent / "fixtures/resources/data/data.json").resolve()
        with resources.temporary_dirs([]):
            json = resources.data.load(DataDescription(path=path, kind="json"))
            self.assertEqual(json, {"test": "Hello"})