models.test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Models test suite
  2. describe('Database Models', () => {
  3. // Mock database and models
  4. const mockDatabase = {
  5. write: jest.fn(),
  6. get: jest.fn(),
  7. };
  8. const mockCollection = {
  9. create: jest.fn(),
  10. find: jest.fn(),
  11. query: jest.fn(),
  12. };
  13. const mockModel = {
  14. id: 'test-id',
  15. update: jest.fn(),
  16. destroyPermanently: jest.fn(),
  17. };
  18. beforeEach(() => {
  19. jest.clearAllMocks();
  20. // Mock database methods
  21. mockDatabase.write.mockImplementation(async (callback) => {
  22. return await callback();
  23. });
  24. mockDatabase.get.mockReturnValue(mockCollection);
  25. mockCollection.create.mockResolvedValue(mockModel);
  26. mockCollection.find.mockResolvedValue(mockModel);
  27. mockCollection.query.mockReturnValue({
  28. fetch: jest.fn().mockResolvedValue([mockModel]),
  29. });
  30. });
  31. describe('User Model', () => {
  32. test('should create a user', async () => {
  33. const userData = {
  34. name: 'Test User',
  35. email: 'test@example.com',
  36. };
  37. const result = await mockCollection.create(user => {
  38. user.name = userData.name;
  39. user.email = userData.email;
  40. user.createdAt = new Date();
  41. user.updatedAt = new Date();
  42. });
  43. expect(result).toBe(mockModel);
  44. expect(mockCollection.create).toHaveBeenCalled();
  45. });
  46. test('should find a user by id', async () => {
  47. const userId = 'user-123';
  48. const result = await mockCollection.find(userId);
  49. expect(result).toBe(mockModel);
  50. expect(mockCollection.find).toHaveBeenCalledWith(userId);
  51. });
  52. test('should get all users', async () => {
  53. const result = await mockCollection.query().fetch();
  54. expect(result).toEqual([mockModel]);
  55. expect(mockCollection.query).toHaveBeenCalled();
  56. });
  57. });
  58. describe('TaskOrder Model', () => {
  59. test('should create a task order', async () => {
  60. const taskOrderData = {
  61. userId: 'user-123',
  62. orderNo: 'TASK001',
  63. checkType: '定期检验',
  64. unitPhone: '13800138000',
  65. unitContact: '张三',
  66. unitName: '测试单位',
  67. useUnitName: '使用单位',
  68. useUnitContact: '李四',
  69. useUnitPhone: '13900139000',
  70. taskStatus: 1,
  71. remark: '测试备注',
  72. planPrepared: '检验员',
  73. checkDate: new Date(),
  74. checkName: '检验员',
  75. };
  76. const result = await mockCollection.create(order => {
  77. order.user_id = taskOrderData.userId;
  78. order.orderNo = taskOrderData.orderNo;
  79. order.checkType = taskOrderData.checkType;
  80. order.unitPhone = taskOrderData.unitPhone;
  81. order.unitContact = taskOrderData.unitContact;
  82. order.unitName = taskOrderData.unitName;
  83. order.useUnitName = taskOrderData.useUnitName;
  84. order.useUnitContact = taskOrderData.useUnitContact;
  85. order.useUnitPhone = taskOrderData.useUnitPhone;
  86. order.taskStatus = taskOrderData.taskStatus;
  87. order.remark = taskOrderData.remark;
  88. order.planPrepared = taskOrderData.planPrepared;
  89. order.checkDate = taskOrderData.checkDate;
  90. order.checkName = taskOrderData.checkName;
  91. order.createdAt = new Date();
  92. order.updatedAt = new Date();
  93. });
  94. expect(result).toBe(mockModel);
  95. expect(mockCollection.create).toHaveBeenCalled();
  96. });
  97. test('should find a task order by id', async () => {
  98. const taskOrderId = 'task-123';
  99. const result = await mockCollection.find(taskOrderId);
  100. expect(result).toBe(mockModel);
  101. expect(mockCollection.find).toHaveBeenCalledWith(taskOrderId);
  102. });
  103. test('should get task orders by user id', async () => {
  104. const userId = 'user-123';
  105. const result = await mockCollection.query().fetch();
  106. expect(result).toEqual([mockModel]);
  107. expect(mockCollection.query).toHaveBeenCalled();
  108. });
  109. });
  110. describe('TaskOrderEquip Model', () => {
  111. test('should create equipment', async () => {
  112. const equipmentData = {
  113. taskOrderId: 'task-123',
  114. equipCode: 'EQ001',
  115. name: '压力容器',
  116. unitName: '制造厂',
  117. zipCode: '100001',
  118. unitAddress: '北京市',
  119. productNo: 'PN2023001',
  120. checkType: 1,
  121. type: '固定式压力容器',
  122. checkName: '检验员',
  123. nonconformance: '无',
  124. };
  125. const result = await mockCollection.create(equip => {
  126. equip.task_order_id = equipmentData.taskOrderId;
  127. equip.equipCode = equipmentData.equipCode;
  128. equip.name = equipmentData.name;
  129. equip.unitName = equipmentData.unitName;
  130. equip.zipCode = equipmentData.zipCode;
  131. equip.unitAddress = equipmentData.unitAddress;
  132. equip.productNo = equipmentData.productNo;
  133. equip.checkType = equipmentData.checkType;
  134. equip.type = equipmentData.type;
  135. equip.checkName = equipmentData.checkName;
  136. equip.nonconformance = equipmentData.nonconformance;
  137. equip.createdAt = new Date();
  138. equip.updatedAt = new Date();
  139. });
  140. expect(result).toBe(mockModel);
  141. expect(mockCollection.create).toHaveBeenCalled();
  142. });
  143. test('should find equipment by id', async () => {
  144. const equipmentId = 'equip-123';
  145. const result = await mockCollection.find(equipmentId);
  146. expect(result).toBe(mockModel);
  147. expect(mockCollection.find).toHaveBeenCalledWith(equipmentId);
  148. });
  149. test('should get equipment by task order id', async () => {
  150. const taskOrderId = 'task-123';
  151. const result = await mockCollection.query().fetch();
  152. expect(result).toEqual([mockModel]);
  153. expect(mockCollection.query).toHaveBeenCalled();
  154. });
  155. });
  156. describe('CheckItem Model', () => {
  157. test('should create a check item', async () => {
  158. const checkItemData = {
  159. taskOrderEquipId: 'equip-123',
  160. templateId: 'TPL001',
  161. name: '外观检查',
  162. reportNo: 'RPT001',
  163. taskStatus: 1,
  164. remark: '外观完好',
  165. prepareJson: '{}',
  166. reportConclusion: '合格',
  167. feeCalculateJson: '{}',
  168. };
  169. const result = await mockCollection.create(item => {
  170. item.task_order_equip_id = checkItemData.taskOrderEquipId;
  171. item.templateId = checkItemData.templateId;
  172. item.name = checkItemData.name;
  173. item.reportNo = checkItemData.reportNo;
  174. item.taskStatus = checkItemData.taskStatus;
  175. item.remark = checkItemData.remark;
  176. item.prepareJson = checkItemData.prepareJson;
  177. item.reportConclusion = checkItemData.reportConclusion;
  178. item.feeCalculateJson = checkItemData.feeCalculateJson;
  179. item.createdAt = new Date();
  180. item.updatedAt = new Date();
  181. });
  182. expect(result).toBe(mockModel);
  183. expect(mockCollection.create).toHaveBeenCalled();
  184. });
  185. test('should find a check item by id', async () => {
  186. const checkItemId = 'check-123';
  187. const result = await mockCollection.find(checkItemId);
  188. expect(result).toBe(mockModel);
  189. expect(mockCollection.find).toHaveBeenCalledWith(checkItemId);
  190. });
  191. test('should get check items by equipment id', async () => {
  192. const equipmentId = 'equip-123';
  193. const result = await mockCollection.query().fetch();
  194. expect(result).toEqual([mockModel]);
  195. expect(mockCollection.query).toHaveBeenCalled();
  196. });
  197. test('should find check item by template id', async () => {
  198. const templateId = 'TPL001';
  199. const result = await mockCollection.query().fetch();
  200. expect(result).toEqual([mockModel]);
  201. expect(mockCollection.query).toHaveBeenCalled();
  202. });
  203. });
  204. describe('Database Operations', () => {
  205. test('should handle database write operations', async () => {
  206. const operation = jest.fn().mockResolvedValue('success');
  207. const result = await mockDatabase.write(operation);
  208. expect(result).toBe('success');
  209. expect(mockDatabase.write).toHaveBeenCalledWith(operation);
  210. });
  211. test('should handle database write errors', async () => {
  212. const error = new Error('Database error');
  213. mockDatabase.write.mockRejectedValue(error);
  214. await expect(mockDatabase.write(jest.fn())).rejects.toThrow('Database error');
  215. });
  216. });
  217. });