@@ -14,95 +14,90 @@ public class QueryKeyFixture : TestCase
14
14
private readonly SqlString SqlAll =
15
15
new SqlString ( "select entitywith0_.id as id0_, entitywith0_.Description as Descript2_0_, entitywith0_.Value as Value0_ from EntityWithFilters entitywith0_" ) ;
16
16
17
- protected override string MappingsAssembly
18
- {
19
- get { return "NHibernate.Test" ; }
20
- }
17
+ protected override string MappingsAssembly => "NHibernate.Test" ;
21
18
22
- protected override IList Mappings
23
- {
24
- get { return new [ ] { "CacheTest.EntityWithFilters.hbm.xml" } ; }
25
- }
19
+ protected override IList Mappings => new [ ] { "CacheTest.EntityWithFilters.hbm.xml" } ;
26
20
27
21
[ Test ]
28
22
public void EqualityWithFilters ( )
29
23
{
30
- QueryKey qk , qk1 ;
31
- QueryKeyFilterDescLikeToCompare ( out qk , out qk1 ) ;
32
- Assert . That ( qk , Is . EqualTo ( qk1 ) ) ;
24
+ QueryKeyFilterDescLikeToCompare ( out var qk , out var qk1 , true ) ;
25
+ Assert . That ( qk , Is . EqualTo ( qk1 ) , "Like" ) ;
33
26
34
- QueryKeyFilterDescValueToCompare ( out qk , out qk1 ) ;
35
- Assert . That ( qk , Is . EqualTo ( qk1 ) ) ;
27
+ QueryKeyFilterDescValueToCompare ( out qk , out qk1 , true ) ;
28
+ Assert . That ( qk , Is . EqualTo ( qk1 ) , "Value" ) ;
36
29
}
37
30
38
- private void QueryKeyFilterDescLikeToCompare ( out QueryKey qk , out QueryKey qk1 )
31
+ private void QueryKeyFilterDescLikeToCompare ( out QueryKey qk , out QueryKey qk1 , bool sameValue )
39
32
{
40
33
const string filterName = "DescriptionLike" ;
41
34
var f = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
42
35
f . SetParameter ( "pLike" , "so%" ) ;
43
- var fk = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
36
+ var fk = new FilterKey ( f ) ;
44
37
ISet < FilterKey > fks = new HashSet < FilterKey > { fk } ;
45
38
qk = new QueryKey ( Sfi , SqlAll , new QueryParameters ( ) , fks , null ) ;
46
39
47
40
var f1 = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
48
- f1 . SetParameter ( "pLike" , "%ing" ) ;
49
- var fk1 = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
41
+ f1 . SetParameter ( "pLike" , sameValue ? "so%" : "%ing" ) ;
42
+ var fk1 = new FilterKey ( f1 ) ;
50
43
fks = new HashSet < FilterKey > { fk1 } ;
51
44
qk1 = new QueryKey ( Sfi , SqlAll , new QueryParameters ( ) , fks , null ) ;
52
45
}
53
46
54
- private void QueryKeyFilterDescValueToCompare ( out QueryKey qk , out QueryKey qk1 )
47
+ private void QueryKeyFilterDescValueToCompare ( out QueryKey qk , out QueryKey qk1 , bool sameValue )
55
48
{
56
49
const string filterName = "DescriptionEqualAndValueGT" ;
57
50
58
51
var f = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
59
52
f . SetParameter ( "pDesc" , "something" ) . SetParameter ( "pValue" , 10 ) ;
60
- var fk = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
53
+ var fk = new FilterKey ( f ) ;
61
54
ISet < FilterKey > fks = new HashSet < FilterKey > { fk } ;
62
55
qk = new QueryKey ( Sfi , SqlAll , new QueryParameters ( ) , fks , null ) ;
63
56
64
57
var f1 = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
65
- f1 . SetParameter ( "pDesc" , "something" ) . SetParameter ( "pValue" , 11 ) ;
66
- var fk1 = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
58
+ f1 . SetParameter ( "pDesc" , "something" ) . SetParameter ( "pValue" , sameValue ? 10 : 11 ) ;
59
+ var fk1 = new FilterKey ( f1 ) ;
67
60
fks = new HashSet < FilterKey > { fk1 } ;
68
61
qk1 = new QueryKey ( Sfi , SqlAll , new QueryParameters ( ) , fks , null ) ;
69
62
}
70
63
71
64
[ Test ]
72
65
public void NotEqualityWithFilters ( )
73
66
{
74
- QueryKey qk , qk1 ;
75
- QueryKeyFilterDescLikeToCompare ( out qk , out qk1 ) ;
67
+ QueryKeyFilterDescLikeToCompare ( out var qk , out var qk1 , false ) ;
68
+ Assert . That ( qk , Is . Not . EqualTo ( qk1 ) , "qk & qk1" ) ;
76
69
77
- QueryKey qvk , qvk1 ;
78
- QueryKeyFilterDescValueToCompare ( out qvk , out qvk1 ) ;
70
+ QueryKeyFilterDescValueToCompare ( out var qvk , out var qvk1 , false ) ;
71
+ Assert . That ( qvk , Is . Not . EqualTo ( qvk1 ) , "qvk & qvk1" ) ;
79
72
80
- Assert . That ( qk , Is . Not . EqualTo ( qvk ) ) ;
81
- Assert . That ( qk1 , Is . Not . EqualTo ( qvk1 ) ) ;
73
+ Assert . That ( qk , Is . Not . EqualTo ( qvk ) , "qk & qvk" ) ;
74
+ Assert . That ( qk1 , Is . Not . EqualTo ( qvk1 ) , "qk1 & qvk1" ) ;
82
75
}
83
76
84
77
[ Test ]
85
78
public void HashCodeWithFilters ( )
86
79
{
87
- QueryKey qk , qk1 ;
88
- QueryKeyFilterDescLikeToCompare ( out qk , out qk1 ) ;
89
- Assert . That ( qk . GetHashCode ( ) , Is . EqualTo ( qk1 . GetHashCode ( ) ) ) ;
80
+ QueryKeyFilterDescLikeToCompare ( out var qk , out var qk1 , true ) ;
81
+ Assert . That ( qk . GetHashCode ( ) , Is . EqualTo ( qk1 . GetHashCode ( ) ) , "Like" ) ;
90
82
91
- QueryKeyFilterDescValueToCompare ( out qk , out qk1 ) ;
92
- Assert . That ( qk . GetHashCode ( ) , Is . EqualTo ( qk1 . GetHashCode ( ) ) ) ;
83
+ QueryKeyFilterDescValueToCompare ( out qk , out qk1 , true ) ;
84
+ Assert . That ( qk . GetHashCode ( ) , Is . EqualTo ( qk1 . GetHashCode ( ) ) , "Value" ) ;
93
85
}
94
86
95
87
[ Test ]
96
88
public void NotEqualHashCodeWithFilters ( )
97
89
{
98
- QueryKey qk , qk1 ;
99
- QueryKeyFilterDescLikeToCompare ( out qk , out qk1 ) ;
90
+ // GetHashCode semantic does not guarantee no collision may ever occur, but the algorithm should
91
+ // generates different hashcodes for similar but inequal cases. These tests check that cache keys
92
+ // for a query generated for different parameters values are no more equal.
93
+ QueryKeyFilterDescLikeToCompare ( out var qk , out var qk1 , false ) ;
94
+ Assert . That ( qk . GetHashCode ( ) , Is . Not . EqualTo ( qk1 . GetHashCode ( ) ) , "qk & qk1" ) ;
100
95
101
- QueryKey qvk , qvk1 ;
102
- QueryKeyFilterDescValueToCompare ( out qvk , out qvk1 ) ;
96
+ QueryKeyFilterDescValueToCompare ( out var qvk , out var qvk1 , false ) ;
97
+ Assert . That ( qvk . GetHashCode ( ) , Is . Not . EqualTo ( qvk1 . GetHashCode ( ) ) , "qvk & qvk1" ) ;
103
98
104
- Assert . That ( qk . GetHashCode ( ) , Is . Not . EqualTo ( qvk . GetHashCode ( ) ) ) ;
105
- Assert . That ( qk1 . GetHashCode ( ) , Is . Not . EqualTo ( qvk1 . GetHashCode ( ) ) ) ;
99
+ Assert . That ( qk . GetHashCode ( ) , Is . Not . EqualTo ( qvk . GetHashCode ( ) ) , "qk & qvk" ) ;
100
+ Assert . That ( qk1 . GetHashCode ( ) , Is . Not . EqualTo ( qvk1 . GetHashCode ( ) ) , "qk1 & qvk1" ) ;
106
101
}
107
102
108
103
[ Test ]
@@ -111,18 +106,18 @@ public void ToStringWithFilters()
111
106
string filterName = "DescriptionLike" ;
112
107
var f = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
113
108
f . SetParameter ( "pLike" , "so%" ) ;
114
- var fk = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
109
+ var fk = new FilterKey ( f ) ;
115
110
ISet < FilterKey > fks = new HashSet < FilterKey > { fk } ;
116
111
var qk = new QueryKey ( Sfi , SqlAll , new QueryParameters ( ) , fks , null ) ;
117
- Assert . That ( qk . ToString ( ) , Does . Contain ( string . Format ( "filters: ['{0 }']" , fk ) ) ) ;
112
+ Assert . That ( qk . ToString ( ) , Does . Contain ( $ "filters: ['{ fk } ']") , "Like" ) ;
118
113
119
114
filterName = "DescriptionEqualAndValueGT" ;
120
115
f = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
121
116
f . SetParameter ( "pDesc" , "something" ) . SetParameter ( "pValue" , 10 ) ;
122
- fk = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
117
+ fk = new FilterKey ( f ) ;
123
118
fks = new HashSet < FilterKey > { fk } ;
124
119
qk = new QueryKey ( Sfi , SqlAll , new QueryParameters ( ) , fks , null ) ;
125
- Assert . That ( qk . ToString ( ) , Does . Contain ( string . Format ( "filters: ['{0 }']" , fk ) ) ) ;
120
+ Assert . That ( qk . ToString ( ) , Does . Contain ( $ "filters: ['{ fk } ']") , "Value" ) ;
126
121
}
127
122
128
123
[ Test ]
@@ -131,16 +126,16 @@ public void ToStringWithMoreFilters()
131
126
string filterName = "DescriptionLike" ;
132
127
var f = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
133
128
f . SetParameter ( "pLike" , "so%" ) ;
134
- var fk = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
129
+ var fk = new FilterKey ( f ) ;
135
130
136
131
filterName = "DescriptionEqualAndValueGT" ;
137
132
var fv = new FilterImpl ( Sfi . GetFilterDefinition ( filterName ) ) ;
138
133
fv . SetParameter ( "pDesc" , "something" ) . SetParameter ( "pValue" , 10 ) ;
139
- var fvk = new FilterKey ( filterName , f . Parameters , f . FilterDefinition . ParameterTypes ) ;
134
+ var fvk = new FilterKey ( fv ) ;
140
135
141
136
ISet < FilterKey > fks = new HashSet < FilterKey > { fk , fvk } ;
142
137
var qk = new QueryKey ( Sfi , SqlAll , new QueryParameters ( ) , fks , null ) ;
143
- Assert . That ( qk . ToString ( ) , Does . Contain ( string . Format ( "filters: ['{0 }', '{1 }']" , fk , fvk ) ) ) ;
138
+ Assert . That ( qk . ToString ( ) , Does . Contain ( $ "filters: ['{ fk } ', '{ fvk } ']") ) ;
144
139
}
145
140
}
146
141
}
0 commit comments