hydrus/hydrus/test/HelperFunctions.py

92 lines
4.4 KiB
Python
Raw Permalink Normal View History

2023-01-11 21:10:29 +00:00
import random
2022-10-26 20:43:00 +00:00
import unittest
2023-01-11 21:10:29 +00:00
from hydrus.core import HydrusConstants as HC
from hydrus.core import HydrusData
from hydrus.core import HydrusGlobals as HG
2023-04-19 20:38:13 +00:00
from hydrus.core import HydrusTime
2023-01-11 21:10:29 +00:00
from hydrus.client import ClientConstants as CC
from hydrus.client.media import ClientMediaManagers
from hydrus.client.media import ClientMediaResult
2024-01-31 21:20:50 +00:00
from hydrus.client.metadata import ClientContentUpdates
2023-01-11 21:10:29 +00:00
2024-01-31 21:20:50 +00:00
def compare_content_update_packages( ut: unittest.TestCase, content_update_package: ClientContentUpdates.ContentUpdatePackage, expected_content_update_package: ClientContentUpdates.ContentUpdatePackage ):
service_keys_to_content_updates = dict( content_update_package.IterateContentUpdates() )
expected_service_keys_to_content_updates = dict( expected_content_update_package.IterateContentUpdates() )
2022-10-26 20:43:00 +00:00
ut.assertEqual( len( service_keys_to_content_updates ), len( expected_service_keys_to_content_updates ) )
for ( service_key, content_updates ) in service_keys_to_content_updates.items():
expected_content_updates = expected_service_keys_to_content_updates[ service_key ]
2024-01-31 21:20:50 +00:00
content_updates = sorted( content_updates, key = lambda c_u: str( c_u ) )
expected_content_updates = sorted( expected_content_updates, key = lambda c_u: str( c_u ) )
# TODO: go back to this when this works right, with ContentUpdateAction rewrite
# content_update.__hash__ isn't always reliable :(
#ut.assertEqual( content_updates, expected_content_updates )
c_u_tuples = [ ( c_u.ToTuple(), c_u.GetReason() ) for c_u in content_updates ]
e_c_u_tuples = [ ( e_c_u.ToTuple(), e_c_u.GetReason() ) for e_c_u in expected_content_updates ]
2022-10-26 20:43:00 +00:00
ut.assertEqual( c_u_tuples, e_c_u_tuples )
2023-01-11 21:10:29 +00:00
def GetFakeMediaResult( hash: bytes ):
hash_id = random.randint( 0, 200 * ( 1024 ** 2 ) )
size = random.randint( 8192, 20 * 1048576 )
mime = random.choice( [ HC.IMAGE_JPEG, HC.VIDEO_WEBM, HC.APPLICATION_PDF ] )
width = random.randint( 200, 4096 )
height = random.randint( 200, 4096 )
duration = random.choice( [ 220, 16.66667, None ] )
has_audio = random.choice( [ True, False ] )
file_info_manager = ClientMediaManagers.FileInfoManager( hash_id, hash, size = size, mime = mime, width = width, height = height, duration = duration, has_audio = has_audio )
file_info_manager.has_exif = True
file_info_manager.has_icc_profile = True
service_keys_to_statuses_to_tags = { CC.DEFAULT_LOCAL_TAG_SERVICE_KEY : { HC.CONTENT_STATUS_CURRENT : { 'blue_eyes', 'blonde_hair' }, HC.CONTENT_STATUS_PENDING : { 'bodysuit' } } }
service_keys_to_statuses_to_display_tags = { CC.DEFAULT_LOCAL_TAG_SERVICE_KEY : { HC.CONTENT_STATUS_CURRENT : { 'blue eyes', 'blonde hair' }, HC.CONTENT_STATUS_PENDING : { 'bodysuit', 'clothing' } } }
service_keys_to_filenames = {}
tags_manager = ClientMediaManagers.TagsManager( service_keys_to_statuses_to_tags, service_keys_to_statuses_to_display_tags )
2024-01-17 18:57:00 +00:00
times_manager = ClientMediaManagers.TimesManager()
2023-04-12 20:34:43 +00:00
2024-01-17 18:57:00 +00:00
import_timestamp_ms = random.randint( HydrusTime.GetNowMS() - 1000000000, HydrusTime.GetNowMS() - 15 )
2023-01-11 21:10:29 +00:00
2024-01-17 18:57:00 +00:00
file_modified_timestamp_ms = random.randint( import_timestamp_ms - 50000000, import_timestamp_ms - 1 )
2023-01-11 21:10:29 +00:00
2024-01-17 18:57:00 +00:00
times_manager.SetFileModifiedTimestampMS( file_modified_timestamp_ms )
2023-04-12 20:34:43 +00:00
2024-01-17 18:57:00 +00:00
current_to_timestamps_ms = { CC.COMBINED_LOCAL_FILE_SERVICE_KEY : import_timestamp_ms, CC.COMBINED_LOCAL_MEDIA_SERVICE_KEY : import_timestamp_ms, CC.LOCAL_FILE_SERVICE_KEY : import_timestamp_ms }
2023-04-12 20:34:43 +00:00
2024-01-17 18:57:00 +00:00
times_manager.SetImportedTimestampsMS( current_to_timestamps_ms )
2023-01-11 21:10:29 +00:00
locations_manager = ClientMediaManagers.LocationsManager(
2024-01-17 18:57:00 +00:00
set( current_to_timestamps_ms.keys() ),
2023-04-12 20:34:43 +00:00
set(),
2023-01-11 21:10:29 +00:00
set(),
set(),
2024-01-17 18:57:00 +00:00
times_manager,
2023-01-11 21:10:29 +00:00
inbox = False,
urls = set(),
2023-04-12 20:34:43 +00:00
service_keys_to_filenames = service_keys_to_filenames
2023-01-11 21:10:29 +00:00
)
ratings_manager = ClientMediaManagers.RatingsManager( {} )
notes_manager = ClientMediaManagers.NotesManager( { 'note' : 'hello', 'note2' : 'hello2' } )
2024-01-17 18:57:00 +00:00
file_viewing_stats_manager = ClientMediaManagers.FileViewingStatsManager.STATICGenerateEmptyManager( times_manager )
2023-01-11 21:10:29 +00:00
2024-01-17 18:57:00 +00:00
media_result = ClientMediaResult.MediaResult( file_info_manager, tags_manager, times_manager, locations_manager, ratings_manager, notes_manager, file_viewing_stats_manager )
2023-01-11 21:10:29 +00:00
return media_result