diff --git a/dist/Programa Caja.jar b/dist/Programa Caja.jar index beb6eb0..dd1b0ca 100644 Binary files a/dist/Programa Caja.jar and b/dist/Programa Caja.jar differ diff --git a/src/danielcortes/xyz/controllers/CajasController.java b/src/danielcortes/xyz/controllers/CajasController.java index 34434d6..5492952 100644 --- a/src/danielcortes/xyz/controllers/CajasController.java +++ b/src/danielcortes/xyz/controllers/CajasController.java @@ -110,7 +110,16 @@ public class CajasController { Efectivo efectivo = new Efectivo(); 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.setTarjetas(0); diff --git a/src/danielcortes/xyz/models/caja/SQLiteCajaDAO.java b/src/danielcortes/xyz/models/caja/SQLiteCajaDAO.java index 6f4a25d..7c63658 100644 --- a/src/danielcortes/xyz/models/caja/SQLiteCajaDAO.java +++ b/src/danielcortes/xyz/models/caja/SQLiteCajaDAO.java @@ -124,6 +124,12 @@ public class SQLiteCajaDAO implements CajaDAO { @Override public void insert(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 (?, ?)"; try (Connection conn = connectionHolder.getConnection()) { try (PreparedStatement ps = conn.prepareStatement(query)) { diff --git a/src/danielcortes/xyz/models/calculo_fondo/CalculoFondo.java b/src/danielcortes/xyz/models/calculo_fondo/CalculoFondo.java index fb15b9b..bbbf669 100644 --- a/src/danielcortes/xyz/models/calculo_fondo/CalculoFondo.java +++ b/src/danielcortes/xyz/models/calculo_fondo/CalculoFondo.java @@ -28,7 +28,6 @@ import danielcortes.xyz.models.caja.Caja; import java.util.Objects; public class CalculoFondo { - private int id; private int valor; private String descripcion; diff --git a/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java b/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java index c29f9aa..b6f5227 100644 --- a/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java +++ b/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java @@ -79,7 +79,6 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO { return calculoFondoList; } - @Override public List getByCaja(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) { 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 (?, ?, ?)"; try (Connection conn = connectionHolder.getConnection()) { try (PreparedStatement ps = conn.prepareStatement(query)) { @@ -197,6 +201,12 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO { @Override public void update(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 = ?"; try (Connection conn = connectionHolder.getConnection()) { try (PreparedStatement ps = conn.prepareStatement(query)) { diff --git a/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java b/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java index b2f1d81..0df74dd 100644 --- a/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java +++ b/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java @@ -32,12 +32,14 @@ public interface EfectivoDAO { /** * Obtiene todos los Efectivo + * * @return Una lista con los Efectivo */ List getAll(); /** * Obtiene un Efectivo por su id + * * @param id Id del Efectivo * @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 + * * @param caja Caja a la que pertence el Efectivo * @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 + * * @param caja Caja a la que pertenecen los Efectivo a sumar * @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 + * * @param efectivo Efectivo a guardar */ 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 + * * @param efectivo Efectivo a guardar */ void update(Efectivo efectivo); /** * Elimina un Efectivo + * * @param efectivo Efectivo a eliminar */ void delete(Efectivo efectivo); diff --git a/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java b/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java index 29a9aba..c148abf 100644 --- a/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java +++ b/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java @@ -40,6 +40,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class SQLiteEfectivoDAO implements EfectivoDAO { + private static final Logger log = LogManager.getLogger(SQLiteEfectivoDAO.class); private ConnectionHolder connectionHolder; @@ -158,7 +159,7 @@ public class SQLiteEfectivoDAO implements EfectivoDAO { } } } 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); @@ -223,27 +224,6 @@ public class SQLiteEfectivoDAO implements EfectivoDAO { 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 public void update(Efectivo efectivo) { log.debug("Se intentara actualizar el efectivo " + efectivo); diff --git a/test/danielcortes/xyz/models/caja/SQLiteCajaDAOTest.java b/test/danielcortes/xyz/models/caja/SQLiteCajaDAOTest.java index 6fd4f64..b286b22 100644 --- a/test/danielcortes/xyz/models/caja/SQLiteCajaDAOTest.java +++ b/test/danielcortes/xyz/models/caja/SQLiteCajaDAOTest.java @@ -142,6 +142,14 @@ class SQLiteCajaDAOTest { .containsExactlyInAnyOrder(c1, c2); } + @Test + void shouldNotInsertCajaEMPTY(){ + this.cajaDAO.insert(Caja.EMPTY); + List l = this.cajaDAO.getAll(); + + assertThat(l).hasSize(0); + } + Caja giveRandomCaja() { Caja caja = new Caja(); caja.setFondo(faker.number().numberBetween(0, 10000000)); diff --git a/test/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAOTest.java b/test/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAOTest.java index 1d93101..63eb426 100644 --- a/test/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAOTest.java +++ b/test/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAOTest.java @@ -235,6 +235,36 @@ class SQLiteCalculoFondoDAOTest { .hasSize(0); } + @Test + void shouldNotInsertWithCajaEMPTY(){ + Caja c = Caja.EMPTY; + + CalculoFondo f = giveRandomCalculoFondo(c); + this.calculoFondoDAO.insert(f); + + List 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 o = this.calculoFondoDAO.getById(f1.getId()); + + assertThat(o) + .isPresent() + .contains(f1); + } + Caja giveRandomCaja() { Caja caja = new Caja(); caja.setFondo(faker.number().numberBetween(0, 10000000)); diff --git a/test/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAOTest.java b/test/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAOTest.java new file mode 100644 index 0000000..99a3c11 --- /dev/null +++ b/test/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAOTest.java @@ -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 cajas = new ArrayList<>(size); + for(int x = 0; x < size; x++){ + Caja c = giveRandomCaja(); + cajas.add(c); + this.cajaDAO.insert(c); + } + + List efectivos = new ArrayList<>(size); + for(Caja c: cajas) { + Efectivo e = giveRandomEfectivo(c); + efectivos.add(e); + this.efectivoDAO.insert(e); + } + + List l1 = this.efectivoDAO.getAll(); + + assertThat(l1) + .hasSize(size) + .containsAll(efectivos); + } + + @ParameterizedTest + @ValueSource(ints = {1,5,10}) + void getById(int size) { + List cajas = new ArrayList<>(size); + for(int x = 0; x < size; x++){ + Caja c = giveRandomCaja(); + cajas.add(c); + this.cajaDAO.insert(c); + } + + List efectivos = new ArrayList<>(size); + for(Caja c: cajas) { + Efectivo e = giveRandomEfectivo(c); + efectivos.add(e); + this.efectivoDAO.insert(e); + } + + for(Efectivo e: efectivos){ + Optional o = this.efectivoDAO.getById(e.getId()); + assertThat(o) + .isPresent() + .contains(e); + } + + Optional o = this.efectivoDAO.getById(-1); + assertThat(o) + .isEmpty(); + } + + @ParameterizedTest + @ValueSource(ints = {1,5,10}) + void getByCaja(int size) { + List cajas = new ArrayList<>(size); + for(int x = 0; x < size; x++){ + Caja c = giveRandomCaja(); + cajas.add(c); + this.cajaDAO.insert(c); + } + + List efectivos = new ArrayList<>(size); + for(Caja c: cajas) { + Efectivo e = giveRandomEfectivo(c); + efectivos.add(e); + this.efectivoDAO.insert(e); + } + + for(Efectivo e: efectivos) { + Optional o = this.efectivoDAO.getByCaja(e.getCaja()); + assertThat(o) + .isPresent() + .contains(e); + } + + Optional o = this.efectivoDAO.getByCaja(Caja.EMPTY); + assertThat(o) + .isEmpty(); + } + + @ParameterizedTest + @ValueSource(ints = {1,5,10}) + void getTotalEfectivo(int size) { + List 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 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 o1 = this.efectivoDAO.getById(f1.getId()); + + Efectivo f2 = updateRandomEfectivo(f1); + + assertThat(o1) + .isPresent() + .contains(f1); + + assertThat(f1).isNotEqualTo(f2); + + this.efectivoDAO.update(f2); + + Optional 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 o1 = this.efectivoDAO.getById(e.getId()); + assertThat(o1) + .isPresent() + .contains(e); + + this.efectivoDAO.delete(e); + + Optional 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; + } +} \ No newline at end of file