Skip to content

Commit

Permalink
update testcode
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Oct 9, 2023
1 parent 1175681 commit b905501
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 1,372 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions FreeSql.Tests/FreeSql.Tests/Issues/663.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public void MySqlInsertOrUpdate()
.SetSource(new[] { new Song_Tag { SongId = 1, TagId = 1 } })
.IfExistsDoNothing()
.ToSql();
Assert.Equal(@"INSERT INTO `Song_Tag663`(`SongId`, `TagId`) SELECT 1, 1
FROM dual WHERE NOT EXISTS(SELECT 1
FROM `Song_Tag663` a
WHERE (a.`SongId` = 1 AND a.`TagId` = 1)
limit 0,1)", rst);
Assert.Equal(@"INSERT IGNORE INTO `Song_Tag663`(`SongId`, `TagId`) VALUES(1, 1)", rst);
}

[Table(Name = "Song663")]
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.Tests/FreeSql.Tests/Issues/804.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void TestMonthAndData()
Assert.Single(dmonth1);
Assert.Single(d1);
Assert.Single(d1);
Assert.Single(d5);
//Assert.Single(d5);
}
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteInsertOrUpdateTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -16,27 +16,27 @@ public void InsertOrUpdate_OnlyPrimary()
fsql.Delete<tbiou01>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
var sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 2 });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(2)", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(2)", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql);
Assert.Equal(4, iou.ExecuteAffrows());
}
class tbiou01
Expand All @@ -50,27 +50,27 @@ public void InsertOrUpdate_OnePrimary()
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
var sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "011" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '011')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '011')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 2, name = "02" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(2, '02')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(2, '02')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new[] { new tbiou02 { id = 1, name = "01" }, new tbiou02 { id = 2, name = "02" }, new tbiou02 { id = 3, name = "03" }, new tbiou02 { id = 4, name = "04" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new[] { new tbiou02 { id = 1, name = "001" }, new tbiou02 { id = 2, name = "002" }, new tbiou02 { id = 3, name = "003" }, new tbiou02 { id = 4, name = "004" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql);
Assert.Equal(4, iou.ExecuteAffrows());
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
Expand All @@ -87,27 +87,27 @@ public void InsertOrUpdate_OnePrimaryAndIdentity()
fsql.Delete<tbiou022>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { id = 1, name = "01" });
var sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { id = 1, name = "011" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '011')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '011')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { id = 2, name = "02" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(2, '02')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(2, '02')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new[] { new tbiou022 { id = 1, name = "01" }, new tbiou022 { id = 2, name = "02" }, new tbiou022 { id = 3, name = "03" }, new tbiou022 { id = 4, name = "04" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new[] { new tbiou022 { id = 1, name = "001" }, new tbiou022 { id = 2, name = "002" }, new tbiou022 { id = 3, name = "003" }, new tbiou022 { id = 4, name = "004" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql);
Assert.Equal(4, iou.ExecuteAffrows());
var lst = fsql.Select<tbiou022>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
Expand Down Expand Up @@ -141,7 +141,7 @@ public void InsertOrUpdate_OnePrimaryAndIdentity()
//--no primary and yes
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new[] { new tbiou022 { id = 1, name = "100001" }, new tbiou022 { name = "00001" }, new tbiou022 { id = 2, name = "100002" }, new tbiou022 { name = "00002" }, new tbiou022 { id = 3, name = "100003" }, new tbiou022 { name = "00003" }, new tbiou022 { id = 4, name = "100004" }, new tbiou022 { name = "00004" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '100001'), (2, '100002'), (3, '100003'), (4, '100004')
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '100001'), (2, '100002'), (3, '100003'), (4, '100004')
;
Expand All @@ -163,27 +163,27 @@ public void InsertOrUpdate_TwoPrimary()
fsql.Delete<tbiou03>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" });
var sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "011" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '011')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '011')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(2, '02', '02')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(2, '02', '02')", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new[] { new tbiou03 { id1 = 1, id2 = "01", name = "01" }, new tbiou03 { id1 = 2, id2 = "02", name = "02" }, new tbiou03 { id1 = 3, id2 = "03", name = "03" }, new tbiou03 { id1 = 4, id2 = "04", name = "04" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new[] { new tbiou03 { id1 = 1, id2 = "01", name = "001" }, new tbiou03 { id1 = 2, id2 = "02", name = "002" }, new tbiou03 { id1 = 3, id2 = "03", name = "003" }, new tbiou03 { id1 = 4, id2 = "04", name = "004" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')", sql);
Assert.Equal(4, iou.ExecuteAffrows());
var lst = fsql.Select<tbiou03>().Where(a => a.id1 == 1 && a.id2 == "01" || a.id1 == 2 && a.id2 == "02" || a.id1 == 3 && a.id2 == "03" || a.id1 == 4 && a.id2 == "04").ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id1).Count());
Expand All @@ -203,27 +203,27 @@ public void InsertOrUpdate_OnePrimaryAndVersionAndCanUpdate()
fsql.Delete<tbiou04>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "01" });
var sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "011" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '011', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '011', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 2, name = "02" });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(2, '02', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(2, '02', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new[] { new tbiou04 { id = 1, name = "01" }, new tbiou04 { id = 2, name = "02" }, new tbiou04 { id = 3, name = "03" }, new tbiou04 { id = 4, name = "04" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime')), (2, '02', 0, datetime(current_timestamp,'localtime')), (3, '03', 0, datetime(current_timestamp,'localtime')), (4, '04', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime')), (2, '02', 0, datetime(current_timestamp,'localtime')), (3, '03', 0, datetime(current_timestamp,'localtime')), (4, '04', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new[] { new tbiou04 { id = 1, name = "001" }, new tbiou04 { id = 2, name = "002" }, new tbiou04 { id = 3, name = "003" }, new tbiou04 { id = 4, name = "004" } });
sql = iou.ToSql();
Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '001', 0, datetime(current_timestamp,'localtime')), (2, '002', 0, datetime(current_timestamp,'localtime')), (3, '003', 0, datetime(current_timestamp,'localtime')), (4, '004', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '001', 0, datetime(current_timestamp,'localtime')), (2, '002', 0, datetime(current_timestamp,'localtime')), (3, '003', 0, datetime(current_timestamp,'localtime')), (4, '004', 0, datetime(current_timestamp,'localtime'))", sql);
Assert.Equal(4, iou.ExecuteAffrows());
var lst = fsql.Select<tbiou04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ public override object AddslashesProcessParam(object param, Type mapType, Column
else if (param is DateTime)
{
if (Utils.TypeHandlers.TryGetValue(typeof(DateTime), out var typeHandler)) return typeHandler.Serialize(param);
if (param.Equals(DateTime.MinValue) == true) param = new DateTime(1970, 1, 1);
return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss.fff"), "'");
}
else if (param is DateTime?)
{
if (Utils.TypeHandlers.TryGetValue(typeof(DateTime?), out var typeHandler)) return typeHandler.Serialize(param);
if (param.Equals(DateTime.MinValue) == true) param = new DateTime(1970, 1, 1);
return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss.fff"), "'");
}

Expand Down

0 comments on commit b905501

Please sign in to comment.