mirror of
				https://github.com/veops/cmdb.git
				synced 2025-11-04 13:46:17 +08:00 
			
		
		
		
	Merge pull request #36 from OhBonsai/develop
test: add ci and ci relation crud test cases
This commit is contained in:
		@@ -11,6 +11,16 @@ from api.models.cmdb import (
 | 
			
		||||
    CITypeRelation,
 | 
			
		||||
    RelationType
 | 
			
		||||
)
 | 
			
		||||
from api.models.acl import User
 | 
			
		||||
 | 
			
		||||
from api.lib.cmdb.ci_type import CITypeAttributeManager
 | 
			
		||||
from api.lib.cmdb.ci import CIManager, CIRelationManager
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def force_add_user():
 | 
			
		||||
    from flask import g
 | 
			
		||||
    if not getattr(g, "user", None):
 | 
			
		||||
        g.user = User.query.first()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def init_attributes(num=1):
 | 
			
		||||
@@ -88,3 +98,38 @@ def fake_attr_value(attr_dict):
 | 
			
		||||
        return {attr_name: random.randint(0, 1000) / 3.0}
 | 
			
		||||
    elif attr_type == "2":
 | 
			
		||||
        return {attr_name: uuid.uuid4().hex[:8]}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def init_ci(num=1):
 | 
			
		||||
    # store ci need has user
 | 
			
		||||
    force_add_user()
 | 
			
		||||
    ci_type = init_ci_types(1)[0]
 | 
			
		||||
    attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id)
 | 
			
		||||
    manager = CIManager()
 | 
			
		||||
    result = []
 | 
			
		||||
 | 
			
		||||
    for i in range(num):
 | 
			
		||||
        ci_id = manager.add(ci_type.name, **fake_attr_value(attrs[0]))
 | 
			
		||||
        result.append(manager.get_ci_by_id_from_db(ci_id))
 | 
			
		||||
 | 
			
		||||
    return result
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def init_ci_with_type(ci_types):
 | 
			
		||||
    force_add_user()
 | 
			
		||||
    cis = []
 | 
			
		||||
    manager = CIManager()
 | 
			
		||||
    for ci_type in ci_types:
 | 
			
		||||
        attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id)
 | 
			
		||||
        ci_id = manager.add(ci_type.name, **fake_attr_value(attrs[0]))
 | 
			
		||||
        cis.append(manager.get_ci_by_id_from_db(ci_id))
 | 
			
		||||
    return cis
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def init_ci_relation():
 | 
			
		||||
    init_ci_type_relation(1)
 | 
			
		||||
    ci_types = CIType.query.all()
 | 
			
		||||
    cis = init_ci_with_type(ci_types)
 | 
			
		||||
    manager = CIRelationManager()
 | 
			
		||||
    cir_id = manager.add(cis[0]['ci_id'], cis[1]['ci_id'])
 | 
			
		||||
    return cir_id
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,8 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
from .sample import init_ci_types, fake_attr_value
 | 
			
		||||
from api.lib.cmdb.ci_type import CITypeAttributeManager
 | 
			
		||||
from .sample import init_ci_types, fake_attr_value, init_ci
 | 
			
		||||
from api.lib.cmdb.ci_type import CITypeAttributeManager, CITypeManager
 | 
			
		||||
from api.lib.cmdb.ci import CIManager
 | 
			
		||||
from api.models.cmdb import CI
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_create_ci(session, client):
 | 
			
		||||
