1010import subprocess
1111import sys
1212import tempfile
13+ import random
1314import unittest
1415
1516import apsw
@@ -25,6 +26,7 @@ def testLoadExtension(self):
2526 db = apsw .Connection (":memory:" )
2627 for name , extra in self .extras .items ():
2728 if extra ["type" ] == "extension" and apsw .sqlite_extra .has (name ):
29+ self .assertEqual ("extension" , apsw .sqlite_extra .has (name ))
2830 with self .subTest (name = name ):
2931 if self .verbose :
3032 print (f" >> Extension { name } " )
@@ -52,10 +54,10 @@ def testLoadExtension(self):
5254 self .assertGreater (num_diff , 0 )
5355
5456 def testExecutable (self ):
55- with tempfile .TemporaryDirectory (prefix = "apsw-extra-test" ) as tmpd :
56- # https://news.ycombinator.com/item?id=42647101 test case
57+ spicy = "√π⁷≤∞"
5758
58- spicy = "√π⁷≤∞"
59+ with tempfile .TemporaryDirectory (prefix = f"apsw-extra-{ spicy } -test" ) as tmpd :
60+ # https://news.ycombinator.com/item?id=42647101 test case
5961
6062 with open (pathlib .Path (tmpd ) / f"{ spicy } .sql" , "wt" , encoding = "utf8" ) as sqlf :
6163 print (f"CrEaTe Table { spicy } (one, two);\n insert into { spicy } values(7,8)" , file = sqlf )
@@ -78,6 +80,7 @@ def testExecutable(self):
7880
7981 for name , extra in self .extras .items ():
8082 if extra ["type" ] == "executable" and apsw .sqlite_extra .has (name ):
83+ self .assertEqual ("executable" , apsw .sqlite_extra .has (name ))
8184 with self .subTest (name = name ):
8285 if self .verbose :
8386 print (f" >> Executable { name } " )
@@ -187,6 +190,51 @@ def testShell(self):
187190 # deliberate error that shouldn't show sqlite_extra attempt
188191 self .assertRaises (apsw .ExtensionLoadingError , s .process_command , ".load thisdoesnotexistandshouldgiveanerror" )
189192
193+ def testOther (self ):
194+ self .assertIsNone (apsw .sqlite_extra .has ("kjldhsfk does not exist jhdskjfhdsfdsfsd" ))
195+ # we don't type check at the moment
196+ self .assertIsNone (apsw .sqlite_extra .has (3 + 4j ))
197+ if apsw .sqlite_extra .has ("sha1" ):
198+ self .assertTrue (os .path .exists (apsw .sqlite_extra .path ("sha1" )))
199+
200+ def testFileIO (self ):
201+ # we add some extra code to make it compile under windows, so
202+ # test that works
203+ if not apsw .sqlite_extra .has ("fileio" ) or not hasattr (apsw , "enable_load_extension" ):
204+ return
205+
206+ spicy = "√π⁷≤∞"
207+ with tempfile .TemporaryDirectory (prefix = f"apsw-extra-{ spicy } -test" ) as tmpd :
208+ db = apsw .Connection ("" )
209+
210+ size = 12345
211+
212+ blob = db .execute ("SELECT randomblob(?)" , (size ,)).get
213+ db .enable_load_extension (True )
214+ db .load_extension (apsw .sqlite_extra .path ("fileio" ))
215+
216+ # the names came from gemini trying to get contrasting
217+ # utf8 and utf16 encoded lengths
218+ names = (
219+ spicy ,
220+ "𐐀𐐁𐐂𐐃𐐄𐐅𐐆𐐇𐐈𐐉𐐊𐐋𐐌𐐍𐐎𐐏𐐐𐐑𐐒𐐓𐐔𐐕𐐖𐐗𐐘" ,
221+ "The quick brown fox jumps over the lazy dog 1234567" ,
222+ "😀😁😂🤣😃😄😅😆😉😊😋😎😍😘🥰😗😙😚☺️🙂🤗🤩🤔🤨😐😑😶🙄😏😣😥😮🤐" ,
223+ )
224+
225+ for name in names :
226+ fname = str (pathlib .Path (tmpd ) / f"{ name } .blob" )
227+ res = db .execute ("SELECT writefile(?, ?)" , (fname , blob )).get
228+ self .assertEqual (res , size )
229+
230+ self .assertEqual (blob , db .execute ("SELECT readfile(?)" , (fname ,)).get )
231+
232+
233+ # check listing works
234+ for name , data in db .execute ("select name, data from fsdir(?)" , (tmpd ,)):
235+ print (f"{ name = } " )
236+ self .assertEqual (blob , data )
237+
190238
191239__all__ = ("Extra" ,)
192240
0 commit comments