Se comenzaron a realizar unit tests

Junto con ello se modificaron las clases para poder hacer la conexion a
una uri diferente para sqlite.
This commit is contained in:
Daniel Cortés
2019-03-30 00:01:31 -03:00
parent 5a46dde8e5
commit fab3950e07
31 changed files with 1020 additions and 64 deletions

View File

@@ -0,0 +1,159 @@
package danielcortes.xyz.models.caja;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.javafaker.Faker;
import danielcortes.xyz.data.ConnectionHolder;
import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.version.SQLiteVersionDAO;
import danielcortes.xyz.utils.RandomLocalDate;
import java.io.File;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class SQLiteCajaDAOTest {
static private int databaseVersion = 2;
static private SQLiteVersionDAO versionDAO;
static private ConnectionHolder connectionHolder;
static private Faker faker;
private SQLiteCajaDAO cajaDAO;
@BeforeAll
static void setAll() {
connectionHolder = new SQLiteConnectionHolder("jdbc:sqlite:/tmp/tmp.sqlite");
DAOManager.setup(connectionHolder);
versionDAO = new SQLiteVersionDAO(connectionHolder);
faker = new Faker();
}
@BeforeEach
void setUp() {
versionDAO.updateTo(databaseVersion);
this.cajaDAO = new SQLiteCajaDAO(connectionHolder);
}
@AfterEach
void tearDown() {
//noinspection ResultOfMethodCallIgnored
new File("/tmp/tmp.sqlite").delete();
}
@Test
void getAll() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
List<Caja> list = this.cajaDAO.getAll();
assertThat(list)
.hasSize(2)
.containsExactlyInAnyOrder(c1, c2);
}
@Test
void getById() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
Optional<Caja> o1 = this.cajaDAO.getById(c1.getId());
Optional<Caja> o2 = this.cajaDAO.getById(c2.getId());
assertThat(o1)
.isPresent()
.contains(c1);
assertThat(o2)
.isPresent()
.contains(c2);
}
@Test
void getByFecha() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
Optional<Caja> o1 = this.cajaDAO.getByFecha(c1.getFecha());
Optional<Caja> o2 = this.cajaDAO.getByFecha(c2.getFecha());
assertThat(o1)
.isPresent()
.contains(c1);
assertThat(o2)
.isPresent()
.contains(c2);
}
@Test
void insert() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
assertThat(0)
.isEqualTo(c1.getId())
.isEqualTo(c2.getId());
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
assertThat(0)
.isNotEqualTo(c1.getId())
.isNotEqualTo(c2.getId());
List<Caja> l = this.cajaDAO.getAll();
assertThat(l)
.hasSize(2)
.containsExactlyInAnyOrder(c1, c2);
}
@Test
void update() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
Caja u1 = giveRandomCaja(c1.getId());
Caja u2 = giveRandomCaja(c2.getId());
this.cajaDAO.update(c1);
this.cajaDAO.update(c2);
List<Caja> l = this.cajaDAO.getAll();
assertThat(l)
.hasSize(2)
.containsExactlyInAnyOrder(c1, c2);
}
Caja giveRandomCaja() {
Caja caja = new Caja();
caja.setFondo(faker.number().numberBetween(0, 10000000));
caja.setFecha(RandomLocalDate.random());
return caja;
}
Caja giveRandomCaja(int id) {
Caja caja = new Caja();
caja.setId(id);
caja.setFondo(faker.number().numberBetween(0, 10000000));
caja.setFecha(RandomLocalDate.random());
return caja;
}
}

View File

