|
| 1 | +using WebExpress.WebIndex.Memory; |
| 2 | +using WebExpress.WebIndex.Test.Document; |
| 3 | +using WebExpress.WebIndex.Test.Fixture; |
| 4 | +using Xunit.Abstractions; |
| 5 | + |
| 6 | +namespace WebExpress.WebIndex.Test.DocumentStore |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Test class for testing the memory-based document store. |
| 10 | + /// </summary> |
| 11 | + public class UnitTestDocumentStoreMemoryA : UnitTestDocumentStore<UnitTestIndexFixtureIndexA> |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Constructor |
| 15 | + /// </summary> |
| 16 | + /// <param name="fixture">The log.</param> |
| 17 | + /// <param name="output">The test context.</param> |
| 18 | + public UnitTestDocumentStoreMemoryA(UnitTestIndexFixtureIndexA fixture, ITestOutputHelper output) |
| 19 | + : base(fixture, output) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates a document store. |
| 25 | + /// </summary> |
| 26 | + [Fact] |
| 27 | + public void Create() |
| 28 | + { |
| 29 | + // preconditions |
| 30 | + var context = new IndexContext(); |
| 31 | + |
| 32 | + // test execution |
| 33 | + var documentStore = new IndexMemoryDocumentStore<UnitTestIndexTestDocumentA>(context, 5); |
| 34 | + |
| 35 | + // postconditions |
| 36 | + documentStore.Dispose(); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Adds items to a document store. |
| 41 | + /// </summary> |
| 42 | + [Fact] |
| 43 | + public void Add() |
| 44 | + { |
| 45 | + // preconditions |
| 46 | + Preconditions(); |
| 47 | + var documentStore = new IndexMemoryDocumentStore<UnitTestIndexTestDocumentA>(Context, 5); |
| 48 | + |
| 49 | + documentStore.Clear(); |
| 50 | + |
| 51 | + // test execution |
| 52 | + documentStore.Add(Fixture.TestData[0]); |
| 53 | + documentStore.Add(Fixture.TestData[1]); |
| 54 | + |
| 55 | + var i = documentStore.GetItem(Fixture.TestData[0].Id); |
| 56 | + |
| 57 | + Assert.True(i != null && i.Id == Fixture.TestData[0].Id); |
| 58 | + |
| 59 | + // postconditions |
| 60 | + documentStore.Dispose(); |
| 61 | + Postconditions(); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Update an entry in the reverse index where the item has a first name change. |
| 66 | + /// </summary> |
| 67 | + [Fact] |
| 68 | + public void UpdateWithChange() |
| 69 | + { |
| 70 | + // preconditions |
| 71 | + Preconditions(); |
| 72 | + var documentStore = new IndexMemoryDocumentStore<UnitTestIndexTestDocumentA>(Context, 5); |
| 73 | + |
| 74 | + documentStore.Clear(); |
| 75 | + documentStore.Add(Fixture.TestData[0]); |
| 76 | + documentStore.Add(Fixture.TestData[1]); |
| 77 | + |
| 78 | + var name = "Update_" + Fixture.TestData[0].Text; |
| 79 | + var changed = new UnitTestIndexTestDocumentA |
| 80 | + { |
| 81 | + Id = Fixture.TestData[0].Id, |
| 82 | + Text = name, |
| 83 | + }; |
| 84 | + |
| 85 | + // test execution |
| 86 | + documentStore.Update(changed); |
| 87 | + |
| 88 | + var all = documentStore.All; |
| 89 | + |
| 90 | + Assert.Equal(all.Select(x => x.Id).OrderBy(x => x), Fixture.TestData.Take(2).Select(x => x.Id).OrderBy(x => x)); |
| 91 | + Assert.True(all.Where(x => x.Text == name).Any()); |
| 92 | + |
| 93 | + // postconditions |
| 94 | + documentStore.Dispose(); |
| 95 | + Postconditions(); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Changes an entry in the reverse index without the element to be changed having any changes. |
| 100 | + /// </summary> |
| 101 | + [Fact] |
| 102 | + public void UpdateWithoutChanges() |
| 103 | + { |
| 104 | + // preconditions |
| 105 | + Preconditions(); |
| 106 | + var documentStore = new IndexMemoryDocumentStore<UnitTestIndexTestDocumentA>(Context, 5); |
| 107 | + |
| 108 | + documentStore.Clear(); |
| 109 | + documentStore.Add(Fixture.TestData[0]); |
| 110 | + documentStore.Add(Fixture.TestData[1]); |
| 111 | + |
| 112 | + // test execution |
| 113 | + documentStore.Update(Fixture.TestData[0]); |
| 114 | + var all = documentStore.All; |
| 115 | + |
| 116 | + Assert.Equal(all.Select(x => x.Id).OrderBy(x => x), Fixture.TestData.Take(2).Select(x => x.Id).OrderBy(x => x)); |
| 117 | + Assert.True(all.Where(x => x.Text == Fixture.TestData[0].Text).Any()); |
| 118 | + |
| 119 | + // postconditions |
| 120 | + documentStore.Dispose(); |
| 121 | + Postconditions(); |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Removes an entry from the document store. |
| 126 | + /// </summary> |
| 127 | + [Fact] |
| 128 | + public void Remove() |
| 129 | + { |
| 130 | + // preconditions |
| 131 | + Preconditions(); |
| 132 | + var documentStore = new IndexMemoryDocumentStore<UnitTestIndexTestDocumentA>(Context, 5); |
| 133 | + |
| 134 | + documentStore.Clear(); |
| 135 | + documentStore.Add(Fixture.TestData[0]); |
| 136 | + documentStore.Add(Fixture.TestData[1]); |
| 137 | + |
| 138 | + // test execution |
| 139 | + documentStore.Delete(Fixture.TestData[0]); |
| 140 | + var all = documentStore.All; |
| 141 | + |
| 142 | + Assert.Equal(all.Select(x => x.Id).OrderBy(x => x), Fixture.TestData.Where(x => x.Id == Fixture.TestData[1].Id).Select(x => x.Id)); |
| 143 | + |
| 144 | + // postconditions |
| 145 | + documentStore.Dispose(); |
| 146 | + Postconditions(); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Retrieve a entry of the reverse index. |
| 151 | + /// </summary> |
| 152 | + [Fact] |
| 153 | + public void Retrieve() |
| 154 | + { |
| 155 | + // preconditions |
| 156 | + Preconditions(); |
| 157 | + var documentStore = new IndexMemoryDocumentStore<UnitTestIndexTestDocumentA>(Context, 5); |
| 158 | + |
| 159 | + documentStore.Clear(); |
| 160 | + documentStore.Add(Fixture.TestData[0]); |
| 161 | + documentStore.Add(Fixture.TestData[1]); |
| 162 | + |
| 163 | + // test execution |
| 164 | + var item = documentStore.GetItem(Fixture.TestData[0].Id); |
| 165 | + |
| 166 | + Assert.NotNull(documentStore); |
| 167 | + Assert.NotNull(item); |
| 168 | + |
| 169 | + // postconditions |
| 170 | + documentStore.Dispose(); |
| 171 | + Postconditions(); |
| 172 | + } |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Return all entries of the document store. |
| 176 | + /// </summary> |
| 177 | + [Fact] |
| 178 | + public void All() |
| 179 | + { |
| 180 | + // preconditions |
| 181 | + Preconditions(); |
| 182 | + var documentStore = new IndexMemoryDocumentStore<UnitTestIndexTestDocumentA>(Context, 5); |
| 183 | + |
| 184 | + documentStore.Clear(); |
| 185 | + foreach (var item in Fixture.TestData) |
| 186 | + { |
| 187 | + documentStore.Add(item); |
| 188 | + } |
| 189 | + |
| 190 | + // test execution |
| 191 | + var all = documentStore.All; |
| 192 | + |
| 193 | + Assert.True(all.Select(x => x.Id).OrderBy(x => x).SequenceEqual(Fixture.TestData.Select(x => x.Id).OrderBy(x => x))); |
| 194 | + |
| 195 | + // postconditions |
| 196 | + documentStore.Dispose(); |
| 197 | + Postconditions(); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments