Skip to content

Commit

Permalink
Move Logic to Module Root (#5)
Browse files Browse the repository at this point in the history
* move all logic to root level package

* add some tests

* update readme
  • Loading branch information
hf-kklein authored Sep 30, 2022
1 parent 918c805 commit 90f1b0d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 31 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
![Unittest status badge](https://github.com/hochfrequenz/mako_time_converter/workflows/Unittests/badge.svg)
![Coverage status badge](https://github.com/hochfrequenz/mako_time_converter/workflows/coverage/badge.svg)
![Linter status badge](https://github.com/hochfrequenz/mako_time_converter/workflows/golangci-lint/badge.svg)
[![Go Reference](https://pkg.go.dev/badge/github.com/hochfrequenz/mako_time_converter.svg)](https://pkg.go.dev/github.com/hochfrequenz/mako_time_converter)

Go package to convert between German "Gastag" and "Stromtag", between inclusive and exclusive end dates and combinations of all them.
This is a Go module to convert between German "Gastag" and "Stromtag", between inclusive and exclusive end dates and combinations of all them.
This is relevant for German Marktkommunikation ("MaKo").

This is a Golang port of the [`mako_datetime_converter` for .NET](https://github.com/Hochfrequenz/mako_datetime_converter).
Expand Down
4 changes: 2 additions & 2 deletions configuration/config.go → config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package configuration
package mako_time_converter

import (
"github.com/go-playground/validator/v10"
"github.com/hochfrequenz/mako_time_converter/configuration/enddatetimekind"
"github.com/hochfrequenz/mako_time_converter/enddatetimekind"
)

// DateTimeConfiguration describes how a time.Time is meant/interpreted by a system. Two of these configurations allow to convert a time.Time smoothly.
Expand Down
26 changes: 26 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mako_time_converter_test

import (
"encoding/json"
"github.com/corbym/gocrest/is"
"github.com/corbym/gocrest/then"
"github.com/hochfrequenz/mako_time_converter"
"github.com/hochfrequenz/mako_time_converter/enddatetimekind"
"strings"
)

func (s *Suite) Test_Configuration_Serialization() {
configs := []mako_time_converter.DateTimeConversionConfiguration{
{Source: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true), IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.EXCLUSIVE)},
Target: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(false), IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.INCLUSIVE)}},
}
for _, config := range configs {
jsonBytes, err := json.Marshal(config)
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), strings.Contains(string(jsonBytes), "EXCLUSIVE"), is.True()) // json string enum serialization
var deserializedConfig mako_time_converter.DateTimeConversionConfiguration
err = json.Unmarshal(jsonBytes, &deserializedConfig)
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), config, is.EqualTo(deserializedConfig))
}
}
File renamed without changes.
File renamed without changes.
11 changes: 5 additions & 6 deletions conversion/gastagconverter.go → gastagconverter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package conversion
package mako_time_converter

import (
"fmt"
"github.com/go-playground/validator/v10"
mtcconfig "github.com/hochfrequenz/mako_time_converter/configuration"
"github.com/hochfrequenz/mako_time_converter/configuration/enddatetimekind"
"github.com/hochfrequenz/mako_time_converter/enddatetimekind"
"log"
"time"
)
Expand All @@ -22,7 +21,7 @@ type GasTagConverter interface {
// StripTime removes all hours, minutes, seconds, milliseconds (in german local time) from the given timestamp. This is similar to a "round down" or "floor" in German local time.
StripTime(timestamp time.Time) time.Time
// Convert converts the given timestamp to a DateTimeConversionConfiguration.Target by applying all transformations which are derived from the given configuration time is described by DateTimeConversionConfiguration.Source.
Convert(timestamp time.Time, configuration mtcconfig.DateTimeConversionConfiguration) (time.Time, error)
Convert(timestamp time.Time, configuration DateTimeConversionConfiguration) (time.Time, error)
}

type locationBasedGasTagConverter struct {
Expand Down Expand Up @@ -94,9 +93,9 @@ func (l locationBasedGasTagConverter) subtractGermanDay(timestamp time.Time) tim
return localtime.AddDate(0, 0, -1).UTC()
}

