from dataclasses import replace from ppp import PromptPostProcessor # pylint: disable=import-error from ppp_classes import ONWARNING_CHOICES # pylint: disable=import-error from .base_tests import PromptPair, TestPromptPostProcessorBase if __name__ == "__main__": raise SystemExit("This script must not be run directly") class TestVarCommands(TestPromptPostProcessorBase): def setUp(self): # pylint: disable=arguments-differ super().setUp(enable_file_logging=False) # Empty variable test def test_empty_variable(self): self.process( PromptPair( "${v1=}${v3:}", "", ), PromptPair("", ""), variables={"v1": "", "v2": "", "v3": ""}, ) # Echoed variables tests def test_echoed_variable(self): self.process( PromptPair( "${v1=test1}test2${v3:test3}${v3:test4}", "", ), # v3 is echoed withs two defaults, the output prompt has both but the variable value is the last default PromptPair("test3test4", ""), variables={"v1": "test1", "v2": "test2", "v3": "test4"}, ) def test_unknown_echoed_variable(self): self.process( PromptPair( "${v1}", "", ), # v3 is echoed withs two defaults, the output prompt has both but the variable value is the last default PromptPair("", ""), variables={"v1": ""}, ppp=PromptPostProcessor( self.ppp_logger, self.def_env_info, replace( self.defopts, on_warning=ONWARNING_CHOICES.warn, ), self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) # Variable in extranetworks def test_variable_in_extranetwork(self): self.process( PromptPair( "${f=filename}${w=0.5}", "", ), PromptPair("", ""), ) # Variable nesting tests def test_var_nested_1(self): # variable default nested in variable set self.process( PromptPair( "${v1=test ${v2:OK}}${v1}", "", ), PromptPair("test OK", ""), variables={"v1": "test OK", "v2": "OK"}, ) def test_var_nested_2(self): # variable set nested in variable default self.process( PromptPair( "${v1:test ${v2=OK}${v2}}", "", ), PromptPair("test OK", ""), variables={"v1": "test OK", "v2": "OK"}, ) def test_var_nested_3(self): # variable default nested in variable default self.process( PromptPair( "${v1:test ${v2:OK}}", "", ), PromptPair("test OK", ""), variables={"v1": "test OK", "v2": "OK"}, ) # Array variable tests def test_array_variable_1(self): # array variable set with += and test of index value and full array with and without default separator self.process( PromptPair( "${v1[]=val1}${v1[]+=val2}${v1[]+=val3}${v1[1]:defval},${v1[]:defval2},${v1['.']:defval3}", "", ), PromptPair("val2,val1, val2, val3,val1.val2.val3", ""), variables={"v1[]": "val1, val2, val3", "v1[1]": "val2", "v1['.']": "val1.val2.val3"}, ) def test_array_variable_2(self): # override of array variable value, test of default value when array variable is empty, test of default value when array variable is not set self.process( PromptPair( "${v1[]=val1}${v1[]=val2}${v1[]:defval},${v2[]:defval2},${v2[1]:defval3},${v3[]=}${v3[]:defval4}", "", ), PromptPair("val2,defval2,defval3", ""), variables={"v1[]": "val2", "v2[]": "defval2", "v2[1]": "defval3", "v3[]": ""}, ) def test_array_variable_3(self): # access array index by variable, set array variable to expanded array variable and add expanded array self.process( PromptPair( "${v1[]=val1}${v1[]+=val2}${v2=1}${v1[v2]:defval1}${v3[]=${v1[]}}${v3[]+=${v1[]}}, ${v3['.']}", "", ), PromptPair("val2, val1, val2.val1, val2", ""), variables={"v1[]": "val1, val2", "v2": "1", "v1[v2]": "val2", "v3[]": "val1, val2, val1, val2"}, ) def test_array_variable_4(self): # test list in array self.process( PromptPair( "${v1[]=val1}${v1[]+=val2}${v1[]+=val3}OKnot OK", "", ), PromptPair("OK", ""), variables={"v1[]": "val1, val2, val3"}, ) def test_array_variable_5(self): # test empty array self.process( PromptPair( "${v1[]=}OKnot OK,OKnot OK", "", ), PromptPair("OK,OK", ""), variables={"v1[]": ""}, ) def test_array_variable_6(self): # array variable set and addition with expanded values from array variables self.process( PromptPair( "${v1[]=val1}${v1[]+=val2}${v2[]=val3}${v3[]=*v1[]}${v3[]+=*v2[]}", "", ), PromptPair("", ""), variables={"v1[]": "val1, val2", "v2[]": "val3", "v3[]": "val1, val2, val3"}, ) def test_array_variable_7(self): # array variable set and addition with expanded values from wildcards self.process( PromptPair( "${v1[]=*__yaml/wildcard1__}${v1[]+=*__yaml/wildcard2__}${v1[2]:defval}", "", ), PromptPair("choice3", ""), variables={"v1[]": "choice2, choice1, choice3, choice1"}, ) def test_array_variable_8(self): # array variable set and addition with expanded values from lists self.process( PromptPair( "${v1[]=*()}${v1[]+=*('one','two')}${v2=three}${v1[]+=*(v2,'four')}${v1[2]:defval}", "", ), PromptPair("three", ""), variables={"v1[]": "one, two, three, four", "v2": "three"}, ) # Variable-vs-variable comparison tests def test_cmd_if_var_vs_var_eq(self): # var eq var: both set to same value, if-branch taken self.process( PromptPair( "hellohelloOKnot OK", "", ), PromptPair("OK", ""), ) def test_cmd_if_var_vs_var_ne(self): # var ne var: different values, ne condition true self.process( PromptPair( "appleorangeOKnot OK", "", ), PromptPair("OK", ""), ) def test_cmd_if_var_vs_var_contains(self): # var contains var: var1 contains var2's value self.process( PromptPair( "hello worldhelloOKnot OK", "", ), PromptPair("OK", ""), ) def test_cmd_if_var_vs_var_not_contains(self): # var not contains var: var1 does not contain var2's value self.process( PromptPair( "hello worldgoodbyeOKnot OK", "", ), PromptPair("OK", ""), ) def test_cmd_if_var_vs_var_lt(self): # var lt var: var1 less than var2 self.process( PromptPair( "12OKnot OK", "", ), PromptPair("OK", ""), ) # NaN/undefined variable integer comparison tests def test_cmd_if_undefined_var_int_compare_warn(self): # undefined var integer compare with on_warning=warn self.process( PromptPair( "OKnot OK", "", ), PromptPair("not OK", ""), ppp=PromptPostProcessor( self.ppp_logger, self.def_env_info, replace( self.defopts, on_warning=ONWARNING_CHOICES.warn, ), self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) def test_cmd_if_undefined_var_int_compare_stop(self): # undefined var integer compare with on_warning=stop self.process( PromptPair( "OKnot OK", "", ), PromptPair("", ""), interrupted=True, ) def test_cmd_if_nonnumeric_var_int_compare_warn(self): # non-numeric var integer compare with on_warning=warn self.process( PromptPair( "abcOKnot OK", "", ), PromptPair("not OK", ""), ppp=PromptPostProcessor( self.ppp_logger, self.def_env_info, replace( self.defopts, on_warning=ONWARNING_CHOICES.warn, ), self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) def test_cmd_if_nonnumeric_var_int_compare_stop(self): # non-numeric var integer compare with on_warning=stop self.process( PromptPair( "abcOKnot OK", "", ), PromptPair("", ""), interrupted=True, ) def test_cmd_if_empty_var_int_compare(self): # empty string var integer compare with on_warning=warn self.process( PromptPair( "OKnot OK", "", ), PromptPair("not OK", ""), ppp=PromptPostProcessor( self.ppp_logger, self.def_env_info, replace( self.defopts, on_warning=ONWARNING_CHOICES.warn, ), self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) # Command tests def test_cmd_stn_complex_features(self): # complex stn command with AND, BREAK and other features self.process( PromptPair( "[neg5] this \\(is\\): a (([complex|simpleneg6|regular] testneg1)(test:2.0):1.5) \nBREAK, BREAK with [abcneg4:defneg2(neg3:1.6):5]:0.5 AND loratrigger AND AND hypernettrigger :0.3", "normal quality, ", ), PromptPair( "this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger AND hypernettrigger :0.3", "[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]", ), ) def test_cmd_if_complex_features(self): # complex if command self.process( PromptPair( "this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK, BREAK with [abcneg4:def:5]:0.5 AND loratrigger hypernettrigger nothing:0.3", "normal quality", ), PromptPair( "this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK :0.5 AND hypernettrigger :0.3", "normal quality", ), ) def test_cmd_if_nested(self): # nested if command self.process( PromptPair( "this is SD1PONYSD2NOPONYNOPONY", "", ), PromptPair("this is PONY", ""), ppp=PromptPostProcessor( self.ppp_logger, { **self.def_env_info, "model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors", }, self.defopts, self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) def test_cmd_set_if(self): # set and if commands self.process( PromptPair("valuethis test is OKnot OK", ""), PromptPair("this test is OK", ""), ) def test_cmd_set_empty(self): # set to empty self.process( PromptPair("${v2=}this test is not OKOK", ""), PromptPair("this test is OK", ""), ) def test_cmd_set_eval_if(self): # set and if commands self.process( PromptPair("valuethis test is OKnot OK", ""), PromptPair("this test is OK", ""), ) def test_cmd_set_if_echo_nested(self): # nested set, if and echo commands self.process( PromptPair( "1OKnot OK NOK OK", "", ), PromptPair("OK OK OK", ""), ) def test_cmd_set_if_complex_conditions_1(self): # complex conditions (or) self.process( PromptPair( "truefalsethis test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_if_complex_conditions_2(self): # complex conditions (and) self.process( PromptPair( "truetruethis test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_if_complex_conditions_3(self): # complex conditions (not) self.process( PromptPair("falsethis test is OKnot OK", ""), PromptPair("this test is OK", ""), ) def test_cmd_set_if_complex_conditions_4(self): # complex conditions (not, precedence) self.process( PromptPair( "truefalsethis test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_if_complex_conditions_5(self): # complex conditions (not, precedence, comparison) self.process( PromptPair( "1falsethis test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_if_complex_conditions_6(self): # complex conditions self.process( PromptPair( "123this test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_if_complex_conditions_7(self): # complex conditions self.process( PromptPair( "123this test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_if2(self): # set and more complex if commands self.process( PromptPair( "First: value1this test is OKOK2not OK\nSecond: value3this test is OKnot OK", "", ), PromptPair("First: this test is OK\nSecond: this test is OK", ""), ) def test_cmd_set_add_if(self): # set, add and if commands self.process( PromptPair( "value2this test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_add_DP_if(self): # set, add (DP format) and if commands self.process( PromptPair( "${v=value}${v+=2}this test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_immediateeval(self): # set (DP format) with mixed evaluation self.process( PromptPair( "${var=!__yaml/wildcard1__}the choices are: ${var}, ${var}, ${var2:default}, ${var3=__yaml/wildcard1__}${var3}, ${var3}", "", ), PromptPair("the choices are: choice2, choice2, default, choice3, choice1", ""), ppp="nocup", ) def test_cmd_set_mixeval(self): # set and add (DP format) with mixed evaluation self.process( PromptPair( "${var=__yaml/wildcard1__}the choices are: ${var}, ${var}, ${var+=, __yaml/wildcard2__}${var}, ${var}, ${var+=!, __yaml/wildcard3__}${var}, ${var}", "", ), PromptPair( "the choices are: choice2, choice3, choice1, choice1- choice2 -choice3, choice2, choice2 -choice1-choice3, choice2, choice3-choice1- choice2 , choice1, choice2 , choice2, choice3-choice1- choice2 , choice1, choice2 ", "", ), ppp="nocup", ) def test_cmd_set_ifundefined_if(self): # set, ifundefined and if commands self.process( PromptPair( "valuethis test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_ifundefined_if_2(self): # set, ifundefined and if commands self.process( PromptPair( "valuevalue2this test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_ifundefined_DP_if(self): # set, ifundefined (DP format) and if commands self.process( PromptPair( "${v?=value}this test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_set_ifundefined_DP_if_2(self): # set, ifundefined (DP format) and if commands self.process( PromptPair( "${v=!value}${v?=!value2}this test is OKnot OK", "", ), PromptPair("this test is OK", ""), ) def test_cmd_ext(self): # ext self.process( PromptPair( "trigger1trigger2trigger4trigger5", "", ), PromptPair( "trigger1,trigger2,trigger4,trigger5", "", ), ) def test_cmd_ext_map_notrigger(self): # ext mapping, no trigger self.process( PromptPair( "", "", ), PromptPair("triggergeneric1, triggergeneric2, two, triggergeneric1, triggergeneric2, two", ""), ) def test_cmd_ext_map1(self): # ext mapping, no lora self.process( PromptPair( "inlinetrigger", "", ), PromptPair("inlinetrigger, triggergeneric1, triggergeneric2, two", ""), ) def test_cmd_ext_map2(self): # ext mapping, lora with weight self.process( PromptPair( "inlinetrigger", "", ), PromptPair("inlinetrigger, triggerpony1, triggerpony2", ""), ppp=PromptPostProcessor( self.ppp_logger, { **self.def_env_info, "model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors", }, self.defopts, self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) def test_cmd_ext_map3(self): # ext mapping, lora with weight adjusted self.process( PromptPair( "inlinetrigger", "", ), PromptPair("inlinetrigger, triggerpony1, triggerpony2", ""), ppp=PromptPostProcessor( self.ppp_logger, { **self.def_env_info, "model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors", }, self.defopts, self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) def test_cmd_ext_map4(self): # ext mapping, lora with parameters self.process( PromptPair( "inlinetrigger", "", ), PromptPair("inlinetrigger, triggerpony1, triggerpony2", ""), ppp=PromptPostProcessor( self.ppp_logger, { **self.def_env_info, "model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors", }, self.defopts, self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), ) def test_cmd_ext_map5(self): # ext mapping, lora with no parameters self.process( PromptPair( "inlinetrigger", "", ), PromptPair("inlinetrigger, triggerillustrious1, triggerillustrious2", ""), ppp=PromptPostProcessor( self.ppp_logger, { **self.def_env_info, "model_filename": "./webui/models/Stable-diffusion/ilxlmodel.safetensors", }, self.defopts, self.grammar_content, self.interrupt, self.wildcards_obj, self.extranetwork_maps_obj, ), )