@@ -25,3 +26,64 @@ def test_create_ci(session, client):
 | 
			
		||||
    assert ci[attrs[0]["name"]] == fake_value[attrs[0]['name']]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_update_ci(session, client):
 | 
			
		||||
    ci = init_ci(1)[0]
 | 
			
		||||
    ci_id = ci.get("ci_id")
 | 
			
		||||
    ci_type = CITypeManager.get_ci_types(ci.get("ci_type"))[0]
 | 
			
		||||
    attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type["id"])
 | 
			
		||||
    url = "/api/v0.1/ci/{}".format(ci_id)
 | 
			
		||||
 | 
			
		||||
    fake_value = fake_attr_value(attrs[0])
 | 
			
		||||
 | 
			
		||||
    payload = {**fake_value}
 | 
			
		||||
 | 
			
		||||
    resp = client.put(url, json=payload)
 | 
			
		||||
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
    assert resp.json["ci_id"] == ci_id
 | 
			
		||||
    ci = CIManager().get_ci_by_id_from_db(ci_id)
 | 
			
		||||
    assert ci[attrs[0]['name']] == fake_value[attrs[0]['name']]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_delete_ci(session, client):
 | 
			
		||||
    ci = init_ci(1)[0]
 | 
			
		||||
    ci_id = ci.get("ci_id")
 | 
			
		||||
    url = "/api/v0.1/ci/{}".format(ci_id)
 | 
			
		||||
 | 
			
		||||
    resp = client.delete(url)
 | 
			
		||||
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
 | 
			
		||||
    ci_from_db = CI.query.filter_by(id=ci_id).first()
 | 
			
		||||
    assert ci_from_db is None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_ci_by_types(session, client):
 | 
			
		||||
    ci = init_ci(1)[0]
 | 
			
		||||
    ci_type = CITypeManager.get_ci_types(ci.get("ci_type"))[0]
 | 
			
		||||
    url = "/api/v0.1/ci/type/{}".format(ci_type["id"])
 | 
			
		||||
 | 
			
		||||
    resp = client.get(url)
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
 | 
			
		||||
    assert resp.json['cis'][0]['ci_id'] == ci['ci_id']
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_ci_by_id(session, client):
 | 
			
		||||
    ci = init_ci(1)[0]
 | 
			
		||||
    url = "/api/v0.1/ci/{}".format(ci["ci_id"])
 | 
			
		||||
 | 
			
		||||
    resp = client.get(url)
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
    assert resp.json['ci_id'] == ci['ci_id']
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_ci_detail_by_id(session, client):
 | 
			
		||||
    ci = init_ci(1)[0]
 | 
			
		||||
    url = "/api/v0.1/ci/{}/detail".format(ci["ci_id"])
 | 
			
		||||
 | 
			
		||||
    resp = client.get(url)
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
    assert resp.json['id'] == ci['ci_id']
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1,39 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
from .sample import init_ci_with_type, init_ci_type_relation, init_ci_relation
 | 
			
		||||
from api.lib.cmdb.ci import CIRelationManager
 | 
			
		||||
from api.models.cmdb import CIRelation, CIType, CI
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_create_ci_relation(session, client):
 | 
			
		||||
    init_ci_type_relation(1)
 | 
			
		||||
    ci_types = CIType.query.all()
 | 
			
		||||
    cis = init_ci_with_type(ci_types)
 | 
			
		||||
 | 
			
		||||
    url = "/api/v0.1/ci_relations/{}/{}".format(cis[0]['ci_id'], cis[1]['ci_id'])
 | 
			
		||||
 | 
			
		||||
    resp = client.post(url)
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
    cr_id = resp.json['cr_id']
 | 
			
		||||
    cr = CIRelation.get_by_id(cr_id)
 | 
			
		||||
    assert cr is not None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_delte_ci_relation_by_ci_id(session, client):
 | 
			
		||||
    cr_id = init_ci_relation()
 | 
			
		||||
    cis = CI.query.all()
 | 
			
		||||
 | 
			
		||||
    url = "/api/v0.1/ci_relations/{}/{}".format(cis[0].id, cis[1].id)
 | 
			
		||||
    resp = client.delete(url)
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
    cr = CIRelation.get_by_id(cr_id)
 | 
			
		||||
    assert cr is None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_delete_ci_relation_by_id(session, client):
 | 
			
		||||
    cr_id = init_ci_relation()
 | 
			
		||||
    url = "/api/v0.1/ci_relations/{cr_id}".format(cr_id=cr_id)
 | 
			
		||||
    resp = client.delete(url)
 | 
			
		||||
    assert resp.status_code == 200
 | 
			
		||||
    cr = CIRelation.get_by_id(cr_id)
 | 
			
		||||
    assert cr is None
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user