File: test_db.py

package info (click to toggle)
fangfrisch 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 804 kB
  • sloc: python: 1,326; sh: 113; makefile: 64; sql: 24
file content (127 lines) | stat: -rw-r--r-- 3,972 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
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
"""
Copyright © 2020-2025 Ralph Seichter

This file is part of "Fangfrisch".

Fangfrisch 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.

Fangfrisch 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 Fangfrisch. If not, see <https://www.gnu.org/licenses/>.
"""

import tempfile
import unittest
import uuid

from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound

from fangfrisch.db import DbMeta
from fangfrisch.db import RefreshLog
from tests import DIGEST_DUMMY
from tests import FangfrischTest
from tests import _ClamavTestItem

URL1 = "https://u1"
URL2 = "https://u2"


class DbTests(FangfrischTest):
    s = None

    def setUp(self) -> None:
        super().setUp()
        self.ci = _ClamavTestItem(url=URL1, section=self.UNITTEST, option="option", path="path")
        RefreshLog.init()
        self.s = RefreshLog._session()
        self.s.query(RefreshLog).delete()
        self.s.add(RefreshLog(self.ci, DIGEST_DUMMY))
        self.s.commit()

    def test_version_match(self):
        self.assertTrue(DbMeta.assert_version_match())

    def test_version_mismatch(self):
        session = DbMeta._session()
        dm: DbMeta = session.query(DbMeta).one()
        dm.db_version = -1
        session.add(dm)
        session.commit()
        with self.assertRaises(SystemExit):
            DbMeta.assert_version_match()

    def test_version_missing(self):
        session = DbMeta._session()
        session.query(DbMeta).delete()
        session.commit()
        with self.assertRaises(NoResultFound):
            DbMeta.assert_version_match()

    def test_create_metadata1(self):
        session = DbMeta._session()
        session.query(DbMeta).delete()
        session.commit()
        self.assertTrue(DbMeta().create_metadata(False))

    def test_create_metadata2(self):
        with self.assertRaises(SystemExit):
            DbMeta().create_metadata(False)

    def test_create_metadata3(self):
        self.assertTrue(DbMeta().create_metadata(True))

    def test_duplicate(self):
        self.s.add(RefreshLog(self.ci, DIGEST_DUMMY))
        with self.assertRaises(IntegrityError):
            self.s.commit()

    def test_missing_path(self):
        self.ci.path = None
        self.s.add(RefreshLog(self.ci, DIGEST_DUMMY))
        with self.assertRaises(IntegrityError):
            self.s.commit()

    def test_insert(self):
        self.ci.url = URL2
        self.s.add(RefreshLog(self.ci, DIGEST_DUMMY))
        self.s.commit()
        self.assertTrue(True)  # Must not raise an exception

    def test_refresh_required(self):
        self.assertTrue(RefreshLog.is_outdated(URL1, 0))

    def test_stamp1(self):
        RefreshLog.update(self.ci, DIGEST_DUMMY)  # Must not raise an exception
        self.assertTrue(True)

    def test_stamp2(self):
        self.ci.url = URL2
        RefreshLog.update(self.ci, DIGEST_DUMMY)  # Must not raise an exception
        self.assertTrue(True)

    def test_cleanup1(self):
        self.assertEqual(0, RefreshLog.cleanup_provider(self.UNKNOWN))

    def test_cleanup2(self):
        file = tempfile.NamedTemporaryFile(mode="w+t", encoding="utf-8", delete=False)
        file.write(self.UNITTEST)
        file.close()
        provider = uuid.uuid4().hex
        self.ci.section = provider
        self.ci.path = file.name
        self.ci.url = URL2
        self.s.add(RefreshLog(self.ci, DIGEST_DUMMY))
        self.s.commit()
        self.assertEqual(1, RefreshLog.cleanup_provider(provider))


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