Closed - Booking up for Beauty - GO

Hello! Got stuck with Booking up for baeuty. Two of four similar time.Parse functions are not working. These are inside of “HasPassed” and “IsAfternoonAppointment” functions. Have copied and tried in my environment - all works fine.

What exactly do you mean by «are not working»?

  • If your solution fails the tests, please include the test report.
  • If you get other errors, please include these as well.
  • If you get unexpected results, please elaborate on what you expected to happen and on what actually happens.

I have printed out result they return “0001-01-01 00:00:00 +0000 UTC”
although other two are OK.

0001-01-01 00:00:00 +0000 UTC
0001-01-01 00:00:00 +0000 UTC
0001-01-01 00:00:00 +0000 UTC
— FAIL: TestHasPassed (0.00s)
— FAIL: TestHasPassed/HasPassed_3 (0.00s)
booking_up_for_beauty_test.go:38: HasPassed(December 9, 2112 11:59:59) = ‘true’, want ‘false’
0001-01-01 00:00:00 +0000 UTC
0001-01-01 00:00:00 +0000 UTC
0001-01-01 00:00:00 +0000 UTC
— FAIL: TestIsAfternoonAppointment (0.00s)
— FAIL: TestIsAfternoonAppointment/IsAfternoonAppointment_2 (0.00s)
booking_up_for_beauty_test.go:56: IsAfternoonAppointment(Friday, March 8, 1974 12:02:02) = ‘false’, want ‘true’
FAIL
exit status 1
FAIL booking 0.553s

and from editor:

func TestHasPassed(t *testing.T) {
	tests := map[string]struct {
		in	string
		want	bool
	}{
		"HasPassed 1":	{in: "October 3, 2019 20:32:00", want: true},
		"HasPassed 2":	{in: "January 28, 1974 2:02:02", want: true},
		"HasPassed 3":	{in: "December 9, 2112 11:59:59", want: false},
	}
	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			if got := HasPassed(tc.in); got != tc.want {
				t.Errorf("HasPassed(%s) = '%v', want '%v'", tc.in, got, tc.want)
			}
		})
	}
}

TEST FAILURE

=== RUN TestHasPassed/HasPassed_3 booking_up_for_beauty_test.go:38: HasPassed(December 9, 2112 11:59:59) = ‘true’, want ‘false’ — FAIL: TestHasPassed/HasPassed_3 (0.00s)


func TestIsAfternoonAppointment(t *testing.T) {
	tests := map[string]struct {
		in	string
		want	bool
	}{
		"IsAfternoonAppointment 1":	{in: "Thursday, May 13, 2010 20:32:00", want: false},
		"IsAfternoonAppointment 2":	{in: "Friday, March 8, 1974 12:02:02", want: true},
		"IsAfternoonAppointment 3":	{in: "Friday, September 9, 2112 11:59:59", want: false},
	}
	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			if got := IsAfternoonAppointment(tc.in); got != tc.want {
				t.Errorf("IsAfternoonAppointment(%s) = '%v', want '%v'", tc.in, got, tc.want)
			}
		})
	}
}

TEST FAILURE

=== RUN TestIsAfternoonAppointment/IsAfternoonAppointment_2 booking_up_for_beauty_test.go:56: IsAfternoonAppointment(Friday, March 8, 1974 12:02:02) = ‘false’, want ‘true’ — FAIL: TestIsAfternoonAppointment/IsAfternoonAppointment_2 (0.00s)

Could you share your code (as code, not as an image)? That makes it much easier for people to (1) read your code and (2) test things out with your code.

Note, the various functions have different layouts. You can’t use the same layout for all functions.

package booking

import (
“fmt”
“time”
)

const layout string = “1/_2/2006 15:04:05”

func Schedule(date string) time.Time {

t, _ := time.Parse(layout, date)

return t

}

func HasPassed(date string) bool {

t1, _ := time.Parse(layout, date)

compare := t1.Before(time.Now())

return compare

}

func IsAfternoonAppointment(date string) bool {

t2, _ := time.Parse(layout, date)

hr := t2.Hour()

if hr >= 12 && hr < 18 {

	return true

} else {

	return false

}

}

func Description(date string) string {

t3, _ := time.Parse(layout, date)

return fmt.Sprintf("You have an appointment on %v, %v %d, %2d, at %02d:%02d.",

	t3.Weekday(), t3.Month(), t3.Day(), t3.Year(),

	t3.Hour(), t3.Minute())

}

func AnniversaryDate() time.Time {

t4 := time.Date(2020, 9, 15, 00, 00, 00, 0, time.UTC)

return t4.AddDate(3, 0, 0)

}

in this case similar lauyout works fine. I tried to separate - same result.

without tester code works fine, I tried all dates tester is using to test -they passed through.

@lio1902 As already mentioned, you will need different date formats to get the tests passed. One layout will not fit for all functions. Check the different test cases to find the needed formats.

So, this is the test program requirement, not the GOLANG?

1 Like

Correct, this is an exercise requirement. Each function is passed data in a different format and requires a different layout.

Also, you can use ``` to form codeblocks to share code in a readable fashion:

```go
func HasPassed(date string) bool {

t1, _ := time.Parse(layout, date)

compare := t1.Before(time.Now())

return compare

}
```

Is rendered:

func HasPassed(date string) bool {

t1, _ := time.Parse(layout, date)

compare := t1.Before(time.Now())

return compare
}

I did tried different layout, they did not work. I saw this format from test program and I applied reference date to it.

This may not sound very satisfying: The correct formats will work. You really will need different formats. One for all will not work.

If some tests are passing, then you probably have the right format for those tests.

Look at the tests which are failing and look at the input for those tests. What layout are you using for that data? Does the layout and the test data line up?

Thank you for pointing out! Will try.