test_dna_loader.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. import unittest
  3. from kalliope.core.ConfigurationManager.DnaLoader import DnaLoader
  4. from kalliope.core.Models.Dna import Dna
  5. class TestDnaLoader(unittest.TestCase):
  6. def setUp(self):
  7. if "/Tests" in os.getcwd():
  8. self.dna_test_file = "modules/test_valid_dna.yml"
  9. else:
  10. self.dna_test_file = "Tests/modules/test_valid_dna.yml"
  11. def tearDown(self):
  12. pass
  13. def test_get_yaml_config(self):
  14. expected_result = {'kalliope_supported_version': [0.4],
  15. 'author': 'Kalliope project team',
  16. 'type': 'neuron',
  17. 'name': 'neuron_test',
  18. 'tags': ['test']}
  19. dna_file_content = DnaLoader(self.dna_test_file).get_yaml_config()
  20. self.assertEqual(dna_file_content, expected_result)
  21. def test_get_dna(self):
  22. expected_result = Dna()
  23. expected_result.name = "neuron_test"
  24. expected_result.module_type = "neuron"
  25. expected_result.tags = ['test']
  26. expected_result.author = 'Kalliope project team'
  27. expected_result.kalliope_supported_version = [0.4]
  28. dna_to_test = DnaLoader(self.dna_test_file).get_dna()
  29. self.assertTrue(dna_to_test.__eq__(expected_result))
  30. def test_load_dna(self):
  31. # test with a valid DNA file
  32. dna_to_test = DnaLoader(self.dna_test_file)._load_dna()
  33. self.assertTrue(isinstance(dna_to_test, Dna))
  34. # test with a non valid DNA file
  35. if "/Tests" in os.getcwd():
  36. dna_invalid_test_file = "modules/test_invalid_dna.yml"
  37. else:
  38. dna_invalid_test_file = "Tests/modules/test_invalid_dna.yml"
  39. self.assertIsNone(DnaLoader(dna_invalid_test_file)._load_dna())
  40. def test_check_dna(self):
  41. # check with valid DNA file
  42. test_dna = {'kalliope_supported_version': [0.4],
  43. 'author': 'Kalliope project team',
  44. 'type': 'neuron',
  45. 'name': 'neuron_test',
  46. 'tags': ['test']}
  47. self.assertTrue(DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
  48. # invalid DNA file, no name
  49. test_dna = {'kalliope_supported_version': [0.4],
  50. 'author': 'Kalliope project team',
  51. 'type': 'neuron',
  52. 'tags': ['test']}
  53. self.assertFalse(DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
  54. # invalid DNA file, no type
  55. test_dna = {'kalliope_supported_version': [0.4],
  56. 'author': 'Kalliope project team',
  57. 'name': 'neuron_test',
  58. 'tags': ['test']}
  59. self.assertFalse(DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
  60. # invalid DNA, wrong type
  61. test_dna = {'kalliope_supported_version': [0.4],
  62. 'author': 'Kalliope project team',
  63. 'type': 'doesnotexist',
  64. 'name': 'neuron_test',
  65. 'tags': ['test']}
  66. self.assertFalse(DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
  67. # invalid DNA, no kalliope_supported_version
  68. test_dna = {'author': 'Kalliope project team',
  69. 'type': 'neuron',
  70. 'name': 'neuron_test',
  71. 'tags': ['test']}
  72. self.assertFalse(DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
  73. # invalid DNA, kalliope_supported_version empty
  74. test_dna = {'kalliope_supported_version': [],
  75. 'author': 'Kalliope project team',
  76. 'type': 'neuron',
  77. 'name': 'neuron_test',
  78. 'tags': ['test']}
  79. self.assertFalse(DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
  80. # invalid DNA, kalliope_supported_version wrong format
  81. test_dna = {'kalliope_supported_version': ['0.4.1'],
  82. 'author': 'Kalliope project team',
  83. 'type': 'neuron',
  84. 'name': 'neuron_test',
  85. 'tags': ['test']}
  86. self.assertFalse(DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))