aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/api/check-params/views.ts48
-rw-r--r--server/tests/api/views/video-views-timeserie-stats.ts97
2 files changed, 141 insertions, 4 deletions
diff --git a/server/tests/api/check-params/views.ts b/server/tests/api/check-params/views.ts
index ca4752345..3dba2a42e 100644
--- a/server/tests/api/check-params/views.ts
+++ b/server/tests/api/check-params/views.ts
@@ -112,6 +112,54 @@ describe('Test videos views', function () {
112 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'hello' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) 112 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'hello' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
113 }) 113 })
114 114
115 it('Should fail with an invalid start date', async function () {
116 await servers[0].videoStats.getTimeserieStats({
117 videoId,
118 metric: 'viewers',
119 startDate: 'fake' as any,
120 endDate: new Date(),
121 expectedStatus: HttpStatusCode.BAD_REQUEST_400
122 })
123 })
124
125 it('Should fail with an invalid end date', async function () {
126 await servers[0].videoStats.getTimeserieStats({
127 videoId,
128 metric: 'viewers',
129 startDate: new Date(),
130 endDate: 'fake' as any,
131 expectedStatus: HttpStatusCode.BAD_REQUEST_400
132 })
133 })
134
135 it('Should fail if start date is specified but not end date', async function () {
136 await servers[0].videoStats.getTimeserieStats({
137 videoId,
138 metric: 'viewers',
139 startDate: new Date(),
140 expectedStatus: HttpStatusCode.BAD_REQUEST_400
141 })
142 })
143
144 it('Should fail if end date is specified but not start date', async function () {
145 await servers[0].videoStats.getTimeserieStats({
146 videoId,
147 metric: 'viewers',
148 endDate: new Date(),
149 expectedStatus: HttpStatusCode.BAD_REQUEST_400
150 })
151 })
152
153 it('Should fail with a too big interval', async function () {
154 await servers[0].videoStats.getTimeserieStats({
155 videoId,
156 metric: 'viewers',
157 startDate: new Date('2021-04-07T08:31:57.126Z'),
158 endDate: new Date(),
159 expectedStatus: HttpStatusCode.BAD_REQUEST_400
160 })
161 })
162
115 it('Should succeed with the correct parameters', async function () { 163 it('Should succeed with the correct parameters', async function () {
116 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' }) 164 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' })
117 }) 165 })
diff --git a/server/tests/api/views/video-views-timeserie-stats.ts b/server/tests/api/views/video-views-timeserie-stats.ts
index 858edeff7..4db76fe89 100644
--- a/server/tests/api/views/video-views-timeserie-stats.ts
+++ b/server/tests/api/views/video-views-timeserie-stats.ts
@@ -47,21 +47,31 @@ describe('Test views timeserie stats', function () {
47 let liveVideoId: string 47 let liveVideoId: string
48 let command: FfmpegCommand 48 let command: FfmpegCommand
49 49
50 function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) { 50 function expectTodayLastValue (result: VideoStatsTimeserie, lastValue: number) {
51 const { data } = result 51 const { data } = result
52 expect(data).to.have.lengthOf(30)
53 52
54 const last = data[data.length - 1] 53 const last = data[data.length - 1]
55
56 const today = new Date().getDate() 54 const today = new Date().getDate()
57 expect(new Date(last.date).getDate()).to.equal(today) 55 expect(new Date(last.date).getDate()).to.equal(today)
58 expect(last.value).to.equal(lastValue) 56 }
57
58 function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) {
59 const { data } = result
60 expect(data).to.have.lengthOf(30)
61
62 expectTodayLastValue(result, lastValue)
59 63
60 for (let i = 0; i < data.length - 2; i++) { 64 for (let i = 0; i < data.length - 2; i++) {
61 expect(data[i].value).to.equal(0) 65 expect(data[i].value).to.equal(0)
62 } 66 }
63 } 67 }
64 68
69 function expectInterval (result: VideoStatsTimeserie, intervalMs: number) {
70 const first = result.data[0]
71 const second = result.data[1]
72 expect(new Date(second.date).getTime() - new Date(first.date).getTime()).to.equal(intervalMs)
73 }
74
65 before(async function () { 75 before(async function () {
66 this.timeout(120000); 76 this.timeout(120000);
67 77
@@ -98,6 +108,85 @@ describe('Test views timeserie stats', function () {
98 } 108 }
99 }) 109 })
100 110
111 it('Should use a custom start/end date', async function () {
112 const now = new Date()
113 const tenDaysAgo = new Date()
114 tenDaysAgo.setDate(tenDaysAgo.getDate() - 9)
115
116 const result = await servers[0].videoStats.getTimeserieStats({
117 videoId: vodVideoId,
118 metric: 'aggregateWatchTime',
119 startDate: tenDaysAgo,
120 endDate: now
121 })
122
123 expect(result.groupInterval).to.equal('one_day')
124 expect(result.data).to.have.lengthOf(10)
125
126 const first = result.data[0]
127 expect(new Date(first.date).toLocaleDateString()).to.equal(tenDaysAgo.toLocaleDateString())
128
129 expectInterval(result, 24 * 3600 * 1000)
130 expectTodayLastValue(result, 9)
131 })
132
133 it('Should automatically group by hours', async function () {
134 const now = new Date()
135 const twoDaysAgo = new Date()
136 twoDaysAgo.setDate(twoDaysAgo.getDate() - 1)
137
138 const result = await servers[0].videoStats.getTimeserieStats({
139 videoId: vodVideoId,
140 metric: 'aggregateWatchTime',
141 startDate: twoDaysAgo,
142 endDate: now
143 })
144
145 expect(result.groupInterval).to.equal('one_hour')
146 expect(result.data).to.have.length.above(24).and.below(50)
147
148 expectInterval(result, 3600 * 1000)
149 expectTodayLastValue(result, 9)
150 })
151
152 it('Should automatically group by ten minutes', async function () {
153 const now = new Date()
154 const twoHoursAgo = new Date()
155 twoHoursAgo.setHours(twoHoursAgo.getHours() - 1)
156
157 const result = await servers[0].videoStats.getTimeserieStats({
158 videoId: vodVideoId,
159 metric: 'aggregateWatchTime',
160 startDate: twoHoursAgo,
161 endDate: now
162 })
163
164 expect(result.groupInterval).to.equal('ten_minutes')
165 expect(result.data).to.have.length.above(6).and.below(18)
166
167 expectInterval(result, 60 * 10 * 1000)
168 expectTodayLastValue(result, 9)
169 })
170
171 it('Should automatically group by one minute', async function () {
172 const now = new Date()
173 const thirtyAgo = new Date()
174 thirtyAgo.setMinutes(thirtyAgo.getMinutes() - 30)
175
176 const result = await servers[0].videoStats.getTimeserieStats({
177 videoId: vodVideoId,
178 metric: 'aggregateWatchTime',
179 startDate: thirtyAgo,
180 endDate: now
181 })
182
183 expect(result.groupInterval).to.equal('one_minute')
184 expect(result.data).to.have.length.above(20).and.below(40)
185
186 expectInterval(result, 60 * 1000)
187 expectTodayLastValue(result, 9)
188 })
189
101 after(async function () { 190 after(async function () {
102 await stopFfmpeg(command) 191 await stopFfmpeg(command)
103 }) 192 })