Skip to content

Commit e17d8f3

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
ColumnProperty extension tests
1 parent 4b75e5c commit e17d8f3

File tree

3 files changed

+93
-52
lines changed

3 files changed

+93
-52
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Migrator.Framework;
2+
using NUnit.Framework;
3+
using DotNetProjects.Migrator.Framework;
4+
using System;
5+
using System.Linq;
6+
7+
namespace Migrator.Tests.Framework.ColumnProperties;
8+
9+
public class ColumnPropertyExtensionsTests
10+
{
11+
[Test]
12+
public void Clear()
13+
{
14+
// Arrange
15+
var columnProperty = ColumnProperty.PrimaryKey | ColumnProperty.NotNull;
16+
17+
// Act
18+
columnProperty = columnProperty.Clear(ColumnProperty.PrimaryKey);
19+
20+
// Assert
21+
Assert.That(columnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
22+
}
23+
24+
[Test]
25+
public void IsSet()
26+
{
27+
// Arrange
28+
var columnProperty = ColumnProperty.PrimaryKeyWithIdentity | ColumnProperty.NotNull;
29+
30+
// Act
31+
var actualData = GetAllSingleColumnProperties().Select(x => new
32+
{
33+
ColumnPropertyString = x.ToString(),
34+
IsSet = columnProperty.IsSet(x),
35+
IsNotSet = columnProperty.IsNotSet(x)
36+
})
37+
.ToList();
38+
39+
// Assert
40+
string[] expectedSet = [nameof(ColumnProperty.PrimaryKey), nameof(ColumnProperty.NotNull), nameof(ColumnProperty.Identity)];
41+
var actualDataShouldBeTrue = actualData.Where(x => expectedSet.Any(y => y == x.ColumnPropertyString)).ToList();
42+
var actualDataShouldBeFalse = actualData.Where(x => !expectedSet.Any(y => y == x.ColumnPropertyString)).ToList();
43+
44+
Assert.That(actualDataShouldBeTrue.Select(x => x.IsSet), Has.All.True);
45+
Assert.That(actualDataShouldBeFalse.Select(x => x.IsSet), Has.All.False);
46+
}
47+
48+
[Test]
49+
public void IsNotSet()
50+
{
51+
// Arrange
52+
var columnProperty = ColumnProperty.PrimaryKeyWithIdentity | ColumnProperty.NotNull;
53+
54+
// Act
55+
var actualData = GetAllSingleColumnProperties().Select(x => new
56+
{
57+
ColumnPropertyString = x.ToString(),
58+
IsSet = columnProperty.IsNotSet(x),
59+
IsNotSet = columnProperty.IsNotSet(x)
60+
})
61+
.ToList();
62+
63+
// Assert
64+
string[] expectedSet = [nameof(ColumnProperty.PrimaryKey), nameof(ColumnProperty.NotNull), nameof(ColumnProperty.Identity)];
65+
var actualDataShouldBeFalse = actualData.Where(x => expectedSet.Any(y => y == x.ColumnPropertyString)).ToList();
66+
var actualDataShouldBeTrue = actualData.Where(x => !expectedSet.Any(y => y == x.ColumnPropertyString)).ToList();
67+
68+
Assert.That(actualDataShouldBeTrue.Select(x => x.IsNotSet), Has.All.True);
69+
Assert.That(actualDataShouldBeFalse.Select(x => x.IsNotSet), Has.All.False);
70+
}
71+
72+
[Test]
73+
public void Set_Success()
74+
{
75+
// Arrange
76+
var columnProperty = ColumnProperty.NotNull;
77+
78+
// Act
79+
var result = columnProperty.Set(ColumnProperty.PrimaryKeyWithIdentity);
80+
81+
// Assert
82+
var expected = ColumnProperty.NotNull | ColumnProperty.PrimaryKeyWithIdentity;
83+
84+
Assert.That(result, Is.EqualTo(expected));
85+
}
86+
87+
private ColumnProperty[] GetAllSingleColumnProperties()
88+
{
89+
return [.. Enum.GetValues<ColumnProperty>().Where(x => x == 0 || (x & (x - 1)) == 0)];
90+
}
91+
}

src/Migrator.Tests/Framework/ColumnProperties/ColumnPropertyTests.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Migrator/Framework/ColumnPropertyExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public static class ColumnPropertyExtensions
66
{
77
public static bool IsSet(this ColumnProperty columnProperty, ColumnProperty flags)
88
{
9-
return (columnProperty & flags) == flags;
9+
return flags != ColumnProperty.None && (columnProperty & flags) == flags;
1010
}
1111

1212
public static bool IsNotSet(this ColumnProperty columnProperty, ColumnProperty flags)
1313
{
14-
return (columnProperty & (~flags)) == 0;
14+
return (columnProperty & flags) == 0;
1515
}
1616

1717
public static ColumnProperty Set(this ColumnProperty columnProperty, ColumnProperty flags)

0 commit comments

Comments
 (0)