Skip to content

Commit ce7170e

Browse files
committed
Use pytest for the unit tests.
1 parent 7969ada commit ce7170e

File tree

1 file changed

+98
-87
lines changed

1 file changed

+98
-87
lines changed

tests/test_libzim.py

Lines changed: 98 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,56 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19-
import unittest
19+
import pytest
2020
import os,sys,inspect
2121

22-
# Import local libzim module from parent
23-
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
24-
parent_dir = os.path.dirname(current_dir)
25-
sys.path.insert(0, parent_dir)
26-
2722
from libzim import ZimArticle, ZimBlob, ZimCreator
2823

2924
# test files https://wiki.kiwix.org/wiki/Content_in_all_languages
3025

3126

3227
# https://wiki.openzim.org/wiki/Metadata
33-
34-
TEST_METADATA = {
35-
# Mandatory
36-
"Name" : "wikipedia_fr_football",
37-
"Title": "English Wikipedia",
38-
"Creator": "English speaking Wikipedia contributors",
39-
"Publisher": "Wikipedia user Foobar",
40-
"Date": "2009-11-21",
41-
"Description": "All articles (without images) from the english Wikipedia",
42-
"Language": "eng",
43-
# Optional
44-
"Longdescription": "This ZIM file contains all articles (without images) from the english Wikipedia by 2009-11-10. The topics are ...",
45-
"Licence": "CC-BY",
46-
"Tags": "wikipedia;_category:wikipedia;_pictures:no;_videos:no;_details:yes;_ftindex:yes",
47-
"Flavour": "nopic",
48-
"Source": "https://en.wikipedia.org/",
49-
"Counter": "image/jpeg=5;image/gif=3;image/png=2",
50-
"Scraper": "sotoki 1.2.3"
51-
}
28+
@pytest.fixture(scope="session")
29+
def metadata():
30+
return {
31+
# Mandatory
32+
"Name" : "wikipedia_fr_football",
33+
"Title": "English Wikipedia",
34+
"Creator": "English speaking Wikipedia contributors",
35+
"Publisher": "Wikipedia user Foobar",
36+
"Date": "2009-11-21",
37+
"Description": "All articles (without images) from the english Wikipedia",
38+
"Language": "eng",
39+
# Optional
40+
"Longdescription": "This ZIM file contains all articles (without images) from the english Wikipedia by 2009-11-10. The topics are ...",
41+
"Licence": "CC-BY",
42+
"Tags": "wikipedia;_category:wikipedia;_pictures:no;_videos:no;_details:yes;_ftindex:yes",
43+
"Flavour": "nopic",
44+
"Source": "https://en.wikipedia.org/",
45+
"Counter": "image/jpeg=5;image/gif=3;image/png=2",
46+
"Scraper": "sotoki 1.2.3"
47+
}
48+
49+
@pytest.fixture(scope="session")
50+
def article_content():
51+
content = '''<!DOCTYPE html>
52+
<html class="client-js">
53+
<head><meta charset="UTF-8">
54+
<title>Monadical</title>
55+
</head>
56+
<h1> ñññ Hello, it works ñññ </h1></html>'''
57+
url = "A/Monadical_SAS"
58+
title = "Monadical SAS"
59+
mime_type = "text/html"
60+
return (content, url, title, mime_type)
5261

5362
class ZimTestArticle(ZimArticle):
54-
content = '''<!DOCTYPE html>
55-
<html class="client-js">
56-
<head><meta charset="UTF-8">
57-
<title>Monadical</title>
58-
</head>
59-
<h1> ñññ Hello, it works ñññ </h1></html>'''
60-
61-
def __init__(self):
63+
def __init__(self, content, url, title, mime_type):
6264
ZimArticle.__init__(self)
65+
self.content = content
66+
self.url = url
67+
self.title = title
68+
self.mime_type = mime_type
6369

6470
def is_redirect(self):
6571
return False
@@ -69,17 +75,17 @@ def can_write(self):
6975
return True
7076

7177
def get_url(self):
72-
return "A/Monadical_SAS"
78+
return self.url
7379

