strangedb/src/test/java/it/cavallium/strangedb/tests/QueryTests.java
2019-04-24 21:32:36 +02:00

69 lines
2.5 KiB
Java

package it.cavallium.strangedb.tests;
import it.cavallium.strangedb.java.database.DatabaseJava;
import it.cavallium.strangedb.java.objects.lists.ElementsArrayList;
import it.cavallium.strangedb.java.objects.lists.ListQuery;
import it.cavallium.strangedb.java.objects.lists.ValuePointer;
import it.cavallium.strangedb.java.objects.lists.operations.Equals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import static org.junit.Assert.assertNotEquals;
public class QueryTests {
public static final String constantFirstName = "Jesus christ this is a very strange first name";
public static final String constantUsername = "is this an username?";
public static final String constantBio = "and is this a bio??? Are you mad?";
private DatabaseJava db;
private Path dir;
@Before
public void setUp() throws Exception {
dir = Files.createTempDirectory("db-test");
db = new DatabaseJava(dir.resolve("data.db"), dir.resolve("refs.db"));
int i = 0;
db.registerClass(UserFullInfo.class, i++);
db.registerClass(User.class, i++);
db.registerClass(ListContainer.class, i++);
}
@Test
public void shouldCreateListAndQuery() throws IOException {
ListContainer root = db.loadRoot((db) -> new ListContainer(db, 2000));
ListQuery query = ListQuery.create(ValuePointer.ofField(User.class, "firstName"), Equals.to(constantFirstName))
.and(ValuePointer.ofField(User.class, "username"), Equals.to(constantUsername))
.and(ValuePointer.ofField(User.class, "fullInfo").field(UserFullInfo.class, "bio"), Equals.to(constantBio));
long time1 = System.currentTimeMillis();
ElementsArrayList<User> elements = root.usersList.query(query);
System.out.println("Time elapsed: " + String.format("%.2f", (System.currentTimeMillis() - time1) / 1000d) + " seconds");
System.out.println("Found " + elements.size() + " elements. First 5 items:");
assertNotEquals(elements.size(), 0);
for (int i = 0; i < (elements.size() > 10 ? 10 : elements.size()); i++) {
System.out.println(elements.get(i));
}
db.close();
}
@After
public void tearDown() throws Exception {
deleteDirectoryRecursion(dir);
}
void deleteDirectoryRecursion(Path path) throws IOException {
if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
try (DirectoryStream<Path> entries = Files.newDirectoryStream(path)) {
for (Path entry : entries) {
deleteDirectoryRecursion(entry);
}
}
}
Files.delete(path);
}
}