@@ -0,0 +1,258 @@
package danielcortes.xyz.models.calculo_fondo;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.javafaker.Faker;
import danielcortes.xyz.data.ConnectionHolder;
import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.caja.CajaDAO;
import danielcortes.xyz.models.version.SQLiteVersionDAO;
import danielcortes.xyz.models.version.VersionDAO;
import danielcortes.xyz.utils.RandomLocalDate;
import java.io.File;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class SQLiteCalculoFondoDAOTest {
private static Faker faker;
private static final int databaseVersion = 2;
private static VersionDAO versionDAO;
private CalculoFondoDAO calculoFondoDAO;
private CajaDAO cajaDAO;
@BeforeAll
static void setAll() {
ConnectionHolder connectionHolder = new SQLiteConnectionHolder("jdbc:sqlite:/tmp/tmp.sqlite");
DAOManager.setup(connectionHolder);
versionDAO = new SQLiteVersionDAO(connectionHolder);
faker = new Faker();
}
@BeforeEach
void setUp() {
versionDAO.updateTo(databaseVersion);
this.calculoFondoDAO = DAOManager.getCalculoFondoDAO();
this.cajaDAO = DAOManager.getCajaDAO();
}
@AfterEach
void tearDown() {
//noinspection ResultOfMethodCallIgnored
new File("/tmp/tmp.sqlite").delete();
}
@Test
void getAll() {
Caja c = giveRandomCaja();
this.cajaDAO.insert(c);
CalculoFondo f1 = giveRandomCalculoFondo(c);
CalculoFondo f2 = giveRandomCalculoFondo(c);
this.calculoFondoDAO.insert(f1);
this.calculoFondoDAO.insert(f2);
List<CalculoFondo> list = this.calculoFondoDAO.getAll();
assertThat(list)
.hasSize(2)
.contains(f1)
.contains(f2);
}
@Test
void getByCaja() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
CalculoFondo f1 = giveRandomCalculoFondo(c1);
CalculoFondo f2 = giveRandomCalculoFondo(c1);
CalculoFondo f3 = giveRandomCalculoFondo(c2);
CalculoFondo f4 = giveRandomCalculoFondo(c2);
CalculoFondo f5 = giveRandomCalculoFondo(c2);
this.calculoFondoDAO.insert(f1);
this.calculoFondoDAO.insert(f2);
this.calculoFondoDAO.insert(f3);
this.calculoFondoDAO.insert(f4);
this.calculoFondoDAO.insert(f5);
List<CalculoFondo> l1 = this.calculoFondoDAO.getByCaja(c1);
List<CalculoFondo> l2 = this.calculoFondoDAO.getByCaja(c2);
List<CalculoFondo> l3 = this.calculoFondoDAO.getByCaja(Caja.EMPTY);
assertThat(l1)
.hasSize(2)
.containsExactlyInAnyOrder(f1, f2)
.doesNotContain(f3, f4, f5);
assertThat(l2)
.hasSize(3)
.containsExactlyInAnyOrder(f3, f4, f5)
.doesNotContain(f1, f2);
assertThat(l3)
.hasSize(0);
}
@Test
void getById() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
CalculoFondo f1 = giveRandomCalculoFondo(c1);
CalculoFondo f2 = giveRandomCalculoFondo(c1);
this.calculoFondoDAO.insert(f1);
this.calculoFondoDAO.insert(f2);
Optional<CalculoFondo> o1 = this.calculoFondoDAO.getById(f1.getId());
Optional<CalculoFondo> o2 = this.calculoFondoDAO.getById(f2.getId());
Optional<CalculoFondo> o3 = this.calculoFondoDAO.getById(-1);
assertThat(o1)
.isPresent()
.contains(f1);
assertThat(o2)
.isPresent()
.contains(f2);
assertThat(o3)
.isEmpty();
}
@Test
void getTotalCalculoFondo() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
CalculoFondo f1 = giveRandomCalculoFondo(c1);
CalculoFondo f2 = giveRandomCalculoFondo(c1);
CalculoFondo f3 = giveRandomCalculoFondo(c2);
CalculoFondo f4 = giveRandomCalculoFondo(c2);
CalculoFondo f5 = giveRandomCalculoFondo(c2);
this.calculoFondoDAO.insert(f1);
this.calculoFondoDAO.insert(f2);
this.calculoFondoDAO.insert(f3);
this.calculoFondoDAO.insert(f4);
this.calculoFondoDAO.insert(f5);
int t1 = this.calculoFondoDAO.getTotalCalculoFondo(c1);
int t2 = this.calculoFondoDAO.getTotalCalculoFondo(c2);
assertThat(t1)
.isEqualTo(f1.getValor() + f2.getValor());
assertThat(t2)
.isEqualTo(f3.getValor() + f4.getValor() + f5.getValor());
}
@Test
void insert() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
CalculoFondo f1 = giveRandomCalculoFondo(c1);
CalculoFondo f2 = giveRandomCalculoFondo(c1);
assertThat(0)
.isEqualTo(f1.getId())
.isEqualTo(f2.getId());
this.calculoFondoDAO.insert(f1);
this.calculoFondoDAO.insert(f2);
assertThat(0)
.isNotEqualTo(f1.getId())
.isNotEqualTo(f2.getId());
List<CalculoFondo> l = this.calculoFondoDAO.getAll();
assertThat(l)
.hasSize(2)
.containsExactlyInAnyOrder(f1, f2);
}
@Test
void update() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
CalculoFondo f1 = giveRandomCalculoFondo(c1);
CalculoFondo f2 = giveRandomCalculoFondo(c1);
this.calculoFondoDAO.insert(f1);
this.calculoFondoDAO.insert(f2);
f1 = giveRandomCalculoFondo(c2, f1.getId());
f2 = giveRandomCalculoFondo(c2, f2.getId());
this.calculoFondoDAO.update(f1);
this.calculoFondoDAO.update(f2);
List<CalculoFondo> l = this.calculoFondoDAO.getAll();
assertThat(l)
.hasSize(2)
.containsExactlyInAnyOrder(f1, f2);
}
@Test
void delete() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
CalculoFondo f1 = giveRandomCalculoFondo(c1);
CalculoFondo f2 = giveRandomCalculoFondo(c1);
this.calculoFondoDAO.insert(f1);
this.calculoFondoDAO.insert(f2);
List<CalculoFondo> l1 = this.calculoFondoDAO.getAll();
assertThat(l1)
.hasSize(2);
this.calculoFondoDAO.delete(f1);
this.calculoFondoDAO.delete(f2);
List<CalculoFondo> l2 = this.calculoFondoDAO.getAll();
assertThat(l2)
.hasSize(0);
}
Caja giveRandomCaja() {
Caja caja = new Caja();
caja.setFondo(faker.number().numberBetween(0, 10000000));
caja.setFecha(RandomLocalDate.random());
return caja;
}
CalculoFondo giveRandomCalculoFondo(Caja caja) {
CalculoFondo calculoFondo = new CalculoFondo();
calculoFondo.setCaja(caja);
calculoFondo.setDescripcion(faker.lorem().characters());
calculoFondo.setValor(faker.number().numberBetween(0, 10000000));
return calculoFondo;
}
CalculoFondo giveRandomCalculoFondo(Caja caja, int id) {
CalculoFondo calculoFondo = giveRandomCalculoFondo(caja);
calculoFondo.setId(id);
return calculoFondo;
}
}

