Skip to content

Commit

Permalink
Refactor location provider
Browse files Browse the repository at this point in the history
  • Loading branch information
sepisoltani committed Apr 30, 2024
1 parent ce3c4f2 commit 8456f91
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
17 changes: 13 additions & 4 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ package persian_faker
import (
"github.com/sepisoltani/persian-faker/providers/bank"
"github.com/sepisoltani/persian-faker/providers/bill"
"github.com/sepisoltani/persian-faker/providers/digit"
"github.com/sepisoltani/persian-faker/providers/location"
"github.com/sepisoltani/persian-faker/providers/phonenumber"
)

// DataGenerator is a facade for accessing all fake data generation functionalities.
type DataGenerator struct {
Bank *bank.Bank
Bill *bill.Bill
Bank *bank.Bank
Bill *bill.Bill
Digit *digit.Digit
PhoneNumber *phonenumber.PhoneNumber
Location *location.Location
}

func NewDataGenerator() *DataGenerator {
return &DataGenerator{
Bank: &bank.Bank{},
Bill: &bill.Bill{},
Bank: &bank.Bank{},
Bill: &bill.Bill{},
Digit: &digit.Digit{},
PhoneNumber: &phonenumber.PhoneNumber{},
Location: &location.Location{},
}
}
23 changes: 11 additions & 12 deletions providers/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"time"
)

var rng = rand.New(rand.NewSource(time.Now().UnixNano()))

var provinces = []string{
"آذربایجان شرقی", "آذربایجان غربی", "اردبیل", "اصفهان", "البرز",
"ایلام", "بوشهر", "تهران", "چهارمحال و بختیاری", "خراسان جنوبی",
Expand Down Expand Up @@ -223,23 +225,20 @@ var countries = []string{
"یونان",
}

// RandomProvince returns a random province.
func RandomProvince() string {
src := rand.NewSource(time.Now().UnixNano())
rng := rand.New(src)
type Location struct {
}

// GenerateProvince returns a random province.
func (Location) GenerateProvince() string {
return provinces[rng.Intn(len(provinces))]
}

// RandomCity returns a random city.
func RandomCity() string {
src := rand.NewSource(time.Now().UnixNano())
rng := rand.New(src)
// GenerateCity returns a random city.
func (Location) GenerateCity() string {
return cities[rng.Intn(len(cities))]
}

// RandomCountry returns a random country.
func RandomCountry() string {
src := rand.NewSource(time.Now().UnixNano())
rng := rand.New(src)
// GenerateCountry returns a random country.
func (Location) GenerateCountry() string {
return countries[rng.Intn(len(cities))]
}
27 changes: 15 additions & 12 deletions providers/location/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"testing"
)

// TestRandomProvince checks if the randomly selected province is from the list.
func TestRandomProvince(t *testing.T) {
randomProvince := RandomProvince()
// TestGenerateProvince checks if the randomly selected province is from the list.
func TestGenerateProvince(t *testing.T) {
location := &Location{}
randomProvince := location.GenerateProvince()
found := false
for _, province := range provinces {
if province == randomProvince {
Expand All @@ -15,13 +16,14 @@ func TestRandomProvince(t *testing.T) {
}
}
if !found {
t.Errorf("RandomProvince returned a value not in the provinces list: %s", randomProvince)
t.Errorf("GenerateProvince returned a value not in the provinces list: %s", randomProvince)
}
}

// TestRandomCity checks if the randomly selected city is from the list.
func TestRandomCity(t *testing.T) {
randomCity := RandomCity()
// TestGenerateCity checks if the randomly selected city is from the list.
func TestGenerateCity(t *testing.T) {
location := &Location{}
randomCity := location.GenerateCity()
found := false
for _, city := range cities {
if city == randomCity {
Expand All @@ -30,13 +32,14 @@ func TestRandomCity(t *testing.T) {
}
}
if !found {
t.Errorf("RandomCity returned a value not in the cities list: %s", randomCity)
t.Errorf("GenerateCity returned a value not in the cities list: %s", randomCity)
}
}

// TestRandomCountry checks if the randomly selected city is from the list.
func TestRandomCountry(t *testing.T) {
randomCountry := RandomCountry()
// TestGenerateCountry checks if the randomly selected city is from the list.
func TestGenerateCountry(t *testing.T) {
location := &Location{}
randomCountry := location.GenerateCountry()
found := false
for _, country := range countries {
if country == randomCountry {
Expand All @@ -45,6 +48,6 @@ func TestRandomCountry(t *testing.T) {
}
}
if !found {
t.Errorf("RandomCountry returned a value not in the cities list: %s", randomCountry)
t.Errorf("GenerateCountry returned a value not in the cities list: %s", randomCountry)
}
}
4 changes: 2 additions & 2 deletions providers/phonenumber/phonenumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

var rng = rand.New(rand.NewSource(time.Now().UnixNano()))

type phoneNumber struct {
type PhoneNumber struct {
}

// GeneratePersianMobileNumber generates a Persian mobile phone number.
func (phoneNumber) GeneratePersianMobileNumber() string {
func (PhoneNumber) GeneratePersianMobileNumber() string {
prefixes := []string{"0912", "0913", "0914", "0915", "0916"}

prefix := prefixes[rng.Intn(len(prefixes))]
Expand Down

0 comments on commit 8456f91

Please sign in to comment.