1
+ using System ;
2
+ using System . Data ;
3
+ using System . Linq ;
4
+ using NHibernate . Cfg . MappingSchema ;
5
+ using NHibernate . Linq ;
6
+ using NHibernate . Mapping . ByCode ;
7
+ using NHibernate . SqlTypes ;
8
+ using NHibernate . UserTypes ;
9
+ using NUnit . Framework ;
10
+
11
+ namespace NHibernate . Test . NHSpecificTest . NH2839
12
+ {
13
+ class Entity
14
+ {
15
+ public virtual Guid Id { get ; set ; }
16
+ public virtual string Name { get ; set ; }
17
+ public virtual bool IsActive { get ; set ; }
18
+ }
19
+
20
+ public class MyBooleanType : IEnhancedUserType
21
+ {
22
+ public SqlType [ ] SqlTypes
23
+ {
24
+ get { return new [ ] { SqlTypeFactory . Int32 } ; }
25
+ }
26
+
27
+ public System . Type ReturnedType
28
+ {
29
+ get { return typeof ( bool ) ; }
30
+ }
31
+
32
+ public bool Equals ( object x , object y )
33
+ {
34
+ if ( x == y ) return true ;
35
+
36
+ if ( x is bool && y is bool )
37
+ return ( bool ) x == ( bool ) y ;
38
+ return false ;
39
+ }
40
+
41
+ public int GetHashCode ( object x )
42
+ {
43
+ return x . GetHashCode ( ) ;
44
+ }
45
+
46
+ public object NullSafeGet ( IDataReader rs , string [ ] names , object owner )
47
+ {
48
+ var ordinal = rs . GetOrdinal ( names [ 0 ] ) ;
49
+ if ( rs . IsDBNull ( ordinal ) )
50
+ return false ;
51
+ return rs . GetInt32 ( ordinal ) == 1 ;
52
+ }
53
+
54
+ public void NullSafeSet ( IDbCommand cmd , object value , int index )
55
+ {
56
+ ( ( IDbDataParameter ) cmd . Parameters [ index ] ) . Value = ( ( bool ) value ) ? 1 : - 1 ;
57
+ }
58
+
59
+ public object DeepCopy ( object value )
60
+ {
61
+ return ( bool ) value ;
62
+ }
63
+
64
+ public bool IsMutable
65
+ {
66
+ get { return false ; }
67
+ }
68
+
69
+ public object Replace ( object original , object target , object owner )
70
+ {
71
+ return original ;
72
+ }
73
+
74
+ public object Assemble ( object cached , object owner )
75
+ {
76
+ return cached ;
77
+ }
78
+
79
+ public object Disassemble ( object value )
80
+ {
81
+ return value ;
82
+ }
83
+
84
+ public object FromXMLString ( string xml )
85
+ {
86
+ return xml ;
87
+ }
88
+
89
+ public string ObjectToSQLString ( object value )
90
+ {
91
+ return ( ( bool ) value ) ? "1" : "-1" ;
92
+ }
93
+
94
+ public string ToXMLString ( object value )
95
+ {
96
+ return ( ( bool ) value ) ? "1" : "-1" ;
97
+ }
98
+ }
99
+
100
+ public class FixtureByCode : TestCaseMappingByCode
101
+ {
102
+ protected override HbmMapping GetMappings ( )
103
+ {
104
+ var mapper = new ModelMapper ( ) ;
105
+ mapper . Class < Entity > ( rc =>
106
+ {
107
+ rc . Id ( x => x . Id , m => m . Generator ( Generators . GuidComb ) ) ;
108
+ rc . Property ( x => x . Name ) ;
109
+ rc . Property ( x => x . IsActive , pm=> pm . Type < MyBooleanType > ( ) ) ;
110
+ } ) ;
111
+
112
+ return mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
113
+ }
114
+
115
+ protected override void OnSetUp ( )
116
+ {
117
+ using ( ISession session = OpenSession ( ) )
118
+ using ( ITransaction transaction = session . BeginTransaction ( ) )
119
+ {
120
+ var e1 = new Entity { Name = "Bob" , IsActive = true } ;
121
+ session . Save ( e1 ) ;
122
+
123
+ var e2 = new Entity { Name = "Sally" , IsActive = false } ;
124
+ session . Save ( e2 ) ;
125
+
126
+ session . Flush ( ) ;
127
+ transaction . Commit ( ) ;
128
+ }
129
+ }
130
+
131
+ protected override void OnTearDown ( )
132
+ {
133
+ using ( ISession session = OpenSession ( ) )
134
+ using ( ITransaction transaction = session . BeginTransaction ( ) )
135
+ {
136
+ session . Delete ( "from System.Object" ) ;
137
+
138
+ session . Flush ( ) ;
139
+ transaction . Commit ( ) ;
140
+ }
141
+ }
142
+
143
+ [ Test ]
144
+ public void YourTestName ( )
145
+ {
146
+ using ( ISession session = OpenSession ( ) )
147
+ using ( session . BeginTransaction ( ) )
148
+ {
149
+ var result = from e in session . Query < Entity > ( )
150
+ where e . IsActive
151
+ select e ;
152
+
153
+ Assert . AreEqual ( 1 , result . ToList ( ) . Count ) ;
154
+ }
155
+ }
156
+ }
157
+ }
0 commit comments