Ejemplo n.º 1
0
        public async Task UserPetService_PetPet()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);
            var userPet           = GetUserPetTest();
            var actionType        = ActionTypeEnum.Pet;
            var action            = new Domain.Entity.Action
            {
                ActionType = actionType, Date = new DateTime(2019, 9, 5), Id = 1, UserPet = userPet
            };

            userPetRepoMock.Setup(repo => repo.GetUserPet(userPet.Id))
            .ReturnsAsync(userPet)
            .Verifiable();

            userPetRepoMock.Setup(repo => repo.UpdateUserPet(userPet))
            .ReturnsAsync(userPet)
            .Verifiable();

            actionServiceMock.Setup(service => service.CreateAction(actionType, userPet))
            .ReturnsAsync(action)
            .Verifiable();

            //Act
            var result = await userPetService.PetPet(userPet.Id);

            Assert.IsType <UserPet>(result);
            actionServiceMock.Verify();
            userPetRepoMock.Verify();
        }
Ejemplo n.º 2
0
        public async Task UserPetService_GetUserPetList_NullUser()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);
            var user = GetUserTest();
            var list = GetUserPetListTest();

            userPetRepoMock.Setup(repo => repo.GetUserPetsByUser(user))
            .ReturnsAsync(list)
            .Verifiable();

            userServiceMock.Setup(service => service.GetUser(2))
            .ReturnsAsync((User)null)
            .Verifiable();

            //Act
            var result = await userPetService.GetUserPetList(2);

            //Assert
            Assert.Null(result);
            userPetRepoMock.Verify(repo => repo.GetUserPetsByUser(It.IsAny <User>()), Times.Never());
            userServiceMock.Verify();
        }
Ejemplo n.º 3
0
        public async Task UserPetService_GetUserPetList()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);
            var user = GetUserTest();
            var list = GetUserPetListTest();

            userPetRepoMock.Setup(repo => repo.GetUserPetsByUser(user))
            .ReturnsAsync(list)
            .Verifiable();

            userServiceMock.Setup(service => service.GetUser(user.Id))
            .ReturnsAsync(user)
            .Verifiable();

            //Act
            var result = await userPetService.GetUserPetList(user.Id);

            //Assert
            Assert.IsType <List <UserPet> >(result);
            userPetRepoMock.Verify();
            userServiceMock.Verify();
            Assert.Equal(list.Count, result.Count);
        }
Ejemplo n.º 4
0
        public async Task UserPetService_CreateUserPetTest_NullPet()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);
            var user = GetUserTest();

            userPetRepoMock.Setup(repo => repo.CreateUserPet(user, null))
            .ReturnsAsync((UserPet)null)
            .Verifiable();

            petServiceMock.Setup(service => service.GetPet(2))
            .ReturnsAsync((Pet)null)
            .Verifiable();

            userServiceMock.Setup(service => service.GetUser(user.Id))
            .ReturnsAsync(user)
            .Verifiable();

            //Act
            var result = await userPetService.CreateUserPet(2, user.Id);

            //Assert
            Assert.Null(result);
            userPetRepoMock.Verify(repo => repo.CreateUserPet(user, null), Times.Never());
            petServiceMock.Verify();
            userServiceMock.Verify();
        }
Ejemplo n.º 5
0
 public PetDeal(PetService petService, MemberInfoService memberInfoService, UserPetService userPetService,
                IDatabase database)
 {
     _database         = database;
     PetService        = petService;
     MemberInfoService = memberInfoService;
     UserPetService    = userPetService;
 }
Ejemplo n.º 6
0
        public async Task <GroupRes> Run(string msg, string account, string groupNo, Lazy <string> getLoginAccount)
        {
            var key = CacheConst.GetMemberOptKey(account, groupNo, CacheConst.AddPet);

            var cache = await _database.StringGetAsync(key);

            if (int.TryParse(cache, out var petId))
            {
                await _database.KeyDeleteAsync(key);

                var arr = msg.Split('@');

                // 格式错误直接跳过
                if (arr.Length != 2 || arr[1].Length != 1 || string.IsNullOrWhiteSpace(arr[0]))
                {
                    return(null);
                }

                var pet = await PetService.GetAsync(petId);

                Gender sex;

                if ("男".Equals(arr[1]))
                {
                    sex = Gender.MALE;
                }
                else if ("女".Equals(arr[1]))
                {
                    sex = Gender.FAMALE;
                }
                else
                {
                    return("性别错误!");
                }

                if (pet == null)
                {
                    return("宠物已下架!");
                }

                // 记录流水
                await BillFlowService.AddBillAsync(groupNo, account, pet.Price, pet.Price, BillTypes.Consume, "领养宠物");

                // 修正账户
                await MemberInfoService.ChangeAmountAsync(groupNo, account, -pet.Price);

                // 添加宠物
                var userPet = await UserPetService.AddPetAsync(pet, groupNo, account, sex, arr[0]);

                return(GroupRes.GetSuccess(new GroupItemRes()
                {
                    AtTa = true, Msg = $"恭喜您获得一只{userPet.Quality}品质的{pet.Name}"
                }));
            }

            return(null);
        }
