aboutsummaryrefslogtreecommitdiff
blob: 94693d803680167fc9a07f75764739bf59a592e4 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    test_package_db.py
    ~~~~~~~~~~~~~~~~~~
    
    package database test suite
    
    :copyright: (c) 2013 by Jauhien Piatlicki
    :license: GPL-2, see LICENSE for more details.
"""

import json, http.server, os, shutil, tempfile, threading, \
  unittest

from g_sorcery import package_db


class Server(threading.Thread):
    def __init__(self):
        super().__init__()
        server_address = ('127.0.0.1', 8080)
        self.httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
    
    def run(self):
        self.httpd.serve_forever()

    def shutdown(self):
        self.httpd.shutdown()


class TestFileJSON(unittest.TestCase):
    
    def setUp(self):
        self.tempdir = tempfile.TemporaryDirectory()
        self.path = os.path.join(self.tempdir.name, 'tst')
        self.name = 'tst.json'

    def tearDown(self):
        del self.tempdir

    def do_test_read_ok(self, mandatories, value_suffix=""):
        f = package_db.FileJSON(self.path, self.name, mandatories)
        content = f.read()
        for key in mandatories:
            self.assertTrue(key in content)
            if value_suffix:
                value = key + value_suffix
            else:
                value = ""
            self.assertEqual(content[key], value)
        self.assertTrue(os.path.isfile(os.path.join(self.path, self.name)))
        with open(os.path.join(self.path, self.name), 'r') as f:
            content_f = json.load(f)
        self.assertEqual(content, content_f)
        
    def test_read_dir_does_not_exist(self):
        mandatories = ['tst1', 'tst2', 'tst3']
        self.do_test_read_ok(mandatories)

    def test_read_file_does_not_exist(self):
        os.makedirs(self.path)
        mandatories = ['tst1', 'tst2', 'tst3']
        self.do_test_read_ok(mandatories)

    def test_read_all_keys(self):
        os.makedirs(self.path)
        mandatories = ['tst1', 'tst2', 'tst3']
        content = {}
        for key in mandatories:
            content[key] = key + "_v"
        with open(os.path.join(self.path, self.name), 'w') as f:
            json.dump(content, f)
        self.do_test_read_ok(mandatories, "_v")

    def test_read_missing_keys(self):
        os.makedirs(self.path)
        mandatories = ['tst1', 'tst2', 'tst3']
        content = {}
        for key in mandatories:
            content[key] = key + "_v"
        with open(os.path.join(self.path, self.name), 'w') as f:
            json.dump(content, f)
        f = package_db.FileJSON(self.path, self.name, mandatories)
        mandatories.append("tst4")
        self.assertRaises(KeyError, f.read)

    def do_test_write_ok(self):
        mandatories = ['tst1', 'tst2', 'tst3']
        content = {}
        for key in mandatories:
            content[key] = key + '_v'
        f = package_db.FileJSON(self.path, self.name, mandatories)
        f.write(content)
        self.assertTrue(os.path.isfile(os.path.join(self.path, self.name)))
        with open(os.path.join(self.path, self.name), 'r') as f:
            content_f = json.load(f)
        self.assertEqual(content, content_f)

    def test_write_missing_keys(self):
        content = {'tst1' : '', 'tst2' : ''}
        mandatories = ['tst1', 'tst2', 'tst3']
        f = package_db.FileJSON(self.path, self.name, mandatories)
        self.assertRaises(KeyError, f.write, content)

    def test_write_dir_does_not_exist(self):
        self.do_test_write_ok()

    def test_write_file_does_not_exist(self):
        os.makedirs(self.path)
        self.do_test_write_ok()

    def test_write_all_keys(self):
        os.makedirs(self.path)
        mandatories = ['tst11', 'tst12']
        content = {}
        for key in mandatories:
            content[key] = key + "_v"
        with open(os.path.join(self.path, self.name), 'w') as f:
            json.dump(content, f)
        self.do_test_write_ok()


class DummyDB(package_db.PackageDB):
    def __init__(self, directory, packages):
        super().__init__(directory)
        self.packages = packages

    def generate_tree(self):
        for category in [x.category for x in self.packages]:
            self.add_category(category)
        for package in self.packages:
            self.add_package(package)

    def get_real_db_uri(self):
        print(self.db_uri)
        return self.db_uri + '/dummy.tar.gz'


class TestDummyDB(unittest.TestCase):
    
    def setUp(self):
        self.tempdir = tempfile.TemporaryDirectory()
        category1 = 'app-test'
        category2 = 'dev-test'
        self.packages = [package_db.Package(category1, 'test', '0.2'),
            package_db.Package(category1, 'tst', '0.1'),
            package_db.Package(category1, 'dummy', '1'),
            package_db.Package(category2, 'test', '0.1'),
            package_db.Package(category2, 'test', '0.2'),
            package_db.Package(category2, 'tst', '0.1')]

    def tearDown(self):
        del self.tempdir

    def test_manifest(self):
        db = DummyDB(self.tempdir.name, self.packages)
        db.generate()
        self.assertEqual(db.check_manifest(), (True, []))

    def test_read(self):
        db = DummyDB(self.tempdir.name, self.packages)
        db.generate()
        db2 = DummyDB(self.tempdir.name, self.packages)
        db2.read()
        self.assertEqual(db.db, db2.db)

    def test_list_categories(self):
        db = DummyDB(self.tempdir.name, self.packages)
        db.generate()
        categories = list(set([x.category for x in self.packages]))
        self.assertEqual(categories, db.list_categories())

    def test_list_package_names(self):
        db = DummyDB(self.tempdir.name, self.packages)
        db.generate()
        categories = list(set([x.category for x in self.packages]))
        for category in categories:
            package_names = list(set([x.name for x in self.packages if x.category == category]))
            self.assertEqual(package_names, db.list_package_names(category))
        self.assertRaises(Exception, db.list_package_names, 'no_such_category')

    def test_list_package_versions(self):
        db = DummyDB(self.tempdir.name, self.packages)
        db.generate()
        categories = list(set([x.category for x in self.packages]))
        for category in categories:
            package_names = list(set([x.name for x in self.packages if x.category == category]))
            for name in package_names:
                versions = [x.version for x in self.packages if x.category == category and x.name == name]
                self.assertEqual(versions, db.list_package_versions(category, name))
        self.assertRaises(Exception, db.list_package_names, 'no_such_category', 'a')
        self.assertRaises(Exception, db.list_package_names, categories[0], 'no_such_package')

    def test_sync(self):
        src_db = DummyDB(os.path.join(self.tempdir.name, 'src_testdb'), self.packages)
        src_db.generate()

        prev = os.getcwd()
        os.chdir(self.tempdir.name)
        os.system('tar cvzf dummy.tar.gz src_testdb')
        
        server = Server()
        server.start()

        db = DummyDB(os.path.join(self.tempdir.name, 'testdb'), self.packages)
        db.sync(db_uri='127.0.0.1:8080')
        
        server.shutdown()
        server.join()

        os.chdir(prev)

        self.assertEqual(src_db.db, db.db)

    def test_sync_fail(self):
        db = DummyDB(os.path.join(self.tempdir.name, 'testdb'), self.packages)
        self.assertRaises(Exception, db.sync, db_uri='127.0.0.1:8080')

            
def suite():
    suite = unittest.TestSuite()
    suite.addTest(TestFileJSON('test_read_dir_does_not_exist'))
    suite.addTest(TestFileJSON('test_read_file_does_not_exist'))
    suite.addTest(TestFileJSON('test_read_all_keys'))
    suite.addTest(TestFileJSON('test_read_missing_keys'))
    suite.addTest(TestFileJSON('test_write_missing_keys'))
    suite.addTest(TestFileJSON('test_write_dir_does_not_exist'))
    suite.addTest(TestFileJSON('test_write_file_does_not_exist'))
    suite.addTest(TestFileJSON('test_write_all_keys'))
    suite.addTest(TestDummyDB('test_manifest'))
    suite.addTest(TestDummyDB('test_read'))
    suite.addTest(TestDummyDB('test_list_categories'))
    suite.addTest(TestDummyDB('test_list_package_names'))
    suite.addTest(TestDummyDB('test_list_package_versions'))
    suite.addTest(TestDummyDB('test_sync'))
    suite.addTest(TestDummyDB('test_sync_fail'))
    return suite