using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
using Xunit;
using Xunit.Extensions;
using Xunit.Serialization;
using Xunit.Abstractions;
using Xunit.Sdk;

using RedisStudyModel;
using RedisStuy;

namespace RedisStudyTest
{
    /// <summary>
    /// Redis Hash 类型测试
    /// </summary>
    public class RedisHashStudyTest : IDisposable
    {
        #region 初始化
        private IDatabase redisDatabase = null;
        private RedisHashStudy hashStudy = null;
        private List<Student> students;
        private Student student = null;
        private string preHashKey = "RedisStudy:Student:";

        /// <summary>
        /// 构造
        /// </summary>
        public RedisHashStudyTest()
        {
            redisDatabase = RedisHelper.GetRedisDatabase();
            hashStudy = new RedisHashStudy();
            student = new Student()
            {
                Id = 1,
                Name = "王高峰",
                Age = 2 * 9
            };
            students = new List<Student>()
            {
                new Student()
                {
                    Id = 1001,
                    Name = "王高峰",
                    Age = 11
                },
                new Student()
                {
                    Id = 1002,
                    Name = "王高峰2",
                    Age = 22
                },
                new Student()
                {
                    Id = 1003,
                    Name = "王高峰3",
                    Age = 33
                },
                new Student()
                {
                    Id = 1004,
                    Name = "王高峰4",
                    Age = 44
                },
                new Student()
                {
                    Id = 1005,
                    Name = "王高峰5",
                    Age = 55
                },
            };
        }
        #endregion

        #region 添加或更新学习
        [Fact]
        public void AddStudentExceptionTest()
        {
            string redisKey = preHashKey + student.Id;
            //参数异常测试
            Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(string.Empty, null));
            Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet("", null));
            Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", null));
            Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { }));
        }

        [Fact]
        public void AddStudentWhenTest()
        {
            string redisKey = preHashKey + student.Id;

            //当前上下文不能使用: When.Exists

            var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists);
            Assert.True(id_When_NotExists_No);

            var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists);
            Assert.False(id_When_NotExists_Yes);

            var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.Always);
            Assert.False(id_When_Always_Exists);

            var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", student.Id + 1, When.Always);
            Assert.True(id_When_Always_NotExists);
        }

        #endregion

        [Fact]
        public void Test()
        {
            Assert.IsType<int>(1);
        }

        /// <summary>
        /// 清理
        /// </summary>
        public void Dispose()
        {
            if (student != null)
            {
                redisDatabase.KeyDelete(preHashKey + student.Id);
            }

            foreach (var temp in students)
            {
                redisDatabase.KeyDelete(preHashKey + temp.Id);
            }
        }
    }
}