1
+ package io .doubleloop .drivenpull ;
2
+
3
+ import org .junit .jupiter .api .BeforeEach ;
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .boot .test .autoconfigure .data .mongo .DataMongoTest ;
7
+ import org .springframework .boot .testcontainers .service .connection .ServiceConnection ;
8
+ import org .testcontainers .containers .MongoDBContainer ;
9
+ import org .testcontainers .junit .jupiter .Container ;
10
+ import org .testcontainers .junit .jupiter .Testcontainers ;
11
+
12
+ import java .math .BigDecimal ;
13
+ import java .time .LocalDate ;
14
+
15
+ import static org .assertj .core .api .Assertions .assertThat ;
16
+
17
+ @ DataMongoTest
18
+ @ Testcontainers
19
+ class OperationRepositoryTest {
20
+
21
+ @ Container
22
+ @ ServiceConnection
23
+ private static MongoDBContainer container = new MongoDBContainer ("mongo:latest" );
24
+
25
+ @ Autowired
26
+ private OperationRepository repository ;
27
+
28
+ @ BeforeEach
29
+ void setUp () {
30
+ repository .deleteAll ();
31
+ }
32
+
33
+ @ Test
34
+ void findByUserIdAndDateMatchOne () {
35
+ repository .save (new Operation ("user1" , "EUR" , BigDecimal .TEN , LocalDate .of (2024 , 11 , 10 )));
36
+ repository .save (new Operation ("user1" , "EUR" , BigDecimal .TEN .negate (), LocalDate .of (2024 , 11 , 11 )));
37
+ repository .save (new Operation ("user2" , "EUR" , BigDecimal .TEN , LocalDate .of (2024 , 11 , 10 )));
38
+
39
+ final var result = repository .findByUserIdAndDate ("user1" , LocalDate .of (2024 , 11 , 10 ));
40
+
41
+ assertThat (result .size ()).isEqualTo (1 );
42
+ assertThat (result .get (0 ).getCurrency ()).isEqualTo ("EUR" );
43
+ assertThat (result .get (0 ).getAmount ()).isEqualTo (BigDecimal .TEN );
44
+ }
45
+
46
+ @ Test
47
+ void findByUserIdAndDateMatchMany () {
48
+ repository .save (new Operation ("user1" , "EUR" , BigDecimal .TEN , LocalDate .of (2024 , 11 , 10 )));
49
+ repository .save (new Operation ("user1" , "EUR" , BigDecimal .TEN .negate (), LocalDate .of (2024 , 11 , 10 )));
50
+ repository .save (new Operation ("user2" , "EUR" , BigDecimal .TEN , LocalDate .of (2024 , 11 , 10 )));
51
+
52
+ final var result = repository .findByUserIdAndDate ("user1" , LocalDate .of (2024 , 11 , 10 ));
53
+
54
+ assertThat (result .size ()).isEqualTo (2 );
55
+ }
56
+
57
+ @ Test
58
+ void findByUserIdAndDateNoMatch () {
59
+ repository .save (new Operation ("user1" , "EUR" , BigDecimal .TEN , LocalDate .of (2024 , 11 , 10 )));
60
+ repository .save (new Operation ("user2" , "EUR" , BigDecimal .TEN , LocalDate .of (2024 , 11 , 10 )));
61
+
62
+ final var result = repository .findByUserIdAndDate ("user3" , LocalDate .of (2024 , 11 , 10 ));
63
+
64
+ assertThat (result .size ()).isEqualTo (0 );
65
+ }
66
+ }
0 commit comments