22using Xunit . Abstractions ;
33using System . Drawing ;
44using ClipboardR . Core ;
5+ using Dapper ;
6+ using Microsoft . Data . Sqlite ;
7+ using System ;
8+ using System . Globalization ;
59
610namespace ClipboardR . Core . Test ;
711
812public class DbHelperTest
913{
1014 private static string _defaultIconPath = "Images/clipboard.png" ;
1115
12- private readonly ClipboardData TestRecord = new ( )
13- {
14- Text = "" ,
15- Type = CbMonitor . ContentTypes . Image ,
16- Data = ( object ) "Test Text" ,
17- SenderApp = "Source.exe" ,
18- DisplayTitle = "Text Test" ,
19- IconPath = _defaultIconPath ,
20- Icon = new BitmapImage ( new Uri ( _defaultIconPath , UriKind . RelativeOrAbsolute ) ) ,
21- PreviewImagePath = _defaultIconPath ,
22- Score = 241 ,
23- InitScore = 1 ,
24- Time = DateTime . Now ,
25- Pined = false ,
26- CreateTime = DateTime . Now ,
27- } ;
16+ private readonly Image _defaultImage = new Bitmap ( _defaultIconPath ) ;
17+
18+ private readonly ClipboardData TestRecord =
19+ new ( )
20+ {
21+ HashId = Utils . GetGuid ( ) ,
22+ Text = "Text" ,
23+ Type = CbContentType . Text ,
24+ Data = "Test Data" ,
25+ SenderApp = "Source.exe" ,
26+ DisplayTitle = "Test Display Title" ,
27+ IconPath = _defaultIconPath ,
28+ Icon = new BitmapImage ( new Uri ( _defaultIconPath , UriKind . RelativeOrAbsolute ) ) ,
29+ PreviewImagePath = _defaultIconPath ,
30+ Score = 241 ,
31+ InitScore = 1 ,
32+ Time = DateTime . Now ,
33+ Pined = false ,
34+ CreateTime = DateTime . Now ,
35+ } ;
2836
2937 private readonly ITestOutputHelper _testOutputHelper ;
3038
@@ -33,54 +41,90 @@ public DbHelperTest(ITestOutputHelper testOutputHelper)
3341 _testOutputHelper = testOutputHelper ;
3442 }
3543
44+ public ClipboardData GetRandomClipboardData ( )
45+ {
46+ var rand = new Random ( ) ;
47+ var data =
48+ new ClipboardData ( )
49+ {
50+ HashId = Utils . GetGuid ( ) ,
51+ Text = Utils . RandomString ( 10 ) ,
52+ Type = ( CbContentType ) rand . Next ( 3 ) ,
53+ Data = Utils . RandomString ( 10 ) ,
54+ SenderApp = Utils . RandomString ( 5 ) + ".exe" ,
55+ DisplayTitle = Utils . RandomString ( 10 ) ,
56+ IconPath = _defaultIconPath ,
57+ Icon = new BitmapImage ( new Uri ( _defaultIconPath , UriKind . RelativeOrAbsolute ) ) ,
58+ PreviewImagePath = _defaultIconPath ,
59+ Score = rand . Next ( 1000 ) ,
60+ InitScore = rand . Next ( 1000 ) ,
61+ Time = DateTime . Now ,
62+ Pined = false ,
63+ CreateTime = DateTime . Now ,
64+ } ;
65+ if ( data . Type == CbContentType . Image )
66+ data . Data = _defaultImage ;
67+ else if ( data . Type == CbContentType . Files )
68+ data . Data = new string [ ] { Utils . RandomString ( 10 ) , Utils . RandomString ( 10 ) } ;
69+ return data ;
70+ }
71+
3672 [ Fact ]
3773 public void TestCreateDb ( )
3874 {
39- var helper = new DbHelper ( "Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared" ) ;
75+ var helper = new DbHelper ( "TestDb" , mode : SqliteOpenMode . Memory , cache : SqliteCacheMode . Shared ) ;
76+ _testOutputHelper . WriteLine ( helper . Connection . ConnectionString ) ;
4077 helper . CreateDb ( ) ;
41- var command = helper . Connection . CreateCommand ( ) ;
42- command . CommandText = "SELECT name from sqlite_master WHERE name='record'" ;
43- Assert . True ( command . ExecuteScalar ( ) is string name && name . ToString ( ) == "record" ) ;
78+ var sql = @"SELECT name from sqlite_master WHERE name IN ('record', 'assets') ORDER BY name ASC;" ;
79+ var r = helper . Connection . Query ( sql ) . AsList ( ) ;
80+ Assert . True ( r . Count == 2 && r [ 0 ] . name == "assets" && r [ 1 ] . name == "record" ) ;
81+ helper . Close ( ) ;
4482 }
4583
4684 [ Fact ]
47- public void TestInsertRecord ( )
85+ public async void TestInsertRecord ( )
4886 {
4987 // test text
50- var exampleTextRecord = TestRecord ;
51- var helper = new DbHelper ( "Data Source=InMemoryTestDb;Mode= Memory;Cache= Shared" ) ;
88+ var exampleTextRecord = GetRandomClipboardData ( ) ;
89+ var helper = new DbHelper ( "TestDb" , mode : SqliteOpenMode . Memory , cache : SqliteCacheMode . Shared ) ;
5290 helper . CreateDb ( ) ;
5391 helper . AddOneRecord ( exampleTextRecord ) ;
54- var c = helper . GetAllRecord ( ) . First ( ) ;
92+ var c = ( await helper . GetAllRecord ( ) ) . First ( ) ;
5593 helper . Close ( ) ;
56- Assert . True ( c . Equals ( exampleTextRecord ) ) ;
94+ Assert . True ( c == exampleTextRecord ) ;
5795 }
5896
5997 [ Theory ]
60- [ InlineData ( 0 , "2023-06-01" , 3 ) ]
61- [ InlineData ( 1 , "2023-06-01" , 24 ) ]
62- [ InlineData ( 2 , "2023-06-01" , 72 ) ]
63- public void TestDeleteRecordBefore ( int type , string creatTime , uint keepTime )
98+ [ InlineData ( 0 , "2023-05-28 11:35:00.1+08:00" , 3 ) ]
99+ [ InlineData ( 0 , "2023-05-28 11:35:00.1+08:00" , 3600 ) ]
100+ [ InlineData ( 1 , "2023-05-28 11:35:00.1+08:00" , 24 ) ]
101+ [ InlineData ( 1 , "2023-05-28 11:35:00.1+08:00" , 3600 ) ]
102+ [ InlineData ( 2 , "2023-05-28 11:35:00.1+08:00" , 72 ) ]
103+ public async void TestDeleteRecordBefore ( int type , string creatTime , int keepTime )
64104 {
65- var exampleTextRecord = TestRecord ;
66- exampleTextRecord . CreateTime = DateTime . Parse ( creatTime ) ;
67- var helper = new DbHelper ( "Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared" ) ;
105+ var helper = new DbHelper ( "TestDb" , mode : SqliteOpenMode . Memory , cache : SqliteCacheMode . Shared ) ;
68106 helper . CreateDb ( ) ;
107+ var now = DateTime . Now ;
108+ var ctime = DateTime . ParseExact (
109+ creatTime , "yyyy-MM-dd HH:mm:ss.fzzz" , CultureInfo . CurrentCulture ) ;
69110 var spans = Enumerable . Range ( 0 , 5000 ) . Skip ( 12 ) . Select ( i => TimeSpan . FromHours ( i ) ) ;
70- var rng = new Random ( ) ;
71111 foreach ( var s in spans )
72112 {
73- var tmpRecord = exampleTextRecord ;
74- tmpRecord . Type = ( CbMonitor . ContentTypes ) rng . Next ( 0 , 4 ) ;
113+ var tmpRecord = GetRandomClipboardData ( ) ;
114+ tmpRecord . CreateTime = ctime + s ;
75115 helper . AddOneRecord ( tmpRecord ) ;
76116 }
117+ // helper.Connection.BackupDatabase(new SqliteConnection("Data Source=a.db"));
77118
78119 helper . DeleteRecordByKeepTime ( type , keepTime ) ;
79-
80- var recordsAfterDelete = helper . GetAllRecord ( ) ;
81- foreach ( var record in recordsAfterDelete )
120+
121+ var recordsAfterDelete = await helper . GetAllRecord ( ) ;
122+ foreach ( var record in recordsAfterDelete . Where ( r => r . Type == ( CbContentType ) type ) )
82123 {
83- Assert . True ( record . CreateTime + TimeSpan . FromDays ( keepTime / 24 ) >= DateTime . Now ) ;
124+ var expTime = record . CreateTime + TimeSpan . FromHours ( keepTime ) ;
125+ if ( expTime < now )
126+ _testOutputHelper . WriteLine ( $ "{ record } ") ;
127+ Assert . True ( expTime >= now ) ;
84128 }
85129 helper . Close ( ) ;
86130 }
0 commit comments