将label-studio django项目中的单元测试添加到azure存储

问题描述 投票:0回答:1

我正在尝试向 label-studio 项目添加新的单元测试

我无法成功创建下面的存储上传

- name: stage
  request:
    data:
      container: pytest-azure-images
      project: '{project_pk}'
      title: Testing Azure SPI storage (bucket from conftest.py)
      use_blob_urls: true
    method: POST
    url: '{django_live_url}/api/storages/azure_spi'
  response:
    save:
      json:
        storage_pk: id
    status_code: 201

因为我在结果中得到

last_sync_count: 0
。 “conftest.py”中的存储桶是如何上传的?

- name: stage
  request:
    method: POST
    url: '{django_live_url}/api/storages/azure_spi/{storage_pk}/sync'
  response:
    json:
      last_sync_count: 0
    status_code: 200

这是我的分支和 PR 如果你能帮忙的话: https://github.com/HumanSignal/label-studio/pull/5926/files

django pytest azure-storage label-studio tavern
1个回答
0
投票

按照以下步骤将单元测试添加到 Label Studio 项目,以将文件上传到 Azure 存储:

更新 Label Studio 项目的

settings.py
以包含 Azure 存储设置。

# settings.py
AZURE_ACCOUNT_NAME = 'your_account_name'
AZURE_ACCOUNT_KEY = 'your_account_key'
AZURE_CONTAINER = 'azure-images'
  • 使用此参考

    将 Label Studio 连接到 Azure Blob 存储
  • 我使用此 link 在 Django 应用程序中编写单元测试,并使用

    django.test
    运行测试。

  • test_azure_storage.py
    目录下创建单元测试文件
    tests

# tests/test_azure_storage.py

import pytest
from django.conf import settings
from rest_framework.test import APIClient

@pytest.fixture
def api_client():
    return APIClient()

@pytest.mark.django_db
def test_azure_storage_upload(api_client):
    # Create a storage
    response = api_client.post(
        f'{settings.DJANGO_LIVE_URL}/api/storages/azure_spi',
        data={
            'container': settings.AZURE_CONTAINER,
            'project': '{project_pk}',
            'title': 'Testing Azure SPI storage (bucket from conftest.py)',
            'use_blob_urls': True,
        }
    )
    assert response.status_code == 201
    storage_pk = response.json().get('id')

    # Sync the storage
    response = api_client.post(
        f'{settings.DJANGO_LIVE_URL}/api/storages/azure_spi/{storage_pk}/sync'
    )
    assert response.status_code == 200
    assert response.json().get('last_sync_count') > 0

    def test_upload_blob(self):
        container_client = self.blob_service_client.get_container_client(self.container_name)
        blob_client = container_client.get_blob_client("test_blob.txt")
        blob_client.upload_blob("This is a test blob")
      

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.