Back to Repositories

Testing QianFan Image Generation Integration in ruoyi-vue-pro

This test suite validates the integration and functionality of the QianFan Image Model within the RuoYi Vue Pro framework. It focuses on testing image generation capabilities using Spring AI’s QianFan implementation, ensuring proper API interaction and response handling.

Test Coverage Overview

The test suite covers the core functionality of QianFan image generation API integration.

Key areas tested include:
  • API authentication and initialization
  • Image generation with specific dimension constraints
  • Response handling and image output validation
  • Support for various image dimension configurations (1024×1024, 768×768, etc.)

Implementation Analysis

The testing approach utilizes JUnit 5 framework with Spring AI integration. The implementation demonstrates a clean separation of concerns between API configuration and execution.

Notable patterns include:
  • Builder pattern for image options configuration
  • Dependency injection for API client initialization
  • Disabled test annotation for controlled execution

Technical Details

Testing infrastructure includes:
  • Spring AI QianFan client libraries
  • JUnit 5 testing framework
  • Custom image viewing utility
  • QianFanImageApi configuration with authentication credentials
  • Stable Diffusion XL model integration

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Clear test method naming and organization
  • Proper exception handling and resource management
  • Configurable test parameters
  • Isolated test environment setup
  • Comprehensive API interaction testing

yunaiv/ruoyi-vue-pro

yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/QianFanImageTests.java

            
package cn.iocoder.yudao.framework.ai.image;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.qianfan.QianFanImageModel;
import org.springframework.ai.qianfan.QianFanImageOptions;
import org.springframework.ai.qianfan.api.QianFanImageApi;

import static cn.iocoder.yudao.framework.ai.image.StabilityAiImageModelTests.viewImage;

/**
 * {@link QianFanImageModel} 集成测试类
 */
public class QianFanImageTests {

    private final QianFanImageApi imageApi = new QianFanImageApi(
            "qS8k8dYr2nXunagK4SSU8Xjj", "pHGbx51ql2f0hOyabQvSZezahVC3hh3e");
    private final QianFanImageModel imageModel = new QianFanImageModel(imageApi);

    @Test
    @Disabled
    public void testCall() {
        // 准备参数
        // 只支持 1024x1024、768x768、768x1024、1024x768、576x1024、1024x576
        QianFanImageOptions imageOptions = QianFanImageOptions.builder()
                .withModel(QianFanImageApi.ImageModel.Stable_Diffusion_XL.getValue())
                .withWidth(1024).withHeight(1024)
                .withN(1)
                .build();
        ImagePrompt prompt = new ImagePrompt("good", imageOptions);

        // 方法调用
        ImageResponse response = imageModel.call(prompt);
        // 打印结果
        String b64Json = response.getResult().getOutput().getB64Json();
        System.out.println(response);
        viewImage(b64Json);
    }

}