1 2from PyObjCTools.TestSupport import * 3from SearchKit import * 4from Foundation import NSMutableData 5import os 6 7try: 8 unicode 9except NameError: 10 unicode = str 11 12try: 13 long 14except NameError: 15 long = int 16 17class TestSKIndex (TestCase): 18 def testTypes(self): 19 self.assertIsCFType(SKIndexRef) 20 self.assertIsCFType(SKIndexDocumentIteratorRef) 21 22 def testConstants(self): 23 self.assertEqual(kSKIndexUnknown, 0) 24 self.assertEqual(kSKIndexInverted, 1) 25 self.assertEqual(kSKIndexVector, 2) 26 self.assertEqual(kSKIndexInvertedVector, 3) 27 28 self.assertEqual(kSKDocumentStateNotIndexed, 0) 29 self.assertEqual(kSKDocumentStateIndexed, 1) 30 self.assertEqual(kSKDocumentStateAddPending, 2) 31 self.assertEqual(kSKDocumentStateDeletePending, 3) 32 33 def testFunctions(self): 34 35 self.assertIsInstance(SKIndexGetTypeID(), (int, long)) 36 self.assertIsInstance(SKIndexDocumentIteratorGetTypeID(), (int, long)) 37 38 self.assertResultIsCFRetained(SKIndexCreateWithURL) 39 try: 40 url = CFURLCreateWithFileSystemPath( 41 None, b"/tmp/pyobjc.test.index".decode('latin1'), 42 kCFURLPOSIXPathStyle, False) 43 self.assertIsInstance(url, CFURLRef) 44 ref = SKIndexCreateWithURL( 45 url, 46 "pyobjc.test", 47 kSKIndexInverted, 48 None) 49 self.assertIsInstance(ref, SKIndexRef) 50 51 v = SKIndexFlush(ref) 52 self.assertIsInstance(v, bool) 53 CFRetain(ref) 54 SKIndexClose(ref) 55 56 del ref 57 58 ref = SKIndexOpenWithURL(url, "pyobjc.test", False) 59 if ref is not None: 60 # XXX: Don't understand why this doesn't work as planned... 61 self.assertIsInstance(ref, SKIndexRef) 62 63 finally: 64 os.unlink('/tmp/pyobjc.test.index') 65 66 data = NSMutableData.data() 67 68 self.assertResultIsCFRetained(SKIndexCreateWithMutableData) 69 ref = SKIndexCreateWithMutableData( 70 data, 71 "pyobjc.test", kSKIndexInverted, None) 72 self.assertIsInstance(ref, SKIndexRef) 73 del ref 74 75 ref = SKIndexOpenWithData( 76 data, 77 "pyobjc.test") 78 self.assertIsInstance(ref, SKIndexRef) 79 del ref 80 81 ref = SKIndexOpenWithMutableData( 82 data, 83 "pyobjc.test") 84 if ref is not None: 85 self.assertIsInstance(ref, SKIndexRef) 86 87 data = NSMutableData.data() 88 self.assertResultIsCFRetained(SKIndexCreateWithMutableData) 89 ref = SKIndexCreateWithMutableData( 90 data, 91 "pyobjc.test", kSKIndexInverted, None) 92 self.assertIsInstance(ref, SKIndexRef) 93 94 95 SKIndexSetMaximumBytesBeforeFlush(ref, 10000) 96 97 v = SKIndexGetMaximumBytesBeforeFlush(ref) 98 self.assertIsInstance(v, (int, long)) 99 100 v = SKIndexCompact(ref) 101 self.assertIsInstance(v, bool) 102 103 v = SKIndexGetIndexType(ref) 104 self.assertIsInstance(v, (int, long)) 105 106 v = SKIndexGetAnalysisProperties(ref) 107 self.failUnless(v is None) 108 109 v = SKIndexGetDocumentCount(ref) 110 self.assertIsInstance(v, (int, long)) 111 112 113 self.assertResultIsBOOL(SKIndexAddDocumentWithText) 114 self.assertArgIsBOOL(SKIndexAddDocumentWithText, 3) 115 116 117 fn = b"/Library/Documentation/Acknowledgements.rtf".decode('latin1') 118 if not os.path.exists(fn): 119 fn = b"/Library/Documentation/AirPort Acknowledgements.rtf".decode('latin1') 120 121 doc = SKDocumentCreateWithURL( 122 CFURLCreateWithFileSystemPath( 123 None, fn, 124 kCFURLPOSIXPathStyle, False)) 125 126 v = SKIndexAddDocumentWithText(ref, 127 doc, "hello world", True) 128 self.failUnless(v) 129 130 self.assertResultIsBOOL(SKIndexAddDocument) 131 self.assertArgIsBOOL(SKIndexAddDocument, 3) 132 v = SKIndexAddDocument(ref, doc, None, True) 133 self.failUnless(v is True) 134 135 SKIndexSetDocumentProperties(ref, doc, {"demo": "pyobjc"}) 136 137 v = SKIndexCopyDocumentProperties(ref, doc) 138 self.assertIsInstance(v, CFDictionaryRef) 139 140 v = SKIndexGetDocumentState(ref, doc) 141 self.assertIsInstance(v, (int, long)) 142 143 v = docID = SKIndexGetDocumentID(ref, doc) 144 self.assertIsInstance(v, (int, long)) 145 146 self.assertResultIsCFRetained(SKIndexCopyDocumentForDocumentID) 147 v = SKIndexCopyDocumentForDocumentID(ref, v) 148 self.failUnless(v is doc) 149 150 r = SKIndexFlush(ref) 151 self.assertIs(r, True) 152 153 self.assertResultIsCFRetained(SKIndexDocumentIteratorCreate) 154 it = SKIndexDocumentIteratorCreate(ref, None) 155 self.assertIsInstance(it, SKIndexDocumentIteratorRef) 156 157 self.assertResultIsCFRetained(SKIndexDocumentIteratorCopyNext) 158 v = SKIndexDocumentIteratorCopyNext(it) 159 self.assertIsInstance(v, SKDocumentRef) 160 161 v = SKIndexDocumentIteratorCopyNext(it) 162 self.failUnless(v is None) 163 164 v = SKIndexGetMaximumDocumentID(ref) 165 self.assertIsInstance(v, (int, long)) 166 167 v = SKIndexGetDocumentTermCount(ref, docID) 168 self.assertIsInstance(v, (int, long)) 169 170 171 v = SKIndexCopyTermIDArrayForDocumentID(ref, docID) 172 self.assertIsInstance(v, CFArrayRef) 173 174 tID = SKIndexGetMaximumTermID(ref) - 1 175 176 177 v = SKIndexGetDocumentTermFrequency(ref, docID, tID) 178 self.assertIsInstance(v, (int, long)) 179 180 v = SKIndexGetMaximumTermID(ref) 181 self.assertIsInstance(v, (int, long)) 182 183 v = SKIndexGetTermDocumentCount(ref, tID) 184 self.assertIsInstance(v, (int, long)) 185 186 v = SKIndexCopyDocumentIDArrayForTermID(ref, tID) 187 self.assertIsInstance(v, (CFArrayRef, type(None))) 188 189 v = SKIndexCopyTermStringForTermID(ref, tID) 190 self.assertIsInstance(v, (unicode, type(None))) 191 192 v = SKIndexGetTermIDForTermString(ref, v) 193 self.assertIsInstance(v, (int, long)) 194 195 SKLoadDefaultExtractorPlugIns() 196 197 v = SKIndexRenameDocument(ref, doc, "osx-acks.rtf") 198 self.failUnless(v is True) 199 200 v = SKIndexMoveDocument(ref, doc, None) 201 self.failUnless(v is True) 202 203 v = SKIndexRemoveDocument(ref, doc) 204 self.failUnless(v) 205 206 207if __name__ == "__main__": 208 main() 209