inventory_test.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import os
  2. os.chdir(os.path.dirname(__file__))
  3. import sys
  4. sys.path.append("..")
  5. sys.path.append("..\\Sources")
  6. from Sources.Modules.Inventory.inventory import * #altered
  7. import json
  8. import Sources.main as main_func
  9. import Sources.Model.model_manager as model_manager
  10. item_id_to_delete = 0
  11. import pytest
  12. @pytest.fixture(scope="session", autouse=True)
  13. def execute_before_any_test():
  14. main_func.init()
  15. persistence.wipeout_database()
  16. persistence.init()
  17. def test_create_inventory_item() :
  18. Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",
  19. assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
  20. wear_score = 100, tags = "#router #mikrotik #saline")
  21. created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  22. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  23. print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
  24. assert json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder) == '{"assign_date_epoch": 5000, "assigned": true, "brand": "Mikrotik", "contact_id": null, "custom_fields_json": null, "group_id": null, "id": 1, "name": "RB2011", "note": "saline RT 2, chez ashvin", "owned": true, "registry": null, "serial_number": "12345678", "site_id": null, "tags": "#router #mikrotik #saline", "type": "router", "wear_score": 100}'
  25. def test_bad_create_inventory_item() :
  26. Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",site_id = 500)
  27. created_item = None
  28. with pytest.raises(BaseException) :
  29. created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  30. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  31. print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
  32. def test_modify_inventory_item() :
  33. Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 1, chez jesai",
  34. assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
  35. wear_score = 100, tags = "#router #mikrotik #saline")
  36. created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  37. Item.name = "RB3011"
  38. Item.type = "MEGAROUTER"
  39. Item.serial_number = "546785"
  40. Item.assigned = False
  41. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  42. modifiedItem = inventory_item_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  43. #encoding then decoding to remove "not json" compliant variable
  44. attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
  45. cls=model_manager.JsonAlchemyDecoder)
  46. for key in attributes_dict:
  47. print("modified item value : " + str(modifiedItem.__dict__[key]))
  48. if key == "id" : # the id should be not checked
  49. continue
  50. assert modifiedItem.__dict__[key] == attributes_dict[key]
  51. def test_delete_inventory_item() :
  52. with pytest.raises(BaseException) :
  53. inventory_item_delete(5456)
  54. inventory_item_delete(1)
  55. with pytest.raises(BaseException) :
  56. get_inventory_item(1)
  57. def test_create_inventory_site() :
  58. Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai")
  59. created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  60. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  61. print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
  62. attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
  63. cls=model_manager.JsonAlchemyDecoder)
  64. get_inventory_site(1)
  65. for key in attributes_dict:
  66. if key == "id" : # the id should be not checked
  67. continue
  68. assert created_item.__dict__[key] == attributes_dict[key]
  69. def test_bad_create_inventory_site() :
  70. Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai", contact_id = 452634)
  71. created_item = None
  72. with pytest.raises(BaseException) :
  73. created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  74. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  75. print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
  76. def test_modify_inventory_site() :
  77. Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai")
  78. created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  79. Item.address = "56 rue de la mega Hess"
  80. Item.note = "chez jesai et ses parents"
  81. Item.tags = "#jesai #debranchelantenne"
  82. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  83. modifiedItem = inventory_site_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  84. #encoding then decoding to remove "not json" compliant variable
  85. attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
  86. cls=model_manager.JsonAlchemyDecoder)
  87. for key in attributes_dict:
  88. print("modified item value : " + str(modifiedItem.__dict__[key]))
  89. if key == "id" : # the id should be not checked
  90. continue
  91. assert modifiedItem.__dict__[key] == attributes_dict[key]
  92. def test_delete_inventory_site() :
  93. with pytest.raises(BaseException) :
  94. inventory_site_delete(5456)
  95. inventory_site_delete(1)
  96. with pytest.raises(BaseException) :
  97. get_inventory_site(1)
  98. def test_create_inventory_contact() :
  99. Item = inventory_contact(contact_person = "Jesai Dourdaine", contact_mail = "antenne@gmail.com",
  100. contact_number = "0262991200", note = "chez jesai",tags = "#pasfiable")
  101. created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  102. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  103. print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
  104. attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
  105. cls=model_manager.JsonAlchemyDecoder)
  106. get_inventory_contact(1)
  107. for key in attributes_dict:
  108. if key == "id" : # the id should be not checked
  109. continue
  110. assert created_item.__dict__[key] == attributes_dict[key]
  111. def test_modify_inventory_contact() :
  112. Item = inventory_contact(contact_person = "Jesai Dour", contact_mail = "antenne@gmail.com",
  113. contact_number = "0262991200", note = "chez jesai",tags = "#pasfiable")
  114. created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  115. Item.contact_person = "Jesai Dourdaine"
  116. Item.note = "un gars moyennement fiable"
  117. Item.tags = "#jesai #debranchelantenne #pasfiable"
  118. print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  119. modifiedItem = inventory_contact_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
  120. #encoding then decoding to remove "not json" compliant variable
  121. attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
  122. cls=model_manager.JsonAlchemyDecoder)
  123. for key in attributes_dict:
  124. print("modified item value : " + str(modifiedItem.__dict__[key]))
  125. if key == "id" : # the id should be not checked
  126. continue
  127. assert modifiedItem.__dict__[key] == attributes_dict[key]
  128. def test_delete_inventory_contact() :
  129. with pytest.raises(BaseException) :
  130. inventory_contact_delete(5456)
  131. inventory_contact_delete(1)
  132. with pytest.raises(BaseException) :
  133. get_inventory_contact(1)
  134. def test_create_inventory_group() :
  135. Item1 = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",
  136. assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
  137. wear_score = 100, tags = "#router #mikrotik #saline")
  138. Item2 = inventory_item(name="RB4011-UAS", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",
  139. assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
  140. wear_score = 100, tags = "#router #mikrotik #saline")
  141. group1 = inventory_group(name = "Saline 2 routers", note = "tout les routeur chez ashvin")
  142. created_item1 = inventory_item_create(json.dumps(Item1, cls=model_manager.JsonAlchemyEncoder))
  143. created_item2 = inventory_item_create(json.dumps(Item2, cls=model_manager.JsonAlchemyEncoder))
  144. created_group1 = inventory_group_create([],json.dumps(group1, cls=model_manager.JsonAlchemyEncoder))
  145. inventory_group_add_item(created_group1.id,created_item1.id)
  146. inventory_group_add_item(created_group1.id,created_item2.id)
  147. global tmp_id1
  148. global tmp_id2
  149. global tmp_id_gr
  150. tmp_id_gr = created_group1.id
  151. tmp_id1 = created_item1.id
  152. tmp_id2 = created_item2.id
  153. print("INTERNAL GROUP ID : " + str(tmp_id_gr))
  154. for item in get_all_inventory_items() :
  155. if item.id == created_item1.id or item.id == created_item2.id :
  156. if not item.group_id == created_group1.id :
  157. raise BaseException("group id is not set correctly for this item")
  158. def test_delete_item_from_group() :
  159. print("EXTERNAL GROUP ID : " + str(tmp_id_gr))
  160. group = get_inventory_group(tmp_id_gr)
  161. inventory_group_delete_item(group.id,tmp_id2) #deleting item 2
  162. for item in get_all_inventory_items() :
  163. if item.id == tmp_id2:
  164. if item.group_id == group.id :
  165. raise BaseException("item not deleted from group")
  166. def test_add_item_from_group() :
  167. group = get_inventory_group(tmp_id_gr)
  168. inventory_group_add_item(group.id,tmp_id2) #deleting item 2
  169. for item in get_all_inventory_items() :
  170. if item.id == tmp_id2:
  171. if not item.group_id == group.id :
  172. raise BaseException("item not added to group")
  173. def test_delete_group() :
  174. inventory_group_delete(tmp_id_gr)
  175. with pytest.raises(BaseException) :
  176. get_inventory_group(tmp_id_gr)
  177. for item in get_all_inventory_items() :
  178. if item.id == tmp_id2 or item.id == tmp_id1:
  179. if item.group_id == tmp_id_gr :
  180. raise BaseException("group not deleted")