test_neuron_parameter_loader.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import unittest
  2. from kalliope.core.NeuronParameterLoader import NeuronParameterLoader
  3. class TestNeuronParameterLoader(unittest.TestCase):
  4. def test_get_parameters(self):
  5. synapse_order = "this is the {{ sentence }}"
  6. user_order = "this is the value"
  7. expected_result = {'sentence': 'value'}
  8. self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
  9. expected_result,
  10. "Fail to retrieve 'the params' of the synapse_order from the order")
  11. # Multiple match
  12. synapse_order = "this is the {{ sentence }}"
  13. user_order = "this is the value with multiple words"
  14. expected_result = {'sentence': 'value with multiple words'}
  15. self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
  16. expected_result,
  17. "Fail to retrieve the 'multiple words params' of the synapse_order from the order")
  18. # Multiple params
  19. synapse_order = "this is the {{ sentence }} with multiple {{ params }}"
  20. user_order = "this is the value with multiple words"
  21. expected_result = {'sentence': 'value',
  22. 'params': 'words'}
  23. self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
  24. expected_result,
  25. "Fail to retrieve the 'multiple params' of the synapse_order from the order")
  26. # Multiple params with multiple words
  27. synapse_order = "this is the {{ sentence }} with multiple {{ params }}"
  28. user_order = "this is the multiple values with multiple values as words"
  29. expected_result = {'sentence': 'multiple values',
  30. 'params': 'values as words'}
  31. self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
  32. expected_result)
  33. # params at the begining of the sentence
  34. synapse_order = "{{ sentence }} this is the sentence"
  35. user_order = "hello world this is the multiple values with multiple values as words"
  36. expected_result = {'sentence': 'hello world'}
  37. self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
  38. expected_result)
  39. # all of the sentence is a variable
  40. synapse_order = "{{ sentence }}"
  41. user_order = "this is the all sentence is a variable"
  42. expected_result = {'sentence': 'this is the all sentence is a variable'}
  43. self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
  44. expected_result)
  45. def test_associate_order_params_to_values(self):
  46. ##
  47. # Testing the brackets position behaviour
  48. ##
  49. # Success
  50. order_brain = "This is the {{ variable }}"
  51. order_user = "This is the value"
  52. expected_result = {'variable': 'value'}
  53. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  54. expected_result)
  55. # Success
  56. order_brain = "This is the {{variable }}"
  57. order_user = "This is the value"
  58. expected_result = {'variable': 'value'}
  59. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  60. expected_result)
  61. # Success
  62. order_brain = "This is the {{ variable}}"
  63. order_user = "This is the value"
  64. expected_result = {'variable': 'value'}
  65. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  66. expected_result)
  67. # Success
  68. order_brain = "This is the {{variable}}"
  69. order_user = "This is the value"
  70. expected_result = {'variable': 'value'}
  71. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  72. expected_result)
  73. # Fail
  74. order_brain = "This is the {variable}"
  75. order_user = "This is the value"
  76. expected_result = {'variable': 'value'}
  77. self.assertNotEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  78. expected_result)
  79. # Fail
  80. order_brain = "This is the { variable}}"
  81. order_user = "This is the value"
  82. expected_result = {'variable': 'value'}
  83. self.assertNotEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  84. expected_result)
  85. ##
  86. # Testing the brackets position in the sentence
  87. ##
  88. # Success
  89. order_brain = "{{ variable }} This is the"
  90. order_user = "value This is the"
  91. expected_result = {'variable': 'value'}
  92. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  93. expected_result)
  94. # Success
  95. order_brain = "This is {{ variable }} the"
  96. order_user = " This is value the"
  97. expected_result = {'variable': 'value'}
  98. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  99. expected_result)
  100. ##
  101. # Testing multi variables
  102. ##
  103. # Success
  104. order_brain = "This is {{ variable }} the {{ variable2 }}"
  105. order_user = "This is value the value2"
  106. expected_result = {'variable': 'value',
  107. 'variable2': 'value2'}
  108. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  109. expected_result)
  110. ##
  111. # Testing multi words in variable
  112. ##
  113. # Success
  114. order_brain = "This is the {{ variable }}"
  115. order_user = "This is the value with multiple words"
  116. expected_result = {'variable': 'value with multiple words'}
  117. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  118. expected_result)
  119. # Success
  120. order_brain = "This is the {{ variable }} and {{ variable2 }}"
  121. order_user = "This is the value with multiple words and second value multiple"
  122. expected_result = {'variable': 'value with multiple words',
  123. 'variable2': 'second value multiple'}
  124. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  125. expected_result)
  126. ##
  127. # Specific Behaviour
  128. ##
  129. # Upper/Lower case
  130. order_brain = "This Is The {{ variable }}"
  131. order_user = "ThiS is tHe VAlue"
  132. expected_result = {'variable': 'VAlue'}
  133. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  134. expected_result)
  135. # Upper/Lower case between multiple variables
  136. order_brain = "This Is The {{ variable }} And The {{ variable2 }}"
  137. order_user = "ThiS is tHe VAlue aND tHE vAlUe2"
  138. expected_result = {'variable': 'VAlue',
  139. 'variable2': 'vAlUe2'}
  140. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  141. expected_result)
  142. # Upper/Lower case between multiple variables and at the End
  143. order_brain = "This Is The {{ variable }} And The {{ variable2 }} And Again"
  144. order_user = "ThiS is tHe VAlue aND tHE vAlUe2 and aGAIN"
  145. expected_result = {'variable': 'VAlue',
  146. 'variable2': 'vAlUe2'}
  147. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  148. expected_result)
  149. # integers variables
  150. order_brain = "This Is The {{ variable }} And The {{ variable2 }}"
  151. order_user = "ThiS is tHe 1 aND tHE 2"
  152. expected_result = {'variable': '1',
  153. 'variable2': '2'}
  154. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  155. expected_result)
  156. # ##
  157. # # More words in the order brain.
  158. # # /!\ Not working but not needed !
  159. # ##
  160. #
  161. # # more words in the middle of order but matching
  162. # order_brain = "this is the {{ variable }} and the {{ variable2 }}"
  163. # order_user = "this the foo and the bar" # missing "is" but matching because all words are present !
  164. # expected_result = {'variable': 'foo',
  165. # 'variable2': 'bar'}
  166. # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  167. # expected_result)
  168. #
  169. # # more words in the beginning of order but matching + bonus with mixed uppercases
  170. # order_brain = "blaBlabla bla This Is The {{ variable }} And The {{ variable2 }}"
  171. # order_user = "ThiS is tHe foo aND tHE bar"
  172. # expected_result = {'variable': 'foo',
  173. # 'variable2': 'bar'}
  174. # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  175. # expected_result)
  176. #
  177. # # more words in the end of order but matching + bonus with mixed uppercases
  178. # order_brain = "This Is The bla BLa bla BLa {{ variable }} And The {{ variable2 }}"
  179. # order_user = "ThiS is tHe foo aND tHE bar"
  180. # expected_result = {'variable': 'foo',
  181. # 'variable2': 'bar'}
  182. # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  183. # expected_result)
  184. #
  185. # # complex more words in the end of order but matching + bonus with mixed uppercases
  186. # order_brain = "Hi theRe This Is bla BLa The bla BLa {{ variable }} And The {{ variable2 }}"
  187. # order_user = "ThiS is tHe foo aND tHE bar"
  188. # expected_result = {'variable': 'foo',
  189. # 'variable2': 'bar'}
  190. # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  191. # expected_result)
  192. #
  193. # # complex more words everywhere in the order but matching + bonus with mixed uppercases
  194. # order_brain = "Hi theRe This Is bla BLa The bla BLa {{ variable }} And Oops The {{ variable2 }} Oopssss"
  195. # order_user = "ThiS is tHe foo aND tHE bar"
  196. # expected_result = {'variable': 'foo',
  197. # 'variable2': 'bar'}
  198. # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  199. # expected_result)
  200. #
  201. ##
  202. # More words in the user order brain
  203. ##
  204. # 1 not matching word in the middle of user order but matching
  205. order_brain = "this the {{ variable }} and the {{ variable2 }}"
  206. order_user = "this is the foo and the bar" # adding "is" but matching because all words are present !
  207. expected_result = {'variable': 'foo',
  208. 'variable2': 'bar'}
  209. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  210. expected_result)
  211. # 2 not matching words in the middle of user order but matching
  212. order_brain = "this the {{ variable }} and the {{ variable2 }}"
  213. order_user = "this is Fake the foo and the bar"
  214. expected_result = {'variable': 'foo',
  215. 'variable2': 'bar'}
  216. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  217. expected_result)
  218. # 1 not matching word at the beginning and 1 not matching word in the middle of user order but matching
  219. order_brain = "this the {{ variable }} and the {{ variable2 }}"
  220. order_user = "Oops this is the foo and the bar"
  221. expected_result = {'variable': 'foo',
  222. 'variable2': 'bar'}
  223. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  224. expected_result)
  225. # 2 not matching words at the beginning and 2 not matching words in the middle of user order but matching
  226. order_brain = "this the {{ variable }} and the {{ variable2 }}"
  227. order_user = "Oops Oops this is BlaBla the foo and the bar"
  228. expected_result = {'variable': 'foo',
  229. 'variable2': 'bar'}
  230. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  231. expected_result)
  232. # Adding complex not matching words in the middle of user order and between variable but matching
  233. order_brain = "this the {{ variable }} and the {{ variable2 }}"
  234. order_user = "Oops Oops this is BlaBla the foo and ploup ploup the bar"
  235. expected_result = {'variable': 'foo',
  236. 'variable2': 'bar'}
  237. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  238. expected_result)
  239. # Adding complex not matching words in the middle of user order and between variable and at the end but matching
  240. order_brain = "this the {{ variable }} and the {{ variable2 }} hello"
  241. order_user = "Oops Oops this is BlaBla the foo and ploup ploup the bar hello test"
  242. expected_result = {'variable': 'foo',
  243. 'variable2': 'bar'}
  244. self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
  245. expected_result)
  246. if __name__ == '__main__':
  247. unittest.main()