avance en los test

This commit is contained in:
Daniel Cortés
2019-04-08 11:41:42 -04:00
parent c8a51cff7f
commit e6b0bfaa27
10 changed files with 337 additions and 31 deletions

BIN
dist/Programa Caja.jar vendored

Binary file not shown.

View File

@@ -110,7 +110,16 @@ public class CajasController {
Efectivo efectivo = new Efectivo(); Efectivo efectivo = new Efectivo();
efectivo.setCaja(c); efectivo.setCaja(c);
DAOManager.getEfectivoDAO().insertDefault(efectivo); efectivo.setDiez(0);
efectivo.setCincuenta(0);
efectivo.setCien(0);
efectivo.setQuinientos(0);
efectivo.setMil(0);
efectivo.setDosMil(0);
efectivo.setCincoMil(0);
efectivo.setDiezMil(0);
efectivo.setVeinteMil(0);
DAOManager.getEfectivoDAO().insert(efectivo);
Documentos documentos = new Documentos(); Documentos documentos = new Documentos();
documentos.setTarjetas(0); documentos.setTarjetas(0);

View File

@@ -124,6 +124,12 @@ public class SQLiteCajaDAO implements CajaDAO {
@Override @Override
public void insert(Caja caja) { public void insert(Caja caja) {
log.debug("Se intentara insertar la caja " + caja); log.debug("Se intentara insertar la caja " + caja);
if(caja == Caja.EMPTY){
log.debug("La caja era Caja.EMPTY, no se puede insertar");
return;
}
String query = "insert into caja (fecha, fondo) values (?, ?)"; String query = "insert into caja (fecha, fondo) values (?, ?)";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) { try (PreparedStatement ps = conn.prepareStatement(query)) {

View File

@@ -28,7 +28,6 @@ import danielcortes.xyz.models.caja.Caja;
import java.util.Objects; import java.util.Objects;
public class CalculoFondo { public class CalculoFondo {
private int id; private int id;
private int valor; private int valor;
private String descripcion; private String descripcion;

View File

@@ -79,7 +79,6 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
return calculoFondoList; return calculoFondoList;
} }
@Override @Override
public List<CalculoFondo> getByCaja(Caja caja) { public List<CalculoFondo> getByCaja(Caja caja) {
log.debug("Se intentara conseguir todos los calculos fondo de la caja " + caja); log.debug("Se intentara conseguir todos los calculos fondo de la caja " + caja);
@@ -172,6 +171,11 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
public void insert(CalculoFondo calculoFondo) { public void insert(CalculoFondo calculoFondo) {
log.debug("Se intentara insertar el calculoFondo " + calculoFondo); log.debug("Se intentara insertar el calculoFondo " + calculoFondo);
if(calculoFondo.getCaja() == Caja.EMPTY){
log.error("El calculo fondo tiene una caja EMPTY, no se insertara");
return;
}
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)"; String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) { try (PreparedStatement ps = conn.prepareStatement(query)) {
@@ -197,6 +201,12 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
@Override @Override
public void update(CalculoFondo calculoFondo) { public void update(CalculoFondo calculoFondo) {
log.debug("Se intentara actualizar el CalculoFondo " + calculoFondo); log.debug("Se intentara actualizar el CalculoFondo " + calculoFondo);
if (calculoFondo.getCaja() == Caja.EMPTY) {
log.error("El calculo fondo tiene una caja EMPTY, no se actualizara");
return;
}
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?"; String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) { try (PreparedStatement ps = conn.prepareStatement(query)) {

View File

@@ -32,12 +32,14 @@ public interface EfectivoDAO {
/** /**
* Obtiene todos los Efectivo * Obtiene todos los Efectivo
*
* @return Una lista con los Efectivo * @return Una lista con los Efectivo
*/ */
List<Efectivo> getAll(); List<Efectivo> getAll();
/** /**
* Obtiene un Efectivo por su id * Obtiene un Efectivo por su id
*
* @param id Id del Efectivo * @param id Id del Efectivo
* @return Un Optional que puede contener un Efectivo o puede estar vacio * @return Un Optional que puede contener un Efectivo o puede estar vacio
*/ */
@@ -45,6 +47,7 @@ public interface EfectivoDAO {
/** /**
* Obtiene un Efectivo por su Caja * Obtiene un Efectivo por su Caja
*
* @param caja Caja a la que pertence el Efectivo * @param caja Caja a la que pertence el Efectivo
* @return Un Optional que puede contener un Efectivo o puede estar vacio * @return Un Optional que puede contener un Efectivo o puede estar vacio
*/ */
@@ -52,6 +55,7 @@ public interface EfectivoDAO {
/** /**
* Obtiene la suma de los valores de los Efectivo pertencientes a una Caja * Obtiene la suma de los valores de los Efectivo pertencientes a una Caja
*
* @param caja Caja a la que pertenecen los Efectivo a sumar * @param caja Caja a la que pertenecen los Efectivo a sumar
* @return La suma de los valores de Efectivo * @return La suma de los valores de Efectivo
*/ */
@@ -59,24 +63,21 @@ public interface EfectivoDAO {
/** /**
* Guarda un Efectivo, le agrega un id a la instancia * Guarda un Efectivo, le agrega un id a la instancia
*
* @param efectivo Efectivo a guardar * @param efectivo Efectivo a guardar
*/ */
void insert(Efectivo efectivo); void insert(Efectivo efectivo);
/**
* Guarda un Efectivo con valores por defecto, le agrega un id a la instancia
* @param efectivo Instancia de efectivo a guardar
*/
void insertDefault(Efectivo efectivo);
/** /**
* Actualiza un Efectivo * Actualiza un Efectivo
*
* @param efectivo Efectivo a guardar * @param efectivo Efectivo a guardar
*/ */
void update(Efectivo efectivo); void update(Efectivo efectivo);
/** /**
* Elimina un Efectivo * Elimina un Efectivo
*
* @param efectivo Efectivo a eliminar * @param efectivo Efectivo a eliminar
*/ */
void delete(Efectivo efectivo); void delete(Efectivo efectivo);

View File

@@ -40,6 +40,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
public class SQLiteEfectivoDAO implements EfectivoDAO { public class SQLiteEfectivoDAO implements EfectivoDAO {
private static final Logger log = LogManager.getLogger(SQLiteEfectivoDAO.class); private static final Logger log = LogManager.getLogger(SQLiteEfectivoDAO.class);
private ConnectionHolder connectionHolder; private ConnectionHolder connectionHolder;
@@ -158,7 +159,7 @@ public class SQLiteEfectivoDAO implements EfectivoDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error al intentar conseguir un Efectivo perteneciente a la caja " + caja ); log.error("Error al intentar conseguir un Efectivo perteneciente a la caja " + caja);
} }
log.debug("Se obtuvo el efectivo " + efectivo); log.debug("Se obtuvo el efectivo " + efectivo);
@@ -223,27 +224,6 @@ public class SQLiteEfectivoDAO implements EfectivoDAO {
log.debug("Se inserto el efectivo " + efectivo); log.debug("Se inserto el efectivo " + efectivo);
} }
@Override
public void insertDefault(Efectivo efectivo) {
log.debug("Se intentara insertar el efectivo por default " + efectivo);
String query = "insert into efectivos (veinte_mil, diez_mil, cinco_mil, dos_mil, mil, quinientos, cien, cincuenta, diez, caja_id) values (0,0,0,0,0,0,0,0,0,?)";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, efectivo.getCaja().getId());
ps.executeUpdate();
}
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
try (ResultSet rs = ps.executeQuery()) {
rs.next();
efectivo.setId(rs.getInt(1));
}
}
} catch (SQLException e) {
log.error("Error al insertar el efectivo por default " + efectivo, e);
}
log.debug("Se inserto el efectivo por default " + efectivo);
}
@Override @Override
public void update(Efectivo efectivo) { public void update(Efectivo efectivo) {
log.debug("Se intentara actualizar el efectivo " + efectivo); log.debug("Se intentara actualizar el efectivo " + efectivo);

View File

@@ -142,6 +142,14 @@ class SQLiteCajaDAOTest {
.containsExactlyInAnyOrder(c1, c2); .containsExactlyInAnyOrder(c1, c2);
} }
@Test
void shouldNotInsertCajaEMPTY(){
this.cajaDAO.insert(Caja.EMPTY);
List<Caja> l = this.cajaDAO.getAll();
assertThat(l).hasSize(0);
}
Caja giveRandomCaja() { Caja giveRandomCaja() {
Caja caja = new Caja(); Caja caja = new Caja();
caja.setFondo(faker.number().numberBetween(0, 10000000)); caja.setFondo(faker.number().numberBetween(0, 10000000));

View File

@@ -235,6 +235,36 @@ class SQLiteCalculoFondoDAOTest {
.hasSize(0); .hasSize(0);
} }
@Test
void shouldNotInsertWithCajaEMPTY(){
Caja c = Caja.EMPTY;
CalculoFondo f = giveRandomCalculoFondo(c);
this.calculoFondoDAO.insert(f);
List<CalculoFondo> l = this.calculoFondoDAO.getAll();
assertThat(l).hasSize(0);
}
@Test
void shouldNotUpdateWithCajaEMPTY(){
Caja c = giveRandomCaja();
this.cajaDAO.insert(c);
CalculoFondo f1 = giveRandomCalculoFondo(c);
this.calculoFondoDAO.insert(f1);
CalculoFondo f2 = giveRandomCalculoFondo(Caja.EMPTY, f1.getId());
this.calculoFondoDAO.update(f2);
Optional<CalculoFondo> o = this.calculoFondoDAO.getById(f1.getId());
assertThat(o)
.isPresent()
.contains(f1);
}
Caja giveRandomCaja() { Caja giveRandomCaja() {
Caja caja = new Caja(); Caja caja = new Caja();
caja.setFondo(faker.number().numberBetween(0, 10000000)); caja.setFondo(faker.number().numberBetween(0, 10000000));

View File

@@ -0,0 +1,263 @@
package danielcortes.xyz.models.efectivo;
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.ArrayList;
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;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class SQLiteEfectivoDAOTest {
private static final int databaseVersion = 2;
private static VersionDAO versionDAO;
private static Faker faker;
private CajaDAO cajaDAO;
private EfectivoDAO efectivoDAO;
@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.cajaDAO = DAOManager.getCajaDAO();
this.efectivoDAO = DAOManager.getEfectivoDAO();
}
@AfterEach
void tearDown() {
//noinspection ResultOfMethodCallIgnored
new File("/tmp/tmp.sqlite").delete();
}
@ParameterizedTest
@ValueSource(ints = {1,5,10})
void getAll(int size) {
List<Caja> cajas = new ArrayList<>(size);
for(int x = 0; x < size; x++){
Caja c = giveRandomCaja();
cajas.add(c);
this.cajaDAO.insert(c);
}
List<Efectivo> efectivos = new ArrayList<>(size);
for(Caja c: cajas) {
Efectivo e = giveRandomEfectivo(c);
efectivos.add(e);
this.efectivoDAO.insert(e);
}
List<Efectivo> l1 = this.efectivoDAO.getAll();
assertThat(l1)
.hasSize(size)
.containsAll(efectivos);
}
@ParameterizedTest
@ValueSource(ints = {1,5,10})
void getById(int size) {
List<Caja> cajas = new ArrayList<>(size);
for(int x = 0; x < size; x++){
Caja c = giveRandomCaja();
cajas.add(c);
this.cajaDAO.insert(c);
}
List<Efectivo> efectivos = new ArrayList<>(size);
for(Caja c: cajas) {
Efectivo e = giveRandomEfectivo(c);
efectivos.add(e);
this.efectivoDAO.insert(e);
}
for(Efectivo e: efectivos){
Optional<Efectivo> o = this.efectivoDAO.getById(e.getId());
assertThat(o)
.isPresent()
.contains(e);
}
Optional<Efectivo> o = this.efectivoDAO.getById(-1);
assertThat(o)
.isEmpty();
}
@ParameterizedTest
@ValueSource(ints = {1,5,10})
void getByCaja(int size) {
List<Caja> cajas = new ArrayList<>(size);
for(int x = 0; x < size; x++){
Caja c = giveRandomCaja();
cajas.add(c);
this.cajaDAO.insert(c);
}
List<Efectivo> efectivos = new ArrayList<>(size);
for(Caja c: cajas) {
Efectivo e = giveRandomEfectivo(c);
efectivos.add(e);
this.efectivoDAO.insert(e);
}
for(Efectivo e: efectivos) {
Optional<Efectivo> o = this.efectivoDAO.getByCaja(e.getCaja());
assertThat(o)
.isPresent()
.contains(e);
}
Optional<Efectivo> o = this.efectivoDAO.getByCaja(Caja.EMPTY);
assertThat(o)
.isEmpty();
}
@ParameterizedTest
@ValueSource(ints = {1,5,10})
void getTotalEfectivo(int size) {
List<Caja> cajas = new ArrayList<>(size);
for(int x = 0; x < size; x++){
Caja c = giveRandomCaja();
cajas.add(c);
this.cajaDAO.insert(c);
}
for(Caja c: cajas) {
Efectivo e = giveRandomEfectivo(c);
this.efectivoDAO.insert(e);
}
for(Caja c: cajas) {
Optional<Efectivo> o = this.efectivoDAO.getByCaja(c);
int sum = this.efectivoDAO.getTotalEfectivo(c);
assertThat(sum)
.isEqualTo(
o.get().getDiez() +
o.get().getCincuenta() +
o.get().getCien() +
o.get().getQuinientos() +
o.get().getMil() +
o.get().getDosMil() +
o.get().getCincoMil() +
o.get().getDiezMil() +
o.get().getVeinteMil()
);
}
assertThat(this.efectivoDAO.getTotalEfectivo(Caja.EMPTY))
.isEqualTo(0);
}
@ParameterizedTest
@ValueSource(ints = {1, 5, 10})
void insert(int size) {
Caja c = giveRandomCaja();
this.cajaDAO.insert(c);
Efectivo e = giveRandomEfectivo(c);
assertThat(e.getId())
.isEqualTo(0);
this.efectivoDAO.insert(e);
assertThat(e.getId())
.isNotEqualTo(0);
}
@Test
void update() {
Caja c1 = giveRandomCaja();
this.cajaDAO.insert(c1);
Caja c2 = giveRandomCaja();
this.cajaDAO.insert(c2);
Efectivo f1 = giveRandomEfectivo(c1);
this.efectivoDAO.insert(f1);
Optional<Efectivo> o1 = this.efectivoDAO.getById(f1.getId());
Efectivo f2 = updateRandomEfectivo(f1);
assertThat(o1)
.isPresent()
.contains(f1);
assertThat(f1).isNotEqualTo(f2);
this.efectivoDAO.update(f2);
Optional<Efectivo> o2 = this.efectivoDAO.getById(f2.getId());
assertThat(o2)
.isPresent()
.contains(f2);
}
@Test
void delete() {
Caja c = giveRandomCaja();
this.cajaDAO.insert(c);
Efectivo e = giveRandomEfectivo(c);
this.efectivoDAO.insert(e);
Optional<Efectivo> o1 = this.efectivoDAO.getById(e.getId());
assertThat(o1)
.isPresent()
.contains(e);
this.efectivoDAO.delete(e);
Optional<Efectivo> o2 = this.efectivoDAO.getById(e.getId());
assertThat(o2)
.isEmpty();
}
Caja giveRandomCaja() {
Caja c= new Caja();
c.setFondo(faker.number().numberBetween(0, 10000000));
c.setFecha(RandomLocalDate.random());
return c;
}
Efectivo giveRandomEfectivo(Caja caja){
Efectivo e = new Efectivo();
e.setDiez(faker.number().numberBetween(0, 10000000));
e.setCincuenta(faker.number().numberBetween(0, 10000000));
e.setCien(faker.number().numberBetween(0, 10000000));
e.setQuinientos(faker.number().numberBetween(0, 10000000));
e.setMil(faker.number().numberBetween(0, 10000000));
e.setDosMil(faker.number().numberBetween(0, 10000000));
e.setCincoMil(faker.number().numberBetween(0, 10000000));
e.setDiezMil(faker.number().numberBetween(0, 10000000));
e.setVeinteMil(faker.number().numberBetween(0, 10000000));
e.setCaja(caja);
return e;
}
Efectivo updateRandomEfectivo(Efectivo efectivo){
Efectivo e = giveRandomEfectivo(efectivo.getCaja());
e.setId(efectivo.getId());
return e;
}
}