def test_file_system_predicates(self): self._clear_db() hash = '\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3' path = HC.STATIC_DIR + os.path.sep + 'hydrus.png' self._write('import_file', path) # result = self._read('file_system_predicates', HC.LOCAL_FILE_SERVICE_KEY) predicates = [] predicates.append( HC.Predicate(HC.PREDICATE_TYPE_SYSTEM, (HC.SYSTEM_PREDICATE_TYPE_EVERYTHING, None), counts={HC.CURRENT: 1})) predicates.append( HC.Predicate(HC.PREDICATE_TYPE_SYSTEM, (HC.SYSTEM_PREDICATE_TYPE_INBOX, None), counts={HC.CURRENT: 1})) predicates.append( HC.Predicate(HC.PREDICATE_TYPE_SYSTEM, (HC.SYSTEM_PREDICATE_TYPE_ARCHIVE, None), counts={HC.CURRENT: 0})) predicates.extend([ HC.Predicate(HC.PREDICATE_TYPE_SYSTEM, (system_predicate_type, None)) for system_predicate_type in [ HC.SYSTEM_PREDICATE_TYPE_UNTAGGED, HC. SYSTEM_PREDICATE_TYPE_NUM_TAGS, HC.SYSTEM_PREDICATE_TYPE_LIMIT, HC.SYSTEM_PREDICATE_TYPE_SIZE, HC.SYSTEM_PREDICATE_TYPE_AGE, HC.SYSTEM_PREDICATE_TYPE_HASH, HC.SYSTEM_PREDICATE_TYPE_WIDTH, HC.SYSTEM_PREDICATE_TYPE_HEIGHT, HC. SYSTEM_PREDICATE_TYPE_RATIO, HC.SYSTEM_PREDICATE_TYPE_DURATION, HC.SYSTEM_PREDICATE_TYPE_NUM_WORDS, HC.SYSTEM_PREDICATE_TYPE_MIME, HC.SYSTEM_PREDICATE_TYPE_RATING, HC.SYSTEM_PREDICATE_TYPE_SIMILAR_TO, HC.SYSTEM_PREDICATE_TYPE_FILE_SERVICE ] ]) self.assertEqual(result, predicates) for i in range(len(predicates)): self.assertEqual(result[i].GetCount(), predicates[i].GetCount())
def test_gui_sessions(self): info = [] info.append(('blank', 'class_text', (HC.LOCAL_FILE_SERVICE_KEY, ), { 'initial_hashes': [], 'initial_media_results': [], 'initial_predicates': [] })) info.append(('system', 'class_text', (HC.LOCAL_FILE_SERVICE_KEY, ), { 'initial_hashes': [os.urandom(32) for i in range(8)], 'initial_media_results': [], 'initial_predicates': [HC.SYSTEM_PREDICATE_ARCHIVE] })) info.append(('tags', 'class_text', (HC.LOCAL_FILE_SERVICE_KEY, ), { 'initial_hashes': [os.urandom(32) for i in range(4)], 'initial_media_results': [], 'initial_predicates': [ HC.Predicate(HC.PREDICATE_TYPE_TAG, 'tag', counts={ HC.CURRENT: 1, HC.PENDING: 3 }) ] })) self._write('gui_session', 'normal', info) result = self._read('gui_sessions') self.assertEqual(result, {'normal': info})
def ExpandPredicates(self, service_key, predicates): # for now -- we will make an option, later service_key = HC.COMBINED_TAG_SERVICE_KEY results = [] with self._lock: for predicate in predicates: results.append(predicate) if predicate.GetPredicateType() == HC.PREDICATE_TYPE_TAG: tag = predicate.GetValue() parents = self._service_keys_to_children_to_parents[ service_key][tag] for parent in parents: parent_predicate = HC.Predicate( HC.PREDICATE_TYPE_PARENT, parent) results.append(parent_predicate) return results
def test_collapse_predicates(self): predicates = [] predicates.append( HC.Predicate(HC.PREDICATE_TYPE_TAG, ('+', 'chain_a'), 10)) predicates.append( HC.Predicate(HC.PREDICATE_TYPE_TAG, ('+', 'chain_b'), 5)) predicates.append( HC.Predicate(HC.PREDICATE_TYPE_TAG, ('+', 'chain_c'), 20)) results = [HC.Predicate(HC.PREDICATE_TYPE_TAG, ('+', 'chain_c'), 35)] self.assertEqual( self._tag_siblings_manager.CollapsePredicates(predicates), results) predicates = [] predicates.append( HC.Predicate(HC.PREDICATE_TYPE_TAG, ('+', 'chain_a'), 10)) predicates.append( HC.Predicate(HC.PREDICATE_TYPE_TAG, ('+', 'chain_b'), 5)) predicates.append( HC.Predicate(HC.PREDICATE_TYPE_TAG, ('+', 'chain_c'), 20)) (result, ) = self._tag_siblings_manager.CollapsePredicates(predicates) self.assertEqual(result.GetCount(), 35)
def run_system_predicate_tests(tests): for (predicate_type, info, result) in tests: predicates = [ HC.Predicate(HC.PREDICATE_TYPE_SYSTEM, (predicate_type, info)) ] search_context = CC.FileSearchContext( file_service_key=HC.LOCAL_FILE_SERVICE_KEY, predicates=predicates) file_query_ids = self._read('file_query_ids', search_context) self.assertEqual(len(file_query_ids), result)
def run_tag_predicate_tests(tests): for (inclusive, tag, result) in tests: predicates = [ HC.Predicate(HC.PREDICATE_TYPE_TAG, tag, inclusive=inclusive) ] search_context = CC.FileSearchContext( file_service_key=HC.LOCAL_FILE_SERVICE_KEY, predicates=predicates) file_query_ids = self._read('file_query_ids', search_context) self.assertEqual(len(file_query_ids), result)
def test_collapse_predicates( self ): predicates = [] predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'chain_a', counts = { HC.CURRENT : 10 } ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'chain_b', counts = { HC.CURRENT : 5 } ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'chain_c', counts = { HC.CURRENT : 20 } ) ) results = [ HC.Predicate( HC.PREDICATE_TYPE_TAG, 'chain_c', counts = { HC.CURRENT : 35 } ) ] self.assertEqual( self._tag_siblings_manager.CollapsePredicates( predicates ), results ) predicates = [] predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'chain_a', counts = { HC.CURRENT : 10 } ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'chain_b', counts = { HC.CURRENT : 5 } ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'chain_c', counts = { HC.CURRENT : 20 } ) ) ( result, ) = self._tag_siblings_manager.CollapsePredicates( predicates ) self.assertEqual( result.GetCount(), 35 )
def test_autocomplete(self): self._clear_db() result = self._read('autocomplete_tags', half_complete_tag='c') self.assertEqual(result.GetMatches('c'), []) result = self._read('autocomplete_tags', half_complete_tag='series:') self.assertEqual(result.GetMatches('series:'), []) # hash = '\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3' path = HC.STATIC_DIR + os.path.sep + 'hydrus.png' self._write('import_file', path) # service_keys_to_content_updates = {} content_updates = [] content_updates.append( HC.ContentUpdate(HC.CONTENT_DATA_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, ('car', (hash, )))) content_updates.append( HC.ContentUpdate(HC.CONTENT_DATA_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, ('series:cars', (hash, )))) content_updates.append( HC.ContentUpdate(HC.CONTENT_DATA_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, ('maker:ford', (hash, )))) service_keys_to_content_updates[ HC.LOCAL_TAG_SERVICE_KEY] = content_updates self._write('content_updates', service_keys_to_content_updates) # cars result = self._read('autocomplete_tags', half_complete_tag='c') preds = set() preds.add( HC.Predicate(HC.PREDICATE_TYPE_TAG, 'car', counts={HC.CURRENT: 1})) preds.add( HC.Predicate(HC.PREDICATE_TYPE_TAG, 'series:cars', counts={HC.CURRENT: 1})) read_preds = result.GetMatches('c') # count isn't tested in predicate.__eq__, I think for p in read_preds: self.assertEqual(p.GetCount(HC.CURRENT), 1) self.assertEqual(set(read_preds), preds) # result = self._read('autocomplete_tags', half_complete_tag='ser') read_preds = result.GetMatches('ser') self.assertEqual(read_preds, []) # result = self._read('autocomplete_tags', half_complete_tag='series:c') pred = HC.Predicate(HC.PREDICATE_TYPE_TAG, 'series:cars', counts={HC.CURRENT: 1}) (read_pred, ) = result.GetMatches('series:c') self.assertEqual(read_pred.GetCount(HC.CURRENT), 1) self.assertEqual(pred, read_pred) # result = self._read('autocomplete_tags', tag='car') pred = HC.Predicate(HC.PREDICATE_TYPE_TAG, 'car', counts={HC.CURRENT: 1}) (read_pred, ) = result.GetMatches('car') self.assertEqual(read_pred.GetCount(HC.CURRENT), 1) self.assertEqual(pred, read_pred) # result = self._read('autocomplete_tags', tag='c') read_preds = result.GetMatches('c') self.assertEqual(read_preds, [])
def CollapsePredicates(self, predicates): with self._lock: results = [ predicate for predicate in predicates if predicate.GetPredicateType() != HC.PREDICATE_TYPE_TAG ] tag_predicates = [ predicate for predicate in predicates if predicate.GetPredicateType() == HC.PREDICATE_TYPE_TAG ] tags_to_predicates = { predicate.GetValue(): predicate for predicate in predicates if predicate.GetPredicateType() == HC.PREDICATE_TYPE_TAG } tags = tags_to_predicates.keys() tags_to_include_in_results = set() for tag in tags: if tag in self._siblings: old_tag = tag old_predicate = tags_to_predicates[old_tag] new_tag = self._siblings[old_tag] if new_tag not in tags_to_predicates: (old_pred_type, old_value, old_inclusive) = old_predicate.GetInfo() new_predicate = HC.Predicate(old_pred_type, old_value, inclusive=old_inclusive) tags_to_predicates[new_tag] = new_predicate tags_to_include_in_results.add(new_tag) new_predicate = tags_to_predicates[new_tag] current_count = old_predicate.GetCount(HC.CURRENT) pending_count = old_predicate.GetCount(HC.PENDING) new_predicate.AddToCount(HC.CURRENT, current_count) new_predicate.AddToCount(HC.PENDING, pending_count) else: tags_to_include_in_results.add(tag) results.extend([ tags_to_predicates[tag] for tag in tags_to_include_in_results ]) return results
def test_expand_predicates( self ): predicates = [] predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'grandmother', counts = { HC.CURRENT : 10 } ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'grandfather', counts = { HC.CURRENT : 15 } ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'not_exist', counts = { HC.CURRENT : 20 } ) ) self.assertEqual( self._tag_parents_manager.ExpandPredicates( HC.COMBINED_TAG_SERVICE_KEY, predicates ), predicates ) predicates = [] predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'child', counts = { HC.CURRENT : 10 } ) ) results = [] results.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'child', counts = { HC.CURRENT : 10 } ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'mother' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'father' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'grandmother' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'grandfather' ) ) self.assertEqual( set( self._tag_parents_manager.ExpandPredicates( HC.COMBINED_TAG_SERVICE_KEY, predicates ) ), set( results ) ) predicates = [] predicates.append( HC.Predicate( HC.PREDICATE_TYPE_NAMESPACE, 'series' ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'child', counts = { HC.CURRENT : 10 } ) ) predicates.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'cousin', counts = { HC.CURRENT : 5 } ) ) results = [] results.append( HC.Predicate( HC.PREDICATE_TYPE_NAMESPACE, 'series' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'child', counts = { HC.CURRENT : 10 } ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'mother' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'father' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'grandmother' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'grandfather' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_TAG, 'cousin', counts = { HC.CURRENT : 5 } ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'aunt' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'uncle' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'grandmother' ) ) results.append( HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'grandfather' ) ) self.assertEqual( set( self._tag_parents_manager.ExpandPredicates( HC.COMBINED_TAG_SERVICE_KEY, predicates ) ), set( results ) )
def test_predicates( self ): p = HC.Predicate( HC.PREDICATE_TYPE_TAG, 'tag' ) self.assertEqual( p.GetUnicode(), u'tag' ) p = HC.Predicate( HC.PREDICATE_TYPE_TAG, 'tag', counts = { HC.CURRENT : 1, HC.PENDING : 2 } ) self.assertEqual( p.GetUnicode( with_count = False ), u'tag' ) self.assertEqual( p.GetUnicode( with_count = True ), u'tag (1) (+2)' ) p = HC.Predicate( HC.PREDICATE_TYPE_TAG, 'tag', inclusive = False ) self.assertEqual( p.GetUnicode(), u'-tag' ) p = HC.Predicate( HC.PREDICATE_TYPE_TAG, 'tag', inclusive = False, counts = { HC.CURRENT : 1, HC.PENDING : 2 } ) self.assertEqual( p.GetUnicode( with_count = False ), u'-tag' ) self.assertEqual( p.GetUnicode( with_count = True ), u'-tag (1) (+2)' ) # p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_AGE, ( '<', 1, 2, 3, 4 ) ) ) self.assertEqual( p.GetUnicode(), u'system:age < 1y2m3d4h' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_AGE, ( u'\u2248', 1, 2, 3, 4 ) ) ) self.assertEqual( p.GetUnicode(), u'system:age ' + u'\u2248' + ' 1y2m3d4h' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_AGE, ( '>', 1, 2, 3, 4 ) ) ) self.assertEqual( p.GetUnicode(), u'system:age > 1y2m3d4h' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_ARCHIVE, None ), counts = { HC.CURRENT : 1000 } ) self.assertEqual( p.GetUnicode(), u'system:archive (1,000)' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_DURATION, ( '<', 1000 ) ) ) self.assertEqual( p.GetUnicode(), u'system:duration < 1,000' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_EVERYTHING, None ), counts = { HC.CURRENT : 2000 } ) self.assertEqual( p.GetUnicode(), u'system:everything (2,000)' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_FILE_SERVICE, ( True, HC.CURRENT, HC.LOCAL_FILE_SERVICE_KEY ) ) ) self.assertEqual( p.GetUnicode(), u'system:is currently in local files' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_FILE_SERVICE, ( False, HC.PENDING, HC.LOCAL_FILE_SERVICE_KEY ) ) ) self.assertEqual( p.GetUnicode(), u'system:is not pending to local files' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_HASH, 'abcd'.decode( 'hex' ) ) ) self.assertEqual( p.GetUnicode(), u'system:hash is abcd' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_HEIGHT, ( '<', 2000 ) ) ) self.assertEqual( p.GetUnicode(), u'system:height < 2,000' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_INBOX, None ), counts = { HC.CURRENT : 1000 } ) self.assertEqual( p.GetUnicode(), u'system:inbox (1,000)' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_LIMIT, 2000 ) ) self.assertEqual( p.GetUnicode(), u'system:limit is 2,000' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_LOCAL, None ), counts = { HC.CURRENT : 100 } ) self.assertEqual( p.GetUnicode(), u'system:local (100)' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_MIME, HC.IMAGES ) ) self.assertEqual( p.GetUnicode(), u'system:mime is image' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_MIME, HC.VIDEO_WEBM ) ) self.assertEqual( p.GetUnicode(), u'system:mime is video/webm' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_NOT_LOCAL, None ), counts = { HC.CURRENT : 100 } ) self.assertEqual( p.GetUnicode(), u'system:not local (100)' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_NUM_TAGS, ( '<', 2 ) ) ) self.assertEqual( p.GetUnicode(), u'system:number of tags < 2' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_NUM_WORDS, ( '<', 5000 ) ) ) self.assertEqual( p.GetUnicode(), u'system:number of words < 5,000' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_RATING, ( HC.LOCAL_FILE_SERVICE_KEY, '>', 0.2 ) ) ) self.assertEqual( p.GetUnicode(), u'system:rating for local files > 0.2' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_RATIO, ( '=', 16, 9 ) ) ) self.assertEqual( p.GetUnicode(), u'system:ratio = 16:9' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_SIMILAR_TO, ( 'abcd'.decode( 'hex' ), 5 ) ) ) self.assertEqual( p.GetUnicode(), u'system:similar to abcd using max hamming of 5' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_SIZE, ( '>', 5, 1048576 ) ) ) self.assertEqual( p.GetUnicode(), u'system:size > 5MB' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_UNTAGGED, HC.IMAGES ) ) self.assertEqual( p.GetUnicode(), u'system:untagged' ) p = HC.Predicate( HC.PREDICATE_TYPE_SYSTEM, ( HC.SYSTEM_PREDICATE_TYPE_WIDTH, ( '=', 1920 ) ) ) self.assertEqual( p.GetUnicode(), u'system:width = 1,920' ) # p = HC.Predicate( HC.PREDICATE_TYPE_NAMESPACE, 'series' ) self.assertEqual( p.GetUnicode(), u'series:*' ) p = HC.Predicate( HC.PREDICATE_TYPE_TAG, 'series', inclusive = False ) self.assertEqual( p.GetUnicode(), u'-series' ) # p = HC.Predicate( HC.PREDICATE_TYPE_WILDCARD, 'a*i:o*' ) self.assertEqual( p.GetUnicode(), u'a*i:o*' ) p = HC.Predicate( HC.PREDICATE_TYPE_TAG, 'a*i:o*', inclusive = False ) self.assertEqual( p.GetUnicode(), u'-a*i:o*' ) # p = HC.Predicate( HC.PREDICATE_TYPE_PARENT, 'series:game of thrones' ) self.assertEqual( p.GetUnicode(), u' series:game of thrones' )