Skip to content

Commit

Permalink
Ingestor provider unit tests (#19)
Browse files Browse the repository at this point in the history
* Adding unit tests for ingestor & provider

* On Docs : redirect test task to build
  • Loading branch information
amengus87 authored May 24, 2024
1 parent 1a59d76 commit 21cc899
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ai.dragon.repository;

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

import java.util.UUID;

import org.dizitart.no2.repository.Cursor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import ai.dragon.entity.IngestorEntity;

@SpringBootTest
@ActiveProfiles("test")
public class IngestorRepositoryTest {
@Autowired
private IngestorRepository ingestorRepository;

@Test
void insertIngestors() {
ingestorRepository.deleteAll();
String ingestorName = "dRAGon Ingestor #1";

IngestorEntity ingestor = new IngestorEntity();
ingestor.setUuid(UUID.randomUUID());
ingestor.setName(ingestorName);
ingestorRepository.save(ingestor);

IngestorEntity retrievedIngestor = ingestorRepository.getByUuid(ingestor.getUuid());
assertNotNull(ingestorRepository.getByUuid(retrievedIngestor.getUuid()));
assertEquals(ingestorName, retrievedIngestor.getName());
}

@Test
void findAllIngestors() {
ingestorRepository.deleteAll();
int nbIngestorsToInsert = 3;

for (int i = 0; i < nbIngestorsToInsert; i++) {
ingestorRepository.save(new IngestorEntity());
}

assertEquals(nbIngestorsToInsert, ingestorRepository.countAll());

ingestorRepository.find().forEach(ingestor -> {
assertNotNull(ingestor.getUuid());
});
}

@Test
void findIngestorsByName() {
ingestorRepository.deleteAll();
int nbIngestorsToInsert = 3;

for (int i = 0; i < nbIngestorsToInsert; i++) {
IngestorEntity ingestor = new IngestorEntity();
ingestor.setName(String.format("Ingestor %d", i));
ingestorRepository.save(ingestor);
}

assertEquals(nbIngestorsToInsert, ingestorRepository.countAll());

Cursor<IngestorEntity> cursor = ingestorRepository.findByFieldValue("name", "Ingestor 1");
assertEquals(1, cursor.size());
}

@Test
void deleteIngestors() {
ingestorRepository.deleteAll();
int nbIngestorsToInsert = 3;

for (int i = 0; i < nbIngestorsToInsert; i++) {
ingestorRepository.save(new IngestorEntity());
}

assertEquals(nbIngestorsToInsert, ingestorRepository.countAll());

ingestorRepository.find().forEach(ingestor -> {
ingestorRepository.delete(ingestor.getUuid());
});

assertEquals(0, ingestorRepository.countAll());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ai.dragon.repository;

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

import java.util.UUID;

import org.dizitart.no2.repository.Cursor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import ai.dragon.entity.ProviderEntity;
import ai.dragon.enumeration.ProviderType;

@SpringBootTest
@ActiveProfiles("test")
public class ProviderRepositoryTest {
@Autowired
private ProviderRepository providerRepository;

@Test
void insertProviders() {
providerRepository.deleteAll();
String providerName = "dRAGon Provider #1";

ProviderEntity provider = new ProviderEntity();
provider.setUuid(UUID.randomUUID());
provider.setName(providerName);
provider.setType(ProviderType.OPENAI);
providerRepository.save(provider);

ProviderEntity retrievedProvider = providerRepository.getByUuid(provider.getUuid());
assertNotNull(providerRepository.getByUuid(retrievedProvider.getUuid()));
assertEquals(providerName, retrievedProvider.getName());
}

@Test
void findAllProviders() {
providerRepository.deleteAll();
int nbProvidersToInsert = 3;

for (int i = 0; i < nbProvidersToInsert; i++) {
ProviderEntity provider = new ProviderEntity();
provider.setName(String.format("Provider %d", i));
provider.setType(ProviderType.OPENAI);
providerRepository.save(provider);
}

assertEquals(nbProvidersToInsert, providerRepository.countAll());

providerRepository.find().forEach(provider -> {
assertNotNull(provider.getUuid());
});
}

@Test
void findProvidersByName() {
providerRepository.deleteAll();
int nbProvidersToInsert = 3;

for (int i = 0; i < nbProvidersToInsert; i++) {
ProviderEntity provider = new ProviderEntity();
provider.setName(String.format("Provider %d", i));
provider.setType(ProviderType.OPENAI);
providerRepository.save(provider);
}

assertEquals(nbProvidersToInsert, providerRepository.countAll());

Cursor<ProviderEntity> cursor = providerRepository.findByFieldValue("name", "Provider 1");
assertEquals(1, cursor.size());
}

@Test
void deleteProviders() {
providerRepository.deleteAll();
int nbProvidersToInsert = 3;

for (int i = 0; i < nbProvidersToInsert; i++) {
ProviderEntity provider = new ProviderEntity();
provider.setType(ProviderType.OPENAI);
providerRepository.save(provider);
}

assertEquals(nbProvidersToInsert, providerRepository.countAll());

providerRepository.find().forEach(provider -> {
providerRepository.delete(provider.getUuid());
});

assertEquals(0, providerRepository.countAll());
}
}
6 changes: 6 additions & 0 deletions docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ task npmBuild(type: NpmTask) {
task npmServe(type: NpmTask) {
args = ['run', 'serve']
}

task test(type: NpmTask) {
inputs.dir("src")
outputs.dir("dist")
args = ['run', 'build']
}

0 comments on commit 21cc899

Please sign in to comment.