/
ilyassab
/
homework6
Обзор
Документация
Войти
/
ilyassab
/
homework6
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/test/java/PersonRepositoryTests.java
177 строк
5 KB
sabirzyanov.ilyas
homework
16 ноя 2025, 23:15
16 ноя 2025, 23:15
6241856
Код
Авторство
О чём код?
import org.junit.Assert; import org.junit.Test; import sbp.person.PersonRepository; import sbp.person.Person; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; public class PersonRepositoryTests { private final String url = "jdbc:sqlite:sample.db"; private PersonRepository pr = null; private void prepareTable() throws SQLException { Connection connection = DriverManager.getConnection(url); pr = new PersonRepository(connection); pr.deleteTable(); pr.createTable(); } @Test public void createPersonTest() throws SQLException { prepareTable(); Assert.assertEquals(1, pr.create("Ilyas", "Spb", 27)); Assert.assertEquals(1, pr.create("Ilya", "Ekb", 27)); List<Person> persons = pr.findAll(); Assert.assertEquals(new Person(27, "Spb", "Ilyas", persons.get(0).getId()), persons.get(0)); Assert.assertEquals(new Person(27, "Ekb", "Ilya", persons.get(1).getId()), persons.get(1)); } @Test public void updatePersonTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Assert.assertEquals(1, pr.update(new Person(20, "Msc", "Vitya", 1))); Assert.assertEquals(1, pr.update(new Person(22, "Izh", "Viktor", 2))); List<Person> persons = pr.findAll(); Assert.assertEquals(new Person(20, "Msc", "Vitya", 1), persons.get(0)); Assert.assertEquals(new Person(22, "Izh", "Viktor", 2), persons.get(1)); } @Test public void updateNoPersonTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Assert.assertEquals(0, pr.update(new Person(20, "Msc", "Vitya", 4))); List<Person> persons = pr.findAll(); Assert.assertEquals(new Person(27, "Spb", "Ilyas", 1), persons.get(0)); Assert.assertEquals(new Person(27, "Ekb", "Ilya", 2), persons.get(1)); } @Test(expected = IllegalArgumentException.class) public void updatePersonErrorTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Assert.assertEquals(1, pr.update(new Person(20, "Msc", "Vitya", null))); } @Test public void deletePersonTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Assert.assertEquals(1, pr.deleteById(1)); List<Person> persons = pr.findAll(); Assert.assertEquals(1, persons.size()); Assert.assertEquals(new Person(27, "Ekb", "Ilya", 2), persons.get(0)); } @Test public void deleteNoPersonTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Assert.assertEquals(0, pr.deleteById(5)); List<Person> persons = pr.findAll(); Assert.assertEquals(2, persons.size()); Assert.assertEquals(new Person(27, "Spb", "Ilyas", 1), persons.get(0)); Assert.assertEquals(new Person(27, "Ekb", "Ilya", 2), persons.get(1)); } @Test(expected = IllegalArgumentException.class) public void deletePersonErrorTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Assert.assertEquals(0, pr.deleteById(null)); } @Test public void findByIdPersonTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Person person1 = pr.findById(1); Person person2 = pr.findById(2); Assert.assertEquals(person1, new Person(27, "Spb", "Ilyas", 1)); Assert.assertEquals(person2, new Person(27, "Ekb", "Ilya", 2)); } @Test public void findByIdNoPersonTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); Person person1 = pr.findById(5); Assert.assertNull(person1); } @Test(expected = IllegalArgumentException.class) public void findByIdPersonErrorTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); pr.findById(null); } @Test public void findAllPersonTest() throws SQLException { prepareTable(); pr.create("Ilyas", "Spb", 27); pr.create("Ilya", "Ekb", 27); List<Person> persons = pr.findAll(); Assert.assertEquals(2, persons.size()); Assert.assertEquals(new Person(27, "Spb", "Ilyas", 1), persons.get(0)); Assert.assertEquals(new Person(27, "Ekb", "Ilya", 2), persons.get(1)); } @Test public void findAllNoPersonTest() throws SQLException { prepareTable(); List<Person> persons = pr.findAll(); Assert.assertEquals(0, persons.size()); } }