File: test_json.py

package info (click to toggle)
pymoc 0.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: python: 1,428; sh: 25; makefile: 4
file content (78 lines) | stat: -rw-r--r-- 2,564 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
# Copyright (C) 2014 Science and Technology Facilities Council.
# Copyright (C) 2017-2024 East Asian Observatory.
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

from io import BytesIO
from unittest import TestCase

from pymoc import MOC
from pymoc.io.json import read_moc_json, write_moc_json


class JSONTestCase(TestCase):
    def test_json(self):
        test_json = b'{"1":[1,2,4],"2":[12,13,14,21,23,25]}'
        in_ = BytesIO(test_json)

        moc = MOC()
        read_moc_json(moc, file=in_)

        self.assertEqual(moc.order, 2)
        self.assertEqual(moc[0], frozenset())
        self.assertEqual(moc[1], frozenset([1, 2, 4]))
        self.assertEqual(moc[2], frozenset([12, 13, 14, 21, 23, 25]))

        out = BytesIO()
        write_moc_json(moc, file=out)

        self.assertEqual(out.getvalue(), test_json)

    def test_json_trailing(self):
        # Check MOC 1.1 addition of trailing section to
        # signify the MOCORDER.
        in_ = BytesIO(b'{"13":[5,6,7],"14":[]}')

        moc = MOC()
        read_moc_json(moc, file=in_)

        self.assertEqual(moc[13], frozenset([5, 6, 7]))
        self.assertEqual(moc[14], frozenset())

    def test_json_large(self):
        orig = MOC()
        orig.add(29, [
            3458700000000000000, 3458700000000000007,
            3458700000000000008, 3458700000000000009,
        ])

        out = BytesIO()
        write_moc_json(orig, file=out)
        json = out.getvalue()

        self.assertEqual(
            json, b'{"29":[3458700000000000000,3458700000000000007,'
            b'3458700000000000008,3458700000000000009]}')

        copy = MOC()
        in_ = BytesIO(json)
        read_moc_json(copy, file=in_)

        self.assertEqual(copy.order, 29)
        self.assertEqual(copy.cells, 4)
        self.assertEqual(copy[29], frozenset([
            3458700000000000000, 3458700000000000007,
            3458700000000000008, 3458700000000000009]))