forked from litedb-org/LiteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
litedb-org#1860 enum type in constructor
- Loading branch information
Showing
2 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
using System.Linq; | ||
|
||
namespace LiteDB.Tests.Issues | ||
{ | ||
public class Issue1860_Tests | ||
{ | ||
[Fact] | ||
public void Constructor_has_enum_bsonctor() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
|
||
// Get a collection (or create, if doesn't exist) | ||
var col1 = db.GetCollection<C1>("c1"); | ||
var col3 = db.GetCollection<C3>("c3"); | ||
|
||
var c1 = new C1 | ||
{ | ||
Id = 1, | ||
EnumAB = EnumAB.B | ||
}; | ||
|
||
col1.Insert(c1); | ||
|
||
var c3 = new C3 | ||
( | ||
id: 1, | ||
enumAB: EnumAB.B | ||
); | ||
|
||
col3.Insert(c3); | ||
|
||
var value1 = col1.FindAll().FirstOrDefault(); | ||
Assert.NotNull(value1); | ||
Assert.Equal(c1.EnumAB, value1.EnumAB); | ||
|
||
var value3 = col3.FindAll().FirstOrDefault(); | ||
Assert.NotNull(value3); | ||
Assert.Equal(c3.EnumAB, value3.EnumAB); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_has_enum() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
|
||
// Get a collection (or create, if doesn't exist) | ||
var col1 = db.GetCollection<C1>("c1"); | ||
var col2 = db.GetCollection<C2>("c2"); | ||
|
||
var c1 = new C1 | ||
{ | ||
Id = 1, | ||
EnumAB = EnumAB.B | ||
}; | ||
|
||
col1.Insert(c1); | ||
|
||
var c2 = new C2 | ||
( | ||
id: 1, | ||
enumAB: EnumAB.B | ||
); | ||
|
||
col2.Insert(c2); | ||
|
||
var value1 = col1.FindAll().FirstOrDefault(); | ||
Assert.NotNull(value1); | ||
Assert.Equal(c1.EnumAB, value1.EnumAB); | ||
|
||
var value2 = col2.FindAll().FirstOrDefault(); | ||
Assert.NotNull(value2); | ||
Assert.Equal(c2.EnumAB, value2.EnumAB); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_has_enum_asint() | ||
{ | ||
BsonMapper.Global.EnumAsInteger = true; | ||
using var db = new LiteDatabase(":memory:"); | ||
|
||
// Get a collection (or create, if doesn't exist) | ||
var col1 = db.GetCollection<C1>("c1"); | ||
var col2 = db.GetCollection<C2>("c2"); | ||
|
||
var c1 = new C1 | ||
{ | ||
Id = 1, | ||
EnumAB = EnumAB.B | ||
}; | ||
|
||
col1.Insert(c1); | ||
|
||
var c2 = new C2 | ||
( | ||
id: 1, | ||
enumAB: EnumAB.B | ||
); | ||
|
||
col2.Insert(c2); | ||
|
||
var value1 = col1.FindAll().FirstOrDefault(); | ||
Assert.NotNull(value1); | ||
Assert.Equal(c1.EnumAB, value1.EnumAB); | ||
|
||
var value2 = col2.FindAll().FirstOrDefault(); | ||
Assert.NotNull(value2); | ||
Assert.Equal(c2.EnumAB, value2.EnumAB); | ||
} | ||
|
||
public enum EnumAB | ||
{ | ||
|
||
A = 1, | ||
B = 2, | ||
} | ||
|
||
public class C1 | ||
{ | ||
public int Id { get; set; } | ||
|
||
public EnumAB? EnumAB { get; set; } | ||
} | ||
|
||
public class C2 | ||
{ | ||
public int Id { get; set; } | ||
|
||
public EnumAB EnumAB { get; set; } | ||
|
||
public C2(int id, EnumAB enumAB) | ||
{ | ||
Id = id; | ||
EnumAB = enumAB; | ||
} | ||
} | ||
|
||
public class C3 | ||
{ | ||
public int Id { get; set; } | ||
|
||
public EnumAB EnumAB { get; set; } | ||
|
||
[BsonCtor] | ||
public C3(int id, EnumAB enumAB) | ||
{ | ||
Id = id; | ||
EnumAB = enumAB; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters