From 12a812e675ed2cca915877ddb2185ba7f7c1cef4 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 5 Jan 2025 15:52:25 +0100 Subject: [PATCH] refactor tests --- internal/service/booking/sync.go | 43 +++++++++++++++++++++------ internal/service/booking/sync_test.go | 18 +++++------ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/internal/service/booking/sync.go b/internal/service/booking/sync.go index e4e6d52..b0d69fc 100644 --- a/internal/service/booking/sync.go +++ b/internal/service/booking/sync.go @@ -12,7 +12,22 @@ import ( ) func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { - content := strings.ReplaceAll(strings.TrimSpace(rawContent), "\u00a0", " ") + b, err := ParseBooking(rawContent) + if err != nil { + return nil, err + } + + b = bs.Create(b.From, b.To, b.Name, b.PhoneNumber, b.Email, b.Platform, b.CustomerNumber, b.PlatformFees, b.ExternalId) + itm := b.Items[0] + if item, ok := config.NewHost().Items[itm.Item]; ok { + bs.CreateItem(b.Id, item, itm.Quantity, itm.Price, itm.PaymentMethod, b.CustomerNumber, b.Platform) + } + + return b, nil +} + +func ParseBooking(content string) (*Booking, error) { + content = strings.ReplaceAll(strings.TrimSpace(content), "\u00a0", " ") arrivalDate, err := extractDate(`Date d'arrivée `, content) if err != nil { @@ -65,12 +80,24 @@ func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { return nil, err } - 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) - } - - return b, nil + return &Booking{ + From: *formatDate(arrivalDate), + To: *formatDate(departureDate), + Name: customerName, + Email: customerEmail, + Platform: "Booking", + CustomerNumber: customerNumber, + PlatformFees: commissionAmount, + ExternalId: &externalId, + Items: []Item{ + { + Item: itemName, + Quantity: stayLength, + Price: standardRate, + PaymentMethod: "Card", + }, + }, + }, nil } func extractDate(pattern, content string) (string, error) { @@ -84,7 +111,6 @@ func extractDate(pattern, content string) (string, error) { // Regular expression to remove the prefix rePrefix := regexp.MustCompile(pattern + `\w+\.\s*`) dateString := rePrefix.ReplaceAllString(dateMatch, "") - log.Println(dateString) return dateString, nil } @@ -117,7 +143,6 @@ func extractFloat(pattern, content string) (float64, error) { func extractString(pattern, content string) (string, error) { re := regexp.MustCompile(pattern) match := re.FindStringSubmatch(content) - log.Println(match) if len(match) > 1 { return strings.TrimSpace(match[1]), nil } else if len(match) > 0 { diff --git a/internal/service/booking/sync_test.go b/internal/service/booking/sync_test.go index ca75235..7881cbc 100644 --- a/internal/service/booking/sync_test.go +++ b/internal/service/booking/sync_test.go @@ -4,8 +4,9 @@ import ( "testing" "time" - "github.com/rjNemo/rentease/internal/service/booking" "github.com/stretchr/testify/assert" + + "github.com/rjNemo/rentease/internal/service/booking" ) 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 ` @@ -25,16 +26,17 @@ func TestParseFromApi(t *testing.T) { 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", + Platform: "Booking", CustomerNumber: 2, PlatformFees: 31.86, ExternalId: &externalId, + Canceled: false, Items: []booking.Item{ { - BookingId: 1, - Item: "Maison 1 Chambre (T2 - VillaFleurie au bourg du Gosier)", - Quantity: 1, - Price: 186.0, + BookingId: 0, + Item: "T2", + Quantity: 3, + Price: 59.0, PaymentMethod: "Card", }, }, @@ -42,12 +44,10 @@ func TestParseFromApi(t *testing.T) { }, } - 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) + actual, err := booking.ParseBooking(tt.rawContent) if err != nil { t.Errorf("unexpected error: %v", err) }