Skip to content

Commit 4519bf8

Browse files
fix: add test for scoped schema set
1 parent 4c480a5 commit 4519bf8

25 files changed

Lines changed: 434 additions & 49 deletions

File tree

end2end/EndToEndScaffold/Templates/MySqlTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,25 +646,30 @@ await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs
646646
{
647647
AuthorName = {{Consts.BojackAuthor}},
648648
Name = {{Consts.BojackBookTitle}},
649-
BioType = ExtendedBiosBioType.Memoir
649+
BioType = ExtendedBiosBioType.Memoir,
650+
AuthorType = new ExtendedBiosAuthorType[] { ExtendedBiosAuthorType.Author, ExtendedBiosAuthorType.Translator }
650651
});
651652
var expected = new QuerySql.GetFirstExtendedBioByTypeRow
652653
{
653654
AuthorName = {{Consts.BojackAuthor}},
654655
Name = {{Consts.BojackBookTitle}},
655-
BioType = ExtendedBiosBioType.Memoir
656+
BioType = ExtendedBiosBioType.Memoir,
657+
AuthorType = new ExtendedBiosAuthorType[] { ExtendedBiosAuthorType.Author, ExtendedBiosAuthorType.Translator }
656658
};
657659
658660
var actual = await this.QuerySql.GetFirstExtendedBioByType(new QuerySql.GetFirstExtendedBioByTypeArgs
659661
{
660662
BioType = ExtendedBiosBioType.Memoir
661663
});
662-
Assert.That(SingularEquals(expected, actual{{Consts.UnknownRecordValuePlaceholder}}));
664+
AssertSingularEquals(expected, actual{{Consts.UnknownRecordValuePlaceholder}});
663665
}
664666
665-
private static bool SingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
667+
private void AssertSingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
666668
{
667-
return x.AuthorName.Equals(y.AuthorName) && x.Name.Equals(y.Name) && x.BioType.Equals(y.BioType);
669+
Assert.That(x.AuthorName, Is.EqualTo(y.AuthorName));
670+
Assert.That(x.Name, Is.EqualTo(y.Name));
671+
Assert.That(x.BioType, Is.EqualTo(y.BioType));
672+
Assert.That(x.AuthorType, Is.EqualTo(y.AuthorType));
668673
}
669674
"""
670675
},

end2end/EndToEndTests/MySqlConnectorDapperTester.generated.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,28 @@ private static void AssertSingularEquals(QuerySql.GetMysqlFunctionsRow expected,
519519
[Test]
520520
public async Task TestMySqlScopedSchemaEnum()
521521
{
522-
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir });
522+
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir, AuthorType = new ExtendedBiosAuthorType[] { ExtendedBiosAuthorType.Author, ExtendedBiosAuthorType.Translator } });
523523
var expected = new QuerySql.GetFirstExtendedBioByTypeRow
524524
{
525525
AuthorName = "Bojack Horseman",
526526
Name = "One Trick Pony",
527-
BioType = ExtendedBiosBioType.Memoir
527+
BioType = ExtendedBiosBioType.Memoir,
528+
AuthorType = new ExtendedBiosAuthorType[]
529+
{
530+
ExtendedBiosAuthorType.Author,
531+
ExtendedBiosAuthorType.Translator
532+
}
528533
};
529534
var actual = await this.QuerySql.GetFirstExtendedBioByType(new QuerySql.GetFirstExtendedBioByTypeArgs { BioType = ExtendedBiosBioType.Memoir });
530-
Assert.That(SingularEquals(expected, actual));
535+
AssertSingularEquals(expected, actual);
531536
}
532537

533-
private static bool SingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
538+
private void AssertSingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
534539
{
535-
return x.AuthorName.Equals(y.AuthorName) && x.Name.Equals(y.Name) && x.BioType.Equals(y.BioType);
540+
Assert.That(x.AuthorName, Is.EqualTo(y.AuthorName));
541+
Assert.That(x.Name, Is.EqualTo(y.Name));
542+
Assert.That(x.BioType, Is.EqualTo(y.BioType));
543+
Assert.That(x.AuthorType, Is.EqualTo(y.AuthorType));
536544
}
537545

538546
[Test]

end2end/EndToEndTests/MySqlConnectorTester.generated.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,28 @@ private static void AssertSingularEquals(QuerySql.GetMysqlFunctionsRow expected,
519519
[Test]
520520
public async Task TestMySqlScopedSchemaEnum()
521521
{
522-
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir });
522+
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir, AuthorType = new ExtendedBiosAuthorType[] { ExtendedBiosAuthorType.Author, ExtendedBiosAuthorType.Translator } });
523523
var expected = new QuerySql.GetFirstExtendedBioByTypeRow
524524
{
525525
AuthorName = "Bojack Horseman",
526526
Name = "One Trick Pony",
527-
BioType = ExtendedBiosBioType.Memoir
527+
BioType = ExtendedBiosBioType.Memoir,
528+
AuthorType = new ExtendedBiosAuthorType[]
529+
{
530+
ExtendedBiosAuthorType.Author,
531+
ExtendedBiosAuthorType.Translator
532+
}
528533
};
529534
var actual = await this.QuerySql.GetFirstExtendedBioByType(new QuerySql.GetFirstExtendedBioByTypeArgs { BioType = ExtendedBiosBioType.Memoir });
530-
Assert.That(SingularEquals(expected, actual.Value));
535+
AssertSingularEquals(expected, actual.Value);
531536
}
532537

533-
private static bool SingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
538+
private void AssertSingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
534539
{
535-
return x.AuthorName.Equals(y.AuthorName) && x.Name.Equals(y.Name) && x.BioType.Equals(y.BioType);
540+
Assert.That(x.AuthorName, Is.EqualTo(y.AuthorName));
541+
Assert.That(x.Name, Is.EqualTo(y.Name));
542+
Assert.That(x.BioType, Is.EqualTo(y.BioType));
543+
Assert.That(x.AuthorType, Is.EqualTo(y.AuthorType));
536544
}
537545

538546
[Test]

end2end/EndToEndTestsLegacy/MySqlConnectorDapperTester.generated.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,28 @@ private static void AssertSingularEquals(QuerySql.GetMysqlFunctionsRow expected,
519519
[Test]
520520
public async Task TestMySqlScopedSchemaEnum()
521521
{
522-
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir });
522+
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir, AuthorType = new ExtendedBiosAuthorType[] { ExtendedBiosAuthorType.Author, ExtendedBiosAuthorType.Translator } });
523523
var expected = new QuerySql.GetFirstExtendedBioByTypeRow
524524
{
525525
AuthorName = "Bojack Horseman",
526526
Name = "One Trick Pony",
527-
BioType = ExtendedBiosBioType.Memoir
527+
BioType = ExtendedBiosBioType.Memoir,
528+
AuthorType = new ExtendedBiosAuthorType[]
529+
{
530+
ExtendedBiosAuthorType.Author,
531+
ExtendedBiosAuthorType.Translator
532+
}
528533
};
529534
var actual = await this.QuerySql.GetFirstExtendedBioByType(new QuerySql.GetFirstExtendedBioByTypeArgs { BioType = ExtendedBiosBioType.Memoir });
530-
Assert.That(SingularEquals(expected, actual));
535+
AssertSingularEquals(expected, actual);
531536
}
532537

533-
private static bool SingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
538+
private void AssertSingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
534539
{
535-
return x.AuthorName.Equals(y.AuthorName) && x.Name.Equals(y.Name) && x.BioType.Equals(y.BioType);
540+
Assert.That(x.AuthorName, Is.EqualTo(y.AuthorName));
541+
Assert.That(x.Name, Is.EqualTo(y.Name));
542+
Assert.That(x.BioType, Is.EqualTo(y.BioType));
543+
Assert.That(x.AuthorType, Is.EqualTo(y.AuthorType));
536544
}
537545

538546
[Test]

end2end/EndToEndTestsLegacy/MySqlConnectorTester.generated.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,28 @@ private static void AssertSingularEquals(QuerySql.GetMysqlFunctionsRow expected,
519519
[Test]
520520
public async Task TestMySqlScopedSchemaEnum()
521521
{
522-
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir });
522+
await this.QuerySql.CreateExtendedBio(new QuerySql.CreateExtendedBioArgs { AuthorName = "Bojack Horseman", Name = "One Trick Pony", BioType = ExtendedBiosBioType.Memoir, AuthorType = new ExtendedBiosAuthorType[] { ExtendedBiosAuthorType.Author, ExtendedBiosAuthorType.Translator } });
523523
var expected = new QuerySql.GetFirstExtendedBioByTypeRow
524524
{
525525
AuthorName = "Bojack Horseman",
526526
Name = "One Trick Pony",
527-
BioType = ExtendedBiosBioType.Memoir
527+
BioType = ExtendedBiosBioType.Memoir,
528+
AuthorType = new ExtendedBiosAuthorType[]
529+
{
530+
ExtendedBiosAuthorType.Author,
531+
ExtendedBiosAuthorType.Translator
532+
}
528533
};
529534
var actual = await this.QuerySql.GetFirstExtendedBioByType(new QuerySql.GetFirstExtendedBioByTypeArgs { BioType = ExtendedBiosBioType.Memoir });
530-
Assert.That(SingularEquals(expected, actual));
535+
AssertSingularEquals(expected, actual);
531536
}
532537

533-
private static bool SingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
538+
private void AssertSingularEquals(QuerySql.GetFirstExtendedBioByTypeRow x, QuerySql.GetFirstExtendedBioByTypeRow y)
534539
{
535-
return x.AuthorName.Equals(y.AuthorName) && x.Name.Equals(y.Name) && x.BioType.Equals(y.BioType);
540+
Assert.That(x.AuthorName, Is.EqualTo(y.AuthorName));
541+
Assert.That(x.Name, Is.EqualTo(y.Name));
542+
Assert.That(x.BioType, Is.EqualTo(y.BioType));
543+
Assert.That(x.AuthorType, Is.EqualTo(y.AuthorType));
536544
}
537545

538546
[Test]

examples/MySqlConnectorDapperExample/Models.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class ExtendedBio
6565
public string? AuthorName { get; init; }
6666
public string? Name { get; init; }
6767
public ExtendedBiosBioType? BioType { get; init; }
68+
public ExtendedBiosAuthorType[]? AuthorType { get; init; }
6869
};
6970
public enum MysqlTypesCEnum
7071
{
@@ -148,4 +149,32 @@ public static ExtendedBiosBioType[] ToExtendedBiosBioTypeArr(this string me)
148149
{
149150
return me.Split(',').ToList().Select(v => StringToEnum[v]).ToArray();
150151
}
152+
}
153+
154+
public enum ExtendedBiosAuthorType
155+
{
156+
Invalid = 0, // reserved for invalid enum value
157+
Author = 1,
158+
Editor = 2,
159+
Translator = 3
160+
}
161+
162+
public static class ExtendedBiosAuthorTypeExtensions
163+
{
164+
private static readonly Dictionary<string, ExtendedBiosAuthorType> StringToEnum = new Dictionary<string, ExtendedBiosAuthorType>()
165+
{
166+
[string.Empty] = ExtendedBiosAuthorType.Invalid,
167+
["Author"] = ExtendedBiosAuthorType.Author,
168+
["Editor"] = ExtendedBiosAuthorType.Editor,
169+
["Translator"] = ExtendedBiosAuthorType.Translator
170+
};
171+
public static ExtendedBiosAuthorType ToExtendedBiosAuthorType(this string me)
172+
{
173+
return StringToEnum[me];
174+
}
175+
176+
public static ExtendedBiosAuthorType[] ToExtendedBiosAuthorTypeArr(this string me)
177+
{
178+
return me.Split(',').ToList().Select(v => StringToEnum[v]).ToArray();
179+
}
151180
}

examples/MySqlConnectorDapperExample/QuerySql.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,19 +902,21 @@ public async Task TruncateMysqlTypes()
902902
await this.Transaction.Connection.ExecuteAsync(TruncateMysqlTypesSql, transaction: this.Transaction);
903903
}
904904

905-
private const string CreateExtendedBioSql = "INSERT INTO extended.bios (author_name, name, bio_type) VALUES (@author_name, @name, @bio_type)";
905+
private const string CreateExtendedBioSql = "INSERT INTO extended.bios (author_name, name, bio_type, author_type) VALUES (@author_name, @name, @bio_type, @author_type)";
906906
public class CreateExtendedBioArgs
907907
{
908908
public string? AuthorName { get; init; }
909909
public string? Name { get; init; }
910910
public ExtendedBiosBioType? BioType { get; init; }
911+
public ExtendedBiosAuthorType[]? AuthorType { get; init; }
911912
};
912913
public async Task CreateExtendedBio(CreateExtendedBioArgs args)
913914
{
914915
var queryParams = new Dictionary<string, object?>();
915916
queryParams.Add("author_name", args.AuthorName);
916917
queryParams.Add("name", args.Name);
917918
queryParams.Add("bio_type", args.BioType);
919+
queryParams.Add("author_type", args.AuthorType != null ? string.Join(",", args.AuthorType) : null);
918920
if (this.Transaction == null)
919921
{
920922
using (var connection = new MySqlConnection(ConnectionString))
@@ -933,12 +935,13 @@ public async Task CreateExtendedBio(CreateExtendedBioArgs args)
933935
await this.Transaction.Connection.ExecuteAsync(CreateExtendedBioSql, queryParams, transaction: this.Transaction);
934936
}
935937

936-
private const string GetFirstExtendedBioByTypeSql = "SELECT author_name, name, bio_type FROM extended.bios WHERE bio_type = @bio_type LIMIT 1";
938+
private const string GetFirstExtendedBioByTypeSql = "SELECT author_name, name, bio_type, author_type FROM extended.bios WHERE bio_type = @bio_type LIMIT 1";
937939
public class GetFirstExtendedBioByTypeRow
938940
{
939941
public string? AuthorName { get; init; }
940942
public string? Name { get; init; }
941943
public ExtendedBiosBioType? BioType { get; init; }
944+
public ExtendedBiosAuthorType[]? AuthorType { get; init; }
942945
};
943946
public class GetFirstExtendedBioByTypeArgs
944947
{

examples/MySqlConnectorDapperExample/Utils.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static void ConfigureSqlMapper()
2929
{
3030
SqlMapper.AddTypeHandler(typeof(JsonElement), new JsonElementTypeHandler());
3131
SqlMapper.AddTypeHandler(typeof(MysqlTypesCSet[]), new MysqlTypesCSetTypeHandler());
32+
SqlMapper.AddTypeHandler(typeof(ExtendedBiosAuthorType[]), new ExtendedBiosAuthorTypeTypeHandler());
3233
}
3334

3435
public static string TransformQueryForSliceArgs(string originalSql, int sliceSize, string paramName)
@@ -52,6 +53,21 @@ public override void SetValue(IDbDataParameter parameter, MysqlTypesCSet[] value
5253
}
5354
}
5455

56+
private class ExtendedBiosAuthorTypeTypeHandler : SqlMapper.TypeHandler<ExtendedBiosAuthorType[]>
57+
{
58+
public override ExtendedBiosAuthorType[] Parse(object value)
59+
{
60+
if (value is string s)
61+
return s.ToExtendedBiosAuthorTypeArr();
62+
throw new DataException($"Cannot convert {value?.GetType()} to ExtendedBiosAuthorType[]");
63+
}
64+
65+
public override void SetValue(IDbDataParameter parameter, ExtendedBiosAuthorType[] value)
66+
{
67+
parameter.Value = string.Join(",", value);
68+
}
69+
}
70+
5571
public class MysqlTypesCSetCsvConverter : DefaultTypeConverter
5672
{
5773
public override string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)

examples/MySqlConnectorDapperExample/request.json

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,14 @@
534534
"Biography",
535535
"Memoir"
536536
]
537+
},
538+
{
539+
"name": "bios_author_type",
540+
"vals": [
541+
"Author",
542+
"Editor",
543+
"Translator"
544+
]
537545
}
538546
]
539547
},
@@ -578,6 +586,17 @@
578586
"type": {
579587
"name": "bios_bio_type"
580588
}
589+
},
590+
{
591+
"name": "author_type",
592+
"length": 24,
593+
"table": {
594+
"schema": "extended",
595+
"name": "bios"
596+
},
597+
"type": {
598+
"name": "bios_author_type"
599+
}
581600
}
582601
]
583602
}
@@ -3362,7 +3381,7 @@
33623381
"filename": "query.sql"
33633382
},
33643383
{
3365-
"text": "INSERT INTO extended.bios (author_name, name, bio_type) VALUES (?, ?, ?)",
3384+
"text": "INSERT INTO extended.bios (author_name, name, bio_type, author_type) VALUES (?, ?, ?, ?)",
33663385
"name": "CreateExtendedBio",
33673386
"cmd": ":exec",
33683387
"parameters": [
@@ -3410,6 +3429,21 @@
34103429
},
34113430
"originalName": "bio_type"
34123431
}
3432+
},
3433+
{
3434+
"number": 4,
3435+
"column": {
3436+
"name": "author_type",
3437+
"length": 24,
3438+
"table": {
3439+
"schema": "extended",
3440+
"name": "bios"
3441+
},
3442+
"type": {
3443+
"name": "bios_author_type"
3444+
},
3445+
"originalName": "author_type"
3446+
}
34133447
}
34143448
],
34153449
"filename": "query.sql",
@@ -3419,7 +3453,7 @@
34193453
}
34203454
},
34213455
{
3422-
"text": "SELECT author_name, name, bio_type FROM extended.bios WHERE bio_type = ? LIMIT 1",
3456+
"text": "SELECT author_name, name, bio_type, author_type FROM extended.bios WHERE bio_type = ? LIMIT 1",
34233457
"name": "GetFirstExtendedBioByType",
34243458
"cmd": ":one",
34253459
"columns": [
@@ -3458,6 +3492,18 @@
34583492
"name": "bios_bio_type"
34593493
},
34603494
"originalName": "bio_type"
3495+
},
3496+
{
3497+
"name": "author_type",
3498+
"length": 24,
3499+
"table": {
3500+
"schema": "extended",
3501+
"name": "bios"
3502+
},
3503+
"type": {
3504+
"name": "bios_author_type"
3505+
},
3506+
"originalName": "author_type"
34613507
}
34623508
],
34633509
"parameters": [
272 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)