File: test_backup.py

package info (click to toggle)
python-gammu 3.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,024 kB
  • sloc: ansic: 11,446; python: 3,323; sql: 527; makefile: 6
file content (122 lines) | stat: -rw-r--r-- 4,218 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# vim: expandtab sw=4 ts=4 sts=4:
#
# Copyright © 2003 - 2018 Michal Čihař <michal@cihar.com>
#
# This file is part of python-gammu <https://wammu.eu/python-gammu/>
#
# 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.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import glob
import os
import os.path
import tempfile
import unittest

import gammu

TEST_DIR = os.path.join(os.path.dirname(__file__), "data")
TEST_FILES_CALENDAR = glob.glob(os.path.join(TEST_DIR, "*.ics")) + glob.glob(
    os.path.join(TEST_DIR, "*.vcs")
)
TEST_FILES_CONTACTS = glob.glob(os.path.join(TEST_DIR, "*.vcf"))
TEST_CONTACTS = (".lmb", ".vcf", ".backup")
TEST_CALENDAR = (".vcs", ".ics", ".backup")


class BackupTest(unittest.TestCase):
    def perform_test(self, filename, extensions):
        out_files = [
            tempfile.NamedTemporaryFile(suffix=extension, delete=False)
            for extension in extensions
        ]
        out_backup = tempfile.NamedTemporaryFile(suffix=".backup", delete=False)
        # Close all files (needed on Windows)
        for handle in out_files:
            handle.close()
        out_backup.close()
        try:
            backup = gammu.ReadBackup(filename)
            for out in out_files:
                # Save to new format
                gammu.SaveBackup(out.name, backup)

                # Parse created file
                backup_2 = gammu.ReadBackup(out.name)

                # Check content length
                self.assertEqual(
                    len(backup["Calendar"]),
                    len(backup_2["Calendar"]),
                    f"Failed to compare calendar in {filename}",
                )
                self.assertEqual(
                    len(backup["PhonePhonebook"]) + len(backup["SIMPhonebook"]),
                    len(backup_2["PhonePhonebook"]) + len(backup_2["SIMPhonebook"]),
                    f"Failed to compare phonebook in {filename}",
                )

                # Try converting to .backup
                gammu.SaveBackup(out_backup.name, backup)
        finally:
            for handle in out_files:
                os.unlink(handle.name)
            os.unlink(out_backup.name)

    def test_convert_contacts(self):
        for filename in TEST_FILES_CONTACTS:
            self.perform_test(filename, TEST_CONTACTS)

    def test_convert_calendar(self):
        for filename in TEST_FILES_CALENDAR:
            self.perform_test(filename, TEST_CALENDAR)

    def test_calendar(self):
        entry = gammu.ReadBackup(os.path.join(TEST_DIR, "rrule.ics"))["Calendar"][0]

        # Convert it to vCard
        vc_entry = gammu.EncodeVCALENDAR(entry)
        ic_entry = gammu.EncodeICALENDAR(entry)

        # Convert it back to entry
        entry2 = gammu.DecodeVCS(vc_entry)
        entry3 = gammu.DecodeICS(ic_entry)

        self.assertEqual(entry2["Type"], entry3["Type"])

    def test_todo(self):
        entry = gammu.ReadBackup(os.path.join(TEST_DIR, "02.vcs"))["ToDo"][0]

        # Convert it to vCard
        vt_entry = gammu.EncodeVTODO(entry)
        it_entry = gammu.EncodeITODO(entry)

        # Convert it back to entry
        entry2 = gammu.DecodeVCS(vt_entry)
        entry3 = gammu.DecodeICS(it_entry)

        self.assertEqual(entry2["Type"], entry3["Type"])

    def test_contact(self):
        entry = gammu.ReadBackup(os.path.join(TEST_DIR, "gammu.vcf"))["PhonePhonebook"][
            0
        ]

        # Convert it to vCard
        vc_entry = gammu.EncodeVCARD(entry)

        # Convert it back to entry
        entry2 = gammu.DecodeVCARD(vc_entry)

        self.assertEqual(entry, entry2)