Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

covered solution with tests #817

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

dima-semeniuk
Copy link

No description provided.

Copy link

@HovorukhaBohdan HovorukhaBohdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

Copy link

@bhdnchui bhdnchui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job.
Let's add .DS_Store files to gitignore.

static void beforeAll() {
storageDao = new StorageDaoImpl();
expectedMap = new HashMap<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Map<Fruit, Integer> actualMap = storageDao.getALl();
assertEquals(expectedMap, actualMap);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have method isInStorage, test it too.

transactionWithNegativeAmount.setOperation(Operation.BALANCE);
transactionWithNegativeAmount.setFruitName(FRUIT_NAME);
transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Integer expectedFruitAmount = FRUIT_AMOUNT;
Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME));
assertEquals(expectedFruitAmount, actualFruitAmount);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


@Test
void updateStorage_fruitAmountIsNegative_isNotOk() {
assertThrows(RuntimeException.class, () -> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the message of exception or use other exception type.

transactionWithLessAmount.setOperation(Operation.PURCHASE);
transactionWithLessAmount.setFruitName(FRUIT_NAME);
transactionWithLessAmount.setAmount(FRUIT_LESS_AMOUNT);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


@Test
void updateStorage_emptyStorage_isNotOk() {
assertThrows(RuntimeException.class, () -> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be sure that business exception was thrown.

transactionWithNegativeAmount.setOperation(Operation.RETURN);
transactionWithNegativeAmount.setFruitName(FRUIT_NAME);
transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION;
Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME));
assertEquals(expectedFruitAmount, actualFruitAmount);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

transactionWithNegativeAmount.setOperation(Operation.SUPPLY);
transactionWithNegativeAmount.setFruitName(FRUIT_NAME);
transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link

@mnyshenko mnyshenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical points to consider

}

@Test
public void readData_fileNotExistOrBadPath_notOk() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose to put public here?

Comment on lines 34 to 36
Storage.fruits.put(BANANA, 100);
Storage.fruits.put(GRAPE, 40);
Storage.fruits.put(APPLE, 90);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be magic numbers


public static Operation getOperationOf(String letter) {
return Arrays.stream(Operation.values())
.filter(o -> o.getCodeOfOperation().equals(letter))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please choose a proper name for parameters in the lambdas; other developers will need to spend additional time figuring out what 'o' means

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

}

public boolean isInStorage(String name) {
return (get(name) != null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(get(name) != null)
these parentheses are redundant

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.


boolean isInStorage(String name);

Map<Fruit, Integer> getALl();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: a little problem with the name

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

Comment on lines 18 to 19
if (!storageDao.isInStorage(transaction.getFruitName())) {
storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transaction.getFruitName() can be extracted to a separate variable to avoid code duplication

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

Integer fruitAmount = fruitAndAmount.getValue();
Integer fruitAmountFromTransaction = transaction.getAmount();
fruitAndAmount.setValue(fruitAmount - fruitAmountFromTransaction);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line

@Override
public String createReport() {
return storageDao.getALl().entrySet().stream()
.map(e -> e.getKey().getName() + SEPARATOR + e.getValue())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameter naming again

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

Comment on lines 38 to 79
void storageDao_add_isOk() {
storageDao.add(fruit, AMOUNT);
Map<Fruit, Integer> actualMap = Storage.fruits;
assertEquals(expectedMap, actualMap);
}

@Test
void storageDao_getExistingFruit_isOk() {
Storage.fruits.put(fruit, AMOUNT);
Map.Entry<Fruit, Integer> actualEntry = storageDao.get(fruit.getName());
Fruit actualFruit = actualEntry.getKey();
Integer actualAmount = actualEntry.getValue();
assertEquals(fruit, actualFruit);
assertEquals(AMOUNT, actualAmount);
}

@Test
void storageDao_getNullIfNotExist_isOk() {
Storage.fruits.put(fruit, AMOUNT);
Map.Entry<Fruit, Integer> actualEntry = storageDao.get(NAME_OF_NOT_EXISTING_FRUIT);
assertNull(actualEntry);
}

@Test
void storageDao_getAll_isOk() {
Storage.fruits.put(fruit, AMOUNT);
Map<Fruit, Integer> actualMap = storageDao.getALl();
assertEquals(expectedMap, actualMap);
}

@Test
void storageDao_isInStorage_isOk() {
Storage.fruits.put(fruit, AMOUNT);
boolean actual = storageDao.isInStorage(fruit.getName());
assertTrue(actual);
}

@Test
void storageDao_isNotInStorage_isOk() {
boolean actual = storageDao.isInStorage(FRUIT_NAME);
assertFalse(actual);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to divide statements in tests by the following principle:
https://medium.com/@gitaeklee/given-when-then-junit-test-ba49564303e7

writing comments like //when //then is optional; you can divide everything by adding empty lines between statements

Copy link

@d1sam d1sam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other mentors' comments are still exist.


boolean isInStorage(String name);

Map<Fruit, Integer> getALl();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

}

public boolean isInStorage(String name) {
return (get(name) != null);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.


public static Operation getOperationOf(String letter) {
return Arrays.stream(Operation.values())
.filter(o -> o.getCodeOfOperation().equals(letter))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

Comment on lines 18 to 19
if (!storageDao.isInStorage(transaction.getFruitName())) {
storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

Integer fruitAmount = fruitAndAmount.getValue();
Integer fruitAmountFromTransaction = transaction.getAmount();
fruitAndAmount.setValue(fruitAmount - fruitAmountFromTransaction);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

.skip(ROW_TO_SKIP)
.toList();
} catch (IOException e) {
throw new RuntimeException("Can't read data from file: " + file);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this message as constant.

@Override
public String createReport() {
return storageDao.getALl().entrySet().stream()
.map(e -> e.getKey().getName() + SEPARATOR + e.getValue())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

@Override
public OperationHandler getOperationHandler(Operation operation) {
if (operationHandlerMap == null) {
throw new RuntimeException("Map with handlers is NULL!!!");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make message as constant.

try {
Files.write(Path.of(toFile), report.getBytes());
} catch (IOException e) {
throw new RuntimeException("Can't write data to file: " + toFile);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this message as constant.

}

@Test
public void readData_fileNotExistOrBadPath_notOk() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void readData_fileNotExistOrBadPath_notOk() {
void readData_fileNotExistOrBadPath_notOk() {

@dima-semeniuk dima-semeniuk requested a review from d1sam October 27, 2023 09:58
Copy link

@kshuryhin kshuryhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job

dlvsn pushed a commit to dlvsn/jv-fruit-shop-tests that referenced this pull request Sep 6, 2024
…/add_link_to_readme

add link to readme file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants