1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using NUnit . Framework ;
4
+
5
+ namespace ServiceStack . Redis . Tests . Examples
6
+ {
7
+ [ TestFixture , Explicit ]
8
+ public class TestData
9
+ : RedisClientTestsBase
10
+ {
11
+ public class Article
12
+ {
13
+ public int Id { get ; set ; }
14
+ public string Title { get ; set ; }
15
+ public DateTime ModifiedDate { get ; set ; }
16
+ }
17
+
18
+ [ Test ]
19
+ public void Create_test_data_for_all_types ( )
20
+ {
21
+ AddLists ( ) ;
22
+ AddSets ( ) ;
23
+ AddSortedSets ( ) ;
24
+ AddHashes ( ) ;
25
+ }
26
+
27
+ private void AddLists ( )
28
+ {
29
+ var storeMembers = new List < string > { "one" , "two" , "three" , "four" } ;
30
+ storeMembers . ForEach ( x => Redis . AddItemToList ( "testlist" , x ) ) ;
31
+ }
32
+
33
+ private void AddSets ( )
34
+ {
35
+ var storeMembers = new List < string > { "one" , "two" , "three" , "four" } ;
36
+ storeMembers . ForEach ( x => Redis . AddItemToSet ( "testset" , x ) ) ;
37
+ }
38
+
39
+ private void AddHashes ( )
40
+ {
41
+ var stringMap = new Dictionary < string , string > {
42
+ { "one" , "a" } , { "two" , "b" } , { "three" , "c" } , { "four" , "d" }
43
+ } ;
44
+ var stringIntMap = new Dictionary < string , int > {
45
+ { "one" , 1 } , { "two" , 2 } , { "three" , 3 } , { "four" , 4 }
46
+ } ;
47
+
48
+ stringMap . Each ( x => Redis . SetEntryInHash ( "testhash" , x . Key , x . Value ) ) ;
49
+
50
+ var hash = Redis . Hashes [ "testhash" ] ;
51
+ stringIntMap . Each ( x => hash . Add ( x . Key , x . Value . ToString ( ) ) ) ;
52
+ }
53
+
54
+ private void AddSortedSets ( )
55
+ {
56
+ var i = 0 ;
57
+ var storeMembers = new List < string > { "one" , "two" , "three" , "four" } ;
58
+ storeMembers . ForEach ( x => Redis . AddItemToSortedSet ( "testzset" , x , i ++ ) ) ;
59
+
60
+ var redisArticles = Redis . As < Article > ( ) ;
61
+
62
+ var articles = new [ ]
63
+ {
64
+ new Article { Id = 1 , Title = "Article 1" , ModifiedDate = new DateTime ( 2015 , 01 , 02 ) } ,
65
+ new Article { Id = 2 , Title = "Article 2" , ModifiedDate = new DateTime ( 2015 , 01 , 01 ) } ,
66
+ new Article { Id = 3 , Title = "Article 3" , ModifiedDate = new DateTime ( 2015 , 01 , 03 ) } ,
67
+ } ;
68
+
69
+ redisArticles . StoreAll ( articles ) ;
70
+
71
+ const string LatestArticlesSet = "urn:Article:modified" ;
72
+
73
+ foreach ( var article in articles )
74
+ {
75
+ Redis . AddItemToSortedSet ( LatestArticlesSet , article . Id . ToString ( ) , article . ModifiedDate . Ticks ) ;
76
+ }
77
+ }
78
+ }
79
+ }
0 commit comments