From 171a5e65158f9439e0e4633136042a39be7b2bf1 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 5 Jan 2025 14:22:38 +0100 Subject: [PATCH] test: add test for booking parsing --- go.mod | 6 +-- go.sum | 7 ++++ internal/service/booking/sync.go | 8 +++- internal/service/booking/sync_test.go | 57 +++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 internal/service/booking/sync_test.go diff --git a/go.mod b/go.mod index 537d0ed..6528ddf 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/labstack/gommon v0.4.2 github.com/rjNemo/underscore v0.7.0 github.com/stretchr/testify v1.10.0 - golang.org/x/oauth2 v0.24.0 + golang.org/x/oauth2 v0.25.0 google.golang.org/api v0.214.0 gorm.io/driver/postgres v1.5.11 gorm.io/gorm v1.25.12 @@ -58,9 +58,9 @@ require ( golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329 // indirect golang.org/x/net v0.33.0 // indirect golang.org/x/sync v0.10.0 // indirect - golang.org/x/sys v0.28.0 // indirect + golang.org/x/sys v0.29.0 // indirect golang.org/x/text v0.21.0 // indirect - golang.org/x/time v0.8.0 // indirect + golang.org/x/time v0.9.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d // indirect google.golang.org/grpc v1.69.2 // indirect google.golang.org/protobuf v1.36.1 // indirect diff --git a/go.sum b/go.sum index f0ea9bb..3cca4f3 100644 --- a/go.sum +++ b/go.sum @@ -142,6 +142,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= @@ -194,6 +195,8 @@ golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE= golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70= +golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -207,12 +210,16 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg= golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY= +golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= diff --git a/internal/service/booking/sync.go b/internal/service/booking/sync.go index 8cf7858..623288e 100644 --- a/internal/service/booking/sync.go +++ b/internal/service/booking/sync.go @@ -26,10 +26,12 @@ func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { externalId := extractString(`Numéro de réservation : \n\s+(\d+)`, content) standardRate := extractFloat(`Standard Rate\n\s+€ (\d+)`, content) + log.Println(arrivalDate, departureDate, stayLength, customerName, customerEmail, customerNumber, commissionAmount, itemName, externalId, standardRate) b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount, &externalId) if item, ok := config.NewHost().Items[itemName]; ok { bs.CreateItem(b.Id, item, stayLength, standardRate, "Card", customerNumber, b.Platform) } + log.Println(b) return b, nil } @@ -76,10 +78,14 @@ func extractFloat(pattern, content string) float64 { func extractString(pattern, content string) string { re := regexp.MustCompile(pattern) match := re.FindStringSubmatch(content) + log.Println(match) if len(match) > 1 { return strings.TrimSpace(match[1]) + } else if len(match) > 0 { + return strings.TrimSpace(match[0]) } - return strings.TrimSpace(match[0]) + log.Println("pattern not found") + return "" } func formatDate(date string) *time.Time { diff --git a/internal/service/booking/sync_test.go b/internal/service/booking/sync_test.go new file mode 100644 index 0000000..ca75235 --- /dev/null +++ b/internal/service/booking/sync_test.go @@ -0,0 +1,57 @@ +package booking_test + +import ( + "testing" + "time" + + "github.com/rjNemo/rentease/internal/service/booking" + "github.com/stretchr/testify/assert" +) + +const content = ` Date d'arrivée jeu. 3 avr. 2025 Date de départ dim. 6 avr. 2025 Durée de séjour : 3 nuits Nombre de personnes : \n 2\n Nombre d'hébergements \n 1\n Montant total € 186 Nom du client : \n Olga Korovina\n \n ru\n \n okorov.905387@guest.booking.com\n Contactez vos clients ! Indiquez-leur l'heure à laquelle vous souhaitez les accueillir ou l'endroit où ils récupéreront leurs clés. Un simple appel suffit : Afficher le numéro de téléphone Vous pouvez également leur envoyer un e-mail ou un message. Langue préférée \n russe\n Canal : Booking.com Code IATA/TIDS : \n PC029090\n Numéro de réservation : \n 4453602306\n Montant soumis à commission : € 177 Reçu jeu. 2 janv. 2025 Commission : € 31,86 Bloc-notes (usage interne) Ajoutez une note ici \n\n Maison 1 Chambre (T2 - VillaFleurie au bourg du Gosier)\n € 186 jeu. 3 avr. 2025 dim. 6 avr. 2025 Nom du client \n Olga Korovina\n Occupation maximum 2 adultes, 2 enfants (3 personnes max.) Photo de l'hébergement Date Tarif Tarif par nuit \n 03 - 04 avril\n \n Standard Rate\n € 59\n 04 - 05 avril\n \n Standard Rate\n € 59\n 05 - 06 avril\n \n Standard Rate\n € 59Sous-total € 177\n Taxe de séjour\n € 1.50 par personne et par nuit € 9Tarif total de l'hébergement € 186 Le tarif comprend 8.9 % de TVA Conversation avec le client \n Aucun message\n \n Les conversations avec vos clients apparaîtront ici.\n Booking.com reçoit tous les messages écrits ici et les traite selon sa Charte de confidentialité et informations sur les cookies Conditions ` + +func TestParseFromApi(t *testing.T) { + externalId := "4453602306" + tests := []struct { + name string + rawContent string + expected *booking.Booking + }{ + { + name: "parse booking from raw content", + rawContent: content, + expected: &booking.Booking{ + From: time.Date(2025, time.April, 3, 0, 0, 0, 0, time.UTC), + To: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC), + Name: "Olga Korovina", + Email: "okorov.905387@guest.booking.com", + Platform: "Booking.com", + CustomerNumber: 2, + PlatformFees: 31.86, + ExternalId: &externalId, + Items: []booking.Item{ + { + BookingId: 1, + Item: "Maison 1 Chambre (T2 - VillaFleurie au bourg du Gosier)", + Quantity: 1, + Price: 186.0, + PaymentMethod: "Card", + }, + }, + }, + }, + } + + bs, _ := booking.NewService(&booking.MockStore{}, nil, nil) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + actual, err := bs.ParseFromApi(tt.rawContent) + t.Logf("actual: %+v", actual) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + assert.Equal(t, tt.expected, actual, "expected %v, got %v", tt.expected, actual) + }) + } +}