Теееееееесты

This commit is contained in:
Вячеслав Иванов 2024-05-15 21:41:45 +04:00
parent 06853aa86a
commit 2ec04cf086
4 changed files with 144 additions and 283 deletions

View File

@ -1,117 +1,71 @@
// package com.example.demo; package com.example.demo;
// import java.util.Arrays; import java.util.Arrays;
// import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions;
// import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
// import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Order;
// import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
// import org.springframework.beans.factory.annotation.Autowired; import org.junit.jupiter.api.TestMethodOrder;
// import org.springframework.boot.test.context.SpringBootTest; import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.transaction.annotation.Transactional; import org.springframework.boot.test.context.SpringBootTest;
// import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
// import com.example.demo.orders.api.OrderProductDto; import com.example.demo.orders.api.OrderProductDto;
// import com.example.demo.orders.model.OrderEntity; import com.example.demo.orders.service.OrderService;
// import com.example.demo.orders.service.OrderService; import com.example.demo.product.model.ProductEntity;
// import com.example.demo.product.model.ProductEntity; import com.example.demo.product.service.ProductService;
// import com.example.demo.product.service.ProductService; import com.example.demo.types.model.TypeEntity;
// import com.example.demo.types.model.TypeEntity; import com.example.demo.types.service.TypeService;
// import com.example.demo.types.service.TypeService; import com.example.demo.users.model.UserEntity;
// import com.example.demo.users.model.UserEntity; import com.example.demo.users.service.UserService;
// import com.example.demo.users.service.UserService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class OrderServiceTests {
@Autowired
private TypeService typeService;
// @SpringBootTest @Autowired
// public class OrderServiceTests { private ProductService productService;
// @Autowired
// private TypeService typeService;
// @Autowired @Autowired
// private ProductService productService; private UserService userService;
// @Autowired @Autowired
// private UserService userService; private OrderService orderService;
// @Autowired @Test
// private OrderService orderService; @Order(1)
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> orderService.getAll(0L));
}
// private OrderEntity order; @Test
@Order(2)
void createTest() {
final var type1 = typeService.create(new TypeEntity("1"));
final var type2 = typeService.create(new TypeEntity("2"));
final var type3 = typeService.create(new TypeEntity("3"));
// private UserEntity user; final var product1 = productService.create(new ProductEntity("test1", type1, 399.00));
final var product2 = productService.create(new ProductEntity("test2", type1, 499.00));
productService.create(new ProductEntity("test3", type2, 450.50));
productService.create(new ProductEntity("test4", type2, 900.50));
productService.create(new ProductEntity("test5", type2, 600.00));
productService.create(new ProductEntity("test6", type3, 750.00));
// private ProductEntity product1; final var user1 = userService.create(new UserEntity("user11", "user11"));
// private ProductEntity product2;
// private ProductEntity product3;
// @BeforeEach OrderProductDto product10 = new OrderProductDto();
// void createData() { product10.setProductId(product1.getId());
// removeData(); product10.setQuantity(4);
// final var type = typeService.create(new TypeEntity("Пепперони")); OrderProductDto product20 = new OrderProductDto();
// product1 = productService.create(new ProductEntity("test1", type, 399.00)); product20.setProductId(product2.getId());
// product2 = productService.create(new ProductEntity("test2", type, 499.00)); product20.setQuantity(6);
// product3 = productService.create(new ProductEntity("test3", type, 450.50));
// user = userService.create(new UserEntity("kruviii", "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123"));
// order = orderService.create(user.getId(), new OrderEntity("ул. Северный венец, 32"), Arrays.asList(
// new OrderProductDto(product1.getId(), 3),
// new OrderProductDto(product2.getId(), 1),
// new OrderProductDto(product3.getId(), 2)));
// orderService.create(user.getId(), new OrderEntity("ул. Северный венец, 32"), Arrays.asList(
// new OrderProductDto(product1.getId(), 5)));
// orderService.create(user.getId(), new OrderEntity("ул. Северный венец, 32"), Arrays.asList(
// new OrderProductDto(product2.getId(), 1),
// new OrderProductDto(product3.getId(), 1)));
// }
// @AfterEach orderService.create(user1.getId(), Arrays.asList(product10, product20));
// void removeData() { Assertions.assertEquals(1, orderService.getAll(user1.getId()).size());
// userService.getAll().forEach(item -> userService.delete(item.getId())); }
// productService.getAll(0L).forEach(item -> productService.delete(item.getId())); }
// typeService.getAll().forEach(item -> typeService.delete(item.getId()));
// }
// @Test
// void getTest() {
// Assertions.assertThrows(NotFoundException.class, () -> orderService.get(user.getId(), 0L));
// }
// @Transactional
// @Test
// void createTest() {
// Assertions.assertEquals(3, orderService.getAll(user.getId()).size());
// Assertions.assertEquals(order, orderService.get(user.getId(), order.getId()));
// }
// @Transactional
// @Test
// void updateTest() {
// final OrderEntity newEntity = orderService.update(user.getId(), order.getId(), new OrderEntity("ул. Северный венец, 32"), Arrays.asList(
// new OrderProductDto(product1.getId(), 5)));
// Assertions.assertEquals(3, orderService.getAll(user.getId()).size());
// Assertions.assertEquals(newEntity, orderService.get(user.getId(), order.getId()));
// }
// @Test
// void deleteTest() {
// orderService.delete(user.getId(), order.getId());
// Assertions.assertEquals(2, orderService.getAll(user.getId()).size());
// final OrderEntity newEntity = orderService.create(user.getId(), new OrderEntity("ул. Северный венец, 32"), Arrays.asList(
// new OrderProductDto(product1.getId(), 5)));
// Assertions.assertEquals(3, orderService.getAll(user.getId()).size());
// Assertions.assertNotEquals(order.getId(), newEntity.getId());
// }
// @Test
// void getSumTest() {
// Double sum = orderService.getSum(user.getId(), order.getId());
// Assertions.assertEquals(product1.getPrice() * 3 + product2.getPrice() + product3.getPrice() * 2, sum);
// }
// @Test
// void getFullCountTest() {
// Integer count = orderService.getFullCount(user.getId(), order.getId());
// Assertions.assertEquals(6, count);
// }
// }

View File

@ -1,95 +1,60 @@
package com.example.demo; package com.example.demo;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.product.model.ProductEntity; import com.example.demo.product.model.ProductEntity;
import com.example.demo.product.service.ProductService; import com.example.demo.product.service.ProductService;
import com.example.demo.types.model.TypeEntity; import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService; import com.example.demo.types.service.TypeService;
import jakarta.transaction.Transactional;
@SpringBootTest @SpringBootTest
@TestMethodOrder(OrderAnnotation.class) @TestMethodOrder(OrderAnnotation.class)
class ProductsServiceTests { class ProductServiceTests {
@Autowired @Autowired
private TypeService typeService; private TypeService typeService;
private TypeEntity type;
@Autowired @Autowired
private ProductService productService; private ProductService productService;
private ProductEntity product;
void createData() {
removeData();
type = typeService.create(new TypeEntity("Мясная"));
final var type2 = typeService.create(new TypeEntity("Пепперони"));
final var type3 = typeService.create(new TypeEntity("Сырная"));
product = productService.create(new ProductEntity("test", type, 399.00));
productService.create(new ProductEntity("test", type2, 499.00));
productService.create(new ProductEntity("test", type3, 450.50));
}
void removeData() {
productService.getAll(0L).forEach(item -> productService.delete(item.getId()));
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
}
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> productService.get(0L));
}
@Transactional
@Test @Test
@Order(1)
void createTest() { void createTest() {
Assertions.assertEquals(6, productService.getAll(0L).size()); var type = typeService.create(new TypeEntity("NewType"));
product = productService.create(new ProductEntity("test1", type, 399.00)); var product = productService.create(new ProductEntity("test1", type, 399.00));
Assertions.assertEquals(product, productService.get(product.getId())); Assertions.assertEquals(product, productService.get(product.getId()));
} }
@Test @Test
@Order(2)
void createNullableTest() { void createNullableTest() {
final ProductEntity nullableProduct = new ProductEntity(null, type, 399.00);; var type = typeService.create(new TypeEntity("qwe"));
var nullableProduct = new ProductEntity(null, type, 399.00);
Assertions.assertThrows(DataIntegrityViolationException.class, () -> productService.create(nullableProduct)); Assertions.assertThrows(DataIntegrityViolationException.class, () -> productService.create(nullableProduct));
} }
@Transactional
@Test @Test
@Order(3)
void updateTest() { void updateTest() {
final String test = "TEST"; var type = typeService.create(new TypeEntity("UpdateType"));
final TypeEntity newType = typeService.create(new TypeEntity("Фреш")); var product = productService.create(new ProductEntity("UpdateTest", type, 399.00));
product = productService.create(new ProductEntity("te2st", type, 399.00)); var updatedProduct = productService.update(product.getId(), new ProductEntity("UpdatedTest", type, 499.00));
final TypeEntity oldType = product.getType(); Assertions.assertEquals("UpdatedTest", updatedProduct.getName());
final ProductEntity newEntity = productService.update(product.getId(), new ProductEntity(test, newType, 100.00)); Assertions.assertEquals(499.00, updatedProduct.getPrice());
Assertions.assertEquals(newEntity, productService.get(product.getId()));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertEquals(newType, newEntity.getType());
Assertions.assertNotEquals(oldType, newEntity.getType());
} }
@Test @Test
@Order(4)
void deleteTest() { void deleteTest() {
type = typeService.create(new TypeEntity("yynrrnn")); var type = typeService.create(new TypeEntity("DeleteType"));
product = productService.create(new ProductEntity("te2st", type, 399.00)); var product = productService.create(new ProductEntity("ToDelete", type, 399.00));
productService.delete(product.getId()); productService.delete(product.getId());
Assertions.assertEquals(6, productService.getAll(0L).size()); Assertions.assertFalse(productService.getAll(0L).stream().anyMatch(p -> p.getId().equals(product.getId())));
final ProductEntity newEntity = productService.create(new ProductEntity(product.getName(), product.getType(), product.getPrice()));
Assertions.assertEquals(7, typeService.getAll().size());
Assertions.assertNotEquals(product.getId(), newEntity.getId());
} }
} }

