66import datetime
77import itertools
88import subprocess
9-
9+ from typing import Dict
1010
1111import pytest
1212
1818 FileProvider ,
1919 StringProvider ,
2020 Blob ,
21+ Hint ,
2122)
2223from libzim .reader import Archive
2324
@@ -45,6 +46,9 @@ def get_contentprovider(self) -> libzim.writer.ContentProvider:
4546 return FileProvider (filepath = self .filepath )
4647 return StringProvider (content = getattr (self , "content" , "" ))
4748
49+ def get_hints (self ) -> Dict [Hint , int ]:
50+ return getattr (self , "hints" , {Hint .FRONT_ARTICLE : True })
51+
4852
4953@pytest .fixture (scope = "function" )
5054def fpath (tmpdir ):
@@ -423,13 +427,13 @@ def test_creator_redirection(fpath, lipsum_item):
423427 # ensure we can't add if not started
424428 c = Creator (fpath )
425429 with pytest .raises (RuntimeError , match = "not started" ):
426- c .add_redirection ("home" , "hello" , HOME_PATH )
430+ c .add_redirection ("home" , "hello" , HOME_PATH , { Hint . FRONT_ARTICLE : True } )
427431 del c
428432
429433 with Creator (fpath ) as c :
430434 c .add_item (lipsum_item )
431- c .add_redirection ("home" , "hello" , HOME_PATH )
432- c .add_redirection ("accueil" , "bonjour" , HOME_PATH )
435+ c .add_redirection ("home" , "hello" , HOME_PATH , { Hint . FRONT_ARTICLE : True } )
436+ c .add_redirection ("accueil" , "bonjour" , HOME_PATH , { Hint . FRONT_ARTICLE : True } )
433437
434438 zim = Archive (fpath )
435439 assert zim .entry_count == 3
@@ -524,6 +528,9 @@ def get_mimetype(self):
524528 def get_contentprovider (self ):
525529 return ""
526530
531+ def get_hints (self ):
532+ return {}
533+
527534 with Creator (fpath ) as c :
528535 with pytest .raises (RuntimeError , match = "ContentProvider is None" ):
529536 c .add_item (AnItem ())
@@ -540,11 +547,76 @@ def get_title(self):
540547 def get_mimetype (self ):
541548 return ""
542549
550+ def get_hints (self ):
551+ return {}
552+
543553 with Creator (fpath ) as c :
544554 with pytest .raises (RuntimeError , match = "has no attribute" ):
545555 c .add_item (AnItem ())
546556
547557
558+ def test_missing_hints (fpath ):
559+ class AnItem :
560+ def get_path (self ):
561+ return ""
562+
563+ def get_title (self ):
564+ return ""
565+
566+ def get_mimetype (self ):
567+ return ""
568+
569+ with Creator (fpath ) as c :
570+ with pytest .raises (RuntimeError , match = "has no attribute 'get_hints'" ):
571+ c .add_item (AnItem ())
572+
573+ with pytest .raises (RuntimeError , match = "must be implemented" ):
574+ c .add_item (libzim .writer .Item ())
575+
576+
577+ def test_nondict_hints (fpath ):
578+ with Creator (fpath ) as c :
579+ with pytest .raises (RuntimeError , match = "has no attribute 'items'" ):
580+ c .add_item (StaticItem (path = "1" , title = "" , hints = 1 ))
581+
582+ with pytest .raises (TypeError , match = "hints" ):
583+ c .add_redirection ("a" , "" , "b" , hints = 1 )
584+
585+
586+ def test_hints_values (fpath ):
587+ with Creator (fpath ) as c :
588+ # correct values
589+ c .add_item (StaticItem (path = "0" , title = "" , hints = {}))
590+ c .add_item (
591+ StaticItem (
592+ path = "1" ,
593+ title = "" ,
594+ hints = {Hint .FRONT_ARTICLE : True , Hint .COMPRESS : False },
595+ )
596+ )
597+ # non-expected Hints are ignored
598+ c .add_item (StaticItem (path = "2" , title = "" , hints = {"hello" : "world" }))
599+ # Hint values are casted to bool
600+ c .add_item (StaticItem (path = "3" , title = "" , hints = {Hint .FRONT_ARTICLE : "world" }))
601+ c .add_redirection (
602+ path = "4" , title = "" , targetPath = "0" , hints = {Hint .COMPRESS : True }
603+ )
604+ # filtered-out values
605+ c .add_item (StaticItem (path = "5" , title = "" , hints = {5 : True }))
606+ c .add_item (StaticItem (path = "6" , title = "" , hints = {"yolo" : True }))
607+ c .add_item (StaticItem (path = "7" , title = "" , hints = {"FRONT_ARTICLE" : True }))
608+ c .add_item (StaticItem (path = "8" , title = "" , hints = {0 : True }))
609+
610+ # non-existent Hint
611+ with pytest .raises (AttributeError , match = "YOLO" ):
612+ c .add_item (StaticItem (path = "0" , title = "" , hints = {Hint .YOLO : True }))
613+
614+ with pytest .raises (AttributeError , match = "YOLO" ):
615+ c .add_redirection (
616+ path = "5" , title = "" , target_path = "0" , hints = {Hint .YOLO : True }
617+ )
618+
619+
548620def test_reimpfeed (fpath ):
549621 class AContentProvider :
550622 def __init__ (self ):
@@ -569,6 +641,9 @@ def get_title(self):
569641 def get_mimetype (self ):
570642 return ""
571643
644+ def get_hints (self ):
645+ return {}
646+
572647 def get_contentprovider (self ):
573648 return AContentProvider ()
574649
@@ -599,6 +674,9 @@ def get_title(self):
599674 def get_mimetype (self ):
600675 return ""
601676
677+ def get_hints (self ):
678+ return {}
679+
602680 def get_contentprovider (self ):
603681 return AContentProvider ()
604682
0 commit comments