1- using FluentAssertions ;
1+ using System . ComponentModel . DataAnnotations ;
2+ using System . ComponentModel . DataAnnotations . Schema ;
3+ using FluentAssertions ;
24using Xunit ;
35
46namespace LiteDbX . Tests . Mapper ;
@@ -28,14 +30,77 @@ public void Custom_Id_In_Interface()
2830
2931 var obj = new ConcreteClass { CustomId = "myid" , Name = "myname" } ;
3032 var doc = mapper . Serialize ( obj ) as BsonDocument ;
31- doc [ "_id" ] . Should ( ) . NotBeNull ( ) ;
33+ Assert . NotNull ( doc ) ;
34+ doc ! [ "_id" ] . Should ( ) . NotBeNull ( ) ;
3235 doc [ "_id" ] . Should ( ) . Be ( "myid" ) ;
3336 doc [ "CustomName" ] . Should ( ) . NotBe ( BsonValue . Null ) ;
3437 doc [ "CustomName" ] . Should ( ) . Be ( "myname" ) ;
3538 doc [ "Name" ] . Should ( ) . Be ( BsonValue . Null ) ;
3639 doc . Keys . ExpectCount ( 2 ) ;
3740 }
3841
42+ [ Fact ]
43+ public void Key_Attribute_Is_Mapped_As_Id_With_AutoId_Disabled ( )
44+ {
45+ var mapper = new BsonMapper ( ) ;
46+
47+ var entityMapper = mapper . GetEntityMapper ( typeof ( UserWithKeyAttribute ) ) ;
48+
49+ entityMapper . Id . Should ( ) . NotBeNull ( ) ;
50+ entityMapper . Id . MemberName . Should ( ) . Be ( nameof ( UserWithKeyAttribute . Key ) ) ;
51+ entityMapper . Id . FieldName . Should ( ) . Be ( "_id" ) ;
52+ entityMapper . Id . AutoId . Should ( ) . BeFalse ( ) ;
53+ }
54+
55+ [ Fact ]
56+ public void Key_Attribute_Serializes_And_Deserializes_As_Id ( )
57+ {
58+ var mapper = new BsonMapper ( ) ;
59+
60+ var doc = mapper . Serialize ( new UserWithKeyAttribute ( 42 , "John" ) ) as BsonDocument ;
61+
62+ Assert . NotNull ( doc ) ;
63+ doc ! [ "_id" ] . Should ( ) . Be ( 42 ) ;
64+ doc [ "Name" ] . Should ( ) . Be ( "John" ) ;
65+ doc . Keys . ExpectCount ( 2 ) ;
66+
67+ var hydrated = mapper . ToObject < UserWithKeyAttribute > ( new BsonDocument { [ "_id" ] = 7 , [ "Name" ] = "Jane" } ) ;
68+
69+ hydrated . Key . Should ( ) . Be ( 7 ) ;
70+ hydrated . Name . Should ( ) . Be ( "Jane" ) ;
71+ }
72+
73+ [ Fact ]
74+ public void NotMapped_Attribute_Is_Ignored_During_Mapping_And_Serialization ( )
75+ {
76+ var mapper = new BsonMapper ( ) ;
77+
78+ var entityMapper = mapper . GetEntityMapper ( typeof ( ClassWithNotMappedAttribute ) ) ;
79+ entityMapper . Members . Should ( ) . NotContain ( x => x . MemberName == nameof ( ClassWithNotMappedAttribute . Ignore ) ) ;
80+
81+ var doc = mapper . Serialize ( new ClassWithNotMappedAttribute { Id = 1 , Keep = "K" , Ignore = "I" } ) as BsonDocument ;
82+
83+ Assert . NotNull ( doc ) ;
84+ doc ! [ "_id" ] . Should ( ) . Be ( 1 ) ;
85+ doc [ "Keep" ] . Should ( ) . Be ( "K" ) ;
86+ doc . ContainsKey ( "Ignore" ) . Should ( ) . BeFalse ( ) ;
87+ doc . Keys . ExpectCount ( 2 ) ;
88+ }
89+
90+ [ Fact ]
91+ public void NotMapped_Attribute_On_Inherited_Property_Is_Ignored ( )
92+ {
93+ var mapper = new BsonMapper ( ) ;
94+
95+ var doc = mapper . Serialize ( new DerivedClassWithInheritedNotMapped { Id = 9 , Keep = "keep" , Hidden = "secret" } ) as BsonDocument ;
96+
97+ Assert . NotNull ( doc ) ;
98+ doc ! [ "_id" ] . Should ( ) . Be ( 9 ) ;
99+ doc [ "Keep" ] . Should ( ) . Be ( "keep" ) ;
100+ doc . ContainsKey ( "Hidden" ) . Should ( ) . BeFalse ( ) ;
101+ doc . Keys . ExpectCount ( 2 ) ;
102+ }
103+
39104 public class UserWithCustomId
40105 {
41106 public UserWithCustomId ( int key , string name )
@@ -58,4 +123,41 @@ public abstract class BaseClass
58123 }
59124
60125 public class ConcreteClass : BaseClass { }
126+
127+ public class UserWithKeyAttribute
128+ {
129+ public UserWithKeyAttribute ( int key , string name )
130+ {
131+ Key = key ;
132+ Name = name ;
133+ }
134+
135+ [ Key ]
136+ public int Key { get ; }
137+
138+ public string Name { get ; }
139+ }
140+
141+ public class ClassWithNotMappedAttribute
142+ {
143+ public int Id { get ; set ; }
144+
145+ public string Keep { get ; set ; }
146+
147+ [ NotMapped ]
148+ public string Ignore { get ; set ; }
149+ }
150+
151+ public abstract class BaseClassWithNotMapped
152+ {
153+ public int Id { get ; set ; }
154+
155+ [ NotMapped ]
156+ public string Hidden { get ; set ; }
157+ }
158+
159+ public class DerivedClassWithInheritedNotMapped : BaseClassWithNotMapped
160+ {
161+ public string Keep { get ; set ; }
162+ }
61163}
0 commit comments