Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

fix: DayOfWeek now returns random day #24

Merged
merged 2 commits into from
Jul 25, 2018
Merged

fix: DayOfWeek now returns random day #24

merged 2 commits into from
Jul 25, 2018

Conversation

msorens
Copy link
Contributor

@msorens msorens commented Jun 24, 2018

Previously using the day_of_week tag, as in

DayString string `faker:"day_of_week"`

would always return Sunday. Tracked this down to the fact that the code needs to use constants from the reference time "Mon Jan 2 15:04:05 MST 2006" as described https://gobyexample.com/time-formatting-parsing. So the correct value to use is Monday.

Also included in this PR one way to test that we are not always getting the same value returned from the function.

@codecov
Copy link

codecov bot commented Jun 24, 2018

Codecov Report

Merging #24 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #24   +/-   ##
=======================================
  Coverage   97.82%   97.82%           
=======================================
  Files           8        8           
  Lines         368      368           
=======================================
  Hits          360      360           
  Misses          4        4           
  Partials        4        4
Impacted Files Coverage Δ
datetime.go 100% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f76f69b...f93a18d. Read the comment docs.

func TestDayOfWeekReturnsDifferentValues(t *testing.T) {
dayMap := make(map[string]struct{})
iterations := 5 // sufficiently large to assure we don't randomly get the same value again
for i := 0; i < iterations; i++ {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop creates a mathematical set of all returned values. Each call should return a random value but, by pure randomness, a couple calls might return the same value. Hence the loop to call enough times to make sure there are different values returned.

//t.Log(day)
dayMap[day] = struct{}{}
}
assert.True(t, len(dayMap) > 1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is more than one value in the set, we know we have that the underlying function is not always returning the same value.

}
assert.True(t, len(dayMap) > 1)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: If you uncomment the t.Log line you can see that different values get returned on subsequent calls. But if you rerun the test, you get the same sequence of 5 values repeated. Wondering if there is any easy way to randomize the starting value?

Copy link
Owner

@bxcodec bxcodec Jun 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you already using map, I think you can also utilize the map to avoid the same value repeated.

day := GetDateTimer().DayOfWeek()
if _, ok := dayMap[day]; ok {
	i--
	continue
}
dayMap[day] = struct{}{}
t.Log(day) // Will print random and different day 5 times. 

Copy link
Owner

@bxcodec bxcodec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool. Thanks for the PR. I just realized this.
I have some reviews to fixed. 👍

"log"
"reflect"
"testing"
"time"

"github.com/bxcodec/faker/support/slice"
"github.com/stretchr/testify/assert"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know testify is really helpful. But I would recommend to not using it here. I'd prefer if we just using a simple assertion manually like other test-case. It's just to avoid any possible future conflict related to dependencies because we don't use any dependency management tools here (Dep, Glide etc).

//t.Log(day)
dayMap[day] = struct{}{}
}
assert.True(t, len(dayMap) > 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to my first comment about testify, could you please change this into manual assertion without using testify? :D

Thank you

@bxcodec bxcodec merged commit 5f55daf into bxcodec:master Jul 25, 2018
@bxcodec bxcodec changed the title Bugfix: DayOfWeek now returns random day fix: DayOfWeek now returns random day Sep 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants