Skip to content

Devin/1740582256 add user age field #872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public class User extends BaseEntity implements Serializable {
@ApiModelProperty(value = "头像存储的路径", hidden = true)
private String avatarPath;

@ApiModelProperty(value = "年龄")
private Integer age;

@ApiModelProperty(value = "密码")
private String password;

Expand Down Expand Up @@ -122,4 +125,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(id, username);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public class UserDto extends BaseDTO implements Serializable {
@ApiModelProperty(value = "头像路径")
private String avatarPath;

@ApiModelProperty(value = "年龄")
private Integer age;

@ApiModelProperty(value = "密码")
private String password;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public class UserQueryCriteria implements Serializable {
@ApiModelProperty(value = "创建时间")
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;

@Query
@ApiModelProperty(value = "年龄")
private Integer age;
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void update(User resources) throws Exception {
user.setPhone(resources.getPhone());
user.setNickName(resources.getNickName());
user.setGender(resources.getGender());
user.setAge(resources.getAge());
userRepository.save(user);
// 清除缓存
delCaches(user.getId(), user.getUsername());
Expand All @@ -154,6 +155,7 @@ public void updateCenter(User resources) {
user.setNickName(resources.getNickName());
user.setPhone(resources.getPhone());
user.setGender(resources.getGender());
user.setAge(resources.getAge());
userRepository.save(user);
// 清理缓存
delCaches(user.getId(), user.getUsername());
Expand Down Expand Up @@ -259,6 +261,7 @@ public void download(List<UserDto> queryAll, HttpServletResponse response) throw
map.put("邮箱", userDTO.getEmail());
map.put("状态", userDTO.getEnabled() ? "启用" : "禁用");
map.put("手机号码", userDTO.getPhone());
map.put("年龄", userDTO.getAge());
map.put("修改密码的时间", userDTO.getPwdResetTime());
map.put("创建日期", userDTO.getCreateTime());
list.add(map);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.zhengjie.modules.system.domain;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class UserTest {

@Test
public void testAgeField() {
// Create a new User instance
User user = new User();

// Set the age field
Integer expectedAge = 30;
user.setAge(expectedAge);

// Verify that the age field can be retrieved
assertEquals(expectedAge, user.getAge(), "Age field should be correctly set and retrieved");

// Test with null value
user.setAge(null);
assertNull(user.getAge(), "Age field should allow null values");

// Test with boundary values
user.setAge(0);
assertEquals(0, user.getAge(), "Age field should accept 0 as a valid value");

user.setAge(Integer.MAX_VALUE);
assertEquals(Integer.MAX_VALUE, user.getAge(), "Age field should accept maximum integer value");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.zhengjie.modules.system.service.dto;

import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class UserDtoTest {

@Autowired
private UserMapper userMapper;

@Test
public void testAgeFieldMapping() {
// Create a new User instance with age field
User user = new User();
Integer expectedAge = 25;
user.setAge(expectedAge);

// Map User to UserDto
UserDto userDto = userMapper.toDto(user);

// Verify that the age field is correctly mapped
assertEquals(expectedAge, userDto.getAge(), "Age field should be correctly mapped from User to UserDto");

// Create a new UserDto instance with age field
UserDto newUserDto = new UserDto();
Integer newExpectedAge = 35;
newUserDto.setAge(newExpectedAge);

// Map UserDto to User
User newUser = userMapper.toEntity(newUserDto);

// Verify that the age field is correctly mapped
assertEquals(newExpectedAge, newUser.getAge(), "Age field should be correctly mapped from UserDto to User");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class UserServiceDownloadTest {

@Mock
private UserRepository userRepository;

@Mock
private UserMapper userMapper;

@InjectMocks
private UserServiceImpl userService;

private List<UserDto> userDtoList;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);

// Create a test userDto list
userDtoList = new ArrayList<>();
UserDto userDto = new UserDto();
userDto.setId(1L);
userDto.setUsername("testuser");
userDto.setAge(30);
userDtoList.add(userDto);
}

@Test
public void testDownloadIncludesAgeField() throws Exception {
// Create a mock HttpServletResponse
MockHttpServletResponse response = new MockHttpServletResponse();

// Call download method
userService.download(userDtoList, response);

// Since we can't directly verify the Excel file content, we can use reflection to check if the age field is included in the map
// This is a simplified test that assumes the download method creates a list of maps with the user data
// In a real test, we would need to parse the Excel file to verify its content

// For now, we'll just verify that the method doesn't throw an exception
// This test is more of a placeholder and would need to be enhanced in a real testing environment

// No assertion needed as we're just checking that the method doesn't throw an exception
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Optional;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class UserServiceImplTest {

@Mock
private UserRepository userRepository;

@Mock
private UserMapper userMapper;

@InjectMocks
private UserServiceImpl userService;

private User user;
private UserDto userDto;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);

// Create a test user
user = new User();
user.setId(1L);
user.setUsername("testuser");
user.setAge(30);

// Create a test userDto
userDto = new UserDto();
userDto.setId(1L);
userDto.setUsername("testuser");
userDto.setAge(30);

// Mock repository findById
when(userRepository.findById(1L)).thenReturn(Optional.of(user));

// Mock mapper toDto
when(userMapper.toDto(user)).thenReturn(userDto);
}

@Test
public void testUpdateWithAgeField() throws Exception {
// Create a user with updated age
User updatedUser = new User();
updatedUser.setId(1L);
updatedUser.setUsername("testuser");
updatedUser.setAge(35);

// Call update method
userService.update(updatedUser);

// Verify that the age field is updated
verify(userRepository).save(argThat(savedUser ->
savedUser.getAge() != null && savedUser.getAge() == 35
));
}

@Test
public void testUpdateCenterWithAgeField() {
// Create a user with updated age
User updatedUser = new User();
updatedUser.setId(1L);
updatedUser.setUsername("testuser");
updatedUser.setAge(40);

// Call updateCenter method
userService.updateCenter(updatedUser);

// Verify that the age field is updated
verify(userRepository).save(argThat(savedUser ->
savedUser.getAge() != null && savedUser.getAge() == 40
));
}
}
2 changes: 2 additions & 0 deletions sql/add_age_to_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add age column to sys_user table
ALTER TABLE sys_user ADD COLUMN age INT DEFAULT NULL COMMENT '年龄';