Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
chore(entity): change logic of update and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Sevcik committed Jul 15, 2021
1 parent e3c8f58 commit 3896196
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 148 deletions.
4 changes: 3 additions & 1 deletion test/entities/test_objects_to_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def generate_base_component(component_base_factory, link_factory, group_factory)
)
component.save()

group = group_factory(id=1, product_owner="john", project_owner="doe", entity=component)
group = group_factory(
id=1, product_owner="john", project_owner="doe", entity=component
)
group.save()

link_factory(
Expand Down
254 changes: 195 additions & 59 deletions test/entities/test_yaml_to_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,6 @@
pytestmark = pytest.mark.django_db


@pytest.fixture
def create_component_and_service(component_base_factory, repository_factory, link_factory, group_factory, service_factory):
repository = repository_factory(
id=1,
remote_id=11,
name="test_proj1",
owner="john_doe1",
url="https://github.co m/john_doe1/test_proj1",
provider="github",
)
service = service_factory(
id=1,
name="service",
owner="platform",
impact="profit",
pagerduty_service="sales/P019873X9",
slack_channel="https://slackchannel",
lifecycle="fixed",
description="original description",
)

component = component_base_factory(
id=1,
name="original",
type="database",
description="original description",
kind="component",
owner="platform",
tags=["python", "service"],
service=service,
library=None,
source=repository
)
group = group_factory(id=1, product_owner="john", project_owner="doe", entity=component)
link_factory(
id=1,
name="Datadog",
url="https://dashboard.datadog.com",
icon="poop",
entity=component,
)
link_factory(
id=2,
name="Sentry",
url="https://sentry.skypicker.com",
entity=component,
)

def test_create_base_component(mocker, repository_factory):
repository = repository_factory(
id=1,
Expand All @@ -71,7 +23,8 @@ def test_create_base_component(mocker, repository_factory):
- apiVersion: v1alpha1
kind_: component
metadata:
name: base
name: base-component
label: Base Component
owner: platform
group:
product_owner: john
Expand All @@ -94,7 +47,8 @@ def test_create_base_component(mocker, repository_factory):
component_entity = Entity.objects.first()
assert Entity.objects.all().count() == 1
assert component_entity.kind == "component"
assert component_entity.name == "base"
assert component_entity.name == "base-component"
assert component_entity.label == "Base Component"
assert component_entity.owner == "platform"
assert component_entity.service is None
assert component_entity.library is None
Expand All @@ -118,7 +72,8 @@ def test_create_base_component_and_service(mocker, repository_factory):
- apiVersion: v1alpha1
kind_: component
metadata:
name: base
name: base-service
label: Base Service
owner: platform
group:
product_owner: john
Expand Down Expand Up @@ -160,7 +115,8 @@ def test_create_base_component_and_service(mocker, repository_factory):
assert Entity.objects.all().count() == 1
assert Service.objects.all().count() == 1
assert component_entity.kind == "component"
assert component_entity.name == "base"
assert component_entity.name == "base-service"
assert component_entity.label == "Base Service"
assert component_entity.owner == "platform"
assert component_entity.library is None
assert component_entity.type == "service"
Expand All @@ -186,6 +142,7 @@ def test_create_base_component_and_library(mocker, repository_factory):
kind_: component
metadata:
name: base_lib
label: Base Lib
owner: platform
group:
product_owner: john
Expand Down Expand Up @@ -216,6 +173,7 @@ def test_create_base_component_and_library(mocker, repository_factory):
assert Library.objects.all().count() == 1
assert component_entity.kind == "component"
assert component_entity.name == "base_lib"
assert component_entity.label == "Base Lib"
assert component_entity.owner == "platform"
assert component_entity.type == "library"
assert component_entity.links.all().count() == 2
Expand All @@ -230,6 +188,7 @@ def test_create_multiple_components_one_service(mocker, repository_factory):
kind_: component
metadata:
name: base
label: Base
owner: platform
group:
product_owner: john
Expand All @@ -250,6 +209,7 @@ def test_create_multiple_components_one_service(mocker, repository_factory):
kind_: component
metadata:
name: service
label: Service
owner: platform
group:
product_owner: john
Expand Down Expand Up @@ -299,12 +259,14 @@ def test_create_multiple_components_one_service(mocker, repository_factory):
assert Entity.objects.all().count() == 2
assert Service.objects.all().count() == 1


def test_create_multiple_components_multiple_services(mocker, repository_factory):
multiple_components = """
- apiVersion: v1alpha1
kind_: component
metadata:
name: base
label: Base
owner: platform
group:
product_owner: john
Expand All @@ -325,6 +287,7 @@ def test_create_multiple_components_multiple_services(mocker, repository_factory
kind_: component
metadata:
name: service
label: Base
owner: platform
group:
product_owner: john
Expand Down Expand Up @@ -360,7 +323,8 @@ def test_create_multiple_components_multiple_services(mocker, repository_factory
- apiVersion: v1alpha1
kind_: component
metadata:
name: service the second
name: service-the-second
label: Service The Second
owner: platform software
group:
product_owner: john the first
Expand Down Expand Up @@ -411,19 +375,129 @@ def test_create_multiple_components_multiple_services(mocker, repository_factory
assert Service.objects.all().count() == 2


def test_update_service(mocker, create_component_and_service):
def test_update_base_component(
mocker, repository_factory, component_base_factory, link_factory, group_factory
):
component = component_base_factory(
id=1,
name="old-component",
label="Old Component",
type="database",
description="Old Component Description",
kind="component",
owner="platform",
service=None,
library=None,
source__id=1,
source__remote_id=1,
source__owner="jasckson",
source__name="thiwer",
source__url="https://gitlab.com/thiwer/thiwer",
source__provider="gitlab",
)
component.save()

group = group_factory(
id=1, product_owner="Old John", project_owner="Old Doe", entity=component
)
group.save()

service_component = """
link_factory(
id=1,
name="Datadog",
url="https://dashboard.datadog.com",
icon="poop",
entity=component,
)

base_component = """
- apiVersion: v1alpha1
kind_: component
metadata:
name: new-component
label: New Component
owner: platform
group:
product_owner: New John
project_owner: New Doe
maintainers: []
description: New Component Description
tags: []
links:
- name: Datadog
url: https://dashboard.datadog.com
icon: poop
- name: Sentry
url: https://sentry.skypicker.com
spec:
type_: database
"""

repository_dict = {
"id": component.source.remote_id,
"provider": component.source.provider,
}
mocker.patch("zoo.repos.tasks.get_entity_file_content", return_value=base_component)

component_entity = Entity.objects.first()
assert Entity.objects.all().count() == 1
assert component_entity.kind == "component"
assert component_entity.name == "old-component"
assert component_entity.label == "Old Component"
assert component_entity.owner == "platform"
assert component_entity.type == "database"
assert component_entity.links.all().count() == 1
assert component_entity.groups.all().count() == 1
assert component_entity.groups.first().product_owner == "Old John"
assert component_entity.groups.first().project_owner == "Old Doe"
assert component_entity.service is None
assert component_entity.library is None

uut.update_project_from_entity_file(proj=repository_dict)

component_entity = Entity.objects.first()
assert Entity.objects.all().count() == 1
assert component_entity.kind == "component"
assert component_entity.name == "new-component"
assert component_entity.label == "New Component"
assert component_entity.owner == "platform"
assert component_entity.type == "database"
assert component_entity.links.all().count() == 2
assert component_entity.groups.all().count() == 1
assert component_entity.groups.first().product_owner == "New John"
assert component_entity.groups.first().project_owner == "New Doe"
assert component_entity.service is None
assert component_entity.library is None


def test_update_service_library(
mocker, component_base_factory, service_factory, repository_factory, library_factory
):
repository = repository_factory(
id=22,
remote_id=22,
name="test_proj1",
owner="john_doe1",
url="https://github.com/john_doe1/test_proj1",
provider="github",
)
service = service_factory(repository=repository)
library = library_factory(repository=repository)
component_base_factory(source=repository, service=service)
component_base_factory(source=repository, library=library)

component_service_and_library = """
- apiVersion: v1alpha1
kind_: component
metadata:
name: service
name: new-service
label: New Service
owner: platform
group:
product_owner: john
project_owner: doe
product_owner: New John
project_owner: New Doe
maintainers: []
description: This is my fancy component
description: New Description
tags: []
links:
- name: Datadog
Expand All @@ -449,4 +523,66 @@ def test_update_service(mocker, create_component_and_service):
pagerduty_service: pagerduty_service1234
sentry_project: sentry project 15234
lifecycle: production
---
- apiVersion: v1alpha1
kind_: component
metadata:
name: new-lib
label: New Lib
owner: platform
group:
product_owner: john
project_owner: doe
maintainers: []
description: This is my fancy component
tags: []
links:
- name: Datadog
url: https://dashboard.datadog.com
icon: poop
- name: Sentry
url: https://sentry.skypicker.com
spec:
type_: library
impact: profit
integrations:
sonarqube_project: sonarqube
lifecycle: production
"""

repository_dict = {
"id": repository.remote_id,
"provider": repository.provider,
}
mocker.patch(
"zoo.repos.tasks.get_entity_file_content",
return_value=component_service_and_library,
)

assert Entity.objects.all().count() == 2
assert Service.objects.all().count() == 1
assert Library.objects.all().count() == 1

uut.update_project_from_entity_file(proj=repository_dict)

assert Entity.objects.all().count() == 2
assert Service.objects.all().count() == 1
assert Library.objects.all().count() == 1

service_entity = Entity.objects.filter(service__isnull=False).first()
library_entity = Entity.objects.filter(library__isnull=False).first()

assert service_entity.kind == "component"
assert service_entity.type == "service"
assert service_entity.name == "new-service"
assert service_entity.label == "New Component"
assert service_entity.owner == "platform"
assert service_entity.links.all().count() == 2
assert service_entity.environments.all.count() == 2

assert library_entity.kind == "component"
assert library_entity.type == "library"
assert library_entity.name == "new-library"
assert library_entity.label == "New Lbrary"
assert library_entity.owner == "platform"
assert library_entity.links.all().count() == 2
6 changes: 2 additions & 4 deletions zoo/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ..analytics.models import Dependency, DependencyType, DependencyUsage
from ..auditing.models import Issue
from ..entities.models import Link, Entity
from ..entities.models import Entity, Link
from ..globalsearch.indexer import IndexType
from ..globalsearch.meili_client import meili_client
from ..libraries.models import Library
Expand Down Expand Up @@ -204,9 +204,7 @@ def resolve_all_links(self, info, **kwargs):
node = types.Link.from_db(link)
edges.append(types.LinkConnection.Edge(node=node, cursor=cursor))

return types.LinkConnection(
page_info=page_info, edges=edges, total_count=total
)
return types.LinkConnection(page_info=page_info, edges=edges, total_count=total)

def resolve_all_libraries(self, info, **kwargs):
paginator = Paginator(**kwargs)
Expand Down
Loading

0 comments on commit 3896196

Please sign in to comment.