chiark / gitweb /
9dfe2bbcdf1adf94180959cde2ebe21b0bc7b6f6
[fdroidserver.git] / tests / metadata.TestCase
1 #!/usr/bin/env python3
2
3 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
4
5 import inspect
6 import optparse
7 import os
8 import pickle
9 import sys
10 import unittest
11
12 localmodule = os.path.realpath(
13     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
14 print('localmodule: ' + localmodule)
15 if localmodule not in sys.path:
16     sys.path.insert(0, localmodule)
17
18 import fdroidserver.common
19 import fdroidserver.metadata
20
21
22 class MetadataTest(unittest.TestCase):
23     '''fdroidserver/metadata.py'''
24
25     def test_read_metadata(self):
26         testsdir = os.path.dirname(__file__)
27         os.chdir(testsdir)
28
29         self.maxDiff = None
30
31         # these need to be set to prevent code running on None, only
32         # 'accepted_formats' is actually used in metadata.py
33         config = dict()
34         config['sdk_path'] = '/opt/android-sdk'
35         config['ndk_paths'] = dict()
36         config['accepted_formats'] = ['json', 'txt', 'xml', 'yaml']
37         fdroidserver.common.config = config
38
39         apps = fdroidserver.metadata.read_metadata(xref=True)
40         for appid in ('org.smssecure.smssecure', 'org.adaway', 'net.osmand.plus', 'org.videolan.vlc'):
41             app = apps[appid]
42             savepath = os.path.join('metadata', appid + '.pickle')
43             frommeta = app.field_dict()
44             self.assertTrue(appid in apps)
45             with open(savepath, 'rb') as f:
46                 frompickle = pickle.load(f)
47             self.assertEquals(frommeta, frompickle)
48             # Uncomment to overwrite
49             # with open(savepath, 'wb') as f:
50             #     pickle.dump(frommeta, f)
51
52
53 if __name__ == "__main__":
54     parser = optparse.OptionParser()
55     parser.add_option("-v", "--verbose", action="store_true", default=False,
56                       help="Spew out even more information than normal")
57     (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
58
59     newSuite = unittest.TestSuite()
60     newSuite.addTest(unittest.makeSuite(MetadataTest))
61     unittest.main()