File: test_nodepickler.py

package info (click to toggle)
rdflib 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 38,248 kB
  • sloc: python: 39,216; sh: 153; makefile: 110
file content (54 lines) | stat: -rw-r--r-- 1,182 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
import unittest
import pickle

from rdflib.term import Literal

from rdflib.store import NodePickler

# same as nt/more_literals.nt
cases = [
    "no quotes",
    "single ' quote",
    'double " quote',
    'triple """ quotes',
    'mixed "\'""" quotes',
    '"',
    "'",
    '"\'"',
    "\\",  # len 1
    '\\"',  # len 2
    '\\\\"',  # len 3
    '\\"\\',  # len 3
    '<a some="typical" html="content">here</a>',
]


class UtilTestCase(unittest.TestCase):
    def test_to_bits_from_bits_round_trip(self):
        np = NodePickler()

        a = Literal(
            """A test with a \\n (backslash n), "\u00a9" , and newline \n and a second line.
"""
        )
        b = np.loads(np.dumps(a))
        self.assertEqual(a, b)

    def test_literal_cases(self):
        np = NodePickler()

        for l in cases:
            a = Literal(l)
            b = np.loads(np.dumps(a))
            self.assertEqual(a, b)

    def test_pickle(self):
        np = NodePickler()
        dump = pickle.dumps(np)
        np2 = pickle.loads(dump)
        self.assertEqual(np._ids, np2._ids)
        self.assertEqual(np._objects, np2._objects)


if __name__ == "__main__":
    unittest.main()