From 1c00eb454f4bafc5f0f87b525699dc4efef029af Mon Sep 17 00:00:00 2001 From: Karok365 Date: Fri, 25 Oct 2024 12:41:55 +0530 Subject: [PATCH 1/3] added randomBigDecimal function to return random big decimal numbers --- src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java b/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java index 6b36ba9..ae7eb99 100644 --- a/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java +++ b/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java @@ -2,6 +2,7 @@ import net.datafaker.Faker; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.ZoneId; @@ -12,6 +13,11 @@ public final class FakeTestUtil { static { faker = new Faker(); } + + public static BigDecimal randomBigDecimal(){ + double randomValue = Math.random() * 999 + 1; + return BigDecimal.valueOf(randomValue); + } public static Integer randomInteger() { return faker.random() From 863c81bde0e02693c403ecee2e0db0e7aa2dc0bb Mon Sep 17 00:00:00 2001 From: Karok365 Date: Fri, 25 Oct 2024 12:53:26 +0530 Subject: [PATCH 2/3] implemented the function in ShoppingListServiceTestUtil and ProductServiceTestUtil --- .../depensaapp/app/products/ProductServiceTestUtil.java | 7 ++----- .../app/shoppinglist/ShoppingListServiceTestUtil.java | 6 ++---- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/test/java/dev/nmarulo/depensaapp/app/products/ProductServiceTestUtil.java b/src/test/java/dev/nmarulo/depensaapp/app/products/ProductServiceTestUtil.java index 359f2e8..21c5e77 100644 --- a/src/test/java/dev/nmarulo/depensaapp/app/products/ProductServiceTestUtil.java +++ b/src/test/java/dev/nmarulo/depensaapp/app/products/ProductServiceTestUtil.java @@ -7,9 +7,6 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; - -import java.math.BigDecimal; -import java.time.LocalDateTime; import java.util.Collections; @Getter @@ -69,9 +66,9 @@ private static Product initProduct() { product.setId(FakeTestUtil.randomLong()); product.setName(FakeTestUtil.randomWord()); - product.setPrice(BigDecimal.ZERO); + product.setPrice(FakeTestUtil.randomBigDecimal()); product.setImgUrl(FakeTestUtil.randomImage()); - product.setCalories(BigDecimal.ZERO); + product.setCalories(FakeTestUtil.randomBigDecimal()); product.setDescription(FakeTestUtil.randomSentence()); product.setCreatedAt(FakeTestUtil.randomPast()); product.setUpdatedAt(FakeTestUtil.randomFuture()); diff --git a/src/test/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListServiceTestUtil.java b/src/test/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListServiceTestUtil.java index 42fb2dd..dffdf0e 100644 --- a/src/test/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListServiceTestUtil.java +++ b/src/test/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListServiceTestUtil.java @@ -8,8 +8,6 @@ import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; -import java.math.BigDecimal; -import java.time.LocalDateTime; import java.util.Collections; @Getter @@ -48,8 +46,8 @@ private ShoppingList initShoppingList(User user) { shoppingList.setId(FakeTestUtil.randomInteger()); shoppingList.setName(FakeTestUtil.randomSentence()); shoppingList.setTotalProducts(0); - shoppingList.setTotalCalories(BigDecimal.ZERO); - shoppingList.setTotalPrice(BigDecimal.ZERO); + shoppingList.setTotalCalories(FakeTestUtil.randomBigDecimal()); + shoppingList.setTotalPrice(FakeTestUtil.randomBigDecimal()); shoppingList.setCreatedAt(FakeTestUtil.randomPast()); shoppingList.setUpdatedAt(FakeTestUtil.randomFuture()); shoppingList.setProductHasShoppingList(Collections.emptySet()); From 795148cff630c3bb96c1566fd94aa029b524152a Mon Sep 17 00:00:00 2001 From: Karok365 Date: Sat, 26 Oct 2024 16:34:38 +0530 Subject: [PATCH 3/3] Implemented randomBigDecimal function using datafaker which generates random number between 1 to 1000 upto two decimal places. --- src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java b/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java index ae7eb99..5b9e9cd 100644 --- a/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java +++ b/src/test/java/dev/nmarulo/depensaapp/FakeTestUtil.java @@ -15,7 +15,7 @@ public final class FakeTestUtil { } public static BigDecimal randomBigDecimal(){ - double randomValue = Math.random() * 999 + 1; + double randomValue = faker.number().randomDouble(2, 1, 1000); return BigDecimal.valueOf(randomValue); }