Files
unstract/backend/connector_v2/tests/connector_tests.py
Jaseem Jas ba1df894d2 Python 3.9 to 3.12 (#1231)
* python version updated from 3.9 into 3.12

* x2text-service updated with uv and python version 3.12

* x2text-service docker file updated

* Unstract packages updated with uv

* Runner updated with uv

* Promptservice updated with uv

* Platform service updated with uv

* backend service updated with uv

* root pyproject.toml file updated

* sdk version updated in services

* unstract package modules updated based on sdk version:

* docker file update

* pdm lock workflow modified to support uv

* Docs updated based on uv support

* lock automation updated

* snowflake module version updated into 3.14.0

* tox updated to support UV

* tox updated to support UV

* tox updated with pytest

* tox updated with pytest-md-report

* tox updated with module requirements

* python migration from 3.9 to 3.12

* tox updated with module requirements

* runner updated

* Commit uv.lock changes

* runner updated

* Commit uv.lock changes

* pytest.ini added

* x2text-service docker file updated

* pytest.ini removed

* environment updated to test

* docformatter commented on pre-commit

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* some pre-commit issues ignored

* some pre-commit issues ignored

* some pre-commit issues ignored

* some pre-commit issues ignored

* some pre-commit issues ignored

* pre-commit updates

* un used import removed from platfrom service controller

* tox issue fixed

* tox issue fixed

* docker files updated

* backend dockerfile updated

* open installation issue fixed

* Tools docker file updated with base python version 3.12

* python version updated into min 3.12 in pyproject.toml

* linting issue fixed

* uv version upgraded into 0.6.14

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* migrations excluded from ruff

* added PoethePoet task runner

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* feat: Added poe tasks for services (#1248)

* Added poe tasks for services

* reverted FE change made by mistake

* updated tool-sidecar to uv and python to 3.12.9

* minor updates in pyproject descreption

* feat: platform-service logging improvements (#1255)

feat: Used flask util from core to improve logging in platform-service, added core as a dependency to platform-service:

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: Platform-service build issue and numpy issue with Python 3.12 (#1258)

* fix: Platform-service build and numpy issue with Py 3.12

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: Removed backend dockerfile install statements for numpy

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* minor: Handled scenario when cost is not calculated due to no usage

* minor: Corrected content shown for workflow input

* fix: Minor fixes, used gthread for prompt-service, runner

* Commit uv.lock changes

* Removed unused line in tool dockerfile

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Chandrasekharan M <chandrasekharan@zipstack.com>
Co-authored-by: Chandrasekharan M <117059509+chandrasekharan-zipstack@users.noreply.github.com>
Co-authored-by: ali-zipstack <muhammad.ali@zipstack.com>
2025-04-24 16:07:02 +05:30

315 lines
12 KiB
Python

# mypy: ignore-errors
import pytest
from connector_v2.models import ConnectorInstance
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
pytestmark = pytest.mark.django_db
@pytest.mark.connector
class TestConnector(APITestCase):
def test_connector_list(self) -> None:
"""Tests to List the connectors."""
url = reverse("connectors_v1-list")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_connectors_detail(self) -> None:
"""Tests to fetch a connector with given pk."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_connectors_detail_not_found(self) -> None:
"""Tests for negative case to fetch non exiting key."""
url = reverse("connectors_v1-detail", kwargs={"pk": 768})
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_connectors_create(self) -> None:
"""Tests to create a new ConnectorInstance."""
url = reverse("connectors_v1-list")
data = {
"org": 1,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "sample_url",
"sharable_link": True,
},
}
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(ConnectorInstance.objects.count(), 2)
def test_connectors_create_with_json_list(self) -> None:
"""Tests to create a new connector with list included in the json
field.
"""
url = reverse("connectors_v1-list")
data = {
"org": 1,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "sample_url",
"sharable_link": True,
"file_name_list": ["a1", "a2"],
},
}
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(ConnectorInstance.objects.count(), 2)
def test_connectors_create_with_nested_json(self) -> None:
"""Tests to create a new connector with json field as nested json."""
url = reverse("connectors_v1-list")
data = {
"org": 1,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "sample_url",
"sharable_link": True,
"sample_metadata_json": {"key1": "value1", "key2": "value2"},
},
}
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(ConnectorInstance.objects.count(), 2)
def test_connectors_create_bad_request(self) -> None:
"""Tests for negative case to throw error on a wrong access."""
url = reverse("connectors_v1-list")
data = {
"org": 5,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "sample_url",
"sharable_link": True,
"sample_metadata_json": {"key1": "value1", "key2": "value2"},
},
}
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_connectors_update_json_field(self) -> None:
"""Tests to update connector with json field update."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
data = {
"org": 1,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "new_sample_url",
"sharable_link": True,
"sample_metadata_json": {"key1": "value1", "key2": "value2"},
},
}
response = self.client.put(url, data, format="json")
drive_link = response.data["connector_metadata"]["drive_link"]
self.assertEqual(drive_link, "new_sample_url")
def test_connectors_update(self) -> None:
"""Tests to update connector update single field."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
data = {
"org": 1,
"project": 1,
"created_by": 1,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "new_sample_url",
"sharable_link": True,
"sample_metadata_json": {"key1": "value1", "key2": "value2"},
},
}
response = self.client.put(url, data, format="json")
modified_by = response.data["modified_by"]
self.assertEqual(modified_by, 2)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_connectors_update_pk(self) -> None:
"""Tests the PUT method for 400 error."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
data = {
"org": 2,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "new_sample_url",
"sharable_link": True,
"sample_metadata_json": {"key1": "value1", "key2": "value2"},
},
}
response = self.client.put(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_connectors_update_json_fields(self) -> None:
"""Tests to update ConnectorInstance."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
data = {
"org": 1,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "new_sample_url",
"sharable_link": True,
"sample_metadata_json": {"key1": "value1", "key2": "value2"},
},
}
response = self.client.put(url, data, format="json")
nested_value = response.data["connector_metadata"]["sample_metadata_json"]["key1"]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(nested_value, "value1")
def test_connectors_update_json_list_fields(self) -> None:
"""Tests to update connector to the third second level of json."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
data = {
"org": 1,
"project": 1,
"created_by": 2,
"modified_by": 2,
"modified_at": "2023-06-14T05:28:47.759Z",
"connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
"connector_metadata": {
"drive_link": "new_sample_url",
"sharable_link": True,
"sample_metadata_json": {"key1": "value1", "key2": "value2"},
"file_list": ["a1", "a2", "a3"],
},
}
response = self.client.put(url, data, format="json")
nested_value = response.data["connector_metadata"]["sample_metadata_json"]["key1"]
nested_list = response.data["connector_metadata"]["file_list"]
last_val = nested_list.pop()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(nested_value, "value1")
self.assertEqual(last_val, "a3")
# @pytest.mark.xfail(raises=KeyError)
# def test_connectors_update_json_fields_failed(self) -> None:
# """Tests to update connector to the second level of JSON with a wrong
# key."""
# url = reverse("connectors_v1-detail", kwargs={"pk": 1})
# data = {
# "org": 1,
# "project": 1,
# "created_by": 2,
# "modified_by": 2,
# "modified_at": "2023-06-14T05:28:47.759Z",
# "connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
# "connector_metadata": {
# "drive_link": "new_sample_url",
# "sharable_link": True,
# "sample_metadata_json": {"key1": "value1", "key2": "value2"},
# },
# }
# response = self.client.put(url, data, format="json")
# nested_value = response.data["connector_metadata"]["sample_metadata_json"][
# "key00"
# ]
# @pytest.mark.xfail(raises=KeyError)
# def test_connectors_update_json_nested_failed(self) -> None:
# """Tests to update connector to test a first level of json with a wrong
# key."""
# url = reverse("connectors_v1-detail", kwargs={"pk": 1})
# data = {
# "org": 1,
# "project": 1,
# "created_by": 2,
# "modified_by": 2,
# "modified_at": "2023-06-14T05:28:47.759Z",
# "connector_id": "e3a4512m-efgb-48d5-98a9-3983nd77f",
# "connector_metadata": {
# "drive_link": "new_sample_url",
# "sharable_link": True,
# "sample_metadata_json": {"key1": "value1", "key2": "value2"},
# },
# }
# response = self.client.put(url, data, format="json")
# nested_value = response.data["connector_metadata"]["sample_metadata_jsonNew"]
def test_connectors_update_field(self) -> None:
"""Tests the PATCH method."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
data = {"connector_id": "e3a4512m-efgb-48d5-98a9-3983ntest"}
response = self.client.patch(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
connector_id = response.data["connector_id"]
self.assertEqual(
connector_id,
ConnectorInstance.objects.get(connector_id=connector_id).connector_id,
)
def test_connectors_update_json_field_patch(self) -> None:
"""Tests the PATCH method."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
data = {
"connector_metadata": {
"drive_link": "patch_update_url",
"sharable_link": True,
"sample_metadata_json": {
"key1": "patch_update1",
"key2": "value2",
},
}
}
response = self.client.patch(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
drive_link = response.data["connector_metadata"]["drive_link"]
self.assertEqual(drive_link, "patch_update_url")
def test_connectors_delete(self) -> None:
"""Tests the DELETE method."""
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
response = self.client.delete(url, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
url = reverse("connectors_v1-detail", kwargs={"pk": 1})
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)