1+ package com .back .domain .file .repository ;
2+
3+ import com .back .domain .file .entity .AttachmentMapping ;
4+ import com .back .domain .file .entity .EntityType ;
5+ import com .back .domain .file .entity .FileAttachment ;
6+ import jakarta .persistence .EntityManager ;
7+ import org .junit .jupiter .api .Test ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .boot .test .context .SpringBootTest ;
10+ import org .springframework .mock .web .MockMultipartFile ;
11+ import org .springframework .transaction .annotation .Transactional ;
12+
13+
14+ import static org .assertj .core .api .Assertions .assertThat ;
15+
16+ @ SpringBootTest
17+ @ Transactional
18+ class AttachmentMappingRepositoryTest {
19+ @ Autowired
20+ private FileAttachmentRepository fileAttachmentRepository ;
21+ @ Autowired
22+ private AttachmentMappingRepository attachmentMappingRepository ;
23+ @ Autowired
24+ private EntityManager em ;
25+
26+ @ Test
27+ void AttachmentMapping_삭제시_FileAttachment_자동삭제_확인 () throws Exception {
28+ // given
29+ FileAttachment fileAttachment = new FileAttachment (
30+ "test" ,
31+ new MockMultipartFile ("test" , "test" , "image" , "test" .getBytes ()),
32+ null ,
33+ "test.URL"
34+ );
35+ fileAttachmentRepository .save (fileAttachment );
36+ System .out .println ("현재 저장된 파일 개수 : " + fileAttachmentRepository .findAll ().size ());
37+
38+ AttachmentMapping attachmentMapping = new AttachmentMapping (
39+ fileAttachment ,
40+ EntityType .POST ,
41+ 1L
42+ );
43+ attachmentMappingRepository .save (attachmentMapping );
44+
45+ // when
46+ attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (EntityType .POST , 1L );
47+
48+ em .flush (); // 즉시 DB에 변경사항 반영
49+ em .clear (); // 영속성 컨텍스트 초기화
50+
51+ // then
52+ assertThat (fileAttachmentRepository .findAll ().size ()).isEqualTo (0 );
53+ }
54+ }
0 commit comments