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

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.BeforeEach;
// import org.junit.jupiter.api.Test;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.boot.test.context.SpringBootTest;
// import org.springframework.transaction.annotation.Transactional;
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.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
// import com.example.demo.core.error.NotFoundException;
// 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.product.model.ProductEntity;
// import com.example.demo.product.service.ProductService;
// import com.example.demo.types.model.TypeEntity;
// import com.example.demo.types.service.TypeService;
// import com.example.demo.users.model.UserEntity;
// import com.example.demo.users.service.UserService;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.orders.api.OrderProductDto;
import com.example.demo.orders.service.OrderService;
import com.example.demo.product.model.ProductEntity;
import com.example.demo.product.service.ProductService;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService;
import com.example.demo.users.model.UserEntity;
import com.example.demo.users.service.UserService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class OrderServiceTests {
@Autowired
private TypeService typeService;
// @SpringBootTest
// public class OrderServiceTests {
// @Autowired
// private TypeService typeService;
@Autowired
private ProductService productService;
// @Autowired
// private ProductService productService;
@Autowired
private UserService userService;
// @Autowired
// private UserService userService;
@Autowired
private OrderService orderService;
// @Autowired
// private OrderService orderService;
@Test
@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;
// private ProductEntity product2;
// private ProductEntity product3;
final var user1 = userService.create(new UserEntity("user11", "user11"));
// @BeforeEach
// void createData() {
// removeData();
OrderProductDto product10 = new OrderProductDto();
product10.setProductId(product1.getId());
product10.setQuantity(4);
// final var type = typeService.create(new TypeEntity("Пепперони"));
// product1 = productService.create(new ProductEntity("test1", type, 399.00));
// product2 = productService.create(new ProductEntity("test2", type, 499.00));
// 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)));
// }
OrderProductDto product20 = new OrderProductDto();
product20.setProductId(product2.getId());
product20.setQuantity(6);
// @AfterEach
// void removeData() {
// 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);
// }
// }
orderService.create(user1.getId(), Arrays.asList(product10, product20));
Assertions.assertEquals(1, orderService.getAll(user1.getId()).size());
}
}

View File

@ -1,95 +1,60 @@
package com.example.demo;
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.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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.service.ProductService;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService;
import jakarta.transaction.Transactional;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class ProductsServiceTests {
class ProductServiceTests {
@Autowired
private TypeService typeService;
private TypeEntity type;
@Autowired
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
@Order(1)
void createTest() {
Assertions.assertEquals(6, productService.getAll(0L).size());
product = productService.create(new ProductEntity("test1", type, 399.00));
var type = typeService.create(new TypeEntity("NewType"));
var product = productService.create(new ProductEntity("test1", type, 399.00));
Assertions.assertEquals(product, productService.get(product.getId()));
}
@Test
@Order(2)
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));
}
@Transactional
@Test
@Order(3)
void updateTest() {
final String test = "TEST";
final TypeEntity newType = typeService.create(new TypeEntity("Фреш"));
product = productService.create(new ProductEntity("te2st", type, 399.00));
final TypeEntity oldType = product.getType();
final ProductEntity newEntity = productService.update(product.getId(), new ProductEntity(test, newType, 100.00));
Assertions.assertEquals(newEntity, productService.get(product.getId()));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertEquals(newType, newEntity.getType());
Assertions.assertNotEquals(oldType, newEntity.getType());
var type = typeService.create(new TypeEntity("UpdateType"));
var product = productService.create(new ProductEntity("UpdateTest", type, 399.00));
var updatedProduct = productService.update(product.getId(), new ProductEntity("UpdatedTest", type, 499.00));
Assertions.assertEquals("UpdatedTest", updatedProduct.getName());
Assertions.assertEquals(499.00, updatedProduct.getPrice());
}
@Test
@Order(4)
void deleteTest() {
type = typeService.create(new TypeEntity("yynrrnn"));
product = productService.create(new ProductEntity("te2st", type, 399.00));
var type = typeService.create(new TypeEntity("DeleteType"));
var product = productService.create(new ProductEntity("ToDelete", type, 399.00));
productService.delete(product.getId());
Assertions.assertEquals(6, productService.getAll(0L).size());
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());
Assertions.assertFalse(productService.getAll(0L).stream().anyMatch(p -> p.getId().equals(product.getId())));
}
}

View File

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

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.BeforeEach;
// import org.junit.jupiter.api.Test;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.boot.test.context.SpringBootTest;
// import org.springframework.dao.DataIntegrityViolationException;
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.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
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.service.UserService;
import com.example.demo.users.model.UserEntity;
import com.example.demo.users.service.UserService;
// import jakarta.transaction.Transactional;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class UserServiceTests {
@Autowired
private UserService userService;
// @SpringBootTest
// class UserServiceTests {
// @Autowired
// private UserService userService;
@Test
@Order(1)
void createTest() {
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
// void createData() {
// removeData();
@Test
@Order(3)
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"));
// userService.create(new UserEntity("vihoof", "Фёдор", "Лопатин", "test1@test.ru", "22-12-2023", "+79875433210", "qwerty321"));
// userService.create(new UserEntity("fragreg", "Семён", "Кукушкин", "test2@test.ru", "03-02-2023", "+74567433210", "qwerty451"));
// }
// @AfterEach
// 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());
// }
// }
@Test
@Order(4)
void deleteTest() {
var user = userService.create(new UserEntity("Delete", "Delete"));
userService.delete(user.getId());
Assertions.assertFalse(userService.getAll().stream().anyMatch(p -> p.getId().equals(user.getId())));
}
}