|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using FMData.Rest.Tests.TestModels; |
| 7 | +using RichardSzalay.MockHttp; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace FMData.Rest.Tests |
| 11 | +{ |
| 12 | + public class EditDeleteGetByIdTests |
| 13 | + { |
| 14 | + private static readonly string Server = "http://localhost"; |
| 15 | + private static readonly string File = "test-file"; |
| 16 | + private static readonly string Layout = "Users"; |
| 17 | + |
| 18 | + private static ConnectionInfo TestConnection => new ConnectionInfo |
| 19 | + { |
| 20 | + FmsUri = Server, |
| 21 | + Database = File, |
| 22 | + Username = "unit", |
| 23 | + Password = "test" |
| 24 | + }; |
| 25 | + |
| 26 | + private static MockHttpMessageHandler CreateMock() |
| 27 | + { |
| 28 | + var mockHttp = new MockHttpMessageHandler(); |
| 29 | + |
| 30 | + mockHttp.When(HttpMethod.Post, $"{Server}/fmi/data/v1/databases/{File}/sessions") |
| 31 | + .Respond("application/json", DataApiResponses.SuccessfulAuthentication()); |
| 32 | + |
| 33 | + mockHttp.When(HttpMethod.Delete, $"{Server}/fmi/data/v1/databases/{File}/sessions*") |
| 34 | + .Respond(HttpStatusCode.OK, "application/json", ""); |
| 35 | + |
| 36 | + return mockHttp; |
| 37 | + } |
| 38 | + |
| 39 | + #region EditAsync Overloads |
| 40 | + |
| 41 | + [Fact(DisplayName = "EditAsync(int, T) passthrough should succeed")] |
| 42 | + public async Task EditAsync_RecordIdAndModel_ShouldSucceed() |
| 43 | + { |
| 44 | + var mockHttp = CreateMock(); |
| 45 | + |
| 46 | + mockHttp.When(new HttpMethod("PATCH"), $"{Server}/fmi/data/v1/databases/{File}/layouts/{Layout}/records/42") |
| 47 | + .Respond("application/json", DataApiResponses.SuccessfulEdit()); |
| 48 | + |
| 49 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 50 | + |
| 51 | + var response = await client.EditAsync(42, new User { Name = "updated" }); |
| 52 | + |
| 53 | + Assert.NotNull(response); |
| 54 | + Assert.Contains(response.Messages, r => r.Message == "OK"); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact(DisplayName = "EditAsync(int, T, bool, bool) includes null values when flag set")] |
| 58 | + public async Task EditAsync_WithNullValueFlag_ShouldSucceed() |
| 59 | + { |
| 60 | + var mockHttp = CreateMock(); |
| 61 | + |
| 62 | + mockHttp.When(new HttpMethod("PATCH"), $"{Server}/fmi/data/v1/databases/{File}/layouts/{Layout}/records/42") |
| 63 | + .Respond("application/json", DataApiResponses.SuccessfulEdit()); |
| 64 | + |
| 65 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 66 | + |
| 67 | + var response = await client.EditAsync(42, new User { Name = "updated", ForeignKeyId = null }, |
| 68 | + includeNullValues: true, includeDefaultValues: true); |
| 69 | + |
| 70 | + Assert.NotNull(response); |
| 71 | + Assert.Contains(response.Messages, r => r.Message == "OK"); |
| 72 | + } |
| 73 | + |
| 74 | + #endregion |
| 75 | + |
| 76 | + #region GetByFileMakerIdAsync Overloads |
| 77 | + |
| 78 | + [Fact(DisplayName = "GetByFileMakerIdAsync(int) infers layout from type")] |
| 79 | + public async Task GetByFileMakerIdAsync_InfersLayout() |
| 80 | + { |
| 81 | + var mockHttp = CreateMock(); |
| 82 | + |
| 83 | + mockHttp.When(HttpMethod.Get, $"{Server}/fmi/data/v1/databases/{File}/layouts/{Layout}/records/4") |
| 84 | + .Respond("application/json", DataApiResponses.SuccessfulGetById(4)); |
| 85 | + |
| 86 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 87 | + |
| 88 | + var result = await client.GetByFileMakerIdAsync<User>(4, fmId: null); |
| 89 | + |
| 90 | + Assert.NotNull(result); |
| 91 | + Assert.Equal("fuzzzerd", result.Name); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact(DisplayName = "GetByFileMakerIdAsync with fmIdFunc maps RecordId")] |
| 95 | + public async Task GetByFileMakerIdAsync_WithFmIdFunc_MapsRecordId() |
| 96 | + { |
| 97 | + var mockHttp = CreateMock(); |
| 98 | + |
| 99 | + mockHttp.When(HttpMethod.Get, $"{Server}/fmi/data/v1/databases/{File}/layouts/{Layout}/records/4") |
| 100 | + .Respond("application/json", DataApiResponses.SuccessfulGetById(4)); |
| 101 | + |
| 102 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 103 | + |
| 104 | + var result = await client.GetByFileMakerIdAsync<User>(4, |
| 105 | + fmId: (o, id) => o.FileMakerRecordId = id); |
| 106 | + |
| 107 | + Assert.NotNull(result); |
| 108 | + Assert.Equal(4, result.FileMakerRecordId); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact(DisplayName = "GetByFileMakerIdAsync with both mappers maps RecordId and ModId")] |
| 112 | + public async Task GetByFileMakerIdAsync_WithBothMappers_MapsBothIds() |
| 113 | + { |
| 114 | + var mockHttp = CreateMock(); |
| 115 | + |
| 116 | + mockHttp.When(HttpMethod.Get, $"{Server}/fmi/data/v1/databases/{File}/layouts/{Layout}/records/4") |
| 117 | + .Respond("application/json", DataApiResponses.SuccessfulGetById(4)); |
| 118 | + |
| 119 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 120 | + |
| 121 | + var result = await client.GetByFileMakerIdAsync<User>(4, |
| 122 | + fmId: (o, id) => o.FileMakerRecordId = id, |
| 123 | + fmMod: (o, modId) => o.FileMakerModId = modId); |
| 124 | + |
| 125 | + Assert.NotNull(result); |
| 126 | + Assert.Equal(4, result.FileMakerRecordId); |
| 127 | + Assert.Equal(0, result.FileMakerModId); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact(DisplayName = "GetByFileMakerIdAsync with explicit layout works")] |
| 131 | + public async Task GetByFileMakerIdAsync_WithExplicitLayout() |
| 132 | + { |
| 133 | + var mockHttp = CreateMock(); |
| 134 | + |
| 135 | + mockHttp.When(HttpMethod.Get, $"{Server}/fmi/data/v1/databases/{File}/layouts/CustomLayout/records/7") |
| 136 | + .Respond("application/json", DataApiResponses.SuccessfulGetById(7)); |
| 137 | + |
| 138 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 139 | + |
| 140 | + var result = await client.GetByFileMakerIdAsync<User>("CustomLayout", 7, |
| 141 | + fmId: (o, id) => o.FileMakerRecordId = id); |
| 142 | + |
| 143 | + Assert.NotNull(result); |
| 144 | + Assert.Equal(7, result.FileMakerRecordId); |
| 145 | + } |
| 146 | + |
| 147 | + #endregion |
| 148 | + |
| 149 | + #region ProcessContainers (batch) |
| 150 | + |
| 151 | + [Fact(DisplayName = "ProcessContainers processes multiple models")] |
| 152 | + public async Task ProcessContainers_ProcessesMultipleModels() |
| 153 | + { |
| 154 | + var mockHttp = CreateMock(); |
| 155 | + |
| 156 | + // container URLs need to be valid URIs |
| 157 | + var url1 = $"{Server}/container1.jpg"; |
| 158 | + var url2 = $"{Server}/container2.jpg"; |
| 159 | + |
| 160 | + mockHttp.When(HttpMethod.Get, url1) |
| 161 | + .Respond("application/octet-stream", new System.IO.MemoryStream(new byte[] { 1, 2, 3 })); |
| 162 | + |
| 163 | + mockHttp.When(HttpMethod.Get, url2) |
| 164 | + .Respond("application/octet-stream", new System.IO.MemoryStream(new byte[] { 4, 5, 6 })); |
| 165 | + |
| 166 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 167 | + |
| 168 | + var models = new[] |
| 169 | + { |
| 170 | + new ContainerFieldTestModel { SomeContainerField = url1 }, |
| 171 | + new ContainerFieldTestModel { SomeContainerField = url2 } |
| 172 | + }; |
| 173 | + |
| 174 | + await client.ProcessContainers(models); |
| 175 | + |
| 176 | + Assert.NotNull(models[0].SomeContainerFieldData); |
| 177 | + Assert.NotNull(models[1].SomeContainerFieldData); |
| 178 | + } |
| 179 | + |
| 180 | + #endregion |
| 181 | + |
| 182 | + #region Obsolete Metadata Methods with Database Validation |
| 183 | + |
| 184 | + [Fact(DisplayName = "GetLayoutsAsync(database) throws when database doesn't match")] |
| 185 | + public async Task GetLayoutsAsync_WithWrongDatabase_Throws() |
| 186 | + { |
| 187 | + var mockHttp = CreateMock(); |
| 188 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 189 | + |
| 190 | +#pragma warning disable CS0618 // intentionally testing the obsolete overload |
| 191 | + await Assert.ThrowsAsync<ArgumentOutOfRangeException>( |
| 192 | + () => client.GetLayoutsAsync("wrong-database")); |
| 193 | +#pragma warning restore CS0618 |
| 194 | + } |
| 195 | + |
| 196 | + [Fact(DisplayName = "GetScriptsAsync(database) throws when database doesn't match")] |
| 197 | + public async Task GetScriptsAsync_WithWrongDatabase_Throws() |
| 198 | + { |
| 199 | + var mockHttp = CreateMock(); |
| 200 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 201 | + |
| 202 | +#pragma warning disable CS0618 // intentionally testing the obsolete overload |
| 203 | + await Assert.ThrowsAsync<ArgumentOutOfRangeException>( |
| 204 | + () => client.GetScriptsAsync("wrong-database")); |
| 205 | +#pragma warning restore CS0618 |
| 206 | + } |
| 207 | + |
| 208 | + [Fact(DisplayName = "GetLayoutAsync(database, layout) throws when database doesn't match")] |
| 209 | + public async Task GetLayoutAsync_WithWrongDatabase_Throws() |
| 210 | + { |
| 211 | + var mockHttp = CreateMock(); |
| 212 | + using var client = new FileMakerRestClient(mockHttp.ToHttpClient(), TestConnection); |
| 213 | + |
| 214 | +#pragma warning disable CS0618 // intentionally testing the obsolete overload |
| 215 | + await Assert.ThrowsAsync<ArgumentOutOfRangeException>( |
| 216 | + () => client.GetLayoutAsync("wrong-database", "layout")); |
| 217 | +#pragma warning restore CS0618 |
| 218 | + } |
| 219 | + |
| 220 | + #endregion |
| 221 | + } |
| 222 | +} |
0 commit comments