Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
byzaya committed Mar 17, 2024
1 parent ece45da commit c3976d4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/test/java/org/example/controller/TestObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class TestObjects {
public static ItemDto special;

public static Long userId;
public static Long userIdTest;
public static Date creationDate;
public static Date deliveryDate;

Expand All @@ -61,6 +62,7 @@ public class TestObjects {

public static CartDto cart;
public static CartDto cartSecUser;
public static CartDto cartTestUser;
public static CartToItemDto cartItem1;
public static CartToItemDto cartItem2;
public static CartToItemDto[] cartItems;
Expand All @@ -84,6 +86,8 @@ public class TestObjects {
pharmacyId = -1L;
receiptItemId = -1L;

userIdTest = -4L;

SpecialityDto speciality = new SpecialityDto(-1L, "терапевт");

firstPharmacy =
Expand Down Expand Up @@ -195,6 +199,7 @@ public class TestObjects {

cart = new CartDto(-1L, userId, Arrays.stream(cartItems).toList());
cartSecUser = new CartDto(-2L, -2L, Arrays.stream(cartItemsSecUser).toList());
cartTestUser = new CartDto(-4L, -4L, Arrays.stream(cartItemsSecUser).toList());

badRequest = 400;
notFoundCode = 404;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void logoutSuccess() {
@SneakyThrows
void logoutFailed() {
mockMvc
.perform(post("/api/accounts/logout").param("user_id", "-4"))
.perform(post("/api/accounts/logout").param("user_id", "-5"))
.andExpectAll(
status().isNotFound(),
jsonPath("$.*", hasSize(3)),
Expand Down
31 changes: 28 additions & 3 deletions src/test/java/org/example/controller/cart/CartControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.example.controller.cart;

import static org.example.controller.TestObjects.cartSecUser;
import static org.example.controller.TestObjects.cartTestUser;
import static org.example.controller.TestObjects.creationDate;
import static org.example.controller.TestObjects.creationDateStr;
import static org.example.controller.TestObjects.deliveryDate;
import static org.example.controller.TestObjects.deliveryDateStr;
import static org.example.controller.TestObjects.notFound;
import static org.example.controller.TestObjects.notFoundCode;
import static org.example.controller.TestObjects.receipt;
import static org.example.controller.TestObjects.special;
import static org.example.controller.TestObjects.sumPrice;
import static org.example.controller.TestObjects.userId;
import static org.example.controller.TestObjects.userNotFound;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
Expand Down Expand Up @@ -44,7 +51,7 @@ void getAllItemsInCart() {
@SneakyThrows
void getAllItemsInCart_UserNotFound() {
mockMvc
.perform(get("/api/cart/1"))
.perform(get("/api/cart/-5"))
.andExpectAll(status().isNotFound(), jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.code").value(userNotFound))
.andExpect(jsonPath("$.status").value(notFoundCode))
Expand All @@ -70,7 +77,7 @@ void addItemToCart() {
void addItemToCart_UserNotFound() {
mockMvc
.perform(
post("/api/cart/add").param("item_id", "-1").param("user_id", "1").param("count", "1"))
post("/api/cart/add").param("item_id", "-1").param("user_id", "-5").param("count", "1"))
.andExpectAll(status().isNotFound(), jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.code").value(userNotFound))
.andExpect(jsonPath("$.status").value(notFoundCode))
Expand Down Expand Up @@ -103,7 +110,7 @@ void deleteItemFromCart() {
@SneakyThrows
void deleteItemFromCart_UserNotFound() {
mockMvc
.perform(delete("/api/cart/delete").param("item_id", "-1").param("user_id", "1"))
.perform(delete("/api/cart/delete").param("item_id", "-1").param("user_id", "-5"))
.andExpectAll(status().isNotFound(), jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.code").value(userNotFound))
.andExpect(jsonPath("$.status").value(notFoundCode))
Expand All @@ -112,11 +119,29 @@ void deleteItemFromCart_UserNotFound() {

// тут начинаются сложные тесты

// тут УДАЛЯЕТСЯ из корзины, при перезапуске тестов нужно обновить тестовую бд !!
// просмотр корзины пользователя и размещение заказа
@Test
@SneakyThrows
void getCartInfoAndPlaceOrder() {
ResultActions result = mockMvc.perform(get("/api/cart/-4")).andExpect(status().isOk());
mvcUtil.assertContentEquals(result, objectMapper.writeValueAsString(cartTestUser));

mockMvc
.perform(
post("/api/order")
.param("user_id", String.valueOf(-4L))
.param("creation_date", creationDate.toString())
.param("delivery_date", deliveryDate.toString())
.param("sum_price", String.valueOf(sumPrice))
.param("pharmacy_id", "-1")
.param("items", "-2"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.user_id").value(-4L))
.andExpect(jsonPath("$.creation_date").value(creationDateStr))
.andExpect(jsonPath("$.delivery_date").value(deliveryDateStr))
.andExpect(jsonPath("$.sum_price").value(String.valueOf(sumPrice)))
.andExpect(jsonPath("$.pharmacy.id").value("-1"));
}

// добавление препарата в корзину и его удаление из корзины
Expand Down
9 changes: 6 additions & 3 deletions src/test/resources/test-database-content.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ CREATE EXTENSION IF NOT EXISTS pgcrypto;
INSERT INTO user_account (id, full_name, password_hash, phone, role_id, speciality_id)
VALUES (-1, 'Иванов Иван Иванович', digest('123456', 'sha256'), '+79260567450', -1, NULL),
(-2, 'Глазов Степан Фёдорович', digest('654321', 'sha256'), '+79310367450', -2, -1),
(-3, 'Главный Пётр Петрович', digest('12345', 'sha256'), '+79510367450', -3, NULL);
(-3, 'Главный Пётр Петрович', digest('12345', 'sha256'), '+79510367450', -3, NULL),
(-4, 'Я тестовый пользователь', digest('12345', 'sha256'), '+79510367457', -1, NULL);

INSERT INTO orders (creation_date, delivery_date, sum_price, id, pharmacy_id, user_id)
VALUES ('2024-01-11', '2024-01-16', 100500, -1, -1, -1),
Expand All @@ -59,9 +60,11 @@ VALUES (1, -1, -1, -1),
INSERT INTO cart (id, user_id)
VALUES (-1, -1),
(-2, -2),
(-3, -3);
(-3, -3),
(-4, -4);

INSERT INTO cart_to_item (quantity, cart_id, id, item_id)
VALUES (2, -1, -1, -1),
(1, -1, -2, -2),
(1, -2, -3, -2);
(1, -2, -3, -2),
(1, -4, -4, -2);

0 comments on commit c3976d4

Please sign in to comment.