Skip to content

Commit

Permalink
add test for profit value/pct calculation; pct adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriusan committed Dec 3, 2023
1 parent 4c8845c commit 5513481
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ private static BigDecimal calculatePricePerPieceWithShipping(BigDecimal priceWit
BigDecimal shippingCost) {
return priceWithoutShipping
.add(shippingCost)
.divide(BigDecimal.valueOf(pieces), RoundingMode.HALF_UP);
.divide(BigDecimal.valueOf(pieces), 2, RoundingMode.HALF_UP);
}

private static BigDecimal calculateExpectedProfitUsd(BigDecimal pricePerPieceWithShipping,
protected static BigDecimal calculateExpectedProfitUsd(BigDecimal pricePerPieceWithShipping,
BigDecimal referencePricePerPiece, Integer pieces,
BigDecimal meestShippingAndHandlingOverhead) {
return referencePricePerPiece
Expand All @@ -126,12 +126,14 @@ private static BigDecimal calculateExpectedProfitUsd(BigDecimal pricePerPieceWit
.subtract(meestShippingAndHandlingOverhead);
}

private static BigDecimal calculateExpectedProfitPct(BigDecimal expectedProfitUsd, BigDecimal listingPriceUsd,
protected static BigDecimal calculateExpectedProfitPct(BigDecimal expectedProfitUsd, BigDecimal listingPriceUsd,
BigDecimal shippingPriceUsd,
BigDecimal meestShippingAndHandlingOverhead) {
BigDecimal totalExpencesForListing = listingPriceUsd
.add(shippingPriceUsd)
.add(meestShippingAndHandlingOverhead);
return expectedProfitUsd.divide(totalExpencesForListing, RoundingMode.HALF_DOWN);
return expectedProfitUsd
.multiply(BigDecimal.valueOf(100))
.divide(totalExpencesForListing, 2, RoundingMode.HALF_DOWN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.irw.hawk.scraper.service.extractors;

import static io.irw.hawk.scraper.service.extractors.PriceExtractor.calculateExpectedProfitPct;
import static io.irw.hawk.scraper.service.extractors.PriceExtractor.calculateExpectedProfitUsd;
import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigDecimal;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;

class PriceExtractorTest {

public static final int LISTING_PRICE_PER_PIECE_USD = 5;
public static final int SHIPPING_PRICE_PER_PIECE_USD = 1;
public static final int MEEST_SH_OVERHEAD = 10;
public static final int NUMBER_OF_PIECES = 8;
public static final int USUAL_PRICE_PER_PIECE_USD = 9;
public static final double DELTA = 0.01;

@Test
void shouldCalculateExpectedProfitUsd() {
BigDecimal calculatedProfitUsd = calculateExpectedProfitUsd(
BigDecimal.valueOf(LISTING_PRICE_PER_PIECE_USD + SHIPPING_PRICE_PER_PIECE_USD),
BigDecimal.valueOf(USUAL_PRICE_PER_PIECE_USD), NUMBER_OF_PIECES, BigDecimal.valueOf(MEEST_SH_OVERHEAD));
double expectedProfitUsdValue = (USUAL_PRICE_PER_PIECE_USD * NUMBER_OF_PIECES
- (LISTING_PRICE_PER_PIECE_USD + SHIPPING_PRICE_PER_PIECE_USD) * NUMBER_OF_PIECES) - MEEST_SH_OVERHEAD;
assertThat(calculatedProfitUsd.doubleValue()).isEqualTo(expectedProfitUsdValue, Offset.offset(DELTA));
}

@Test
void shouldCalculateExpectedProfitPct() {
double expectedProfitUsd = USUAL_PRICE_PER_PIECE_USD * NUMBER_OF_PIECES
- (LISTING_PRICE_PER_PIECE_USD + SHIPPING_PRICE_PER_PIECE_USD) * NUMBER_OF_PIECES - MEEST_SH_OVERHEAD;
BigDecimal calculatedProfitPct = calculateExpectedProfitPct(BigDecimal.valueOf(expectedProfitUsd),
BigDecimal.valueOf(LISTING_PRICE_PER_PIECE_USD * NUMBER_OF_PIECES),
BigDecimal.valueOf(SHIPPING_PRICE_PER_PIECE_USD * NUMBER_OF_PIECES), BigDecimal.valueOf(MEEST_SH_OVERHEAD));

double expectedProfitPercentValue = (expectedProfitUsd /
((LISTING_PRICE_PER_PIECE_USD + SHIPPING_PRICE_PER_PIECE_USD) * NUMBER_OF_PIECES + MEEST_SH_OVERHEAD))
* 100;
assertThat(calculatedProfitPct).matches(bigDecimal -> bigDecimal.compareTo(BigDecimal.ZERO) > 0);
assertThat(calculatedProfitPct.doubleValue()).isEqualTo(expectedProfitPercentValue, Offset.offset(DELTA));
}
}

0 comments on commit 5513481

Please sign in to comment.