1
+ import { Test , TestingModule } from '@nestjs/testing' ;
2
+ import { getRepositoryToken } from '@nestjs/typeorm' ;
3
+ import { Between , ILike , Repository } from 'typeorm' ;
4
+ import { BackupDataService } from './backupData.service' ;
5
+ import { BackupDataEntity } from './entity/backupData.entity' ;
6
+ import { BackupDataFilterDto } from "./dto/backupDataFilter.dto" ;
7
+ import { BadRequestException } from "@nestjs/common" ;
8
+ import { CreateBackupDataDto } from "./dto/createBackupData.dto" ;
9
+ import { BackupDataOrderByOptions } from "./dto/backupDataOrderOptions.dto" ;
10
+ import { SortOrder } from "../utils/pagination/SortOrder" ;
11
+
12
+ const mockBackupDataEntity : BackupDataEntity = {
13
+ id : '123e4567-e89b-12d3-a456-426614174062' ,
14
+ sizeMB : 100 ,
15
+ creationDate : new Date ( '2023-12-30 00:00:00.000000' ) ,
16
+ bio : 'Test Bio' ,
17
+ } ;
18
+
19
+ const mockBackupDataRepository = {
20
+ findAndCount : jest . fn ( ) . mockResolvedValue ( [ [ mockBackupDataEntity ] , 1 ] ) ,
21
+ save : jest . fn ( ) . mockResolvedValue ( mockBackupDataEntity ) ,
22
+ findOne : jest . fn ( ) . mockResolvedValue ( mockBackupDataEntity ) ,
23
+ } ;
24
+
25
+ describe ( 'BackupDataService' , ( ) => {
26
+ let service : BackupDataService ;
27
+ let repository : Repository < BackupDataEntity > ;
28
+
29
+ beforeEach ( async ( ) => {
30
+ const module : TestingModule = await Test . createTestingModule ( {
31
+ providers : [
32
+ BackupDataService ,
33
+ {
34
+ provide : getRepositoryToken ( BackupDataEntity ) ,
35
+ useValue : mockBackupDataRepository ,
36
+ } ,
37
+ ] ,
38
+ } ) . compile ( ) ;
39
+
40
+ service = module . get ( BackupDataService ) ;
41
+ repository = module . get ( getRepositoryToken ( BackupDataEntity ) ) ;
42
+ } ) ;
43
+
44
+ it ( 'should be defined' , ( ) => {
45
+ expect ( service ) . toBeDefined ( ) ;
46
+ } ) ;
47
+ describe ( 'findAll' , ( ) => {
48
+ it ( 'should return paginated backup data with default sort by createdDate DESC' , async ( ) => {
49
+ const result = await service . findAll ( { offset : 0 , limit : 5 } , { } , { } ) ;
50
+ expect ( result ) . toEqual ( {
51
+ data : [ mockBackupDataEntity ] ,
52
+ paginationData : {
53
+ limit : 5 ,
54
+ offset : 0 ,
55
+ total : 1 ,
56
+ }
57
+ } ) ;
58
+ expect ( repository . findAndCount ) . toHaveBeenCalledWith ( {
59
+ take : 5 ,
60
+ order : { creationDate : 'DESC' } ,
61
+ where : { } ,
62
+ } ) ;
63
+ } ) ;
64
+ } ) ;
65
+
66
+ describe ( 'createWhereClause' , ( ) => {
67
+ it ( 'should create a where clause for ID search' , ( ) => {
68
+ const filterDto : BackupDataFilterDto = { id : '123' } ;
69
+ const where = service . createWhereClause ( filterDto ) ;
70
+ expect ( where ) . toEqual ( { id : ILike ( '%123%' ) } ) ;
71
+ } ) ;
72
+
73
+ it ( 'should create a where clause for date range search' , ( ) => {
74
+ const filterDto : BackupDataFilterDto = { fromDate : '2023-01-01' , toDate : '2023-12-31' } ;
75
+ const where = service . createWhereClause ( filterDto ) ;
76
+ expect ( where ) . toEqual ( { creationDate : Between ( new Date ( '2023-01-01' ) , new Date ( '2023-12-31' ) ) } ) ;
77
+ } ) ;
78
+
79
+ it ( 'should create a where clause for size range search' , ( ) => {
80
+ const filterDto : BackupDataFilterDto = { fromSizeMB : 10 , toSizeMB : 100 } ;
81
+ const where = service . createWhereClause ( filterDto ) ;
82
+ expect ( where ) . toEqual ( { sizeMB : Between ( 10 , 100 ) } ) ;
83
+ } ) ;
84
+
85
+ it ( 'should create a where clause for bio search' , ( ) => {
86
+ const filterDto : BackupDataFilterDto = { bio : 'test bio' } ;
87
+ const where = service . createWhereClause ( filterDto ) ;
88
+ expect ( where ) . toEqual ( { bio : ILike ( '%test bio%' ) } ) ;
89
+ } ) ;
90
+
91
+ it ( 'should create a where clause for combined filters' , ( ) => {
92
+ const filterDto : BackupDataFilterDto = {
93
+ id : '123' ,
94
+ fromDate : '2023-01-01' ,
95
+ toDate : '2023-12-31' ,
96
+ fromSizeMB : 10 ,
97
+ toSizeMB : 100 ,
98
+ bio : 'test bio' ,
99
+ } ;
100
+ const where = service . createWhereClause ( filterDto ) ;
101
+ expect ( where ) . toEqual ( {
102
+ id : ILike ( '%123%' ) ,
103
+ creationDate : Between ( new Date ( '2023-01-01' ) , new Date ( '2023-12-31' ) ) ,
104
+ sizeMB : Between ( 10 , 100 ) ,
105
+ bio : ILike ( '%test bio%' ) ,
106
+ } ) ;
107
+ } ) ;
108
+
109
+ it ( 'should throw an error for invalid fromDate' , ( ) => {
110
+ const filterDto : BackupDataFilterDto = { fromDate : 'invalid-date' } ;
111
+ expect ( ( ) => service . createWhereClause ( filterDto ) ) . toThrow ( BadRequestException ) ;
112
+ } ) ;
113
+
114
+ it ( 'should throw an error for invalid toDate' , ( ) => {
115
+ const filterDto : BackupDataFilterDto = { toDate : 'invalid-date' } ;
116
+ expect ( ( ) => service . createWhereClause ( filterDto ) ) . toThrow ( BadRequestException ) ;
117
+ } ) ;
118
+ } ) ;
119
+
120
+ describe ( 'createOrderClause' , ( ) => {
121
+ it ( 'should create an order clause for creationDate in DESC order by default' , ( ) => {
122
+ const orderClause = service . createOrderClause ( { } ) ;
123
+ expect ( orderClause ) . toEqual ( { creationDate : 'DESC' } ) ;
124
+ } ) ;
125
+
126
+ it ( 'should create an order clause for sizeMB in ASC order' , ( ) => {
127
+ const orderClause = service . createOrderClause ( {
128
+ orderBy : BackupDataOrderByOptions . SIZE ,
129
+ sortOrder : SortOrder . ASC
130
+ } ) ;
131
+ expect ( orderClause ) . toEqual ( { sizeMB : 'ASC' } ) ;
132
+ } ) ;
133
+
134
+ it ( 'should create an order clause for bio in DESC order' , ( ) => {
135
+ const orderClause = service . createOrderClause ( {
136
+ orderBy : BackupDataOrderByOptions . BIO ,
137
+ sortOrder : SortOrder . DESC
138
+ } ) ;
139
+ expect ( orderClause ) . toEqual ( { bio : 'DESC' } ) ;
140
+ } ) ;
141
+
142
+ it ( 'should create an order clause for id in ASC order' , ( ) => {
143
+ const orderClause = service . createOrderClause ( {
144
+ orderBy : BackupDataOrderByOptions . ID ,
145
+ sortOrder : SortOrder . ASC } ) ;
146
+ expect ( orderClause ) . toEqual ( { id : 'ASC' } ) ;
147
+ } ) ;
148
+ } ) ;
149
+
150
+ describe ( 'create' , ( ) => {
151
+ it ( 'should create a new backup data entity' , async ( ) => {
152
+ let createBackupDataDto : CreateBackupDataDto = new CreateBackupDataDto ( ) ;
153
+ Object . assign ( createBackupDataDto , mockBackupDataEntity ) ;
154
+
155
+ const result = await service . create ( createBackupDataDto ) ;
156
+ expect ( result ) . toEqual ( mockBackupDataEntity ) ;
157
+ expect ( repository . save ) . toHaveBeenCalledWith ( mockBackupDataEntity ) ;
158
+ } ) ;
159
+ } ) ;
160
+
161
+ describe ( 'findOneById' , ( ) => {
162
+ it ( 'should return a backup data entity by id' , async ( ) => {
163
+ const result = await service . findOneById ( mockBackupDataEntity . id ) ;
164
+ expect ( result ) . toEqual ( mockBackupDataEntity ) ;
165
+ expect ( repository . findOne ) . toHaveBeenCalledWith ( { where : { id : mockBackupDataEntity . id } } ) ;
166
+ } ) ;
167
+ } ) ;
168
+ } ) ;
0 commit comments