mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-30 06:32:49 +08:00
532 lines
18 KiB
C#
532 lines
18 KiB
C#
using FreeSql.DataAnnotations;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Xunit;
|
||
|
||
namespace FreeSql.Tests
|
||
{
|
||
public class RepositoryTests
|
||
{
|
||
|
||
[Fact]
|
||
public void AddUpdate()
|
||
{
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = repos.Insert(new AddUpdateInfo());
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
item = repos.Insert(new AddUpdateInfo { Id = Guid.NewGuid() });
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
item.Title = "xxx";
|
||
repos.Update(item);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
Console.WriteLine(repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ToSql());
|
||
repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ExecuteAffrows();
|
||
|
||
item = repos.Find(item.Id);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
repos.Orm.Insert(new AddUpdateInfo()).ExecuteAffrows();
|
||
repos.Orm.Insert(new AddUpdateInfo { Id = Guid.NewGuid() }).ExecuteAffrows();
|
||
repos.Orm.Update<AddUpdateInfo>().Set(a => a.Title == "xxx").Where(a => a.Id == item.Id).ExecuteAffrows();
|
||
item = repos.Orm.Select<AddUpdateInfo>(item).First();
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
repos.Orm.Delete<AddUpdateInfo>(item).ExecuteAffrows();
|
||
}
|
||
|
||
[Fact]
|
||
public void UpdateAttach()
|
||
{
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = new AddUpdateInfo { Id = Guid.NewGuid() };
|
||
repos.Attach(item);
|
||
|
||
item.Title = "xxx";
|
||
repos.Update(item); //ÕâÐÐÖ´ÐÐ UPDATE "AddUpdateInfo" SET "Title" = 'xxx' WHERE("Id" = '1942fb53-9700-411d-8895-ce4cecdf3257')
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
repos.Update(item); //ÕâÐв»Ö´ÐÐ SQL£¬Î´±ä»¯
|
||
|
||
repos.AttachOnlyPrimary(item).Update(item); //ÕâÐиüÐÂ״ֵ̬£¬Ö»ÓÐÖ÷¼üÖµ´æÔÚ£¬Ö´ÐиüРset title = xxx
|
||
|
||
Console.WriteLine(repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ToSql());
|
||
repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ExecuteAffrows();
|
||
|
||
item = repos.Find(item.Id);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
}
|
||
|
||
[Fact]
|
||
public void UpdateWhenNotExists()
|
||
{
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = new AddUpdateInfo { Id = Guid.NewGuid() };
|
||
item.Title = "xxx";
|
||
Assert.Throws<Exception>(() => repos.Update(item));
|
||
}
|
||
|
||
[Fact]
|
||
public void Update()
|
||
{
|
||
g.sqlite.Insert(new AddUpdateInfo()).ExecuteAffrows();
|
||
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = new AddUpdateInfo { Id = g.sqlite.Select<AddUpdateInfo>().First().Id };
|
||
|
||
item.Title = "xxx";
|
||
repos.Update(item);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
}
|
||
|
||
public class AddUpdateInfo
|
||
{
|
||
|
||
public Guid Id { get; set; }
|
||
public string Title { get; set; }
|
||
|
||
public int Clicks { get; set; } = 10;
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepository()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, /*g.mysql, g.pgsql, g.oracle, g.sqlserver*/ })
|
||
{
|
||
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
FlowModel flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
flowRepos.Insert(flow);
|
||
|
||
//ÊÂÎñÌí¼Ó
|
||
flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
flowRepos = uow.GetRepository<FlowModel>();
|
||
flowRepos.Insert(flow);
|
||
flowRepos.Orm.Select<FlowModel>().ToList();
|
||
uow.Commit();
|
||
}
|
||
}
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepositoryWithDisableBeforeInsert()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, })
|
||
{
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
|
||
var flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
|
||
//ÇåÀíµôÊý¾Ý¿âÖÐÒÑ´æÔÚµÄÊý¾Ý£¬ÎªÁ˽ÓÏÂÀ´µÄ²åÈë²âÊÔ
|
||
flowRepos.Delete(a => a.UserId == 1 && a.Name == "aaa");
|
||
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
//¹Ø±Õ¹¤×÷µ¥Ôª£¨²»»á¿ªÊ¼ÊÂÎñ£©
|
||
uow.Close();
|
||
var uowFlowRepos = uow.GetRepository<FlowModel>();
|
||
uowFlowRepos.Insert(flow);
|
||
uowFlowRepos.Orm.Select<FlowModel>().ToList();
|
||
//Òѹرչ¤×÷µ¥Ôª£¬Ìá²»Ìá½»¶¼Ã»Ó°Ï죬´Ë´¦×¢ÊÍÀ´È·¶¨¹¤×÷µ¥Ôª¿ª¹ØÊÇ·ñÉúЧ£º¹Ø±ÕÁË£¬²»CommitÒ²Ó¦¸Ã²åÈëÊý¾Ý
|
||
//uow.Commit();
|
||
}
|
||
|
||
Assert.True(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
|
||
}
|
||
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepositoryWithDisableAfterInsert()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, })
|
||
{
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
|
||
//ÇåÀíµôÊý¾Ý¿âÖÐÒÑ´æÔÚµÄÊý¾Ý£¬ÎªÁ˽ÓÏÂÀ´µÄ²åÈë²âÊÔ
|
||
flowRepos.Delete(a => a.UserId == 1 && a.Name == "aaa");
|
||
|
||
var flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
|
||
|
||
Assert.Throws<Exception>(() =>
|
||
{
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
var uowFlowRepos = uow.GetRepository<FlowModel>();
|
||
uowFlowRepos.Insert(flow);
|
||
uowFlowRepos.Orm.Select<FlowModel>().ToList();
|
||
//ÓÐÁËÈÎÒâ Insert/Update/Delete µ÷ÓùرÕuowµÄ·½·¨½«»á·¢ÉúÒì³£
|
||
uow.Close();
|
||
uow.Commit();
|
||
}
|
||
|
||
});
|
||
}
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepositoryWithoutDisable()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, })
|
||
{
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
if (flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"))
|
||
{
|
||
flowRepos.Delete(a => a.UserId == 1);
|
||
}
|
||
|
||
|
||
var flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
|
||
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
var uowFlowRepos = uow.GetRepository<FlowModel>();
|
||
uowFlowRepos.Insert(flow);
|
||
uowFlowRepos.Orm.Select<FlowModel>().ToList();
|
||
//²»µ÷ÓÃcommit½«²»»áÌá½»Êý¾Ý¿â¸ü¸Ä
|
||
//uow.Commit();
|
||
}
|
||
Assert.False(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
|
||
}
|
||
}
|
||
|
||
|
||
public partial class FlowModel
|
||
{
|
||
public int UserId { get; set; }
|
||
public int Id { get; set; }
|
||
public int? ParentId { get; set; }
|
||
public string Name { get; set; }
|
||
public DateTime CreateTime { get; set; }
|
||
public DateTime LastModifyTime { get; set; }
|
||
public string Desc { get; set; }
|
||
}
|
||
|
||
[Fact]
|
||
public void AsType()
|
||
{
|
||
g.sqlite.Insert(new AddUpdateInfo()).ExecuteAffrows();
|
||
|
||
var repos = g.sqlite.GetGuidRepository<object>();
|
||
repos.AsType(typeof(AddUpdateInfo));
|
||
|
||
var item = new AddUpdateInfo();
|
||
repos.Insert(item);
|
||
repos.Update(item);
|
||
|
||
item.Clicks += 1;
|
||
repos.InsertOrUpdate(item);
|
||
|
||
var item2 = repos.Find(item.Id) as AddUpdateInfo;
|
||
Assert.Equal(item.Clicks, item2.Clicks);
|
||
|
||
repos.DataFilter.Apply("xxx", a => (a as AddUpdateInfo).Clicks == 2);
|
||
Assert.Null(repos.Find(item.Id));
|
||
}
|
||
|
||
[Fact]
|
||
public void EnableAddOrUpdateNavigateList_OneToMany()
|
||
{
|
||
var repo = g.sqlite.GetRepository<Cagetory>();
|
||
var cts = new[] {
|
||
new Cagetory
|
||
{
|
||
Name = "·ÖÀà1",
|
||
Goodss = new List<Goods>(new[]
|
||
{
|
||
new Goods { Name = "ÉÌÆ·1" },
|
||
new Goods { Name = "ÉÌÆ·2" },
|
||
new Goods { Name = "ÉÌÆ·3" }
|
||
})
|
||
},
|
||
new Cagetory
|
||
{
|
||
Name = "·ÖÀà2",
|
||
Goodss = new List<Goods>(new[]
|
||
{
|
||
new Goods { Name = "ÉÌÆ·4" },
|
||
new Goods { Name = "ÉÌÆ·5" }
|
||
})
|
||
}
|
||
};
|
||
repo.Insert(cts);
|
||
cts[0].Name = "·ÖÀà11";
|
||
cts[0].Goodss.Clear();
|
||
cts[1].Name = "·ÖÀà22";
|
||
cts[1].Goodss.Clear();
|
||
repo.Update(cts);
|
||
cts[0].Name = "·ÖÀà111";
|
||
cts[0].Goodss.Clear();
|
||
cts[0].Goodss.Add(new Goods { Name = "ÉÌÆ·33" });
|
||
cts[1].Name = "·ÖÀà222";
|
||
cts[1].Goodss.Clear();
|
||
cts[1].Goodss.Add(new Goods { Name = "ÉÌÆ·55" });
|
||
repo.Update(cts);
|
||
|
||
}
|
||
[Table(Name = "EAUNL_OTM_CT")]
|
||
class Cagetory
|
||
{
|
||
public Guid Id { get; set; }
|
||
public string Name { get; set; }
|
||
|
||
[Navigate("CagetoryId")]
|
||
public List<Goods> Goodss { get; set; }
|
||
}
|
||
[Table(Name = "EAUNL_OTM_GD")]
|
||
class Goods
|
||
{
|
||
public Guid Id { get; set; }
|
||
public Guid CagetoryId { get; set; }
|
||
public string Name { get; set; }
|
||
}
|
||
[Fact]
|
||
public void SaveMany_OneToMany()
|
||
{
|
||
var repo = g.sqlite.GetRepository<Cagetory>();
|
||
repo.DbContextOptions.EnableAddOrUpdateNavigateList = false; //¹Ø±Õ¼¶Áª±£´æ¹¦ÄÜ
|
||
var cts = new[] {
|
||
new Cagetory
|
||
{
|
||
Name = "·ÖÀà1",
|
||
Goodss = new List<Goods>(new[]
|
||
{
|
||
new Goods { Name = "ÉÌÆ·1" },
|
||
new Goods { Name = "ÉÌÆ·2" },
|
||
new Goods { Name = "ÉÌÆ·3" }
|
||
})
|
||
},
|
||
new Cagetory
|
||
{
|
||
Name = "·ÖÀà2",
|
||
Goodss = new List<Goods>(new[]
|
||
{
|
||
new Goods { Name = "ÉÌÆ·4" },
|
||
new Goods { Name = "ÉÌÆ·5" }
|
||
})
|
||
}
|
||
};
|
||
repo.Insert(cts);
|
||
repo.SaveMany(cts[0], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
repo.SaveMany(cts[1], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
cts[0].Goodss.RemoveAt(1);
|
||
cts[1].Goodss.RemoveAt(1);
|
||
repo.SaveMany(cts[0], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
repo.SaveMany(cts[1], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
|
||
cts[0].Name = "·ÖÀà11";
|
||
cts[0].Goodss.Clear();
|
||
cts[1].Name = "·ÖÀà22";
|
||
cts[1].Goodss.Clear();
|
||
repo.Update(cts);
|
||
repo.SaveMany(cts[0], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
repo.SaveMany(cts[1], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
cts[0].Name = "·ÖÀà111";
|
||
cts[0].Goodss.Clear();
|
||
cts[0].Goodss.Add(new Goods { Name = "ÉÌÆ·33" });
|
||
cts[1].Name = "·ÖÀà222";
|
||
cts[1].Goodss.Clear();
|
||
cts[1].Goodss.Add(new Goods { Name = "ÉÌÆ·55" });
|
||
repo.Update(cts);
|
||
repo.SaveMany(cts[0], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
repo.SaveMany(cts[1], "Goodss"); //Ö¸¶¨±£´æ Goodss Ò»¶Ô¶àÊôÐÔ
|
||
}
|
||
|
||
[Fact]
|
||
public void EnableAddOrUpdateNavigateList_OneToMany_Parent()
|
||
{
|
||
g.sqlite.Delete<CagetoryParent>().Where("1=1").ExecuteAffrows();
|
||
var repo = g.sqlite.GetRepository<CagetoryParent>();
|
||
var cts = new[] {
|
||
new CagetoryParent
|
||
{
|
||
Name = "·ÖÀà1",
|
||
Childs = new List<CagetoryParent>(new[]
|
||
{
|
||
new CagetoryParent { Name = "·ÖÀà1_1" },
|
||
new CagetoryParent { Name = "·ÖÀà1_2" },
|
||
new CagetoryParent { Name = "·ÖÀà1_3" }
|
||
})
|
||
},
|
||
new CagetoryParent
|
||
{
|
||
Name = "·ÖÀà2",
|
||
Childs = new List<CagetoryParent>(new[]
|
||
{
|
||
new CagetoryParent { Name = "·ÖÀà2_1" },
|
||
new CagetoryParent { Name = "·ÖÀà2_2" }
|
||
})
|
||
}
|
||
};
|
||
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; //´ò¿ª¼¶Áª±£´æ¹¦ÄÜ
|
||
repo.Insert(cts);
|
||
|
||
var notreelist1 = repo.Select.ToList();
|
||
var treelist1 = repo.Select.ToTreeList();
|
||
|
||
//repo.SaveMany(cts[0], "Childs"); //Ö¸¶¨±£´æ Childs Ò»¶Ô¶àÊôÐÔ
|
||
cts[0].Name = "·ÖÀà11";
|
||
cts[0].Childs.Clear();
|
||
cts[1].Name = "·ÖÀà22";
|
||
cts[1].Childs.Clear();
|
||
repo.Update(cts);
|
||
cts[0].Name = "·ÖÀà111";
|
||
cts[0].Childs.Clear();
|
||
cts[0].Childs.Add(new CagetoryParent { Name = "·ÖÀà1_33" });
|
||
cts[1].Name = "·ÖÀà222";
|
||
cts[1].Childs.Clear();
|
||
cts[1].Childs.Add(new CagetoryParent { Name = "·ÖÀà2_22" });
|
||
repo.Update(cts);
|
||
var treelist2 = repo.Select.ToTreeList();
|
||
}
|
||
[Table(Name = "EAUNL_OTMP_CT")]
|
||
class CagetoryParent
|
||
{
|
||
public Guid Id { get; set; }
|
||
public string Name { get; set; }
|
||
|
||
public Guid ParentId { get; set; }
|
||
[Navigate("ParentId")]
|
||
public List<CagetoryParent> Childs { get; set; }
|
||
}
|
||
|
||
[Fact]
|
||
public void EnableAddOrUpdateNavigateList_ManyToMany()
|
||
{
|
||
var tags = new[] {
|
||
new Tag { TagName = "Á÷ÐÐ" },
|
||
new Tag { TagName = "80ºó" },
|
||
new Tag { TagName = "00ºó" },
|
||
new Tag { TagName = "Ò¡¹ö" }
|
||
};
|
||
var ss = new[]
|
||
{
|
||
new Song
|
||
{
|
||
Name = "°®ÄãÒ»ÍòÄê.mp3",
|
||
Tags = new List<Tag>(new[]
|
||
{
|
||
tags[0], tags[1]
|
||
})
|
||
},
|
||
new Song
|
||
{
|
||
Name = "Àî°×.mp3",
|
||
Tags = new List<Tag>(new[]
|
||
{
|
||
tags[0], tags[2]
|
||
})
|
||
}
|
||
};
|
||
var repo = g.sqlite.GetRepository<Song>();
|
||
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; //´ò¿ª¼¶Áª±£´æ¹¦ÄÜ
|
||
repo.Insert(ss);
|
||
//repo.SaveMany(ss[0], "Tags"); //Ö¸¶¨±£´æ Tags ¶à¶Ô¶àÊôÐÔ
|
||
|
||
ss[0].Name = "°®ÄãÒ»ÍòÄê.mp5";
|
||
ss[0].Tags.Clear();
|
||
ss[0].Tags.Add(tags[0]);
|
||
ss[1].Name = "Àî°×.mp5";
|
||
ss[1].Tags.Clear();
|
||
ss[1].Tags.Add(tags[3]);
|
||
repo.Update(ss);
|
||
|
||
ss[0].Name = "°®ÄãÒ»ÍòÄê.mp4";
|
||
ss[0].Tags.Clear();
|
||
ss[1].Name = "Àî°×.mp4";
|
||
ss[1].Tags.Clear();
|
||
repo.Update(ss);
|
||
}
|
||
[Table(Name = "EAUNL_MTM_SONG")]
|
||
class Song
|
||
{
|
||
public Guid Id { get; set; }
|
||
public string Name { get; set; }
|
||
public List<Tag> Tags { get; set; }
|
||
}
|
||
[Table(Name = "EAUNL_MTM_TAG")]
|
||
class Tag
|
||
{
|
||
public Guid Id { get; set; }
|
||
public string TagName { get; set; }
|
||
public List<Song> Songs { get; set; }
|
||
}
|
||
[Table(Name = "EAUNL_MTM_SONGTAG")]
|
||
class SongTag
|
||
{
|
||
public Guid SongId { get; set; }
|
||
public Song Song { get; set; }
|
||
public Guid TagId { get; set; }
|
||
public Tag Tag { get; set; }
|
||
}
|
||
}
|
||
}
|