Skip to content

Commit

Permalink
Add tests for long handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccallum committed Apr 7, 2021
1 parent e5032cf commit 8a7fbb9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 57 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ query {

IDs that are internally represented with `Integer`s, `Guid`s, `Long`s, `String`s. You can opt-out to each as required.

For integer-based or long-based IDs, you can pass `"1"` or `1` and both will be accepted.
For integer-based IDs, you can pass `"1"` or `1` and both will be accepted.

For all other types, you need to pass the string value, e.g. `"26a2dc8f-4dab-408c-88c6-523a0a89a2b5"` for a guid-based ID.
For all other types, you need to pass the string value, e.g.
* `"26a2dc8f-4dab-408c-88c6-523a0a89a2b5"` for a guid-based ID
* `"123456789"` for a long-based ID

### Any downsides?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"nullableIntId": "1",
"nullableIntIdGivenNull": "null",
"intIdList": "1",
"longId": "9223372036854775807",
"nullableLongId": "9223372036854775807",
"nullableLongIdGivenNull": "null",
"longIdList": "9223372036854775807",
"stringId": "abc",
"nullableStringId": "abc",
"nullableStringIdGivenNull": "null",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
{
"message": "The ID \u0060abc\u0060 has an invalid format."
},
{
"message": "The IDs \u00609223372036854775807\u0060 have an invalid format."
},
{
"message": "The ID \u00609223372036854775807\u0060 has an invalid format."
},
{
"message": "The ID \u00609223372036854775807\u0060 has an invalid format."
},
{
"message": "The IDs \u00601\u0060 have an invalid format."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"nullableIntId": "1",
"nullableIntIdGivenNull": "null",
"intIdList": "1",
"longId": "9223372036854775807",
"nullableLongId": "9223372036854775807",
"nullableLongIdGivenNull": "null",
"longIdList": "9223372036854775807",
"stringId": "abc",
"nullableStringId": "abc",
"nullableStringIdGivenNull": "null",
Expand Down
97 changes: 47 additions & 50 deletions src/AutoGuru.HotChocolate.PolymorphicIds.Tests/IdAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,47 @@ public class IdAttributeTests

// TODO: When PR 3440 in HC is merged, uncomment array with nulls field usages below in queries

private const string _argumentsQuery = @"
query foo (
$intId: ID!
$longId: ID!
$stringId: ID!
$guidId: ID!
$null: ID = null)
{
intId(id: $intId)
nullableIntId(id: $intId)
nullableIntIdGivenNull: nullableIntId(id: $null)
intIdList(id: [$intId])
# TODO: nullableIntIdList(id: [$intId, $null])
longId(id: $longId)
nullableLongId(id: $longId)
nullableLongIdGivenNull: nullableLongId(id: $null)
longIdList(id: [$longId])
# TODO: nullableLongIdList(id: [$longId, $null])
stringId(id: $stringId)
nullableStringId(id: $stringId)
nullableStringIdGivenNull: nullableStringId(id: $null)
stringIdList(id: [$stringId])
# TODO: nullableStringIdList(id: [$stringId, $null])
guidId(id: $guidId)
nullableGuidId(id: $guidId)
nullableGuidIdGivenNull: nullableGuidId(id: $null)
guidIdList(id: [$guidId $guidId])
# TODO: nullableGuidIdList(id: [$guidId $null $guidId])
}";

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task PolyId_On_Arguments(bool isEnabled)
{
// arrange
var intId = 1;
var longId = long.MaxValue;
var stringId = "abc";
var guidId = new Guid("26a2dc8f-4dab-408c-88c6-523a0a89a2b5");
var services = new ServiceCollection();
Expand All @@ -60,32 +94,9 @@ public async Task PolyId_On_Arguments(bool isEnabled)
var result = await executor
.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"query foo (
$intId: ID!
$nullIntId: ID = null
$stringId: ID!
$nullStringId: ID = null
$guidId: ID!
$nullGuidId: ID = null)
{
intId(id: $intId)
nullableIntId(id: $intId)
nullableIntIdGivenNull: nullableIntId(id: $nullIntId)
intIdList(id: [$intId])
# TODO: nullableIntIdList(id: [$intId, $nullIntId])
stringId(id: $stringId)
nullableStringId(id: $stringId)
nullableStringIdGivenNull: nullableStringId(id: $nullStringId)
stringIdList(id: [$stringId])
# TODO: nullableStringIdList(id: [$stringId, $nullStringId])
guidId(id: $guidId)
nullableGuidId(id: $guidId)
nullableGuidIdGivenNull: nullableGuidId(id: $nullGuidId)
guidIdList(id: [$guidId $guidId])
# TODO: nullableGuidIdList(id: [$guidId $nullGuidId $guidId])
}")
.SetQuery(_argumentsQuery)
.SetVariableValue("intId", intId)
.SetVariableValue("longId", longId.ToString())
.SetVariableValue("stringId", stringId)
.SetVariableValue("guidId", guidId.ToString())
.Create());
Expand Down Expand Up @@ -225,6 +236,7 @@ public async Task Id_On_Arguments()
// arrange
var idSerializer = new IdSerializer();
var intId = idSerializer.Serialize("Query", 1);
var longId = idSerializer.Serialize("Query", long.MaxValue);
var stringId = idSerializer.Serialize("Query", "abc");
var guidId = idSerializer.Serialize("Query", new Guid("26a2dc8f-4dab-408c-88c6-523a0a89a2b5"));

Expand All @@ -237,32 +249,9 @@ await SchemaBuilder.New()
.MakeExecutable()
.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"query foo (
$intId: ID!
$nullIntId: ID = null
$stringId: ID!
$nullStringId: ID = null
$guidId: ID!
$nullGuidId: ID = null)
{
intId(id: $intId)
nullableIntId(id: $intId)
nullableIntIdGivenNull: nullableIntId(id: $nullIntId)
intIdList(id: [$intId])
# TODO: nullableIntIdList(id: [$intId, $nullIntId])
stringId(id: $stringId)
nullableStringId(id: $stringId)
nullableStringIdGivenNull: nullableStringId(id: $nullStringId)
stringIdList(id: [$stringId])
# TODO: nullableStringIdList(id: [$stringId, $nullStringId])
guidId(id: $guidId)
nullableGuidId(id: $guidId)
nullableGuidIdGivenNull: nullableGuidId(id: $nullGuidId)
guidIdList(id: [$guidId $guidId])
# TODO: nullableGuidIdList(id: [$guidId $nullGuidId $guidId])
}")
.SetQuery(_argumentsQuery)
.SetVariableValue("intId", intId)
.SetVariableValue("longId", longId.ToString())
.SetVariableValue("stringId", stringId)
.SetVariableValue("guidId", guidId)
.Create());
Expand Down Expand Up @@ -450,6 +439,14 @@ public string IntIdList([ID] int[] id) =>
public string NullableIntIdList([ID] int?[] id) =>
string.Join(", ", id.Select(t => t?.ToString() ?? "null"));

public string LongId([ID] long id) => id.ToString();
public string LongIdList([ID] long[] id) =>
string.Join(", ", id.Select(t => t.ToString()));

public string NullableLongId([ID] long? id) => id?.ToString() ?? "null";
public string NullableLongIdList([ID] long?[] id) =>
string.Join(", ", id.Select(t => t?.ToString() ?? "null"));

public string StringId([ID] string id) => id;
public string StringIdList([ID] string[] id) =>
string.Join(", ", id.Select(t => t.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ public PolymorphicIdInputValueFormatter(

private IdValue DeserializeId(string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

if ((_idRuntimeType == typeof(int) || _idRuntimeType == typeof(int?)) &&
value is string rawIntString &&
int.TryParse(rawIntString, out var intValue))
Expand Down

0 comments on commit 8a7fbb9

Please sign in to comment.