diff --git a/generator/column.go b/generator/column.go index 920e028..b4e7503 100644 --- a/generator/column.go +++ b/generator/column.go @@ -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() diff --git a/generator/date_value.go b/generator/date_value.go new file mode 100644 index 0000000..459b26c --- /dev/null +++ b/generator/date_value.go @@ -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() +} diff --git a/generator/date_value_test.go b/generator/date_value_test.go new file mode 100644 index 0000000..eeb0942 --- /dev/null +++ b/generator/date_value_test.go @@ -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) + } +} diff --git a/generator/float_value.go b/generator/float_value.go new file mode 100644 index 0000000..0003ba2 --- /dev/null +++ b/generator/float_value.go @@ -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) +} diff --git a/generator/float_value_test.go b/generator/float_value_test.go new file mode 100644 index 0000000..1c7ba02 --- /dev/null +++ b/generator/float_value_test.go @@ -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) + } +} diff --git a/generator/generator.go b/generator/generator.go index 949d407..ffeffb8 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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" ) @@ -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: diff --git a/generator/id_value.go b/generator/id_value.go new file mode 100644 index 0000000..8e74b73 --- /dev/null +++ b/generator/id_value.go @@ -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) +} diff --git a/generator/id_value_test.go b/generator/id_value_test.go new file mode 100644 index 0000000..6178327 --- /dev/null +++ b/generator/id_value_test.go @@ -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) + } +} diff --git a/generator/int_value.go b/generator/int_value.go new file mode 100644 index 0000000..fe842be --- /dev/null +++ b/generator/int_value.go @@ -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) +} diff --git a/generator/int_value_test.go b/generator/int_value_test.go new file mode 100644 index 0000000..2ee29bc --- /dev/null +++ b/generator/int_value_test.go @@ -0,0 +1,73 @@ +package generator + +import ( + "reflect" + "testing" +) + +func TestIntValueInit1(t *testing.T) { + var r1 intValue + var r2 intValue + var e int64 = 1 + initValue := "1" + 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 = %d. r2 = %d. 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 TestIntValueInit2(t *testing.T) { + var etype int64 + var r1 intValue + var r2 intValue + initValue1 := "1" + initValue2 := "2" + r1.init(initValue1) + r2.init(initValue2) + var e1 int64 = 1 + var e2 int64 = 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("%d == %d. Should be different", r1.currentValue, r2.currentValue) + } +} + +func TestIntValueGenerate1(t *testing.T) { + var r intValue + initValue := "1" + r.init(initValue) + var e int64 = 2 + r.generateValue() + if r.currentValue != e { + t.Errorf("Expected %v. Got %v", e, r.currentValue) + } +} + +func TestIntValueGetCurrentValue1(t *testing.T) { + var r intValue + initValue := "1" + r.init(initValue) + var e string = "1" + ret := r.getCurrentValue() + if ret != e { + t.Errorf("Expected %v. Got %v", e, ret) + } +} diff --git a/generator/string_value.go b/generator/string_value.go new file mode 100644 index 0000000..9baee10 --- /dev/null +++ b/generator/string_value.go @@ -0,0 +1,26 @@ +package generator + +import "strconv" + +type stringValue struct { + prefix string + currentValue string + step int64 + currentStep int64 +} + +func (v *stringValue) generateValue() { + v.currentStep = v.currentStep + v.step + v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) +} + +func (v *stringValue) init(i string) { + v.prefix = i + v.currentStep = 1 + v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) + v.step = 1 +} + +func (v stringValue) getCurrentValue() string { + return v.currentValue +} diff --git a/generator/string_value_test.go b/generator/string_value_test.go new file mode 100644 index 0000000..d94ff39 --- /dev/null +++ b/generator/string_value_test.go @@ -0,0 +1,73 @@ +package generator + +import ( + "reflect" + "testing" +) + +func TestStringValueInit1(t *testing.T) { + var r1 stringValue + var r2 stringValue + var e string = "test_1" + initValue := "test" + r1.init(initValue) + r2.init(initValue) + if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(e) { + t.Errorf("Type is not %v. Is %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("r1 KO. Expected %v. Got %v", r1.currentValue, e) + } + if r2.currentValue != e { + t.Errorf("r2 KO. Expected %v. Got %v", r2.currentValue, e) + } +} + +func TestStringValueInit2(t *testing.T) { + var etype string + var r1 stringValue + var r2 stringValue + initValue1 := "test1" + initValue2 := "test2" + r1.init(initValue1) + r2.init(initValue2) + var e1 string = "test1_1" + var e2 string = "test2_1" + if reflect.TypeOf(r1.currentValue) != reflect.TypeOf(etype) { + t.Errorf("Type is not %v. Is %v", reflect.TypeOf(etype), reflect.TypeOf(r1.currentValue)) + } + if r1.currentValue != e1 { + t.Errorf("r1 KO. Expected %v. Got %v", e1, r1.currentValue) + } + if r2.currentValue != e2 { + t.Errorf("r2 KO. Expected %v. Got %v", e2, r2.currentValue) + } + if r1.currentValue == r2.currentValue { + t.Errorf("%v == %v. Should be different", r1.currentValue, r2.currentValue) + } +} + +func TestStringValueGenerate1(t *testing.T) { + var r stringValue + initValue := "test" + r.init(initValue) + var e string = "test_2" + r.generateValue() + if r.currentValue != e { + t.Errorf("Expected %v, got %v", e, r.currentValue) + } +} + +func TestStringValueGetCurrentValue1(t *testing.T) { + var r stringValue + initValue := "test" + r.init(initValue) + var e string = "test_1" + ret := r.getCurrentValue() + if ret != e { + t.Errorf("Expected %v, got %v", e, ret) + } +} diff --git a/generator/value.go b/generator/value.go index 7c86910..65ed630 100644 --- a/generator/value.go +++ b/generator/value.go @@ -1,94 +1,7 @@ package generator -import ( - "log" - "strconv" - "time" - - "github.com/brianvoe/gofakeit/v4" -) - -type stringValue struct { - prefix string - currentValue string - step int64 - currentStep int64 -} - -type intValue struct { - currentValue int64 - step int64 -} - -type dateValue struct { - currentValue time.Time - step time.Duration -} - -type idIntValue struct { - currentValue uint64 -} - type value interface { getCurrentValue() string generateValue() init(i string) } - -func (v *idIntValue) generateValue() { - v.currentValue = gofakeit.Uint64() -} - -func (v *intValue) generateValue() { - v.currentValue += v.step -} - -func (v *stringValue) generateValue() { - v.currentStep = v.currentStep + v.step - v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) -} - -func (v *dateValue) generateValue() { - v.currentValue = v.currentValue.Add(v.step) -} - -func (v *idIntValue) init(i string) { - v.currentValue = gofakeit.Uint64() -} - -func (v *intValue) init(i string) { - v.currentValue, _ = strconv.ParseInt(i, 10, 64) - v.step = 1 -} - -func (v *stringValue) init(i string) { - v.prefix = i - v.currentStep = 1 - v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) - v.step = 1 -} - -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 = 1000000 // 1M nanoseconds = 1ms -} - -func (v idIntValue) getCurrentValue() string { - return strconv.FormatUint(v.currentValue, 10) -} - -func (v intValue) getCurrentValue() string { - return strconv.FormatInt(v.currentValue, 10) -} - -func (v stringValue) getCurrentValue() string { - return v.currentValue -} - -func (v dateValue) getCurrentValue() string { - return v.currentValue.String() -} diff --git a/plan.json.example b/plan.json.example index 44e77a1..7ea78e3 100644 --- a/plan.json.example +++ b/plan.json.example @@ -23,8 +23,8 @@ "type":"FLOAT", "name":"3_FLOAT", "distinct": 5, - "start": "0.00", - "end": "1.00" + "start": "3.00", + "end": "9.00" }, { "type":"DATE",