Skip to content

Commit e96289a

Browse files
committed
test up
1 parent 459e2a3 commit e96289a

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

.github/workflows/pr.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ jobs:
1010
- uses: actions/setup-dotnet@v1
1111
- uses: actions/checkout@v2
1212
- run: dotnet build --configuration Release
13-
- run: |
13+
- name: unit tests
14+
run: |
1415
cd ${{ github.workspace }}/mongo-declarative-indexes.Tests
1516
dotnet test --configuration Release --no-build
1617

mongo-declarative-indexes.Tests/IndexEnsurerShould.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,25 @@ public void Ensure_DropsExtraAndCreatesMissingIndexes()
7676
{"name", "field_1"},
7777
{"ns", "test.collections"}
7878
};
79+
const string remainingFieldName = "remaining_field";
7980
var remainingDbIndex = new Dictionary<string, object>
8081
{
8182
{"v", 2},
82-
{"key", new Dictionary<string, object> {{"another_field", 1}}},
83-
{"name", "another_field_1"},
83+
{"key", new Dictionary<string, object> {{remainingFieldName, 1}}},
84+
{"name", $"{remainingFieldName}_1"},
8485
{"ns", "test.collections"}
8586
};
86-
database.ListCollectionNames().Returns(new[] {"collectionName"});
87-
database.ListIndexes("collectionName").Returns(new[] {extraIndex, remainingDbIndex});
88-
87+
const string collectionName = "collectionName";
88+
database.ListCollectionNames().Returns(new[] {collectionName});
89+
database.ListIndexes(collectionName).Returns(new[] {extraIndex, remainingDbIndex});
8990

9091
var expectedCreatedIndexes = new[] {new Index(keys: new Key("yet_another_field", IndexType.Descending))};
9192
var ensurer = new IndexEnsurer(database);
92-
var remainingIndex = new Index(keys: new Key("another_field", IndexType.Ascending));
93-
ensurer.Ensure(new CollectionIndexes("testCollection",
93+
var remainingIndex = new Index(keys: new Key(remainingFieldName, IndexType.Ascending));
94+
ensurer.Ensure(new CollectionIndexes(collectionName,
9495
expectedCreatedIndexes.Append(remainingIndex).ToArray()));
95-
database.Received().DropOneIndex("collectionName", "field_1");
96-
database.Received().CreateManyIndexes("testCollection",
96+
database.Received().DropOneIndex(collectionName, "field_1");
97+
database.Received().CreateManyIndexes(collectionName,
9798
Arg.Is<Index[]>(actualIndexes =>
9899
actualIndexes
99100
.SequenceEqual(expectedCreatedIndexes)));

mongo-declarative-indexes/Index.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ public Key(string field, IndexType indexType)
6262
public string Field { get; }
6363

6464
public IndexType IndexType { get; }
65+
66+
protected bool Equals(Key other)
67+
{
68+
return Field == other.Field && IndexType == other.IndexType;
69+
}
70+
71+
public override bool Equals(object obj)
72+
{
73+
if (ReferenceEquals(null, obj)) return false;
74+
if (ReferenceEquals(this, obj)) return true;
75+
return obj.GetType() == GetType() && Equals((Key) obj);
76+
}
77+
78+
public override int GetHashCode()
79+
{
80+
unchecked
81+
{
82+
return ((Field != null ? Field.GetHashCode() : 0) * 397) ^ (int) IndexType;
83+
}
84+
}
6585
}
6686

6787
public enum IndexType

mongo-declarative-indexes/IndexEnsurer.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class IndexEnsurer
77
{
88
private readonly IDatabase _database;
99

10+
private static readonly IndexIgnoringNameEqualityComparer IndexIgnoringNameEqualityComparer =
11+
new IndexIgnoringNameEqualityComparer();
12+
1013
public IndexEnsurer(IDatabase database)
1114
{
1215
_database = database;
@@ -26,7 +29,7 @@ public EnsurerContinuation Begin(IEnumerable<CollectionIndexes> indexes)
2629
!targetIndexesByCollectionName
2730
.ContainsKey(i.CollectionName) ||
2831
!targetIndexesByCollectionName[i.CollectionName]
29-
.Contains(x)).ToArray())).ToList();
32+
.Contains(x, IndexIgnoringNameEqualityComparer)).ToArray())).ToList();
3033

3134
foreach (var collectionIndex in extraIndexes)
3235
foreach (var index in collectionIndex.Indexes)
@@ -64,7 +67,28 @@ private static Index[] GetMissingIndexes(IReadOnlyDictionary<string, Index[]> ex
6467
{
6568
return collectionIndex.Indexes
6669
.Where(i => !existingIndexesByCollectionName.ContainsKey(collectionIndex.CollectionName) ||
67-
!existingIndexesByCollectionName[collectionIndex.CollectionName].Contains(i)).ToArray();
70+
!existingIndexesByCollectionName[collectionIndex.CollectionName]
71+
.Contains(i, IndexIgnoringNameEqualityComparer)).ToArray();
72+
}
73+
}
74+
75+
internal class IndexIgnoringNameEqualityComparer : IEqualityComparer<Index>
76+
{
77+
public bool Equals(Index x, Index y)
78+
{
79+
if (ReferenceEquals(x, y)) return true;
80+
if (ReferenceEquals(x, null)) return false;
81+
if (ReferenceEquals(y, null)) return false;
82+
if (x.GetType() != y.GetType()) return false;
83+
return x.Keys.SequenceEqual(y.Keys) && x.Unique == y.Unique;
84+
}
85+
86+
public int GetHashCode(Index obj)
87+
{
88+
unchecked
89+
{
90+
return ((obj.Keys != null ? obj.Keys.GetHashCode() : 0) * 397) ^ obj.Unique.GetHashCode();
91+
}
6892
}
6993
}
7094

0 commit comments

Comments
 (0)