@@ -721,12 +721,12 @@ def test_invalid_badge(self, data):
721721 expected = 'Aps.badge must be a number.'
722722 assert str (excinfo .value ) == expected
723723
724- @pytest .mark .parametrize ('data' , NON_STRING_ARGS )
724+ @pytest .mark .parametrize ('data' , NON_STRING_ARGS + [ '' ] )
725725 def test_invalid_sound (self , data ):
726726 aps = messaging .Aps (sound = data )
727727 with pytest .raises (ValueError ) as excinfo :
728728 self ._encode_aps (aps )
729- expected = 'Aps.sound must be a string.'
729+ expected = 'Aps.sound must be a non-empty string or an instance of CriticalSound class .'
730730 assert str (excinfo .value ) == expected
731731
732732 @pytest .mark .parametrize ('data' , NON_STRING_ARGS )
@@ -832,6 +832,114 @@ def test_aps_custom_data(self):
832832 check_encoding (msg , expected )
833833
834834
835+ class TestApsSoundEncoder (object ):
836+
837+ def _check_sound (self , sound ):
838+ with pytest .raises (ValueError ) as excinfo :
839+ check_encoding (messaging .Message (
840+ topic = 'topic' , apns = messaging .APNSConfig (
841+ payload = messaging .APNSPayload (aps = messaging .Aps (sound = sound ))
842+ )
843+ ))
844+ return excinfo
845+
846+ @pytest .mark .parametrize ('data' , NON_STRING_ARGS )
847+ def test_invalid_name (self , data ):
848+ sound = messaging .CriticalSound (name = data )
849+ excinfo = self ._check_sound (sound )
850+ expected = 'CriticalSound.name must be a non-empty string.'
851+ assert str (excinfo .value ) == expected
852+
853+ @pytest .mark .parametrize ('data' , [list (), tuple (), dict (), 'foo' ])
854+ def test_invalid_volume (self , data ):
855+ sound = messaging .CriticalSound (name = 'default' , volume = data )
856+ excinfo = self ._check_sound (sound )
857+ expected = 'CriticalSound.volume must be a number.'
858+ assert str (excinfo .value ) == expected
859+
860+ @pytest .mark .parametrize ('data' , [- 0.1 , 1.1 ])
861+ def test_volume_out_of_range (self , data ):
862+ sound = messaging .CriticalSound (name = 'default' , volume = data )
863+ excinfo = self ._check_sound (sound )
864+ expected = 'CriticalSound.volume must be in the interval [0,1].'
865+ assert str (excinfo .value ) == expected
866+
867+ def test_sound_string (self ):
868+ msg = messaging .Message (
869+ topic = 'topic' ,
870+ apns = messaging .APNSConfig (
871+ payload = messaging .APNSPayload (aps = messaging .Aps (sound = 'default' ))
872+ )
873+ )
874+ expected = {
875+ 'topic' : 'topic' ,
876+ 'apns' : {
877+ 'payload' : {
878+ 'aps' : {
879+ 'sound' : 'default' ,
880+ },
881+ }
882+ },
883+ }
884+ check_encoding (msg , expected )
885+
886+ def test_critical_sound (self ):
887+ msg = messaging .Message (
888+ topic = 'topic' ,
889+ apns = messaging .APNSConfig (
890+ payload = messaging .APNSPayload (
891+ aps = messaging .Aps (
892+ sound = messaging .CriticalSound (
893+ name = 'default' ,
894+ critical = True ,
895+ volume = 0.5
896+ )
897+ ),
898+ )
899+ )
900+ )
901+ expected = {
902+ 'topic' : 'topic' ,
903+ 'apns' : {
904+ 'payload' : {
905+ 'aps' : {
906+ 'sound' : {
907+ 'name' : 'default' ,
908+ 'critical' : 1 ,
909+ 'volume' : 0.5 ,
910+ },
911+ },
912+ }
913+ },
914+ }
915+ check_encoding (msg , expected )
916+
917+ def test_critical_sound_name_only (self ):
918+ msg = messaging .Message (
919+ topic = 'topic' ,
920+ apns = messaging .APNSConfig (
921+ payload = messaging .APNSPayload (
922+ aps = messaging .Aps (
923+ sound = messaging .CriticalSound (name = 'default' )
924+ ),
925+ )
926+ )
927+ )
928+ expected = {
929+ 'topic' : 'topic' ,
930+ 'apns' : {
931+ 'payload' : {
932+ 'aps' : {
933+ 'sound' : {
934+ 'name' : 'default' ,
935+ },
936+ },
937+ }
938+ },
939+ }
940+ check_encoding (msg , expected )
941+
942+
835943class TestApsAlertEncoder (object ):
836944
837945 def _check_alert (self , alert ):
0 commit comments