Skip to content

Commit

Permalink
Merge pull request #21 from estebgonza/support-float-generation
Browse files Browse the repository at this point in the history
Support float generation
  • Loading branch information
NicoGGG authored Jan 25, 2021
2 parents 3d265f1 + 236aa9b commit 67a01c6
Show file tree
Hide file tree
Showing 14 changed files with 487 additions and 95 deletions.
1 change: 0 additions & 1 deletion generator/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type Column struct {
}

func (c *Column) nextValue() string {
/** The cardinality magic should be here. */
switch c.valueGenerator.(type) {
case *idIntValue:
c.valueGenerator.generateValue()
Expand Down
28 changes: 28 additions & 0 deletions generator/date_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package generator

import (
"log"
"time"
)

type dateValue struct {
currentValue time.Time
step time.Duration
}

func (v *dateValue) generateValue() {
v.currentValue = v.currentValue.Add(v.step)
}

func (v *dateValue) init(i string) {
var err error
v.currentValue, err = time.Parse("2006-01-02 15:04:05", i)
if err != nil {
log.Fatalln(err)
}
v.step = 1000000000 // 1B nanoseconds = 1s
}

func (v dateValue) getCurrentValue() string {
return v.currentValue.String()
}
78 changes: 78 additions & 0 deletions generator/date_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package generator

import (
"reflect"
"testing"
"time"
)

func TestDateValueInit1(t *testing.T) {
var r1 dateValue
var r2 dateValue
e := time.Date(
2009, 11, 17, 20, 34, 58, 000000000, time.UTC)
initValue := "2009-11-17 20:34:58"
r1.init(initValue)
r2.init(initValue)
if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(e) {
t.Errorf("Expected type is %v. Got %v", reflect.TypeOf(e), reflect.TypeOf(r1.currentValue))
}
if r1.currentValue != r2.currentValue {
t.Errorf("r1 = %v. r2 = %v. Should be the same", r1.currentValue, r2.currentValue)
}
if r1.currentValue != e {
t.Errorf("Expected %v. Got %v", e, r1.currentValue)
}
if r2.currentValue != e {
t.Errorf("Expected %v. Got %v", e, r2.currentValue)
}
}

func TestDateValueInit2(t *testing.T) {
var etype time.Time
var r1 dateValue
var r2 dateValue
initValue1 := "2009-11-17 20:34:58"
initValue2 := "2012-02-01 00:00:00"
r1.init(initValue1)
r2.init(initValue2)
e1 := time.Date(
2009, 11, 17, 20, 34, 58, 000000000, time.UTC)
e2 := time.Date(
2012, 02, 01, 00, 00, 00, 000000000, time.UTC)
if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(etype) {
t.Errorf("Expected type is %v. Got %v", reflect.TypeOf(etype), reflect.TypeOf(r1.currentValue))
}
if r1.currentValue != e1 {
t.Errorf("r1 KO. Expected %v. Got %v", r1.currentValue, e1)
}
if r2.currentValue != e2 {
t.Errorf("r2 KO. Expected %v. Got %v", r2.currentValue, e2)
}
if r1.currentValue == r2.currentValue {
t.Errorf("%v == %v. Should be different", r1.currentValue, r2.currentValue)
}
}

func TestDateValueGenerate1(t *testing.T) {
var r dateValue
initValue := "2009-11-17 20:34:58"
r.init(initValue)
e := time.Date(
2009, 11, 17, 20, 34, 59, 000000000, time.UTC)
r.generateValue()
if r.currentValue != e {
t.Errorf("Expected %v. Got %v", e, r.currentValue)
}
}

func TestDateValueGetCurrentValue1(t *testing.T) {
var r dateValue
initValue := "2009-11-17 20:34:58"
r.init(initValue)
var e string = "2009-11-17 20:34:58 +0000 UTC"
ret := r.getCurrentValue()
if ret != e {
t.Errorf("Expected %v. Got %v", e, ret)
}
}
21 changes: 21 additions & 0 deletions generator/float_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package generator

import "strconv"

type floatValue struct {
currentValue float64
step float64
}

func (v *floatValue) generateValue() {
v.currentValue += v.step
}

func (v *floatValue) init(i string) {
v.currentValue, _ = strconv.ParseFloat(i, 64)
v.step = 0.1
}

func (v floatValue) getCurrentValue() string {
return strconv.FormatFloat(v.currentValue, 'f', 2, 64)
}
73 changes: 73 additions & 0 deletions generator/float_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package generator

import (
"reflect"
"testing"
)

func TestFloatValueInit1(t *testing.T) {
var r1 floatValue
var r2 floatValue
var e float64 = 1.00
initValue := "1.00"
r1.init(initValue)
r2.init(initValue)
if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(e) {
t.Errorf("Expected type is %v. Got %v", reflect.TypeOf(e), reflect.TypeOf(r1.currentValue))
}
if r1.currentValue != r2.currentValue {
t.Errorf("r1 = %v. r2 = %v. Should be the same", r1.currentValue, r2.currentValue)
}
if r1.currentValue != e {
t.Errorf("Expected %v. Got %v", e, r1.currentValue)
}
if r2.currentValue != e {
t.Errorf("Expected %v. Got %v", e, r2.currentValue)
}
}

func TestFloatValueInit2(t *testing.T) {
var etype float64
var r1 floatValue
var r2 floatValue
initValue1 := "1.00"
initValue2 := "2.00"
r1.init(initValue1)
r2.init(initValue2)
var e1 float64 = 1
var e2 float64 = 2
if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(etype) {
t.Errorf("Expected type is %v. Got %v", reflect.TypeOf(etype), reflect.TypeOf(r1.currentValue))
}
if r1.currentValue != e1 {
t.Errorf("r1 KO. Expected %v. Got %v", r1.currentValue, e1)
}
if r2.currentValue != e2 {
t.Errorf("r2 KO. Expected %v. Got %v", r2.currentValue, e2)
}
if r1.currentValue == r2.currentValue {
t.Errorf("%v == %v. Should be different", r1.currentValue, r2.currentValue)
}
}

func TestFloatValueGenerate1(t *testing.T) {
var r floatValue
initValue := "1"
r.init(initValue)
var e float64 = 1.10
r.generateValue()
if r.currentValue != e {
t.Errorf("Expected %v. Got %v", e, r.currentValue)
}
}

func TestFloatValueGetCurrentValue1(t *testing.T) {
var r floatValue
initValue := "1"
r.init(initValue)
var e string = "1.00"
ret := r.getCurrentValue()
if ret != e {
t.Errorf("Expected %v. Got %v", e, ret)
}
}
10 changes: 5 additions & 5 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type Plan struct {
}

const (
intType = "INT"
idIntType = "ID_INT"
// floatType = "FLOAT"
intType = "INT"
idIntType = "ID_INT"
floatType = "FLOAT"
dateType = "DATE"
stringType = "STRING"
)
Expand Down Expand Up @@ -135,8 +135,8 @@ func createValueGenerator(t string) (value, error) {
v = &intValue{}
case idIntType:
v = &idIntValue{}
// case floatType:
// v = &floatValue{}
case floatType:
v = &floatValue{}
case dateType:
v = &dateValue{}
case stringType:
Expand Down
27 changes: 27 additions & 0 deletions generator/id_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package generator

import (
"strconv"

"github.com/brianvoe/gofakeit/v4"
)

type idIntValue struct {
currentValue uint64
}

/*
* All new types need all the methods in interface value in value.go
*/

func (v *idIntValue) generateValue() {
v.currentValue = gofakeit.Uint64()
}

func (v *idIntValue) init(i string) {
v.currentValue = gofakeit.Uint64()
}

func (v idIntValue) getCurrentValue() string {
return strconv.FormatUint(v.currentValue, 10)
}
60 changes: 60 additions & 0 deletions generator/id_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package generator

import (
"reflect"
"strconv"
"testing"
)

func TestIdValueInit1(t *testing.T) {
var etype uint64
var r1 idIntValue
var r2 idIntValue
initValue := "1"
r1.init(initValue)
r2.init(initValue)
if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(etype) {
t.Errorf("Expected type is %v. Got %v", reflect.TypeOf(etype), reflect.TypeOf(r1.currentValue))
}
if r1.currentValue == r2.currentValue {
t.Errorf("%d == %d. Should be different", r1.currentValue, r2.currentValue)
}
}

func TestIdValueInit2(t *testing.T) {
var etype uint64
var r1 idIntValue
var r2 idIntValue
initValue1 := "1"
initValue2 := "2"
r1.init(initValue1)
r2.init(initValue2)
if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(etype) {
t.Errorf("Expected type is %v. Got %v", reflect.TypeOf(etype), reflect.TypeOf(r1.currentValue))
}
if r1.currentValue == r2.currentValue {
t.Errorf("%d == %d. Should be different", r1.currentValue, r2.currentValue)
}
}

func TestIdValueGenerate1(t *testing.T) {
var r idIntValue
initValue := "1"
r.init(initValue)
ne := r.currentValue
r.generateValue()
if r.currentValue == ne {
t.Errorf("Id is the same after a generate, should be different. Before: %v, After %v", ne, r.currentValue)
}
}

func TestIdValueGetCurrentValue1(t *testing.T) {
var r idIntValue
initValue := "1"
r.init(initValue)
e := r.currentValue
ret := r.getCurrentValue()
if ret != strconv.FormatUint(e, 10) {
t.Errorf("Got: %v, Expected %v", e, ret)
}
}
21 changes: 21 additions & 0 deletions generator/int_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package generator

import "strconv"

type intValue struct {
currentValue int64
step int64
}

func (v *intValue) generateValue() {
v.currentValue += v.step
}

func (v *intValue) init(i string) {
v.currentValue, _ = strconv.ParseInt(i, 10, 64)
v.step = 1
}

func (v intValue) getCurrentValue() string {
return strconv.FormatInt(v.currentValue, 10)
}
Loading

0 comments on commit 67a01c6

Please sign in to comment.