7480
def get_title(self):
75-
return "Monadical SAS"
76-
81+
return self.title
82+
7783
def get_mime_type(self):
78-
return "text/html"
79-
84+
return self.mime_type
85+
8086
def get_filename(self):
8187
return ""
82-
88+
8389
def should_compress(self):
8490
return True
8591

@@ -88,51 +94,56 @@ def should_index(self):
8894

8995
def get_data(self):
9096
return ZimBlob(self.content.encode('UTF-8'))
91-
92-
93-
class TestZimCreator(unittest.TestCase):
94-
def setUp(self):
95-
self.test_zim_file_path = "/tmp/python-libzim/tests/kiwix-test"
96-
97-
# Test article
98-
self.test_article = ZimTestArticle()
99-
100-
def tearDown(self):
101-
pass
102-
103-
def _assert_article_properties(self, written_article, article):
104-
pass
105-
106-
def _add_article_to_test_zim_file_read_it_back(self, article, delete_zim_file=True):
107-
pass
108-
109-
def test_write_article(self):
110-
import uuid
111-
rnd_str = str(uuid.uuid1())
112-
zim_creator = ZimCreator(self.test_zim_file_path + '-' + rnd_str + '.zim',main_page = "welcome",index_language= "eng", min_chunk_size= 2048)
113-
zim_creator.add_article(self.test_article)
114-
# Set mandatory metadata
115-
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
116-
zim_creator.close()
117-
118-
def test_article_metadata(self):
119-
import uuid
120-
rnd_str = str(uuid.uuid1())
121-
zim_creator = ZimCreator(self.test_zim_file_path + '-' + rnd_str + '.zim',main_page = "welcome",index_language= "eng", min_chunk_size= 2048)
122-
zim_creator.update_metadata(**TEST_METADATA)
123-
self.assertEqual(zim_creator._metadata, TEST_METADATA)
124-
zim_creator.close()
125-
126-
def test_check_mandatory_metadata(self):
127-
import uuid
128-
rnd_str = str(uuid.uuid1())
129-
zim_creator = ZimCreator(self.test_zim_file_path + '-' + rnd_str + '.zim',main_page = "welcome",index_language= "eng", min_chunk_size= 2048)
130-
self.assertFalse(zim_creator.mandatory_metadata_ok())
131-
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
132-
self.assertTrue(zim_creator.mandatory_metadata_ok())
133-
zim_creator.close()
134-
135-
136-
137-
if __name__ == '__main__':
138-
unittest.main()
97+
98+
@pytest.fixture(scope="session")
99+
def article(article_content):
100+
return ZimTestArticle(*article_content)
101+
102+
103+
def test_write_article(tmpdir, article):
104+
zim_creator = ZimCreator(
105+
str(tmpdir/"test.zim"),
106+
main_page="welcome",
107+
index_language="eng",
108+
min_chunk_size=2048
109+
)
110+
zim_creator.add_article(article)
111+
zim_creator.update_metadata(
112+
creator='python-libzim',
113+
description='Created in python',
114+
name='Hola',
115+
publisher='Monadical',
116+
title='Test Zim'
117+
)
118+
zim_creator.close()
119+
120+
121+
def test_article_metadata(tmpdir, metadata):
122+
zim_creator = ZimCreator(
123+
str(tmpdir/"test.zim"),
124+
main_page = "welcome",
125+
index_language= "eng",
126+
min_chunk_size= 2048
127+
)
128+
zim_creator.update_metadata(**metadata)
129+
assert zim_creator._metadata == metadata
130+
zim_creator.close()
131+
132+
133+
def test_check_mandatory_metadata(tmpdir):
134+
zim_creator = ZimCreator(
135+
str(tmpdir/"test.zim"),
136+
main_page = "welcome",
137+
index_language= "eng",
138+
min_chunk_size= 2048
139+
)
140+
assert not zim_creator.mandatory_metadata_ok()
141+
zim_creator.update_metadata(
142+
creator='python-libzim',
143+
description='Created in python',
144+
name='Hola',
145+
publisher='Monadical',
146+
title='Test Zim'
147+
)
148+
assert zim_creator.mandatory_metadata_ok()
149+
zim_creator.close()

0 commit comments

Comments
 (0)