View File

@ -2,81 +2,57 @@ package com.example.demo;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.types.model.TypeEntity; import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService; import com.example.demo.types.service.TypeService;
@SpringBootTest @SpringBootTest
@TestMethodOrder(OrderAnnotation.class) @TestMethodOrder(OrderAnnotation.class)
class TypeServiceTests { class TypeServiceTests {
@Autowired @Autowired
private TypeService typeService; private TypeService typeService;
private TypeEntity type; @Test
@Order(1)
void createData() { void createTest() {
Assertions.assertEquals(10, typeService.getAll().size());
type = typeService.create(new TypeEntity("Пепперони"));
typeService.create(new TypeEntity("Мясная"));
typeService.create(new TypeEntity("4 сыра"));
} }
@Test
void removeData() { @Order(2)
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
}
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
}
@Test
void createTest() {
type = typeService.create(new TypeEntity("thdgndgneth"));
Assertions.assertEquals(5, typeService.getAll().size());
Assertions.assertEquals(type, typeService.get(type.getId()));
}
@Test
void createNotUniqueTest() { void createNotUniqueTest() {
final TypeEntity nonUniqueType = new TypeEntity("Пепперони"); final TypeEntity nonUniqueType = new TypeEntity("Сырная");
Assertions.assertThrows(IllegalArgumentException.class, () -> typeService.create(nonUniqueType)); Assertions.assertThrows(IllegalArgumentException.class, () -> typeService.create(nonUniqueType));
} }
@Test @Test
@Order(3)
void createNullableTest() { void createNullableTest() {
final TypeEntity nullableType = new TypeEntity(null); final TypeEntity nullableType = new TypeEntity(null);
Assertions.assertThrows(DataIntegrityViolationException.class, () -> typeService.create(nullableType)); Assertions.assertThrows(DataIntegrityViolationException.class, () -> typeService.create(nullableType));
} }
@Test @Test
@Order(4)
void updateTest() { void updateTest() {
final String test = "TEST"; final TypeEntity newEntity = typeService.update(3L, new TypeEntity("TEST"));
type = typeService.create(new TypeEntity("ryjhjhrrnh")); Assertions.assertEquals(10, typeService.getAll().size());
final String oldName = type.getName(); Assertions.assertEquals("TEST", newEntity.getName());
final TypeEntity newEntity = typeService.update(type.getId(), new TypeEntity(test)); Assertions.assertNotEquals("TESTS", newEntity.getName());
Assertions.assertEquals(4, typeService.getAll().size());
Assertions.assertEquals(newEntity, typeService.get(type.getId()));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
} }
@Test @Test
@Order(5)
void deleteTest() { void deleteTest() {
type = typeService.create(new TypeEntity("gnnetnten")); var del = typeService.create(new TypeEntity("TESTSSS"));
typeService.delete(type.getId()); var test = typeService.get(del.getId());
Assertions.assertEquals(5, typeService.getAll().size()); typeService.delete(test.getId());
Assertions.assertEquals(10, typeService.getAll().size());
final TypeEntity newEntity = typeService.create(new TypeEntity(type.getName()));
Assertions.assertEquals(6, typeService.getAll().size());
Assertions.assertNotEquals(type.getId(), newEntity.getId());
} }
} }

