2020-11-14 14:30:45 +03:00
package services
import (
"github.com/muety/wakapi/mocks"
"github.com/muety/wakapi/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"math/rand"
"strings"
"testing"
"time"
)
const (
2021-12-14 17:30:03 +03:00
TestProjectLabel1 = "private"
TestProjectLabel2 = "work"
TestProjectLabel3 = "non-existing"
2020-11-14 14:30:45 +03:00
)
type SummaryServiceTestSuite struct {
suite . Suite
2021-06-12 12:09:24 +03:00
TestUser * models . User
TestStartTime time . Time
2021-12-14 17:30:03 +03:00
TestDurations [ ] * models . Duration
2021-06-12 12:26:15 +03:00
TestLabels [ ] * models . ProjectLabel
2021-06-12 12:09:24 +03:00
SummaryRepository * mocks . SummaryRepositoryMock
2021-12-14 17:30:03 +03:00
DurationService * mocks . DurationServiceMock
2021-06-12 12:09:24 +03:00
AliasService * mocks . AliasServiceMock
ProjectLabelService * mocks . ProjectLabelServiceMock
2020-11-14 14:30:45 +03:00
}
func ( suite * SummaryServiceTestSuite ) SetupSuite ( ) {
suite . TestUser = & models . User { ID : TestUserId }
suite . TestStartTime = time . Unix ( 0 , MinUnixTime1 )
2021-12-14 17:30:03 +03:00
suite . TestDurations = [ ] * models . Duration {
2020-11-14 14:30:45 +03:00
{
UserID : TestUserId ,
Project : TestProject1 ,
Language : TestLanguageGo ,
Editor : TestEditorGoland ,
OperatingSystem : TestOsLinux ,
Machine : TestMachine1 ,
2022-01-02 15:39:20 +03:00
Branch : TestBranchMaster ,
2020-11-14 14:30:45 +03:00
Time : models . CustomTime ( suite . TestStartTime ) ,
2021-12-14 17:30:03 +03:00
Duration : 150 * time . Second ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 2 ,
2020-11-14 14:30:45 +03:00
} ,
{
UserID : TestUserId ,
Project : TestProject1 ,
Language : TestLanguageGo ,
Editor : TestEditorGoland ,
OperatingSystem : TestOsLinux ,
Machine : TestMachine1 ,
2022-01-02 15:39:20 +03:00
Branch : TestBranchMaster ,
2021-12-14 17:30:03 +03:00
Time : models . CustomTime ( suite . TestStartTime . Add ( ( 30 + 130 ) * time . Second ) ) ,
Duration : 20 * time . Second ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 1 ,
2020-11-14 14:30:45 +03:00
} ,
{
UserID : TestUserId ,
Project : TestProject1 ,
Language : TestLanguageGo ,
Editor : TestEditorVscode ,
OperatingSystem : TestOsLinux ,
Machine : TestMachine1 ,
2022-01-02 15:39:20 +03:00
Branch : TestBranchDev ,
2020-11-14 14:30:45 +03:00
Time : models . CustomTime ( suite . TestStartTime . Add ( 3 * time . Minute ) ) ,
2021-12-14 17:30:03 +03:00
Duration : 15 * time . Second ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 3 ,
2020-11-14 14:30:45 +03:00
} ,
}
2021-06-12 12:26:15 +03:00
suite . TestLabels = [ ] * models . ProjectLabel {
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
ProjectKey : TestProject1 ,
Label : TestProjectLabel1 ,
} ,
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
2021-12-26 20:47:16 +03:00
ProjectKey : TestProject3 ,
Label : TestProjectLabel3 ,
2021-06-12 12:26:15 +03:00
} ,
}
2020-11-14 14:30:45 +03:00
}
func ( suite * SummaryServiceTestSuite ) BeforeTest ( suiteName , testName string ) {
suite . SummaryRepository = new ( mocks . SummaryRepositoryMock )
2021-12-14 17:30:03 +03:00
suite . DurationService = new ( mocks . DurationServiceMock )
2020-11-14 14:30:45 +03:00
suite . AliasService = new ( mocks . AliasServiceMock )
2021-06-12 12:09:24 +03:00
suite . ProjectLabelService = new ( mocks . ProjectLabelServiceMock )
2020-11-14 14:30:45 +03:00
}
func TestSummaryServiceTestSuite ( t * testing . T ) {
suite . Run ( t , new ( SummaryServiceTestSuite ) )
}
func ( suite * SummaryServiceTestSuite ) TestSummaryService_Summarize ( ) {
2021-12-14 17:30:03 +03:00
sut := NewSummaryService ( suite . SummaryRepository , suite . DurationService , suite . AliasService , suite . ProjectLabelService )
2021-06-12 12:09:24 +03:00
2020-11-14 14:30:45 +03:00
var (
from time . Time
to time . Time
result * models . Summary
err error
)
/* TEST 1 */
from , to = suite . TestStartTime . Add ( - 1 * time . Hour ) , suite . TestStartTime . Add ( - 1 * time . Minute )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , from , to , suite . TestUser , mock . Anything ) . Return ( filterDurations ( from , to , suite . TestDurations ) , nil )
2020-11-14 14:30:45 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Summarize ( from , to , suite . TestUser , nil )
2020-11-14 14:30:45 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
assert . Equal ( suite . T ( ) , from , result . FromTime . T ( ) )
assert . Equal ( suite . T ( ) , to , result . ToTime . T ( ) )
assert . Zero ( suite . T ( ) , result . TotalTime ( ) )
2021-12-22 12:17:05 +03:00
assert . Zero ( suite . T ( ) , result . NumHeartbeats )
2020-11-14 14:30:45 +03:00
assert . Empty ( suite . T ( ) , result . Projects )
/* TEST 2 */
from , to = suite . TestStartTime . Add ( - 1 * time . Hour ) , suite . TestStartTime . Add ( 1 * time . Second )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , from , to , suite . TestUser , mock . Anything ) . Return ( filterDurations ( from , to , suite . TestDurations ) , nil )
2020-11-14 14:30:45 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Summarize ( from , to , suite . TestUser , nil )
2020-11-14 14:30:45 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
2021-12-14 17:30:03 +03:00
assert . Equal ( suite . T ( ) , suite . TestDurations [ 0 ] . Time . T ( ) , result . FromTime . T ( ) )
assert . Equal ( suite . T ( ) , suite . TestDurations [ 0 ] . Time . T ( ) , result . ToTime . T ( ) )
assert . Equal ( suite . T ( ) , 150 * time . Second , result . TotalTime ( ) )
2021-12-22 12:17:05 +03:00
assert . Equal ( suite . T ( ) , 2 , result . NumHeartbeats )
2020-11-14 14:30:45 +03:00
assertNumAllItems ( suite . T ( ) , 1 , result , "" )
/* TEST 3 */
from , to = suite . TestStartTime , suite . TestStartTime . Add ( 1 * time . Hour )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , from , to , suite . TestUser , mock . Anything ) . Return ( filterDurations ( from , to , suite . TestDurations ) , nil )
2020-11-14 14:30:45 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Summarize ( from , to , suite . TestUser , nil )
2020-11-14 14:30:45 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
2021-12-14 17:30:03 +03:00
assert . Equal ( suite . T ( ) , suite . TestDurations [ 0 ] . Time . T ( ) , result . FromTime . T ( ) )
assert . Equal ( suite . T ( ) , suite . TestDurations [ len ( suite . TestDurations ) - 1 ] . Time . T ( ) , result . ToTime . T ( ) )
assert . Equal ( suite . T ( ) , 185 * time . Second , result . TotalTime ( ) )
2022-01-02 15:39:20 +03:00
assert . Equal ( suite . T ( ) , 185 * time . Second , result . TotalTimeBy ( models . SummaryProject ) )
assert . Equal ( suite . T ( ) , 185 * time . Second , result . TotalTimeBy ( models . SummaryOS ) )
assert . Equal ( suite . T ( ) , 185 * time . Second , result . TotalTimeBy ( models . SummaryMachine ) )
assert . Equal ( suite . T ( ) , 185 * time . Second , result . TotalTimeBy ( models . SummaryLanguage ) )
assert . Equal ( suite . T ( ) , 185 * time . Second , result . TotalTimeBy ( models . SummaryEditor ) )
assert . Zero ( suite . T ( ) , result . TotalTimeBy ( models . SummaryBranch ) ) // no filters -> no branches contained
assert . Zero ( suite . T ( ) , result . TotalTimeBy ( models . SummaryLabel ) )
2021-12-14 17:30:03 +03:00
assert . Equal ( suite . T ( ) , 170 * time . Second , result . TotalTimeByKey ( models . SummaryEditor , TestEditorGoland ) )
assert . Equal ( suite . T ( ) , 15 * time . Second , result . TotalTimeByKey ( models . SummaryEditor , TestEditorVscode ) )
2021-12-22 12:17:05 +03:00
assert . Equal ( suite . T ( ) , 6 , result . NumHeartbeats )
2020-11-14 14:30:45 +03:00
assert . Len ( suite . T ( ) , result . Editors , 2 )
assertNumAllItems ( suite . T ( ) , 1 , result , "e" )
}
func ( suite * SummaryServiceTestSuite ) TestSummaryService_Retrieve ( ) {
2021-12-14 17:30:03 +03:00
sut := NewSummaryService ( suite . SummaryRepository , suite . DurationService , suite . AliasService , suite . ProjectLabelService )
2021-06-12 12:09:24 +03:00
2020-11-14 14:30:45 +03:00
var (
summaries [ ] * models . Summary
from time . Time
to time . Time
result * models . Summary
err error
)
/* TEST 1 */
from , to = suite . TestStartTime . Add ( - 12 * time . Hour ) , suite . TestStartTime . Add ( 12 * time . Hour )
summaries = [ ] * models . Summary {
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
FromTime : models . CustomTime ( from . Add ( 10 * time . Minute ) ) ,
ToTime : models . CustomTime ( to . Add ( - 10 * time . Minute ) ) ,
Projects : [ ] * models . SummaryItem {
{
Type : models . SummaryProject ,
Key : TestProject1 ,
Total : 45 * time . Minute / time . Second , // hack
} ,
} ,
Languages : [ ] * models . SummaryItem { } ,
Editors : [ ] * models . SummaryItem { } ,
OperatingSystems : [ ] * models . SummaryItem { } ,
Machines : [ ] * models . SummaryItem { } ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 100 ,
2020-11-14 14:30:45 +03:00
} ,
}
suite . SummaryRepository . On ( "GetByUserWithin" , suite . TestUser , from , to ) . Return ( summaries , nil )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , from , summaries [ 0 ] . FromTime . T ( ) , suite . TestUser , mock . Anything ) . Return ( models . Durations { } , nil )
suite . DurationService . On ( "Get" , summaries [ 0 ] . ToTime . T ( ) , to , suite . TestUser , mock . Anything ) . Return ( models . Durations { } , nil )
2020-11-14 14:30:45 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Retrieve ( from , to , suite . TestUser , nil )
2020-11-14 14:30:45 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
assert . Len ( suite . T ( ) , result . Projects , 1 )
assert . Equal ( suite . T ( ) , summaries [ 0 ] . Projects [ 0 ] . Total * time . Second , result . TotalTime ( ) )
2021-12-22 12:17:05 +03:00
assert . Equal ( suite . T ( ) , 100 , result . NumHeartbeats )
2021-12-14 17:30:03 +03:00
suite . DurationService . AssertNumberOfCalls ( suite . T ( ) , "Get" , 2 )
2020-11-14 14:30:45 +03:00
/* TEST 2 */
from , to = suite . TestStartTime . Add ( - 10 * time . Minute ) , suite . TestStartTime . Add ( 12 * time . Hour )
summaries = [ ] * models . Summary {
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
FromTime : models . CustomTime ( from . Add ( 20 * time . Minute ) ) ,
ToTime : models . CustomTime ( to . Add ( - 6 * time . Hour ) ) ,
Projects : [ ] * models . SummaryItem {
{
Type : models . SummaryProject ,
Key : TestProject1 ,
Total : 45 * time . Minute / time . Second , // hack
} ,
} ,
Languages : [ ] * models . SummaryItem { } ,
Editors : [ ] * models . SummaryItem { } ,
OperatingSystems : [ ] * models . SummaryItem { } ,
Machines : [ ] * models . SummaryItem { } ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 100 ,
2020-11-14 14:30:45 +03:00
} ,
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
FromTime : models . CustomTime ( to . Add ( - 6 * time . Hour ) ) ,
ToTime : models . CustomTime ( to ) ,
Projects : [ ] * models . SummaryItem {
{
Type : models . SummaryProject ,
Key : TestProject2 ,
Total : 45 * time . Minute / time . Second , // hack
} ,
} ,
Languages : [ ] * models . SummaryItem { } ,
Editors : [ ] * models . SummaryItem { } ,
OperatingSystems : [ ] * models . SummaryItem { } ,
Machines : [ ] * models . SummaryItem { } ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 100 ,
2020-11-14 14:30:45 +03:00
} ,
}
suite . SummaryRepository . On ( "GetByUserWithin" , suite . TestUser , from , to ) . Return ( summaries , nil )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , from , summaries [ 0 ] . FromTime . T ( ) , suite . TestUser , mock . Anything ) . Return ( filterDurations ( from , summaries [ 0 ] . FromTime . T ( ) , suite . TestDurations ) , nil )
2020-11-14 14:30:45 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Retrieve ( from , to , suite . TestUser , nil )
2020-11-14 14:30:45 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
assert . Len ( suite . T ( ) , result . Projects , 2 )
2021-12-14 17:30:03 +03:00
assert . Equal ( suite . T ( ) , 185 * time . Second + 90 * time . Minute , result . TotalTime ( ) )
assert . Equal ( suite . T ( ) , 185 * time . Second + 45 * time . Minute , result . TotalTimeByKey ( models . SummaryProject , TestProject1 ) )
2020-11-14 14:30:45 +03:00
assert . Equal ( suite . T ( ) , 45 * time . Minute , result . TotalTimeByKey ( models . SummaryProject , TestProject2 ) )
2021-12-22 12:17:05 +03:00
assert . Equal ( suite . T ( ) , 206 , result . NumHeartbeats )
2021-12-14 17:30:03 +03:00
suite . DurationService . AssertNumberOfCalls ( suite . T ( ) , "Get" , 2 + 1 )
2021-04-12 23:57:15 +03:00
/* TEST 3 */
from = time . Date ( suite . TestStartTime . Year ( ) , suite . TestStartTime . Month ( ) , suite . TestStartTime . Day ( ) + 1 , 0 , 0 , 0 , 0 , suite . TestStartTime . Location ( ) ) // start of next day
to = time . Date ( from . Year ( ) , from . Month ( ) , from . Day ( ) + 2 , 13 , 30 , 0 , 0 , from . Location ( ) ) // noon of third-next day
summaries = [ ] * models . Summary {
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
FromTime : models . CustomTime ( from ) ,
ToTime : models . CustomTime ( from . Add ( 24 * time . Hour ) ) ,
Projects : [ ] * models . SummaryItem {
{
Type : models . SummaryProject ,
Key : TestProject1 ,
Total : 45 * time . Minute / time . Second , // hack
} ,
} ,
Languages : [ ] * models . SummaryItem { } ,
Editors : [ ] * models . SummaryItem { } ,
OperatingSystems : [ ] * models . SummaryItem { } ,
Machines : [ ] * models . SummaryItem { } ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 100 ,
2021-04-12 23:57:15 +03:00
} ,
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
FromTime : models . CustomTime ( to . Add ( - 2 * time . Hour ) ) ,
ToTime : models . CustomTime ( to ) ,
Projects : [ ] * models . SummaryItem {
{
Type : models . SummaryProject ,
Key : TestProject2 ,
Total : 45 * time . Minute / time . Second , // hack
} ,
} ,
Languages : [ ] * models . SummaryItem { } ,
Editors : [ ] * models . SummaryItem { } ,
OperatingSystems : [ ] * models . SummaryItem { } ,
Machines : [ ] * models . SummaryItem { } ,
2021-12-22 12:17:05 +03:00
NumHeartbeats : 100 ,
2021-04-12 23:57:15 +03:00
} ,
}
suite . SummaryRepository . On ( "GetByUserWithin" , suite . TestUser , from , to ) . Return ( summaries , nil )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , summaries [ 0 ] . ToTime . T ( ) , summaries [ 1 ] . FromTime . T ( ) , suite . TestUser , mock . Anything ) . Return ( filterDurations ( summaries [ 0 ] . ToTime . T ( ) , summaries [ 1 ] . FromTime . T ( ) , suite . TestDurations ) , nil )
2021-04-12 23:57:15 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Retrieve ( from , to , suite . TestUser , nil )
2021-04-12 23:57:15 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
assert . Len ( suite . T ( ) , result . Projects , 2 )
assert . Equal ( suite . T ( ) , 90 * time . Minute , result . TotalTime ( ) )
assert . Equal ( suite . T ( ) , 45 * time . Minute , result . TotalTimeByKey ( models . SummaryProject , TestProject1 ) )
assert . Equal ( suite . T ( ) , 45 * time . Minute , result . TotalTimeByKey ( models . SummaryProject , TestProject2 ) )
2021-12-22 12:17:05 +03:00
assert . Equal ( suite . T ( ) , 200 , result . NumHeartbeats )
2022-04-18 16:18:01 +03:00
suite . DurationService . AssertNumberOfCalls ( suite . T ( ) , "Get" , 2 + 1 )
2020-11-14 14:30:45 +03:00
}
2021-04-19 21:48:07 +03:00
func ( suite * SummaryServiceTestSuite ) TestSummaryService_Retrieve_DuplicateSummaries ( ) {
2021-12-14 17:30:03 +03:00
sut := NewSummaryService ( suite . SummaryRepository , suite . DurationService , suite . AliasService , suite . ProjectLabelService )
2021-06-12 12:09:24 +03:00
suite . ProjectLabelService . On ( "GetByUser" , suite . TestUser . ID ) . Return ( [ ] * models . ProjectLabel { } , nil )
2021-04-19 21:48:07 +03:00
var (
summaries [ ] * models . Summary
from time . Time
to time . Time
result * models . Summary
err error
)
from , to = suite . TestStartTime . Add ( - 12 * time . Hour ) , suite . TestStartTime . Add ( 12 * time . Hour )
summaries = [ ] * models . Summary {
{
ID : uint ( rand . Uint32 ( ) ) ,
UserID : TestUserId ,
FromTime : models . CustomTime ( from . Add ( 10 * time . Minute ) ) ,
ToTime : models . CustomTime ( to . Add ( - 10 * time . Minute ) ) ,
Projects : [ ] * models . SummaryItem {
{
Type : models . SummaryProject ,
Key : TestProject1 ,
Total : 45 * time . Minute / time . Second , // hack
} ,
} ,
Languages : [ ] * models . SummaryItem { } ,
Editors : [ ] * models . SummaryItem { } ,
OperatingSystems : [ ] * models . SummaryItem { } ,
Machines : [ ] * models . SummaryItem { } ,
} ,
}
summaries = append ( summaries , & ( * summaries [ 0 ] ) ) // add same summary again -> mustn't be counted twice!
suite . SummaryRepository . On ( "GetByUserWithin" , suite . TestUser , from , to ) . Return ( summaries , nil )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , from , summaries [ 0 ] . FromTime . T ( ) , suite . TestUser , mock . Anything ) . Return ( models . Durations { } , nil )
suite . DurationService . On ( "Get" , summaries [ 0 ] . ToTime . T ( ) , to , suite . TestUser , mock . Anything ) . Return ( models . Durations { } , nil )
2021-04-19 21:48:07 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Retrieve ( from , to , suite . TestUser , nil )
2021-04-19 21:48:07 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
assert . Len ( suite . T ( ) , result . Projects , 1 )
assert . Equal ( suite . T ( ) , summaries [ 0 ] . Projects [ 0 ] . Total * time . Second , result . TotalTime ( ) )
2021-12-14 17:30:03 +03:00
suite . DurationService . AssertNumberOfCalls ( suite . T ( ) , "Get" , 2 )
2021-04-19 21:48:07 +03:00
}
2020-11-14 14:30:45 +03:00
func ( suite * SummaryServiceTestSuite ) TestSummaryService_Aliased ( ) {
2021-12-14 17:30:03 +03:00
sut := NewSummaryService ( suite . SummaryRepository , suite . DurationService , suite . AliasService , suite . ProjectLabelService )
2021-06-12 12:09:24 +03:00
2021-12-26 19:02:14 +03:00
suite . AliasService . On ( "InitializeUser" , suite . TestUser . ID ) . Return ( nil )
2021-06-12 12:09:24 +03:00
suite . ProjectLabelService . On ( "GetByUser" , suite . TestUser . ID ) . Return ( [ ] * models . ProjectLabel { } , nil )
2020-11-14 14:30:45 +03:00
var (
from time . Time
to time . Time
result * models . Summary
err error
)
from , to = suite . TestStartTime , suite . TestStartTime . Add ( 1 * time . Hour )
2021-08-07 01:12:45 +03:00
2021-12-14 17:30:03 +03:00
durations := filterDurations ( from , to , suite . TestDurations )
durations = append ( durations , & models . Duration {
2021-08-07 01:12:45 +03:00
UserID : TestUserId ,
Project : TestProject2 ,
Language : TestLanguageGo ,
Editor : TestEditorGoland ,
OperatingSystem : TestOsLinux ,
Machine : TestMachine1 ,
2021-12-14 17:30:03 +03:00
Time : models . CustomTime ( durations [ len ( durations ) - 1 ] . Time . T ( ) . Add ( 10 * time . Second ) ) ,
Duration : 0 , // not relevant here
2021-08-07 01:12:45 +03:00
} )
2021-12-26 20:47:16 +03:00
suite . DurationService . On ( "Get" , from , to , suite . TestUser , mock . Anything ) . Return ( durations , nil )
2021-01-21 02:26:52 +03:00
suite . AliasService . On ( "InitializeUser" , TestUserId ) . Return ( nil )
2021-08-07 01:12:45 +03:00
suite . AliasService . On ( "GetAliasOrDefault" , TestUserId , mock . Anything , TestProject1 ) . Return ( TestProject2 , nil )
suite . AliasService . On ( "GetAliasOrDefault" , TestUserId , mock . Anything , TestProject2 ) . Return ( TestProject2 , nil )
2020-11-14 14:30:45 +03:00
suite . AliasService . On ( "GetAliasOrDefault" , TestUserId , mock . Anything , mock . Anything ) . Return ( "" , nil )
2021-08-07 01:12:45 +03:00
suite . ProjectLabelService . On ( "GetByUser" , suite . TestUser . ID ) . Return ( suite . TestLabels , nil ) . Once ( )
2020-11-14 14:30:45 +03:00
2021-12-26 19:02:14 +03:00
result , err = sut . Aliased ( from , to , suite . TestUser , sut . Summarize , nil , false )
2020-11-14 14:30:45 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
assert . Zero ( suite . T ( ) , result . TotalTimeByKey ( models . SummaryProject , TestProject1 ) )
assert . NotZero ( suite . T ( ) , result . TotalTimeByKey ( models . SummaryProject , TestProject2 ) )
2021-12-22 12:17:05 +03:00
assert . Equal ( suite . T ( ) , 6 , result . NumHeartbeats )
2022-01-02 15:39:20 +03:00
assert . Nil ( suite . T ( ) , result . Branches )
2020-11-14 14:30:45 +03:00
}
2021-08-07 01:12:45 +03:00
func ( suite * SummaryServiceTestSuite ) TestSummaryService_Aliased_ProjectLabels ( ) {
2021-12-14 17:30:03 +03:00
sut := NewSummaryService ( suite . SummaryRepository , suite . DurationService , suite . AliasService , suite . ProjectLabelService )
2021-08-07 01:12:45 +03:00
var (
from time . Time
to time . Time
result * models . Summary
err error
)
from , to = suite . TestStartTime , suite . TestStartTime . Add ( 1 * time . Hour )
2021-12-14 17:30:03 +03:00
durations := filterDurations ( from , to , suite . TestDurations )
durations = append ( durations , & models . Duration {
2021-08-07 01:12:45 +03:00
UserID : TestUserId ,
Project : TestProject2 ,
Language : TestLanguageGo ,
Editor : TestEditorGoland ,
OperatingSystem : TestOsLinux ,
Machine : TestMachine1 ,
2021-12-14 17:30:03 +03:00
Time : models . CustomTime ( durations [ len ( durations ) - 1 ] . Time . T ( ) . Add ( 10 * time . Second ) ) ,
Duration : 10 * time . Second ,
2021-08-07 01:12:45 +03:00
} )
suite . ProjectLabelService . On ( "GetByUser" , suite . TestUser . ID ) . Return ( suite . TestLabels , nil ) . Once ( )
2021-12-26 19:02:14 +03:00
suite . DurationService . On ( "Get" , from , to , suite . TestUser , mock . Anything ) . Return ( models . Durations ( durations ) , nil )
2021-08-07 01:12:45 +03:00
suite . AliasService . On ( "InitializeUser" , TestUserId ) . Return ( nil )
suite . AliasService . On ( "GetAliasOrDefault" , TestUserId , mock . Anything , TestProject1 ) . Return ( TestProject1 , nil )
suite . AliasService . On ( "GetAliasOrDefault" , TestUserId , mock . Anything , TestProject2 ) . Return ( TestProject1 , nil )
suite . AliasService . On ( "GetAliasOrDefault" , TestUserId , mock . Anything , mock . Anything ) . Return ( "" , nil )
2021-12-26 19:02:14 +03:00
result , err = sut . Aliased ( from , to , suite . TestUser , sut . Summarize , nil , false )
2021-08-07 01:12:45 +03:00
assert . Nil ( suite . T ( ) , err )
assert . NotNil ( suite . T ( ) , result )
2021-12-14 17:30:03 +03:00
assert . Equal ( suite . T ( ) , 195 * time . Second , result . TotalTimeByKey ( models . SummaryLabel , TestProjectLabel1 ) )
2021-12-22 12:17:05 +03:00
assert . Equal ( suite . T ( ) , 6 , result . NumHeartbeats )
2021-08-07 01:12:45 +03:00
}
2021-12-26 20:47:16 +03:00
func ( suite * SummaryServiceTestSuite ) TestSummaryService_Filters ( ) {
sut := NewSummaryService ( suite . SummaryRepository , suite . DurationService , suite . AliasService , suite . ProjectLabelService )
suite . AliasService . On ( "InitializeUser" , suite . TestUser . ID ) . Return ( nil )
suite . ProjectLabelService . On ( "GetByUser" , suite . TestUser . ID ) . Return ( [ ] * models . ProjectLabel { } , nil )
from , to := suite . TestStartTime , suite . TestStartTime . Add ( 1 * time . Hour )
filters := models . NewFiltersWith ( models . SummaryProject , TestProject1 ) . With ( models . SummaryLabel , TestProjectLabel3 )
suite . DurationService . On ( "Get" , from , to , suite . TestUser , mock . Anything ) . Return ( models . Durations { } , nil )
suite . AliasService . On ( "InitializeUser" , TestUserId ) . Return ( nil )
suite . AliasService . On ( "GetByUserAndKeyAndType" , TestUserId , TestProject1 , models . SummaryProject ) . Return ( [ ] * models . Alias {
{
Type : models . SummaryProject ,
Key : TestProject1 ,
Value : TestProject2 ,
} ,
} , nil )
suite . ProjectLabelService . On ( "GetByUserGroupedInverted" , suite . TestUser . ID ) . Return ( map [ string ] [ ] * models . ProjectLabel {
suite . TestLabels [ 0 ] . Label : suite . TestLabels [ 0 : 1 ] ,
suite . TestLabels [ 1 ] . Label : suite . TestLabels [ 1 : 2 ] ,
} , nil ) . Once ( )
2022-01-02 15:39:20 +03:00
result , _ := sut . Aliased ( from , to , suite . TestUser , sut . Summarize , filters , false )
assert . NotNil ( suite . T ( ) , result . Branches ) // project filters were applied -> include branches
2021-12-26 20:47:16 +03:00
effectiveFilters := suite . DurationService . Calls [ 0 ] . Arguments [ 3 ] . ( * models . Filters )
assert . Contains ( suite . T ( ) , effectiveFilters . Project , TestProject1 ) // because actually requested
assert . Contains ( suite . T ( ) , effectiveFilters . Project , TestProject2 ) // because of alias
assert . Contains ( suite . T ( ) , effectiveFilters . Project , TestProject3 ) // because of label
assert . Contains ( suite . T ( ) , effectiveFilters . Label , TestProjectLabel3 )
}
2022-03-25 14:48:56 +03:00
func ( suite * SummaryServiceTestSuite ) TestSummaryService_getMissingIntervals ( ) {
sut := NewSummaryService ( suite . SummaryRepository , suite . DurationService , suite . AliasService , suite . ProjectLabelService )
from1 , _ := time . Parse ( time . RFC822 , "25 Mar 22 11:00 UTC" )
to1 , _ := time . Parse ( time . RFC822 , "25 Mar 22 13:00 UTC" )
from2 , _ := time . Parse ( time . RFC822 , "25 Mar 22 15:00 UTC" )
to2 , _ := time . Parse ( time . RFC822 , "26 Mar 22 00:00 UTC" )
summaries := [ ] * models . Summary {
{ FromTime : models . CustomTime ( from1 ) , ToTime : models . CustomTime ( to1 ) } ,
{ FromTime : models . CustomTime ( from2 ) , ToTime : models . CustomTime ( to2 ) } ,
}
r1 := sut . getMissingIntervals ( from1 , to1 , summaries , true )
assert . Empty ( suite . T ( ) , r1 )
r2 := sut . getMissingIntervals ( from1 , from1 , summaries , true )
assert . Empty ( suite . T ( ) , r2 )
// non-precise mode will not return intra-day intervals
// we might want to change this ...
r3 := sut . getMissingIntervals ( from1 , to2 , summaries , false )
assert . Len ( suite . T ( ) , r3 , 0 )
r4 := sut . getMissingIntervals ( from1 , to2 , summaries , true )
assert . Len ( suite . T ( ) , r4 , 1 )
assert . Equal ( suite . T ( ) , to1 , r4 [ 0 ] . Start )
assert . Equal ( suite . T ( ) , from2 , r4 [ 0 ] . End )
r5 := sut . getMissingIntervals ( from1 . Add ( - time . Hour ) , to2 . Add ( time . Hour ) , summaries , true )
assert . Len ( suite . T ( ) , r5 , 3 )
assert . Equal ( suite . T ( ) , from1 . Add ( - time . Hour ) , r5 [ 0 ] . Start )
assert . Equal ( suite . T ( ) , from1 , r5 [ 0 ] . End )
assert . Equal ( suite . T ( ) , to1 , r5 [ 1 ] . Start )
assert . Equal ( suite . T ( ) , from2 , r5 [ 1 ] . End )
assert . Equal ( suite . T ( ) , to2 , r5 [ 2 ] . Start )
assert . Equal ( suite . T ( ) , to2 . Add ( time . Hour ) , r5 [ 2 ] . End )
}
2021-12-26 15:25:41 +03:00
func filterDurations ( from , to time . Time , durations models . Durations ) models . Durations {
2021-12-14 17:30:03 +03:00
filtered := make ( [ ] * models . Duration , 0 , len ( durations ) )
for _ , d := range durations {
if ( d . Time . T ( ) . Equal ( from ) || d . Time . T ( ) . After ( from ) ) && d . Time . T ( ) . Before ( to ) {
filtered = append ( filtered , d )
2020-11-14 14:30:45 +03:00
}
}
return filtered
}
func assertNumAllItems ( t * testing . T , expected int , summary * models . Summary , except string ) {
if ! strings . Contains ( except , "p" ) {
assert . Len ( t , summary . Projects , expected )
}
if ! strings . Contains ( except , "e" ) {
assert . Len ( t , summary . Editors , expected )
}
if ! strings . Contains ( except , "l" ) {
assert . Len ( t , summary . Languages , expected )
}
if ! strings . Contains ( except , "o" ) {
assert . Len ( t , summary . OperatingSystems , expected )
}
if ! strings . Contains ( except , "m" ) {
assert . Len ( t , summary . Machines , expected )
}
}