17
17
using System ;
18
18
using System . Collections . Generic ;
19
19
using System . Linq ;
20
+ using System . Threading ;
20
21
using System . Threading . Tasks ;
21
22
using Cassandra . Data . Linq ;
22
23
using Cassandra . IntegrationTests . Linq . Structures ;
23
24
using Cassandra . IntegrationTests . TestBase ;
24
25
using Cassandra . Mapping ;
25
26
using Cassandra . Tests ;
26
27
using Cassandra . Tests . Mapping . Pocos ;
28
+ using Newtonsoft . Json . Linq ;
29
+
27
30
using NUnit . Framework ;
28
31
29
32
namespace Cassandra . IntegrationTests . Linq
@@ -328,6 +331,43 @@ public void LinqUdt_Where_Contains()
328
331
Assert . AreEqual ( song . Title , recordsArr [ 0 ] . Song . Title ) ;
329
332
}
330
333
334
+ [ Test ]
335
+ public void Linq_Writetime_Returns_ValidResult ( )
336
+ {
337
+ DateTimeOffset UnixStart = new DateTimeOffset ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 , TimeSpan . Zero ) ;
338
+ var ticks = ( DateTime . UtcNow - UnixStart ) . Ticks ;
339
+ var now = ( ticks / TimeSpan . TicksPerMillisecond ) * 1000 ;
340
+ ticks = ( DateTime . UtcNow . AddMinutes ( 5 ) - UnixStart ) . Ticks ;
341
+ var nowPlus5Minutes = ( ticks / TimeSpan . TicksPerMillisecond ) * 1000 ;
342
+ var writetimeEntityTableName = "writetime_entities" ;
343
+ Session . Execute ( $ "CREATE TABLE IF NOT EXISTS { writetimeEntityTableName } (id uuid primary key, propertyint int, propertystring text)") ;
344
+
345
+ var table = new Table < WritetimeEntity > ( Session , new MappingConfiguration ( ) . Define (
346
+ new Map < WritetimeEntity > ( ) . TableName ( writetimeEntityTableName ) ) ) ;
347
+ var id = Guid . NewGuid ( ) ;
348
+ var writetimeEntity = new WritetimeEntity ( )
349
+ {
350
+ Id = id ,
351
+ PropertyInt = 100 ,
352
+ PropertyString = "text" ,
353
+ } ;
354
+ table . Insert ( writetimeEntity ) . Execute ( ) ;
355
+ Thread . Sleep ( 200 ) ;
356
+ table . Where ( sr => sr . Id == id ) . Select ( sr => new WritetimeEntity { PropertyInt = 99 } ) . Update ( ) . Execute ( ) ;
357
+ var records = table
358
+ . Select ( wte => new { Id = wte . Id , wt1 = CqlFunction . WriteTime ( wte . PropertyString ) , wt2 = CqlFunction . WriteTime ( wte . PropertyInt ) } )
359
+ . Where ( wte => wte . Id == id ) . Execute ( ) ;
360
+ Assert . NotNull ( records ) ;
361
+ var recordsArr = records . ToArray ( ) ;
362
+ Assert . AreEqual ( 1 , recordsArr . Length ) ;
363
+ Assert . NotNull ( recordsArr [ 0 ] ) ;
364
+ Assert . GreaterOrEqual ( recordsArr [ 0 ] . wt1 , now ) ;
365
+ Assert . Greater ( recordsArr [ 0 ] . wt2 , now ) ;
366
+ Assert . Greater ( recordsArr [ 0 ] . wt2 , recordsArr [ 0 ] . wt1 ) ;
367
+ Assert . Less ( recordsArr [ 0 ] . wt1 , nowPlus5Minutes ) ;
368
+ Assert . Less ( recordsArr [ 0 ] . wt2 , nowPlus5Minutes ) ;
369
+ }
370
+
331
371
private Table < Album > GetAlbumTable ( )
332
372
{
333
373
return new Table < Album > ( Session , new MappingConfiguration ( ) . Define ( new Map < Album > ( ) . TableName ( _tableNameAlbum ) ) ) ;
@@ -339,5 +379,14 @@ internal class SongRecords
339
379
public Song2 Song { get ; set ; }
340
380
public int Broadcast { get ; set ; }
341
381
}
382
+
383
+ internal class WritetimeEntity
384
+ {
385
+ public Guid Id { get ; set ; }
386
+
387
+ public int PropertyInt { get ; set ; }
388
+
389
+ public string PropertyString { get ; set ; }
390
+ }
342
391
}
343
392
}
0 commit comments