TIL day 53

최병은·2024년 3월 14일
  1. 코딩테스트(바탕화면 정리)
class Solution {
    public int[] solution(String[] wallpaper) {
        int a1;
        int a2;
        int a3;
        int a4;

        a1 = a2 = 1000;
        a3 = a4 = 0;

        for (int i = 0; i < wallpaper.length; i++) {
            String[] w1 = wallpaper[i].split("");
            for (int j = 0; j < w1.length; j++) {
                if(w1[j].equals("#")) {
                    a1 = Math.min(a1,i);
                    a2 = Math.min(a2,j);
                    a3 = Math.max(a3,i);
                    a4 = Math.max(a4,j);
                }
            }
        }

        return new int[]{a1,a2,a3+1,a4+1};
    }
}

처음에 최소값, 최대값을 비교할 숫자가 필요하기 때문에 a1,a2는 큰 수인 1000으로, a3,a4는 0으로 초기화했다.


  1. SQL 테스트(상품 별 오프라인 매출 구하기)
SELECT PRODUCT_CODE, SUM(PRICE * SALES_AMOUNT) AS SALES
FROM PRODUCT p
LEFT JOIN OFFLINE_SALE o ON p.PRODUCT_ID = o.PRODUCT_ID
GROUP BY PRODUCT_CODE
ORDER BY SALES DESC, PRODUCT_CODE ASC

QueryDSL을 하면서 어느정도 SQL문에 대해서도 친숙해져야겠다는 생각이 들어서 한 문제씩이라도 풀어보면 좋을 것 같다.


  1. QueryDSL 테스트 작성
    @Test
    void searchByCategoryTest2() throws IOException {
        // given
        CategoryEnum category = CategoryEnum.Electronics;
        Integer currentPage = 1;
        Integer size = 4;
        String sortBy = "category";
        PageDto pageDto = new PageDto(currentPage,size,sortBy);
        
        User user1 = new User("asd","asd@email.com","1234","asdf");
        User user2 = userRepository.save(user1);
        UserDetailsImpl userDetails = new UserDetailsImpl(user2);
        
        MockMultipartFile image = new MockMultipartFile(
                "test",
                "a.png",
                "image/png",
                new FileInputStream(new File("C:/Users/Owner/Pictures/Screenshots/a.png")));
        String imageUrl = "asd";

        Product p1 = new Product(new ProductRequestDto("Electronics","asd","asd",100,image),userDetails,imageUrl);
        productRepository.save(p1);
        
        // when
        var commentedProduct = productService.searchByCategory(category,pageDto);
        
        // then
        assertNotNull(commentedProduct);
    }
}

카테고리가 Electronics인 Product을 QueryDSL을 통해 검색하고 페이징적용하는 기능을 테스트해보았다.

profile
안녕하세요

0개의 댓글