1
+ using System ;
2
+ using System . Data . SqlClient ;
3
+ using NUnit . Framework ;
4
+ using ServiceStack . DataAnnotations ;
5
+
6
+ namespace ServiceStack . OrmLite . SqlServerTests . TableOptions
7
+ {
8
+ [ TestFixture ]
9
+ public class SqlServer2014MemoryOptimizedAttributeTests : SqlServer2014TableOptionsOrmLiteTestBase
10
+ {
11
+ [ TestFixtureSetUp ]
12
+ public void Setup ( )
13
+ {
14
+ base . TestFixtureSetUp ( ) ;
15
+
16
+ if ( Db . TableExists < TypeWithMemTableNoDurability > ( ) )
17
+ Db . DropTable < TypeWithMemTableNoDurability > ( ) ;
18
+
19
+ if ( Db . TableExists < TypeWithMemTableSchemaOnlyDurability > ( ) )
20
+ Db . DropTable < TypeWithMemTableSchemaOnlyDurability > ( ) ;
21
+
22
+ if ( Db . TableExists < TypeWithMemTableSchemaAndDataDurability > ( ) )
23
+ Db . DropTable < TypeWithMemTableSchemaAndDataDurability > ( ) ;
24
+
25
+ if ( Db . TableExists < TypeWithMemTableWithCollatedHashIndex > ( ) )
26
+ Db . DropTable < TypeWithMemTableWithCollatedHashIndex > ( ) ;
27
+ }
28
+
29
+ [ Test ]
30
+ public void Can_Create_Memory_Optimized_Table ( )
31
+ {
32
+ Db . CreateTable < TypeWithMemTableNoDurability > ( true ) ;
33
+
34
+ var sql = Db . GetLastSql ( ) ;
35
+
36
+ Assert . IsTrue ( sql . Contains ( "MEMORY_OPTIMIZED = ON" ) ) ;
37
+ Assert . IsFalse ( sql . Contains ( "DURABILITY = SCHEMA_ONLY" ) ) ;
38
+ Assert . IsFalse ( sql . Contains ( "DURABILITY = SCHEMA_AND_DATA" ) ) ;
39
+
40
+ var name = "Test 1" ;
41
+ var id = Db . Insert ( new TypeWithMemTableNoDurability { Name = name } , selectIdentity : true ) ;
42
+ var fromDb = Db . SingleById < TypeWithMemTableNoDurability > ( id ) ;
43
+
44
+ Assert . AreEqual ( name , fromDb . Name ) ;
45
+ }
46
+
47
+ [ Test ]
48
+ public void Can_Create_Memory_Optimized_Table_With_Schema_Only_Durability ( )
49
+ {
50
+ Db . CreateTable < TypeWithMemTableSchemaOnlyDurability > ( true ) ;
51
+
52
+ var sql = Db . GetLastSql ( ) ;
53
+
54
+ Assert . IsTrue ( sql . Contains ( "MEMORY_OPTIMIZED = ON" ) ) ;
55
+ Assert . IsTrue ( sql . Contains ( "DURABILITY = SCHEMA_ONLY" ) ) ;
56
+ Assert . IsFalse ( sql . Contains ( "DURABILITY = SCHEMA_AND_DATA" ) ) ;
57
+
58
+ var name = "Test 2" ;
59
+ var id = Db . Insert ( new TypeWithMemTableSchemaOnlyDurability { Name = name } , selectIdentity : true ) ;
60
+ var fromDb = Db . SingleById < TypeWithMemTableSchemaOnlyDurability > ( id ) ;
61
+
62
+ Assert . AreEqual ( name , fromDb . Name ) ;
63
+ }
64
+
65
+ [ Test ]
66
+ public void Can_Create_Memory_Optimized_Table_With_Schema_And_Data_Durability ( )
67
+ {
68
+ Db . CreateTable < TypeWithMemTableSchemaAndDataDurability > ( true ) ;
69
+
70
+ var sql = Db . GetLastSql ( ) ;
71
+
72
+ Assert . IsTrue ( sql . Contains ( "MEMORY_OPTIMIZED = ON" ) ) ;
73
+ Assert . IsFalse ( sql . Contains ( "DURABILITY = SCHEMA_ONLY" ) ) ;
74
+ Assert . IsTrue ( sql . Contains ( "DURABILITY = SCHEMA_AND_DATA" ) ) ;
75
+
76
+ var name = "Test 3" ;
77
+ var id = Db . Insert ( new TypeWithMemTableSchemaAndDataDurability { Name = name } , selectIdentity : true ) ;
78
+ var fromDb = Db . SingleById < TypeWithMemTableSchemaAndDataDurability > ( id ) ;
79
+
80
+ Assert . AreEqual ( name , fromDb . Name ) ;
81
+ }
82
+
83
+ [ Test ]
84
+ public void Can_not_Create_Memory_Optimized_Table_Without_BIN2_Collate_Hash_Index ( )
85
+ {
86
+ try
87
+ {
88
+ Db . CreateTable < TypeWithMemTableWithoutBIN2HashIndex > ( true ) ;
89
+ }
90
+ catch ( Exception ex )
91
+ {
92
+ Assert . IsInstanceOf < SqlException > ( ex ) ;
93
+ }
94
+ }
95
+
96
+ [ Test ]
97
+ public void Can_Create_Memory_Optimized_Table_With_Collated_Hash_Index ( )
98
+ {
99
+ var name = "Test 5" ;
100
+ var index = "Test Index" ;
101
+
102
+ Db . CreateTable < TypeWithMemTableWithCollatedHashIndex > ( true ) ;
103
+
104
+ var sql = Db . GetLastSql ( ) ;
105
+
106
+ Assert . IsTrue ( sql . Contains ( "MEMORY_OPTIMIZED = ON" ) ) ;
107
+ Assert . IsTrue ( sql . Contains ( "COLLATE" ) ) ;
108
+
109
+ var id = Db . Insert ( new TypeWithMemTableWithCollatedHashIndex { Name = name , Index = index } , selectIdentity : true ) ;
110
+ var fromDb = Db . SingleById < TypeWithMemTableWithCollatedHashIndex > ( id ) ;
111
+
112
+ Assert . AreEqual ( name , fromDb . Name ) ;
113
+ Assert . AreEqual ( index , fromDb . Index ) ;
114
+ }
115
+ }
116
+
117
+ public class TypeWithNoMemOptimization
118
+ {
119
+ [ PrimaryKey ]
120
+ public int Id { get ; set ; }
121
+
122
+ public string Name { get ; set ; }
123
+ }
124
+
125
+ [ SqlServerMemoryOptimized ]
126
+ public class TypeWithMemTableNoDurability : TypeWithNoMemOptimization { }
127
+
128
+ [ SqlServerMemoryOptimized ( SqlServerDurability . SchemaOnly ) ]
129
+ public class TypeWithMemTableSchemaOnlyDurability : TypeWithNoMemOptimization { }
130
+
131
+ [ SqlServerMemoryOptimized ( SqlServerDurability . SchemaAndData ) ]
132
+ public class TypeWithMemTableSchemaAndDataDurability : TypeWithNoMemOptimization { }
133
+
134
+ [ SqlServerMemoryOptimized ]
135
+ public class TypeWithMemTableWithoutBIN2HashIndex : TypeWithNoMemOptimization
136
+ {
137
+ [ StringLength ( 25 ) ]
138
+ [ SqlServerBucketCount ( 1000 ) ]
139
+ public string Index { get ; set ; }
140
+ }
141
+
142
+ [ SqlServerMemoryOptimized ]
143
+ public class TypeWithMemTableWithCollatedHashIndex : TypeWithNoMemOptimization
144
+ {
145
+ [ StringLength ( 25 ) ]
146
+ [ SqlServerCollate ( "Latin1_General_100_BIN2" ) ]
147
+ [ SqlServerBucketCount ( 1000 ) ]
148
+ public string Index { get ; set ; }
149
+ }
150
+ }
0 commit comments