func (l locationBasedGasTagConverter) Convert(timestamp time.Time, configuration mtcconfig.DateTimeConversionConfiguration) (time.Time, error) {
func (l locationBasedGasTagConverter) Convert(timestamp time.Time, configuration DateTimeConversionConfiguration) (time.Time, error) {
validate := validator.New()
validate.RegisterStructValidation(mtcconfig.DateTimeConversionConfigurationStructLevelValidator, mtcconfig.DateTimeConversionConfiguration{})
validate.RegisterStructValidation(DateTimeConversionConfigurationStructLevelValidator, DateTimeConversionConfiguration{})
err := validate.Struct(configuration)
if err != nil {
return time.Time{}, err
Expand Down
43 changes: 21 additions & 22 deletions conversion/gastagconverter_test.go → gastagconverter_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package conversion_test
package mako_time_converter_test

import (
"github.com/corbym/gocrest/is"
"github.com/corbym/gocrest/then"
"github.com/hochfrequenz/mako_time_converter/configuration"
"github.com/hochfrequenz/mako_time_converter/configuration/enddatetimekind"
"github.com/hochfrequenz/mako_time_converter/conversion"
"github.com/hochfrequenz/mako_time_converter"
"github.com/hochfrequenz/mako_time_converter/enddatetimekind"
"github.com/stretchr/testify/suite"
"testing"
"time"
Expand All @@ -26,8 +25,8 @@ func TestInit(t *testing.T) {
suite.Run(t, new(Suite))
}

func getBerlinConverter() conversion.GasTagConverter {
return conversion.NewGasTagConverter("Europe/Berlin")
func getBerlinConverter() mako_time_converter.GasTagConverter {
return mako_time_converter.NewGasTagConverter("Europe/Berlin")
}

func (s *Suite) Test_IsGermanMidnight_true() {
Expand Down Expand Up @@ -119,9 +118,9 @@ func (s *Suite) Test_Gastag_Aware_To_Non_Gastag_Aware() {
time.Date(2023, 6, 1, 4, 0, 0, 0, time.UTC): time.Date(2023, 5, 31, 22, 0, 0, 0, time.UTC),
time.Date(2023, 12, 1, 5, 0, 0, 0, time.UTC): time.Date(2023, 11, 30, 23, 0, 0, 0, time.UTC),
}
conversion := configuration.DateTimeConversionConfiguration{
Source: configuration.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true)},
Target: configuration.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(false)},
conversion := mako_time_converter.DateTimeConversionConfiguration{
Source: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true)},
Target: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(false)},
}
converter := getBerlinConverter()
for input, expected := range pairs {
Expand All @@ -144,9 +143,9 @@ func (s *Suite) Test_Strom_Inclusive_End_To_Strom_Exclusive_End() {
time.Date(2023, 12, 31, 23, 0, 0, 0, time.UTC): time.Date(2024, 01, 01, 23, 0, 0, 0, time.UTC),
time.Date(2023, 12, 01, 23, 0, 0, 0, time.UTC): time.Date(2023, 12, 02, 23, 0, 0, 0, time.UTC),
}
conversion := configuration.DateTimeConversionConfiguration{
Source: configuration.DateTimeConfiguration{IsGas: false, IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.INCLUSIVE)},
Target: configuration.DateTimeConfiguration{IsGas: false, IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.EXCLUSIVE)},
conversion := mako_time_converter.DateTimeConversionConfiguration{
Source: mako_time_converter.DateTimeConfiguration{IsGas: false, IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.INCLUSIVE)},
Target: mako_time_converter.DateTimeConfiguration{IsGas: false, IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.EXCLUSIVE)},
}
converter := getBerlinConverter()
for input, expected := range pairs {
Expand All @@ -168,9 +167,9 @@ func (s *Suite) Test_Gas_Inclusive_End_To_Gas_Exclusive_End() {
time.Date(2023, 12, 30, 05, 0, 0, 0, time.UTC): time.Date(2023, 12, 31, 05, 0, 0, 0, time.UTC),
time.Date(2023, 12, 01, 05, 0, 0, 0, time.UTC): time.Date(2023, 12, 02, 05, 0, 0, 0, time.UTC),
}
conversion := configuration.DateTimeConversionConfiguration{
Source: configuration.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true), IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.INCLUSIVE)},
Target: configuration.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true), IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.EXCLUSIVE)},
conversion := mako_time_converter.DateTimeConversionConfiguration{
Source: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true), IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.INCLUSIVE)},
Target: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true), IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.EXCLUSIVE)},
}
converter := getBerlinConverter()
for input, expected := range pairs {
Expand All @@ -187,18 +186,18 @@ func (s *Suite) Test_Gas_Inclusive_End_To_Gas_Exclusive_End() {
}

func (s *Suite) Test_Invalid_Configurations_Are_Rejected() {
invalidConfigs := []configuration.DateTimeConversionConfiguration{
invalidConfigs := []mako_time_converter.DateTimeConversionConfiguration{
{
Source: configuration.DateTimeConfiguration{IsGas: false, IsGasTagAware: pointer(true)},
Target: configuration.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true)},
Source: mako_time_converter.DateTimeConfiguration{IsGas: false, IsGasTagAware: pointer(true)},
Target: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true)},
},
{
Source: configuration.DateTimeConfiguration{IsEndDate: true}, // no enddatetime kind given
Target: configuration.DateTimeConfiguration{IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.EXCLUSIVE)},
Source: mako_time_converter.DateTimeConfiguration{IsEndDate: true}, // no enddatetime kind given
Target: mako_time_converter.DateTimeConfiguration{IsEndDate: true, EndDateTimeKind: pointer(enddatetimekind.EXCLUSIVE)},
},
{
Source: configuration.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true)},
Target: configuration.DateTimeConfiguration{IsGas: true}, // no gastag awareness given
Source: mako_time_converter.DateTimeConfiguration{IsGas: true, IsGasTagAware: pointer(true)},
Target: mako_time_converter.DateTimeConfiguration{IsGas: true}, // no gastag awareness given
},
}
converter := getBerlinConverter()
Expand Down

0 comments on commit 90f1b0d

Please sign in to comment.