ash 2 лет назад
Родитель
Сommit
a2163dff27
2 измененных файлов с 48 добавлено и 45 удалено
  1. 4 5
      Backend/Sources/Modules/Inventory/inventory.py
  2. 44 40
      Backend/Tests/inventory_test.py

+ 4 - 5
Backend/Sources/Modules/Inventory/inventory.py

@@ -1,8 +1,5 @@
-import site
-from typing import Type
 from Modules.Inventory.inventory_model import inventory_contact, inventory_group, inventory_site
-from cmath import log
-from sqlalchemy import String, null
+import sqlalchemy.orm
 from sqlalchemy.ext.declarative import DeclarativeMeta
 import persistence
 import Model.model_manager as model_manager
@@ -205,7 +202,9 @@ def inventory_group_delete_item(group_id,inventory_id) :
         if group_id == Item.group_id :
             Item.group_id = None
 
-        if len(sess.query(inventory_item).filter(inventory_item.group_id == group.id).first()) == 0 :
+
+        query : sqlalchemy.orm.Query = sess.query(inventory_item).filter(inventory_item.group_id == group.id)
+        if  query.count() == 0 :
             sess.delete(group) # if group is orphan
         sess.commit()
 

+ 44 - 40
Backend/Tests/inventory_test.py

@@ -9,6 +9,8 @@ import Sources.main as main_func
 import Sources.Model.model_manager as model_manager
 item_id_to_delete = 0
 
+
+
 import pytest
 @pytest.fixture(scope="session", autouse=True)
 def execute_before_any_test():
@@ -26,20 +28,20 @@ def test_create_inventory_item() :
 
 
 
-    created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
-    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}'
+    created_item = inventory_item_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(created_item, cls=model_manager.ComplexEncoder))
+    assert json.dumps(created_item, cls=model_manager.ComplexEncoder) == '{"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}'
 
 def test_bad_create_inventory_item() :
     Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",site_id = 500)
 
     created_item = None
     with pytest.raises(BaseException) :
-        created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+        created_item = inventory_item_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(created_item, cls=model_manager.ComplexEncoder))
 
 def test_modify_inventory_item() :
     Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 1, chez jesai",
@@ -47,7 +49,7 @@ def test_modify_inventory_item() :
                           wear_score = 100, tags = "#router #mikrotik #saline")
 
 
-    created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    created_item = inventory_item_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
 
     Item.name = "RB3011"
@@ -55,13 +57,13 @@ def test_modify_inventory_item() :
     Item.serial_number = "546785"
     Item.assigned = False
 
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
-    modifiedItem = inventory_item_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    modifiedItem = inventory_item_update(created_item.id, json.dumps(Item, cls=model_manager.ComplexEncoder))
 
     #encoding then decoding to remove "not json" compliant variable
-    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
-                                                     cls=model_manager.JsonAlchemyDecoder)
+    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.ComplexEncoder),
+                                                     cls=model_manager.ComplexDecoder)
 
     for key in  attributes_dict:
         print("modified item value : " + str(modifiedItem.__dict__[key]))
@@ -82,11 +84,11 @@ def test_create_inventory_site() :
     Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai")
 
 
-    created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
-    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
-                                                     cls=model_manager.JsonAlchemyDecoder)
+    created_item = inventory_site_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(created_item, cls=model_manager.ComplexEncoder))
+    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.ComplexEncoder),
+                                                     cls=model_manager.ComplexDecoder)
 
 
     get_inventory_site(1)
@@ -102,29 +104,29 @@ def test_bad_create_inventory_site() :
 
     created_item = None
     with pytest.raises(BaseException) :
-        created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+        created_item = inventory_site_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(created_item, cls=model_manager.ComplexEncoder))
 
 def test_modify_inventory_site() :
     Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai")
 
 
-    created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    created_item = inventory_site_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
 
     Item.address = "56 rue de la mega Hess"
     Item.note = "chez jesai et ses parents"
     Item.tags = "#jesai #debranchelantenne"
 
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
-    modifiedItem = inventory_site_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    modifiedItem = inventory_site_update(created_item.id, json.dumps(Item, cls=model_manager.ComplexEncoder))
 
     #encoding then decoding to remove "not json" compliant variable
-    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
-                                                     cls=model_manager.JsonAlchemyDecoder)
+    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.ComplexEncoder),
+                                                     cls=model_manager.ComplexDecoder)
 
     for key in  attributes_dict:
         print("modified item value : " + str(modifiedItem.__dict__[key]))
@@ -147,11 +149,11 @@ def test_create_inventory_contact() :
                              contact_number = "0262991200", note = "chez jesai",tags = "#pasfiable")
 
 
-    created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
-    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
-    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
-                                                     cls=model_manager.JsonAlchemyDecoder)
+    created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
+    print(json.dumps(created_item, cls=model_manager.ComplexEncoder))
+    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.ComplexEncoder),
+                                                     cls=model_manager.ComplexDecoder)
 
 
     get_inventory_contact(1)
@@ -165,20 +167,20 @@ def test_modify_inventory_contact() :
     Item = inventory_contact(contact_person = "Jesai Dour", contact_mail = "antenne@gmail.com",
                              contact_number = "0262991200", note = "chez jesai",tags = "#pasfiable")
 
-    created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
 
     Item.contact_person = "Jesai Dourdaine"
     Item.note = "un gars moyennement fiable"
     Item.tags = "#jesai #debranchelantenne #pasfiable"
 
-    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    print(json.dumps(Item, cls=model_manager.ComplexEncoder))
 
-    modifiedItem = inventory_contact_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
+    modifiedItem = inventory_contact_update(created_item.id, json.dumps(Item, cls=model_manager.ComplexEncoder))
 
     #encoding then decoding to remove "not json" compliant variable
-    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
-                                                     cls=model_manager.JsonAlchemyDecoder)
+    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.ComplexEncoder),
+                                                     cls=model_manager.ComplexDecoder)
 
     for key in  attributes_dict:
         print("modified item value : " + str(modifiedItem.__dict__[key]))
@@ -209,21 +211,23 @@ def test_create_inventory_group() :
     group1 = inventory_group(name = "Saline 2 routers", note = "tout les routeur chez ashvin")
 
 
-    created_item1 = inventory_item_create(json.dumps(Item1, cls=model_manager.JsonAlchemyEncoder))
-    created_item2 = inventory_item_create(json.dumps(Item2, cls=model_manager.JsonAlchemyEncoder))
+    created_item1 = inventory_item_create(json.dumps(Item1, cls=model_manager.ComplexEncoder))
+    created_item2 = inventory_item_create(json.dumps(Item2, cls=model_manager.ComplexEncoder))
 
-    created_group1 = inventory_group_create([],json.dumps(group1, cls=model_manager.JsonAlchemyEncoder))
+    created_group1 : inventory_group = inventory_group_create(json.dumps(group1, cls=model_manager.ComplexEncoder))
+    print("INTERNAL GROUP ID : " + str(created_group1.id))
 
     inventory_group_add_item(created_group1.id,created_item1.id)
     inventory_group_add_item(created_group1.id,created_item2.id)
+
+    global tmp_id_gr
     global tmp_id1
     global tmp_id2
-    global tmp_id_gr
     tmp_id_gr = created_group1.id
     tmp_id1 = created_item1.id
     tmp_id2 = created_item2.id
 
-    print("INTERNAL GROUP ID : " + str(tmp_id_gr))
+    print("EXTERNAL GROUP ID : " + str(tmp_id_gr))
 
     for item in get_all_inventory_items() :
         if item.id == created_item1.id or item.id == created_item2.id :