1
+ using ServiceStack . DataAnnotations ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using NUnit . Framework ;
5
+ using ServiceStack . Text ;
6
+
7
+ namespace ServiceStack . OrmLite . Tests . Issues
8
+ {
9
+ /// <summary>This test set is used to demonstrate that the OrmLite Load methods are unexpectedly case-sensitive for
10
+ /// string-based columns. A reference will not be loaded if there is a difference in case between otherwise matching parent
11
+ /// value and child values. For example, parent table has RegionCode = "WEST" while it's related lookup table Region has
12
+ /// RegionCode = "West".</summary>
13
+ /// <remarks>Target database must be setup as case-insensitive or this exercise is pointless. Refer to custom
14
+ /// openDbConnection method included in this test set.</remarks>
15
+ public class CustomerWithRegion
16
+ {
17
+ [ AutoIncrement ]
18
+ public int Id { get ; set ; }
19
+
20
+ public string Name { get ; set ; }
21
+
22
+ [ CustomField ( "VARCHAR(8000) COLLATE Latin1_General_CI_AS" ) ]
23
+ public string RegionId { get ; set ; }
24
+
25
+ [ Reference ]
26
+ public Region RegionDetail { get ; set ; }
27
+ }
28
+
29
+ public class Region
30
+ {
31
+ [ PrimaryKey ]
32
+ [ CustomField ( "VARCHAR(8000) COLLATE Latin1_General_CI_AS" ) ]
33
+ public string Id { get ; set ; }
34
+
35
+ public string RegionName { get ; set ; }
36
+ }
37
+
38
+ public class LoadReferencesCaseSensitiveTest : OrmLiteTestBase
39
+ {
40
+ const string regionCode = "West" ;
41
+ const string regionName = "Western Region" ;
42
+
43
+ /// <summary>This test is used to demonstrate that the OrmLite Load methods are unexpectedly case-sensitive for
44
+ /// string-based columns. A reference will not be loaded if there is a difference in case between otherwise matching parent
45
+ /// value and child values. For example, parent table has RegionCode = "WEST" while it's related lookup table Region has
46
+ /// RegionCode = "West".</summary>
47
+ [ Test ]
48
+ public void LoadReference_with_Case_Variance ( )
49
+ {
50
+ if ( Dialect != Dialect . SqlServer ) return ;
51
+ OrmLiteConfig . IsCaseInsensitive = true ;
52
+
53
+ using ( var db = OpenDbConnection ( ) )
54
+ {
55
+ db . DropAndCreateTable < CustomerWithRegion > ( ) ;
56
+ //db.GetLastSql().Print();
57
+ db . DropAndCreateTable < Region > ( ) ;
58
+ //db.GetLastSql().Print();
59
+
60
+ var region = new Region { Id = regionCode , RegionName = regionName } ;
61
+ db . Save ( region ) ;
62
+
63
+ var caseInsensitiveRegion = db . Single < Region > ( x => x . Id == regionCode . ToUpper ( ) ) ;
64
+ Assert . That ( caseInsensitiveRegion . Id , Is . EqualTo ( regionCode ) ) ;
65
+
66
+ var customers = new List < CustomerWithRegion >
67
+ {
68
+ new CustomerWithRegion { Name = "Acme Anvil Co." , RegionId = regionCode } ,
69
+ new CustomerWithRegion { Name = "Penny's Poodle Emporium" , RegionId = regionCode . ToUpper ( ) } ,
70
+ } ;
71
+
72
+ foreach ( var customer in customers )
73
+ {
74
+ db . Save ( customer ) ;
75
+
76
+ var dbCustomer = db . LoadSelect < CustomerWithRegion > ( s => s . Name == customer . Name ) . FirstOrDefault ( ) ;
77
+
78
+ dbCustomer . PrintDump ( ) ;
79
+
80
+ Assert . That (
81
+ dbCustomer . RegionId ,
82
+ Is . Not . Null ,
83
+ string . Format ( "Region code missing for {0}" , customer . Name ) ) ;
84
+ Assert . That (
85
+ dbCustomer . RegionId . ToLower ( ) == regionCode . ToLower ( ) ,
86
+ string . Format ( "Region code incorrect for {0}" , customer . Name ) ) ;
87
+
88
+ // The following assertion will fail because LoadSelect considers CustomWithRegion.RegionCode of "WEST" != to Region.RegionCode of "West".
89
+ Assert . That (
90
+ dbCustomer . RegionDetail ,
91
+ Is . Not . Null ,
92
+ string . Format ( "Region detail record missing for {0}" , customer . Name ) ) ;
93
+ Assert . That (
94
+ dbCustomer . RegionDetail . RegionName == regionName ,
95
+ string . Format ( "Region name incorrect for {0}" , customer . Name ) ) ;
96
+ }
97
+ }
98
+
99
+ OrmLiteConfig . IsCaseInsensitive = false ;
100
+ }
101
+ }
102
+ }
0 commit comments