File: test_smsd.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 (133 lines) | stat: -rw-r--r-- 3,841 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
123
124
125
126
127
128
129
130
131
132
133
# 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 os
import os.path
import platform
import sqlite3
import threading
import time
import unittest

import gammu.smsd

from .test_dummy import DummyTest

MESSAGE_1 = {
    "Text": "python-gammu testing message",
    "SMSC": {"Location": 1},
    "Number": "1234567890",
}
MESSAGE_2 = {
    "Text": "python-gammu second testing message",
    "SMSC": {"Location": 1},
    "Number": "1234567890",
}


def get_script():
    """Returns SQL script to create database

    It returns correct script matching used Gammu version.
    """
    version = tuple(int(x) for x in gammu.Version()[0].split("."))

    if version < (1, 36, 7):
        dbver = 14
    elif version < (1, 37, 90):
        dbver = 15
    elif version < (1, 38, 5):
        dbver = 16
    else:
        dbver = 17

    print(f"Gammu version {version}, SMSD DB version {dbver}")

    return os.path.join(os.path.dirname(__file__), "data", f"sqlite-{dbver}.sql")


class SMSDDummyTest(DummyTest):
    def setUp(self):
        if platform.system() == "Windows":
            raise unittest.SkipTest(
                "SMSD testing not supported on Windows (no DBI driver)"
            )
        super().setUp()
        database = sqlite3.connect(os.path.join(self.test_dir, "smsd.db"))
        with open(get_script()) as handle:
            database.executescript(handle.read())

    def get_smsd(self):
        return gammu.smsd.SMSD(self.config_name)

    def test_init_error(self):
        self.assertRaises(TypeError, gammu.smsd.SMSD, Bar=1)

    def test_inject(self):
        smsd = self.get_smsd()
        smsd.InjectSMS([MESSAGE_1])

    def test_smsd(self):
        smsd = self.get_smsd()

        # Inject SMS messages
        # Please note that SMSD is not thread safe, so you can not
        # use inject and main loop from different threads
        smsd.InjectSMS([MESSAGE_1])
        smsd.InjectSMS([MESSAGE_2])

        try:
            # Start SMSD thread
            smsd_thread = threading.Thread(target=smsd.MainLoop)
            smsd_thread.start()
            # We need to let it run for some time here to finish initialization
            time.sleep(10)

            # Show SMSD status
            retries = 0
            while retries < 2:
                status = smsd.GetStatus()
                if status["Sent"] >= 2:
                    break
                time.sleep(10)
                retries += 1

            self.assertEqual(
                status["Received"],
                2,
                "Messages were not received as expected ({:d})!".format(
                    status["Received"]
                ),
            )
            self.assertEqual(
                status["Sent"],
                2,
                "Messages were not sent as expected ({:d})!".format(status["Sent"]),
            )

            time.sleep(1)

        finally:
            # Signal SMSD to stop
            smsd.Shutdown()

            # Wait for it
            smsd_thread.join()