3 add occupancy bson endpoint in backend

This commit is contained in:
survellow
2024-05-23 22:47:52 +02:00
parent d73f7e284b
commit 114a309e8b
9 changed files with 740 additions and 4 deletions

View File

@@ -17,10 +17,12 @@
package room
import (
"github.com/pocketbase/pocketbase/tools/types"
"htwkalender/model"
"reflect"
"testing"
"time"
"github.com/pocketbase/pocketbase/tools/types"
)
func Test_anonymizeRooms(t *testing.T) {
@@ -305,3 +307,275 @@ func Test_getFreeRooms(t *testing.T) {
})
}
}
func Test_encodeRoomSchedule(t *testing.T) {
testTime, _ := time.Parse(time.RFC3339, "2024-12-24T12:00:00Z")
testDateTime, _ := types.ParseDateTime(testTime)
testDateTime_m15, _ := types.ParseDateTime(testTime.Add(-time.Minute * 15))
testDateTime_p10, _ := types.ParseDateTime(testTime.Add(time.Minute * 10))
testDateTime_p15, _ := types.ParseDateTime(testTime.Add(time.Minute * 15))
testDateTime_p30, _ := types.ParseDateTime(testTime.Add(time.Minute * 30))
testDateTime_p45, _ := types.ParseDateTime(testTime.Add(time.Minute * 45))
testDateTime_late, _ := types.ParseDateTime(testTime.Add(time.Hour * 100))
type args struct {
roomSchedule []model.AnonymizedEventDTO
start time.Time
granularity int
blockCount int
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "encode event without length",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime_p10,
End: testDateTime_p10,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 15,
blockCount: 4,
},
want: []byte{
0x00,
},
wantErr: false,
},
{
name: "ignore event with start time after end time",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime_p30,
End: testDateTime_p10,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 15,
blockCount: 4,
},
want: []byte{
0x00,
},
wantErr: false,
},
{
name: "encode time table without length",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime,
End: testDateTime_p10,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 15,
blockCount: 0,
},
want: []byte{},
wantErr: false,
},
{
name: "encode time table without events",
args: args{
roomSchedule: []model.AnonymizedEventDTO{},
start: testTime,
granularity: 15,
blockCount: 24,
},
want: []byte{
0x00, 0x00, 0x00,
},
wantErr: false,
},
{
name: "encode time table with single event",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime_p30,
End: testDateTime_late,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 30,
blockCount: 50,
},
want: []byte{
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
},
},
{
name: "ignore free event",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime_p15,
End: testDateTime_p45,
Rooms: "Room",
Free: false,
},
{
Day: "Montag",
Week: "52",
Start: testDateTime,
End: testDateTime_p30,
Rooms: "Room",
Free: true,
},
},
start: testTime,
granularity: 15,
blockCount: 50,
},
want: []byte{
0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
{
name: "encode time table with multiple events",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime,
End: testDateTime_p15,
Rooms: "Room",
Free: false,
},
{
Day: "Montag",
Week: "52",
Start: testDateTime_p30,
End: testDateTime_p45,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 15,
blockCount: 4,
},
want: []byte{
0xA0,
},
},
{
name: "encode time table with multiple unordered events",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime_p30,
End: testDateTime_p45,
Rooms: "Room",
Free: false,
},
{
Day: "Montag",
Week: "52",
Start: testDateTime,
End: testDateTime_p15,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 15,
blockCount: 4,
},
want: []byte{
0xA0,
},
},
{
name: "encode time table with overlapping events",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime_p15,
End: testDateTime_p30,
Rooms: "Room",
Free: false,
},
{
Day: "Montag",
Week: "52",
Start: testDateTime,
End: testDateTime_p45,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 15,
blockCount: 4,
},
want: []byte{
0xE0,
},
},
{
name: "consider events starting before the start time",
args: args{
roomSchedule: []model.AnonymizedEventDTO{
{
Day: "Montag",
Week: "52",
Start: testDateTime_m15,
End: testDateTime_p15,
Rooms: "Room",
Free: false,
},
},
start: testTime,
granularity: 15,
blockCount: 4,
},
want: []byte{
0x80,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := encodeRoomSchedule(tt.args.roomSchedule, tt.args.start, tt.args.granularity, tt.args.blockCount)
if (err != nil) != tt.wantErr {
t.Errorf("encodeRoomSchedule() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("encodeRoomSchedule() = %v, want %v", got, tt.want)
}
})
}
}