Skip to content

Commit f26c7c4

Browse files
committed
test(IHasMeta): implementing IHasMeta includes meta
1 parent 211c0e5 commit f26c7c4

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/JsonApiDotNetCoreExample/Models/Person.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Collections.Generic;
22
using JsonApiDotNetCore.Internal;
33
using JsonApiDotNetCore.Models;
4+
using JsonApiDotNetCore.Services;
45

56
namespace JsonApiDotNetCoreExample.Models
67
{
7-
public class Person : Identifiable<int>
8+
public class Person : Identifiable<int>, IHasMeta
89
{
910
public override int Id { get; set; }
1011

@@ -15,5 +16,13 @@ public class Person : Identifiable<int>
1516
public string LastName { get; set; }
1617

1718
public virtual List<TodoItem> TodoItems { get; set; }
19+
20+
public Dictionary<string, object> GetMeta(IJsonApiContext context)
21+
{
22+
return new Dictionary<string, object> {
23+
{ "copyright", "Copyright 2015 Example Corp." },
24+
{ "authors", new string[] { "Jared Nance" } }
25+
};
26+
}
1827
}
1928
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/Meta.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using JsonApiDotNetCoreExample.Data;
1313
using System.Linq;
1414
using JsonApiDotNetCoreExampleTests.Startups;
15+
using JsonApiDotNetCoreExample.Models;
16+
using System.Collections;
1517

1618
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec.DocumentTests
1719
{
@@ -50,5 +52,46 @@ public async Task Total_Record_Count_Included()
5052
Assert.NotNull(documents.Meta);
5153
Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
5254
}
55+
56+
[Fact]
57+
public async Task EntityThatImplements_IHasMeta_Contains_MetaData()
58+
{
59+
// arrange
60+
var person = new Person();
61+
var expectedMeta = person.GetMeta(null);
62+
var builder = new WebHostBuilder()
63+
.UseStartup<Startup>();
64+
65+
var httpMethod = new HttpMethod("GET");
66+
var route = $"/api/v1/people";
67+
68+
var server = new TestServer(builder);
69+
var client = server.CreateClient();
70+
var request = new HttpRequestMessage(httpMethod, route);
71+
72+
// act
73+
var response = await client.SendAsync(request);
74+
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
75+
76+
// assert
77+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
78+
Assert.NotNull(documents.Meta);
79+
Assert.NotNull(expectedMeta);
80+
Assert.NotEmpty(expectedMeta);
81+
82+
foreach(var hash in expectedMeta)
83+
{
84+
if(hash.Value is IList)
85+
{
86+
var listValue = (IList)hash.Value;
87+
for(var i=0; i < listValue.Count; i++)
88+
Assert.Equal(listValue[i].ToString(), ((IList)documents.Meta[hash.Key])[i].ToString());
89+
}
90+
else
91+
{
92+
Assert.Equal(hash.Value, documents.Meta[hash.Key]);
93+
}
94+
}
95+
}
5396
}
5497
}

test/JsonApiDotNetCoreExampleTests/Startups/MetaStartup.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
using Microsoft.AspNetCore.Builder;
21
using Microsoft.AspNetCore.Hosting;
3-
using Microsoft.Extensions.Configuration;
42
using Microsoft.Extensions.DependencyInjection;
53
using Microsoft.Extensions.Logging;
6-
using JsonApiDotNetCore.Routing;
74
using JsonApiDotNetCoreExample.Data;
85
using Microsoft.EntityFrameworkCore;
96
using JsonApiDotNetCore.Extensions;
107
using DotNetCoreDocs.Configuration;
11-
using DotNetCoreDocs.Middleware;
128
using System;
139
using JsonApiDotNetCoreExample;
1410

0 commit comments

Comments
 (0)