123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Luticate2.Auth.DataAccess;
- using Luticate2.Auth.DataAccess.Models;
- using Luticate2.Auth.Dbo.Groups;
- using Luticate2.Utils.Dbo;
- using Microsoft.EntityFrameworkCore;
- using Moq;
- using Xunit;
-
- namespace Test.Auth.Groups
- {
- public class LuGroupsDataAccessTest
- {
- [Fact]
- public void TestMock()
- {
- var mockSet = new Mock<DbSet<lu_groups>>();
-
- var mockContext = new Mock<LuDatabaseContext>();
- mockContext.Setup(m => m.lu_groups).Returns(mockSet.Object);
-
- var service = new LuGroupsDataAccess(mockContext.Object);
- var res = service.AddDbo(new LuGroupsAddDbo {Name = "Test."});
- Assert.Equal(LuStatus.Success, res.Status);
- Assert.Equal(new Guid().ToString(), res.Data.Id);
- Assert.Equal("Test.", res.Data.Name);
-
- mockSet.Verify(m => m.AddRange(It.IsAny<IEnumerable<lu_groups>>()), Times.Once());
- mockContext.Verify(m => m.SaveChanges(), Times.Once());
- }
-
- [Fact]
- public void TestReal1()
- {
- Tests.TestRealDb(context =>
- {
- const string name = "Test.";
- var service = new LuGroupsDataAccess(context);
- var res = service.AddDbo(new LuGroupsAddDbo {Name = name});
- Assert.Equal(LuStatus.Success, res.Status);
- Assert.NotEqual(new Guid().ToString(), res.Data.Id);
- Assert.Equal(name, res.Data.Name);
-
- var get = service.GetSingleById(res.Data.Id);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(name, get.Data.Name);
- Assert.Equal(res.Data.Id, get.Data.Id);
- });
- }
-
- [Fact]
- public void TestReal2()
- {
- Tests.TestRealDb(context =>
- {
- const string name = "Test.";
- var service = new LuGroupsDataAccess(context);
- var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
- Assert.Equal(LuStatus.Success, res.Status);
- Assert.NotEqual(new Guid().ToString(), res.Data);
-
- var get = service.GetSingleById(res.Data);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(name, get.Data.Name);
- });
- }
-
- [Fact]
- public void TestReal3()
- {
- Tests.TestRealDb(context =>
- {
- const string name = "Test.";
- var service = new LuGroupsDataAccess(context);
- var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
- Assert.Equal(LuStatus.Success, res.Status);
- Assert.NotEqual(new Guid().ToString(), res.Data);
-
- var get = service.GetSingleById(res.Data);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(name, get.Data.Name);
- });
- }
-
- [Fact]
- public void TestReal4()
- {
- Tests.TestRealDb(context =>
- {
- var names = new List<string>{"001-Test.", "010-Test.", "030-Test.", "020-Test.", "000-Test."};
- var service = new LuGroupsDataAccess(context);
- var res = service.AddGuid(names.Select(s => new LuGroupsAddDbo {Name = s}).ToList());
- Assert.Equal(LuStatus.Success, res.Status);
- foreach (var id in res.Data)
- {
- Assert.NotEqual(new Guid().ToString(), id);
- }
-
- var get = service.GetMultiple(groups => groups.name, 0, 2);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(5, get.Data.Count);
- Assert.Equal(2, get.Data.Data.Count);
- Assert.Equal("000-Test.", get.Data.Data[0].Name);
- Assert.Equal("001-Test.", get.Data.Data[1].Name);
-
- get = service.GetMultiple(groups => groups.name, 2, 2);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(5, get.Data.Count);
- Assert.Equal(1, get.Data.Data.Count);
- Assert.Equal("030-Test.", get.Data.Data[0].Name);
-
- get = service.GetMultiple(groups => groups.name, 3, 2);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(5, get.Data.Count);
- Assert.Equal(0, get.Data.Data.Count);
-
- get = service.GetMultiple(groups => groups.name, groups => groups.name.Contains("0-"), 1, 1);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(4, get.Data.Count);
- Assert.Equal(1, get.Data.Data.Count);
- Assert.Equal("010-Test.", get.Data.Data[0].Name);
- });
- }
-
- [Fact]
- public void TestReal5()
- {
- Tests.TestRealDb(context =>
- {
- string[] names = {"001-Test.", "010-Test.", "030-Test.", "020-Test.", "000-Test."};
- var service = new LuGroupsDataAccess(context);
- var ids = new List<string>();
- foreach (var name in names)
- {
- var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
- Assert.Equal(LuStatus.Success, res.Status);
- Assert.NotEqual(new Guid().ToString(), res.Data);
- ids.Add(res.Data);
- }
-
- var edit = service.Edit(groups => groups.name.Contains("0-"),
- groups => groups.name = groups.name + "_Edited", dbos => dbos);
- Assert.Equal(LuStatus.Success, edit.Status);
- Assert.Equal(4, edit.Data.Count);
-
- var edit2 = service.EditSingleById(ids[0], groups => groups.name = groups.name + "_Edited", dbo => dbo);
- Assert.Equal(LuStatus.Success, edit2.Status);
-
- var get = service.GetMultiple(groups => groups.name, groups => groups.name.EndsWith("_Edited"), 0, names.Length);
- Assert.Equal(LuStatus.Success, get.Status);
- Assert.Equal(names.Length, get.Data.Count);
- Assert.Equal(names.Length, get.Data.Data.Count);
- });
- }
- }
- }
|