private List<User> GetUsers()
 {
     var loc1 = new Location()
     {
         Id = 1,
         Name = "one"
     };
     var loc2 = new Location()
     {
         Id = 2,
         Name = "two"
     };
     var user1 = new User()
     {
         Id = 1,
         Deparments = new List<Department>()
         {
             new Department() {Id = 1, Distance = 123.45m, Size = 2, Location = loc1},
             new Department() {Id = 2, Distance = 400, Size = 1, Location = loc2}
         }
     };
     var user2 = new User()
     {
         Id = 2,
         Deparments = new List<Department>()
         {
             new Department() {Id = 3, Distance = 500, Size = 1, Location = loc2},
             new Department() {Id = 4, Distance = 125.5m, Size = 3, Location = loc1}
         }
     };
     var user3 = new User()
     {
         Id = 3,
         Deparments = new List<Department>()
         {
             new Department() {Id = 5, Distance = 5, Size = 5, Location = loc2},
         }
     };
     var user4 = new User()
     {
         Id = 4,
         Deparments = new List<Department>()
         {
             new Department() {Id = 6, Distance = 100, Size = 10, Location = loc1},
         }
     };
     return new List<User>() { user1, user2, user3, user4 };
 }
 public void UT_CacheSetObject(Context context)
 {
     string key1 = "UT_CacheSetObject1";
     context.Cache.Remove(key1);
     var users = GetUsers();
     var rs = context.Collections.GetRedisSet<User>(key1);
     rs.AddRange(users);
     // Test GetEnumerator
     foreach (var item in rs)
     {
         Assert.IsTrue(users.Any(u => u.Id == item.Id));
     }
     // Test Count
     Assert.AreEqual(users.Count, rs.Count);
     // Test Contains
     Assert.IsTrue(rs.Contains(users[2]));
     // Test Add
     var newUser = new User() {Id = 5};
     rs.Add(newUser);
     Assert.IsTrue(rs.Contains(newUser));
     // Test Remove
     rs.Remove(users[2]);
     Assert.IsFalse(rs.Contains(users[2]));
     // Test CopyTo
     User[] array = new User[50];
     rs.CopyTo(array, 10);
     Assert.AreEqual(users.Count, array.Count(x => x != null));
     // Test Clear
     rs.Clear();
     Assert.AreEqual(0, rs.Count);
     rs.AddRange(new []{  new User() {Id = 3},  new User() {Id = 1},  new User() {Id = 2} });
     Assert.AreEqual(3, rs.Count);
     rs.Remove(new User() { Id = 1 });
     rs.Remove(new User() { Id = 2 });
     Assert.AreEqual(1, rs.Count);
     Assert.IsTrue(rs.Contains(new User() { Id = 3 }));
     // Test GetRandomMember
     var user = rs.GetRandomMember();
     Assert.AreEqual(3, user.Id);
     // Test Pop
     user = rs.Pop();
     Assert.AreEqual(3, user.Id);
     user = rs.Pop();
     Assert.IsNull(user);
 }
 public void UT_CacheDictionaryObject(Context context)
 {
     string key1 = "UT_CacheDictionaryObject1";
     context.Cache.Remove(key1);
     var users = GetUsers();
     var rd = context.Collections.GetRedisDictionary<int, User>(key1);
     // Test AddMultiple
     var usersKv = users.Select(x => new KeyValuePair<int, User>(x.Id, x));
     rd.AddRange(usersKv);
     // Test GetEnumerator
     foreach (var item in rd)
     {
         Assert.IsTrue(users.Any(u => u.Id == item.Key));
     }
     // Test Count
     Assert.AreEqual(users.Count, rd.Count);
     // Test ContainsKey
     Assert.IsTrue(rd.ContainsKey(users[1].Id));
     // Test Contains
     Assert.IsTrue(rd.Contains(new KeyValuePair<int, User>(users.Last().Id, users.Last())));
     // Test Add
     rd.Add(0, new User() {Id = 0});
     Assert.AreEqual(users.Count + 1, rd.Count);
     Assert.AreEqual(0, rd[0].Id);
     // Test Remove
     rd.Remove(0);
     Assert.IsFalse(rd.ContainsKey(0));
     // Test Keys
     foreach (var k in rd.Keys)
     {
         Assert.IsTrue(users.Any(u => u.Id == k));
     }
     // Test Values
     foreach (var u in rd.Values)
     {
         Assert.IsTrue(users.Any(user => user.Id == u.Id));
     }
     // Test TryGetValue
     User userTest = new User();
     bool b = rd.TryGetValue(999, out userTest);
     Assert.IsFalse(b);
     Assert.IsNull(userTest);
     b = rd.TryGetValue(1, out userTest);
     Assert.IsTrue(b);
     Assert.AreEqual(1, userTest.Id);
     // Test CopyTo
     var array = new KeyValuePair<int, User>[50];
     rd.CopyTo(array, 10);
     Assert.AreEqual(users.Count, array.Count(x => x.Value != null));
     // Test Clear
     rd.Clear();
     Assert.AreEqual(0, rd.Count);
 }
 public void UT_Cache_RawOverrideSerializer_object()
 {
     var raw = new RawSerializer();
     raw.SetSerializerFor<object>(o => Encoding.UTF8.GetBytes(o.GetHashCode().ToString()),
         b => int.Parse(Encoding.UTF8.GetString(b)));
     var ctx = new Context(Common.Config, raw);
     Thread.Sleep(1000);
     string key = "UT_Cache_RawOverrideSerializer_object";
     ctx.Cache.Remove(new[] { key });
     User usr = new User();
     ctx.Cache.SetObject<object>(key, usr);
     var v = ctx.Cache.GetObject<object>(key);
     Assert.AreEqual(usr.GetHashCode(), v);
 }
 private User GetValue(int i)
 {
     var user = new User()
     {
         Id = i,
         Deparments = new List<Department>()
     };
     for (int j = 0; j < 10; j++)
     {
         user.Deparments.Add(new Department()
         {
             Id = j + 1,
             Distance = (j + 1) * (decimal)Math.PI,
             Location = new Location()
             {
                 Id = (j + 1) * 10,
                 Name = string.Format("{0}", j + 1)
             },
             Size = (j + 1) * 23
         });
     }
     return user;
 }
 private async Task<List<User>> GetUsersAsync()
 {
     var loc1 = new Location()
     {
         Id = 1,
         Name = "one"
     };
     var loc2 = new Location()
     {
         Id = 2,
         Name = "two"
     };
     var user1 = new User()
     {
         Id = 1,
         Deparments = new List<Department>()
         {
             new Department() {Id = 1, Distance = 123.45m, Size = 2, Location = loc1},
             new Department() {Id = 2, Distance = 400, Size = 1, Location = loc2}
         }
     };
     var user2 = new User()
     {
         Id = 2,
         Deparments = new List<Department>()
         {
             new Department() {Id = 3, Distance = 500, Size = 1, Location = loc2},
             new Department() {Id = 4, Distance = 125.5m, Size = 3, Location = loc1}
         }
     };
     return await Task.FromResult<List<User>>(new List<User>() { user1, user2 });
 }