Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qing-wq committed Oct 10, 2024
1 parent 0f50324 commit 3095a3f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.langchain4j.service.spring.mode.automatic.differentPackage.package1;
package dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DifferentPackageAiServiceApplication {
public class ScanBasePackagesAiServiceApplication {

public static void main(String[] args) {
SpringApplication.run(DifferentPackageAiServiceApplication.class, args);
SpringApplication.run(ScanBasePackagesAiServiceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.langchain4j.service.spring.mode.automatic.differentPackage.package1;
package dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package1;

import dev.langchain4j.service.spring.AiServicesAutoConfig;
import dev.langchain4j.service.spring.mode.automatic.differentPackage.package2.DifferentPackageAiService;
import dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package2.DifferentPackageAiService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand All @@ -10,23 +10,27 @@
import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static org.assertj.core.api.Assertions.assertThat;

class DifferentPackageAiServiceIT {
class ScanBasePackagesAiServiceIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));

@ComponentScan(value = "dev.langchain4j.service.spring.mode.automatic.differentPackage.package2")
@ComponentScan(value = "dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package2")
static class ComponentScanWithValue {
}

@ComponentScan(basePackages = "dev.langchain4j.service.spring.mode.automatic.differentPackage.package2")
@ComponentScan(basePackages = "dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package2")
static class ComponentScanWithBasePackages {
}

@ComponentScan(basePackageClasses = dev.langchain4j.service.spring.mode.automatic.differentPackage.package2.DifferentPackageAiService.class)
@ComponentScan(basePackageClasses = dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package2.DifferentPackageAiService.class)
static class ComponentScanWithBasePackageClasses {
}

@ComponentScan(basePackages = "${test.basePackages}")
static class ComponentScanWithPlaceholder {
}

@Test
void should_create_AI_service_that_use_componentScan_value() {

Expand All @@ -36,7 +40,7 @@ void should_create_AI_service_that_use_componentScan_value() {
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0"
)
.withUserConfiguration(DifferentPackageAiServiceApplication.class)
.withUserConfiguration(ScanBasePackagesAiServiceApplication.class)
.withUserConfiguration(ComponentScanWithValue.class)
.run(context -> {

Expand All @@ -60,7 +64,7 @@ void should_create_AI_service_that_use_componentScan_basePackages() {
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0"
)
.withUserConfiguration(DifferentPackageAiServiceApplication.class)
.withUserConfiguration(ScanBasePackagesAiServiceApplication.class)
.withUserConfiguration(ComponentScanWithBasePackages.class)
.run(context -> {

Expand All @@ -84,7 +88,7 @@ void should_create_AI_service_that_use_componentScan_basePackageClasses() {
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0"
)
.withUserConfiguration(DifferentPackageAiServiceApplication.class)
.withUserConfiguration(ScanBasePackagesAiServiceApplication.class)
.withUserConfiguration(ComponentScanWithBasePackageClasses.class)
.run(context -> {

Expand All @@ -98,4 +102,29 @@ void should_create_AI_service_that_use_componentScan_basePackageClasses() {
assertThat(answer).containsIgnoringCase("Berlin");
});
}

@Test
void should_create_AI_service_that_use_componentScan_with_placeholder() {

contextRunner
.withPropertyValues(
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0",
"test.basePackages=dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package2"
)
.withUserConfiguration(ScanBasePackagesAiServiceApplication.class)
.withUserConfiguration(ComponentScanWithPlaceholder.class)
.run(context -> {

// given
DifferentPackageAiService aiService = context.getBean(DifferentPackageAiService.class);

// when
String answer = aiService.chat("What is the capital of Germany?");

// then
assertThat(answer).containsIgnoringCase("Berlin");
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.langchain4j.service.spring.mode.automatic.differentPackage.package2;
package dev.langchain4j.service.spring.mode.automatic.scanBasePackages.package2;

import dev.langchain4j.service.spring.AiService;

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 3095a3f

Please sign in to comment.