hydrus/include/TestFunctions.py

85 lines
3.3 KiB
Python
Raw Normal View History

2013-07-10 20:25:57 +00:00
import collections
import HydrusConstants as HC
2015-10-07 21:56:22 +00:00
import ClientData
2013-07-10 20:25:57 +00:00
import os
import TestConstants
import unittest
2015-03-25 22:04:19 +00:00
import HydrusData
2015-06-03 21:05:13 +00:00
import ClientConstants as CC
2013-07-10 20:25:57 +00:00
2017-09-06 20:18:20 +00:00
class TestFunctions( unittest.TestCase ):
2013-07-10 20:25:57 +00:00
def test_dict_to_content_updates( self ):
2015-07-01 22:02:07 +00:00
hash = HydrusData.GenerateKey()
2013-07-10 20:25:57 +00:00
2015-10-07 21:56:22 +00:00
hashes = { hash }
2013-07-10 20:25:57 +00:00
2015-06-03 21:05:13 +00:00
local_key = CC.LOCAL_TAG_SERVICE_KEY
2015-07-01 22:02:07 +00:00
remote_key = HydrusData.GenerateKey()
2013-07-10 20:25:57 +00:00
2014-08-27 22:15:22 +00:00
service_keys_to_tags = { local_key : { 'a' } }
2013-07-10 20:25:57 +00:00
2015-10-14 21:02:25 +00:00
content_updates = { local_key : [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, ( 'a', hashes ) ) ] }
2013-07-10 20:25:57 +00:00
2015-10-07 21:56:22 +00:00
self.assertEqual( ClientData.ConvertServiceKeysToTagsToServiceKeysToContentUpdates( { hash }, service_keys_to_tags ), content_updates )
2013-07-10 20:25:57 +00:00
2014-08-27 22:15:22 +00:00
service_keys_to_tags = { remote_key : { 'c' } }
2013-07-10 20:25:57 +00:00
2015-10-14 21:02:25 +00:00
content_updates = { remote_key : [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_PEND, ( 'c', hashes ) ) ] }
2013-07-10 20:25:57 +00:00
2015-10-07 21:56:22 +00:00
self.assertEqual( ClientData.ConvertServiceKeysToTagsToServiceKeysToContentUpdates( { hash }, service_keys_to_tags ), content_updates )
2013-07-10 20:25:57 +00:00
2014-08-27 22:15:22 +00:00
service_keys_to_tags = { local_key : [ 'a', 'character:b' ], remote_key : [ 'c', 'series:d' ] }
2013-07-10 20:25:57 +00:00
content_updates = {}
2015-10-14 21:02:25 +00:00
content_updates[ local_key ] = [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, ( 'a', hashes ) ), HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, ( 'character:b', hashes ) ) ]
content_updates[ remote_key ] = [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_PEND, ( 'c', hashes ) ), HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_PEND, ( 'series:d', hashes ) ) ]
2013-07-10 20:25:57 +00:00
2015-10-14 21:02:25 +00:00
self.assertEqual( HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_PEND, 'c' ), HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_PEND, 'c' ) )
2015-10-07 21:56:22 +00:00
self.assertEqual( ClientData.ConvertServiceKeysToTagsToServiceKeysToContentUpdates( { hash }, service_keys_to_tags ), content_updates )
2013-07-10 20:25:57 +00:00
2014-07-30 21:18:17 +00:00
def test_number_conversion( self ):
i = 123456789
2015-03-25 22:04:19 +00:00
i_pretty = HydrusData.ConvertIntToPrettyString( i )
2014-07-30 21:18:17 +00:00
# this test only works on anglo computers; it is mostly so I can check it is working on mine
self.assertEqual( i_pretty, '123,456,789' )
2015-12-16 22:41:06 +00:00
def test_unicode_conversion( self ):
u = u'here is a unicode character: \u76f4'
b = HydrusData.ToByteString( u )
bu = HydrusData.ToUnicode( b )
self.assertEqual( type( b ), str )
self.assertEqual( b, 'here is a unicode character: \xe7\x9b\xb4' )
self.assertEqual( type( bu ), unicode )
self.assertEqual( bu, u )
d = {}
u = HydrusData.ToUnicode( d )
b = HydrusData.ToByteString( d )
self.assertEqual( type( u ), unicode )
self.assertEqual( u, u'{}' )
self.assertEqual( type( b ), str )
self.assertEqual( b, '{}' )
pretty_num = HydrusData.ConvertIntToPrettyString( 123456789 )
self.assertEqual( type( pretty_num ), unicode )
2017-09-06 20:18:20 +00:00