1- using FluentAssertions ;
1+ using FluentAssertions ;
22
33using LiteDB . Engine ;
44
@@ -38,10 +38,24 @@ public void DocumentUpgrade_Test()
3838
3939 ms . Position = 0 ;
4040
41- var engine = new LiteEngine ( new EngineSettings
41+ using var engine = new LiteEngine ( new EngineSettings
4242 {
4343 DataStream = ms ,
44- ReadTransform = ReadTransform
44+ ReadTransform = ( collectionName , val ) =>
45+ {
46+ if ( val is not BsonDocument doc )
47+ {
48+ return val ;
49+ }
50+
51+ if ( doc . TryGetValue ( "version" , out var version ) && version . AsInt32 == 1 )
52+ {
53+ doc [ "version" ] = 2 ;
54+ doc [ "age" ] = 30 ;
55+ }
56+
57+ return val ;
58+ }
4559 } ) ;
4660
4761 using ( var db = new LiteDatabase ( engine ) )
@@ -58,19 +72,62 @@ public void DocumentUpgrade_Test()
5872 }
5973 }
6074
61- private BsonValue ReadTransform ( string arg1 , BsonValue val )
75+ [ Fact ]
76+ public void DocumentUpgrade_BsonMapper_Test ( )
6277 {
63- if ( ! ( val is BsonDocument bdoc ) )
78+ var ms = new MemoryStream ( ) ;
79+ using ( var db = new LiteDatabase ( ms ) )
6480 {
65- return val ;
81+ var col = db . GetCollection ( "col" ) ;
82+
83+ col . Insert ( new BsonDocument { [ "version" ] = 1 , [ "_id" ] = 1 , [ "name" ] = "John" } ) ;
6684 }
6785
68- if ( bdoc . TryGetValue ( "version" , out var version ) && version . AsInt32 == 1 )
86+ ms . Position = 0 ;
87+
88+ using ( var db = new LiteDatabase ( ms ) )
6989 {
70- bdoc [ "version" ] = 2 ;
71- bdoc [ "age" ] = 30 ;
90+ var col = db . GetCollection ( "col" ) ;
91+
92+ col . Count ( ) . Should ( ) . Be ( 1 ) ;
93+
94+ var doc = col . FindById ( 1 ) ;
95+
96+ doc [ "version" ] . AsInt32 . Should ( ) . Be ( 1 ) ;
97+ doc [ "name" ] . AsString . Should ( ) . Be ( "John" ) ;
98+ doc [ "age" ] . AsInt32 . Should ( ) . Be ( 0 ) ;
7299 }
73100
74- return val ;
101+ ms . Position = 0 ;
102+
103+ var mapper = new BsonMapper ( ) ;
104+ mapper . OnDeserialization = ( sender , type , val ) =>
105+ {
106+ if ( val is not BsonDocument doc )
107+ {
108+ return val ;
109+ }
110+
111+ if ( doc . TryGetValue ( "version" , out var version ) && version . AsInt32 == 1 )
112+ {
113+ doc [ "version" ] = 2 ;
114+ doc [ "age" ] = 30 ;
115+ }
116+
117+ return doc ;
118+ } ;
119+
120+ using ( var db = new LiteDatabase ( ms , mapper ) )
121+ {
122+ var col = db . GetCollection ( "col" ) ;
123+
124+ col . Count ( ) . Should ( ) . Be ( 1 ) ;
125+
126+ var doc = col . FindById ( 1 ) ;
127+
128+ doc [ "version" ] . AsInt32 . Should ( ) . Be ( 2 ) ;
129+ doc [ "name" ] . AsString . Should ( ) . Be ( "John" ) ;
130+ doc [ "age" ] . AsInt32 . Should ( ) . Be ( 30 ) ;
131+ }
75132 }
76133}
0 commit comments