package com.YuyeTech.TPlat.repository;

import com.YuyeTech.TPlat.dataobject.ProductCategory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {

        @Autowired
        private  ProductCategoryRepository repository;

        @Test
        public void findOneTest(){
            ProductCategory productCategory = repository.findById(1).orElse(null);
            System.out.println(productCategory.toString());
        }

        /**
         * @Transactional
         * test : 完全回滚，
         * service: 方法抛出异常时候回滚，把对应数据库数据删除。
         * */
        @Test
        @Transactional
        public void saveTest(){
            //更新属性，先查出对象数据，判断权限，属性之类，在更新
            //值不同才会DynamicUpdate，更新数据，时间；
            ProductCategory productCategory = new ProductCategory("男生最爱",11);
            ProductCategory result = repository.save(productCategory);
            Assert.assertNotNull(result);
//            Assert.assertNotEquals(null,result);
        }

        @Test
        public void findByCategoryTypeInTest(){
            List<Integer> list = Arrays.asList(2,10,4);

            List<ProductCategory> result = repository.findByCategoryTypeIn(list);
            Assert.assertNotEquals(0,result.size());
        }
}