View File

@@ -0,0 +1,243 @@
package danielcortes.xyz.models.documentos;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.javafaker.Faker;
import danielcortes.xyz.data.ConnectionHolder;
import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.caja.CajaDAO;
import danielcortes.xyz.models.version.SQLiteVersionDAO;
import danielcortes.xyz.models.version.VersionDAO;
import danielcortes.xyz.utils.RandomLocalDate;
import java.io.File;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class SQLiteDocumentosDAOTest {
private static final int databaseVersion = 2;
private static Faker faker;
private static VersionDAO versionDAO;
private DocumentosDAO documentosDAO;
private CajaDAO cajaDAO;
@BeforeAll
static void setAll() {
ConnectionHolder connectionHolder = new SQLiteConnectionHolder("jdbc:sqlite:/tmp/tmp.sqlite");
DAOManager.setup(connectionHolder);
versionDAO = new SQLiteVersionDAO(connectionHolder);
faker = new Faker();
}
@BeforeEach
void setUp() {
versionDAO.updateTo(databaseVersion);
this.documentosDAO = DAOManager.getDocumentosDAO();
this.cajaDAO = DAOManager.getCajaDAO();
}
@AfterEach
void tearDown() {
//noinspection ResultOfMethodCallIgnored
new File("/tmp/tmp.sqlite").delete();
}
@Test
void getAll() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
Documentos d1 = giveRandomDocumentos(c1);
Documentos d2 = giveRandomDocumentos(c1);
this.documentosDAO.insert(d1);
this.documentosDAO.insert(d2);
List<Documentos> l = this.documentosDAO.getAll();
assertThat(l)
.hasSize(2)
.containsExactlyInAnyOrder(d1, d2);
}
@Test
void getById() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
Documentos d1 = giveRandomDocumentos(c1);
Documentos d2 = giveRandomDocumentos(c1);
this.documentosDAO.insert(d1);
this.documentosDAO.insert(d2);
Optional<Documentos> o1 = this.documentosDAO.getById(d1.getId());
Optional<Documentos> o2 = this.documentosDAO.getById(d2.getId());
Optional<Documentos> o3 = this.documentosDAO.getById(-1);
assertThat(o1)
.isPresent()
.contains(d1);
assertThat(o2)
.isPresent()
.contains(d2);
assertThat(o3)
.isEmpty();
}
@Test
void getByCaja() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
Documentos d1 = giveRandomDocumentos(c1);
Documentos d2 = giveRandomDocumentos(c2);
this.documentosDAO.insert(d1);
this.documentosDAO.insert(d2);
Optional<Documentos> o1 = this.documentosDAO.getByCaja(c1);
Optional<Documentos> o2 = this.documentosDAO.getByCaja(c2);
Optional<Documentos> o3 = this.documentosDAO.getByCaja(Caja.EMPTY);
assertThat(o1)
.isPresent()
.contains(d1);
assertThat(o2)
.isPresent()
.contains(d2);
assertThat(o3)
.isEmpty();
}
@Test
void getTotalDocumentos() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
Documentos d1 = giveRandomDocumentos(c1);
Documentos d2 = giveRandomDocumentos(c2);
this.documentosDAO.insert(d1);
this.documentosDAO.insert(d2);
int t1 = this.documentosDAO.getTotalDocumentos(c1);
int t2 = this.documentosDAO.getTotalDocumentos(c2);
assertThat(t1)
.isEqualTo(d1.getCheques() + d1.getRetiros() + d1.getTarjetas());
assertThat(t2)
.isEqualTo(d2.getCheques() + d2.getRetiros() + d2.getTarjetas());
}
@Test
void insert() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
Documentos d1 = giveRandomDocumentos(c1);
assertThat(d1.getId())
.isEqualTo(0);
this.documentosDAO.insert(d1);
assertThat(d1.getId())
.isNotEqualTo(0);
List<Documentos> l = this.documentosDAO.getAll();
assertThat(l)
.hasSize(1)
.containsExactlyInAnyOrder(d1);
}
@Test
void update() {
Caja c1 = giveRandomCaja();
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c1);
this.cajaDAO.insert(c2);
Documentos d1 = giveRandomDocumentos(c1);
this.documentosDAO.insert(d1);
Documentos d2 = updateRandomDocumentos(d1);
this.documentosDAO.update(d2);
assertThat(d1.getId()).isEqualTo(d2.getId());
Optional<Documentos> o = this.documentosDAO.getById(d2.getId());
assertThat(o)
.isPresent()
.contains(d2);
}
@Test
void delete() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
Documentos d1 =giveRandomDocumentos(c1);
this.documentosDAO.insert(d1);
assertThat(this.documentosDAO.getById(d1.getId()))
.isPresent()
.contains(d1);
this.documentosDAO.delete(d1);
assertThat(this.documentosDAO.getById(d1.getId()))
.isEmpty();
assertThat(this.cajaDAO.getById(c1.getId()))
.isPresent()
.contains(c1);
}
Caja giveRandomCaja() {
Caja caja = new Caja();
caja.setFondo(faker.number().numberBetween(0, 10000000));
caja.setFecha(RandomLocalDate.random());
return caja;
}
Documentos giveRandomDocumentos(Caja caja) {
Documentos d = new Documentos();
d.setCaja(caja);
d.setRetiros(faker.number().numberBetween(0, 10000000));
d.setCheques(faker.number().numberBetween(0, 10000000));
d.setTarjetas(faker.number().numberBetween(0, 10000000));
return d;
}
Documentos updateRandomDocumentos(Documentos documentos) {
Documentos d = giveRandomDocumentos(documentos.getCaja());
d.setId(documentos.getId());
return d;
}
}