Ejemplo n.º 7
0
 public AddPetCacheDeal(IDatabase database, UserPetService userPetService, MemberInfoService memberInfoService,
                        PetService petService, BillFlowService billFlowService)
 {
     this._database    = database;
     UserPetService    = userPetService;
     MemberInfoService = memberInfoService;
     PetService        = petService;
     BillFlowService   = billFlowService;
 }
Ejemplo n.º 8
0
        public void UserPetService_CalculatePetHunger(int minutes, int hungerRatio, int feedingValue,
                                                      int petHunger, int expectedResult)
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);

            //Act
            var result = userPetService.CalculatePetHunger(minutes, hungerRatio, feedingValue, petHunger);

            //Assert
            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 9
0
        public void UserPetService_CalculatePetHappiness(int minutes, int happinessRatio, int strokingValue,
                                                         int petHappiness, int expectedResult)
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);

            //Act
            var result = userPetService.CalculatePetHappiness(minutes, happinessRatio, strokingValue, petHappiness);

            //Assert
            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 10
0
        public async Task UserPetService_UpdatePetStatus_UserPetIsNull()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);

            //Act
            var result = await userPetService.UpdatePetStatus(null, 0, 0);

            //Assert
            Assert.Null(result);
            userPetRepoMock.Verify(repo => repo.UpdateUserPet(It.IsAny <UserPet>()), Times.Never());
            userPetRepoMock.Verify();
        }
Ejemplo n.º 11
0
        public async Task UserPetService_PetPet_NullUserPet()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);

            userPetRepoMock.Setup(repo => repo.GetUserPet(2))
            .ReturnsAsync((UserPet)null)
            .Verifiable();

            //Act
            var result = await userPetService.PetPet(2);

            Assert.Null(result);
            actionServiceMock.Verify(service => service.CreateAction(ActionTypeEnum.Pet, It.IsAny <UserPet>()), Times.Never());
            userPetRepoMock.Verify();
        }
Ejemplo n.º 12
0
        public async Task UserPetService_UpdatePetStatus()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);
            var pet     = GetPetTest();
            var userPet = GetUserPetTest();

            userPetRepoMock.Setup(repo => repo.UpdateUserPet(userPet))
            .ReturnsAsync(userPet)
            .Verifiable();

            //Act
            var result = await userPetService.UpdatePetStatus(userPet, 0, 0);

            //Assert
            Assert.IsType <UserPet>(result);
            userPetRepoMock.Verify();
        }
Ejemplo n.º 13
0
        public async Task UserPetService_CreateUserPetTest()
        {
            //Arrange
            var userPetRepoMock   = new Mock <IUserPetRepository>();
            var petServiceMock    = new Mock <IPetService>();
            var userServiceMock   = new Mock <IUserService>();
            var actionServiceMock = new Mock <IActionService>();
            var userPetService    = new UserPetService(userPetRepoMock.Object, petServiceMock.Object, userServiceMock.Object, actionServiceMock.Object);
            var userPet           = GetUserPetTest();
            var user = GetUserTest();
            var pet  = GetPetTest();

            userPetRepoMock.Setup(repo => repo.CreateUserPet(user, pet))
            .ReturnsAsync(userPet)
            .Verifiable();

            petServiceMock.Setup(service => service.GetPet(pet.Id))
            .ReturnsAsync(pet)
            .Verifiable();

            userServiceMock.Setup(service => service.GetUser(user.Id))
            .ReturnsAsync(user)
            .Verifiable();

            //Act
            var result = await userPetService.CreateUserPet(pet.Id, user.Id);

            //Assert
            Assert.IsType <UserPet>(result);
            userPetRepoMock.Verify();
            petServiceMock.Verify();
            userServiceMock.Verify();
            Assert.Equal(userPet, result);
            Assert.Equal(user.Id, result.User.Id);
            Assert.Equal(pet.Id, result.Pet.Id);
        }