View File

@ -1,85 +1,51 @@
// package com.example.demo; package com.example.demo;
// import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions;
// import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
// import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Order;
// import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
// import org.springframework.beans.factory.annotation.Autowired; import org.junit.jupiter.api.TestMethodOrder;
// import org.springframework.boot.test.context.SpringBootTest; import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.dao.DataIntegrityViolationException; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
// import com.example.demo.core.error.NotFoundException; import com.example.demo.users.model.UserEntity;
// import com.example.demo.users.model.UserEntity; import com.example.demo.users.service.UserService;
// import com.example.demo.users.service.UserService;
// import jakarta.transaction.Transactional; @SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class UserServiceTests {
@Autowired
private UserService userService;
// @SpringBootTest @Test
// class UserServiceTests { @Order(1)
// @Autowired void createTest() {
// private UserService userService; Assertions.assertEquals(3, userService.getAll().size());
}
// private UserEntity user; @Test
@Order(2)
void createNullableTest() {
var nullableUser = new UserEntity(null, "Павел");
Assertions.assertThrows(DataIntegrityViolationException.class, () -> userService.create(nullableUser));
}
// @BeforeEach @Test
// void createData() { @Order(3)
// removeData(); void updateTest() {
var user = userService.create(new UserEntity("kruvii", "Павелл"));
var newEntity = userService.update(user.getId(),new UserEntity("kruviiii", "Павеллл"));
Assertions.assertEquals(4, userService.getAll().size());
Assertions.assertEquals("kruviiii", newEntity.getLogin());
Assertions.assertEquals("Павеллл", newEntity.getPassword());
}
// user = userService.create(new UserEntity("kruviii", "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123")); @Test
// userService.create(new UserEntity("vihoof", "Фёдор", "Лопатин", "test1@test.ru", "22-12-2023", "+79875433210", "qwerty321")); @Order(4)
// userService.create(new UserEntity("fragreg", "Семён", "Кукушкин", "test2@test.ru", "03-02-2023", "+74567433210", "qwerty451")); void deleteTest() {
// } var user = userService.create(new UserEntity("Delete", "Delete"));
userService.delete(user.getId());
// @AfterEach Assertions.assertFalse(userService.getAll().stream().anyMatch(p -> p.getId().equals(user.getId())));
// void removeData() { }
// userService.getAll().forEach(item -> userService.delete(item.getId())); }
// }
// @Test
// void getTest() {
// Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
// }
// @Transactional
// @Test
// void createTest() {
// Assertions.assertEquals(3, userService.getAll().size());
// Assertions.assertEquals(user, userService.get(user.getId()));
// }
// @Test
// void createNotUniqueTest() {
// final UserEntity nonUniqueUser = new UserEntity("kruviii", "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123");
// Assertions.assertThrows(IllegalArgumentException.class, () -> userService.create(nonUniqueUser));
// }
// @Test
// void createNullableTest() {
// final UserEntity nullableUser = new UserEntity(null, "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123");
// Assertions.assertThrows(DataIntegrityViolationException.class, () -> userService.create(nullableUser));
// }
// @Transactional
// @Test
// void updateTest() {
// final String test = "TEST";
// final String oldLogin = user.getLogin();
// final UserEntity newEntity = userService.update(user.getId(),
// new UserEntity("kruvi", "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123"));
// Assertions.assertEquals(3, userService.getAll().size());
// Assertions.assertEquals(newEntity, userService.get(user.getId()));
// Assertions.assertNotEquals(test, newEntity.getLogin());
// Assertions.assertNotEquals(oldLogin, newEntity.getLogin());
// }
// @Test
// void deleteTest() {
// userService.delete(user.getId());
// Assertions.assertEquals(2, userService.getAll().size());
// final UserEntity newEntity = userService
// .create(new UserEntity("kruviii", "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123"));
// Assertions.assertEquals(3, userService.getAll().size());
// Assertions.assertNotEquals(user.getId(), newEntity.getId());
// }
// }