Skip to content

Commit

Permalink
Added test cases in edgechain-app (#221)
Browse files Browse the repository at this point in the history
* Adding test cases in ReactChain

* Google Java Format

* Adding test cases for Chunker

* Adding test cases for CodeInterpreter

* Google Java Format

* Adding test cases for jsonnet

* Google Java Format

* Adding test cases for PostgreSQLHistoryContextRepository

* Google Java Format

---------

Co-authored-by: github-actions <>
  • Loading branch information
hemantDwivedi authored Sep 9, 2023
1 parent 05448b7 commit d222eeb
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 11 deletions.
5 changes: 5 additions & 0 deletions FlySpring/edgechain-app/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE history_context (
id VARCHAR(255) NOT NULL PRIMARY KEY,
response VARCHAR(1024),
created_at TIMESTAMP
);
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.edgechain.chunker;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

import com.edgechain.lib.chunk.Chunker;
import com.edgechain.lib.chunk.enums.LangType;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class ChunkerTest {

Expand Down Expand Up @@ -82,4 +86,61 @@ public void chunker_BySentenceWithEmptyInput_ReturnEmptyArray(TestInfo testInfo)
String[] expected = {"This is a test."};
assertArrayEquals(expected, result);
}

@Test
@DisplayName("Test By Very Small ChunkSize ")
void chunker_ByVerySmallChunkSize_ReturnedExpectedValue() {
String input = "This is Testing";
Chunker chunker = new Chunker(input);

String[] result = chunker.byChunkSize(1);

String[] expected = {"T", "h", "i", "s", "i", "s", "T", "e", "s", "t", "i", "n", "g"};
assertNotEquals(expected, result);
}

@Test
@DisplayName("Test By ChunkSize - Input Contains Whitespace")
void chunker_ByChunkSize_InputWhiteSpaceCharacter_ReturnedExpectedValue() {
String input = "\n\t\t";
Chunker chunker = new Chunker(input);
int chunkSize = 5;

String[] result = chunker.byChunkSize(chunkSize);

String[] expected = {""};
assertArrayEquals(expected, result);
}

@Test
@DisplayName("Test By Sentence - Contains Only Spaces")
void chunker_BySentence_InputContainsOnlySpaces_ReturnedExpectedValue() {
String input = " ";
Chunker chunker = new Chunker(input);

String[] result = chunker.bySentence(LangType.EN);
logger.info(Arrays.toString(result));
String[] expected = {};
assertArrayEquals(expected, result);
assertEquals(expected.length, result.length);
}

@Test
@DisplayName("Performance Test With Large String")
@Timeout(value = 5, unit = TimeUnit.SECONDS)
void chunker_Performance_LargeInputString_ReturnedExpectedValue() {
String input = "E".repeat(10000);
Chunker chunker = new Chunker(input);
int chunkSize = 5;

long startTime = System.currentTimeMillis();
String[] result = chunker.byChunkSize(chunkSize);
long endTime = System.currentTimeMillis();
long totalExecutionTime = endTime - startTime;
logger.info(String.valueOf(totalExecutionTime));

long maxExecutionTime = 5000; // Execution time in mills
assertEquals(2000, result.length);
assertTrue(totalExecutionTime <= maxExecutionTime);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.edgechain.codeInterpreter;

import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -65,4 +63,14 @@ public void test_empty_example_extraction() throws Exception {
assertNotNull(extractedValue);
assertFalse(extractedValue.contains(prompt));
}

@Test
@DisplayName("Test for empty input")
void test_emptyInput_ReturnedExpectedValue() {
String inputJsonnet = "";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();

assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.edgechain.jsonnet;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

Expand All @@ -20,6 +16,9 @@
import com.edgechain.lib.jsonnet.JsonnetLoader;
import com.edgechain.lib.jsonnet.impl.FileJsonnetLoader;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
public class JsonnetLoaderTest {

Expand Down Expand Up @@ -106,4 +105,36 @@ public void test_external_variable_xtrasonnet() throws Exception {
assertNotNull(externalVar);
assertEquals(externalVar, "5");
}

@Test
void jsonLoader_LoadJsonnet_WithInvalidJsonnet_ThrowsException() {
String inputJsonnet = "This is a test sentence.";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();
assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream));
}

@Test
void jsonLoader_LoadJsonnet_WithEmptyJsonnet_ThrowsExpcetion() {
String inputJsonnet = "{}";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();
jsonnetLoader.load(inputStream);
assertThrows(Exception.class, () -> jsonnetLoader.get("jsonnet"));
}

@Test
void jsonLoader_LoadJsonnetWithArrayOfObjects_ReturnExpectedValue(TestInfo testInfo) {
String inputJsonnet = "{ \"objects\": [{ \"key\": \"value1\" }, { \"key\": \"value2\" }] }";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();

jsonnetLoader.load(inputStream);
JSONArray objects = jsonnetLoader.getArray("objects");

assertNotNull(objects);
assertEquals(2, objects.length());
assertEquals("value1", objects.getJSONObject(0).getString("key"));
assertEquals("value2", objects.getJSONObject(1).getString("key"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.edgechain.lib.context.client.repositories;

import com.edgechain.lib.context.domain.HistoryContext;
import com.edgechain.testutil.PostgresTestContainer;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.jdbc.Sql;

import java.time.LocalDateTime;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Sql(scripts = {"classpath:schema.sql"})
class PostgreSQLHistoryContextRepositoryTest {

Logger logger = LoggerFactory.getLogger(getClass());

@Autowired private PostgreSQLHistoryContextRepository repository;

private static final PostgresTestContainer instance =
new PostgresTestContainer(PostgresTestContainer.PostgresImage.VECTOR);

@BeforeAll
static void setupAll() {
instance.start();
}

@AfterAll
static void tearAll() {
instance.stop();
}

@BeforeEach
void setUp() {
repository.deleteAll();
}

@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", instance::getJdbcUrl);
registry.add("spring.datasource.username", instance::getUsername);
registry.add("spring.datasource.password", instance::getPassword);
}

@Test
void test_Save_And_Retrieve_History_Context() {
HistoryContext historyContext = getHistoryContext();
repository.save(historyContext);

Optional<HistoryContext> result = repository.findById("1");
logger.info("history context {}", result);

assertTrue(result.isPresent());
}

@Test
void test_Delete_History_Context() {
HistoryContext historyContext = getHistoryContext();
repository.save(historyContext);

repository.deleteById("1");
Optional<HistoryContext> result = repository.findById("1");

assertTrue(result.isEmpty());
}

@Test
void test_Find_By_Non_Exist_Context() {
Optional<HistoryContext> result = repository.findById("10");
assertTrue(result.isEmpty());
}

private HistoryContext getHistoryContext() {
return new HistoryContext("1", "testing history context", LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.edgechain.reactChain;

import com.edgechain.lib.jsonnet.JsonnetLoader;
import com.edgechain.lib.jsonnet.exceptions.JsonnetLoaderException;
import com.edgechain.lib.jsonnet.impl.FileJsonnetLoader;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -9,8 +10,8 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
public class ReactChainTest {
Expand Down Expand Up @@ -76,4 +77,48 @@ local callFunction(funcName) =
assertNotNull(searchFunction);
assertEquals(searchFunction, "udf.fn");
}

@Test
@DisplayName("Test extractAction with invalid input")
void test_extractAction_WithInvalidJsonnet() throws Exception {
String inputJsonnet = "This is invalid jsonnet.";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();
assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream));
}

@Test
@DisplayName("Test extractAction with empty input")
void test_extractAction_withEmptyJsonnet() throws Exception {
String inputJsonnet = "";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();
assertThrows(Exception.class, () -> jsonnetLoader.get("action"));
assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream));
}

@Test
@DisplayName("Test extractThought - invalid input")
void test_extractThought_WithInvalidInput() {
String inputJsonnet = "This is not a valid jsonnet pattern";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();
assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream));
}

@Test
@DisplayName("Test Mapper - Missing function")
public void test_mapper_MissingFunction_ReturnedExpectedResult() {
String inputJsonnet =
"""
local config = {
"edgechains.config": {
"mapper": {},
},
};
""";
InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes());
JsonnetLoader jsonnetLoader = new FileJsonnetLoader();
assertThrows(JsonnetLoaderException.class, () -> jsonnetLoader.load(inputStream));
}
}

0 comments on commit d222eeb

Please sign in to comment.