-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncomeRepositoryTest.java
More file actions
68 lines (56 loc) · 2.26 KB
/
IncomeRepositoryTest.java
File metadata and controls
68 lines (56 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.sda.ironhack.Personal.Finance.Tracker.webApp.repos;
import com.sda.ironhack.Personal.Finance.Tracker.webApp.entities.Income;
import com.sda.ironhack.Personal.Finance.Tracker.webApp.entities.User;
import org.junit.jupiter.api.AfterEach;
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 java.time.LocalDate;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class IncomeRepositoryTest {
@Autowired
private IncomeRepository incomeRepository;
@Autowired
private UserRepository userRepository;
private Income income1;
private Income income2;
private Income income3;
private User user1;
@BeforeEach
void setUp() {
// Create a user
user1 = new User("John", "password", "john@example.com", 0);
userRepository.save(user1);
LocalDate date1 = LocalDate.of(2023, 12, 11);
LocalDate date2 = LocalDate.of(2023, 12, 12);
LocalDate date3 = LocalDate.of(2023, 12, 13);
// Create three income records linked to the user
Income income1 = new Income(user1, "work", 200.50, date1);
Income income2 = new Income(user1, "freelance", 300.75, date2);
Income income3 = new Income(user1, "dividends", 150.25, date3);
incomeRepository.save(income1);
incomeRepository.save(income2);
incomeRepository.save(income3);
}
@AfterEach
void tearDown(){
incomeRepository.deleteAll();
}
@Test
public void getAllIncomes(){
List<Income> incomes = incomeRepository.findAll();
}
@Test
public void getAllIncomesByUserId() {
// Retrieve a user from the database based on userIdToTest
User userFromDb = userRepository.findByUserId(user1.getUserId());
// Retrieve incomes for the specified user
List<Income> incomes = incomeRepository.findAllByUser(userFromDb);
// Add your assertions here to validate the expected results
assertNotNull(incomes);
assertEquals(incomes, incomeRepository.findAllByUser(userFromDb));
}
}