Skip to content

Commit

Permalink
add datatypes module and fix some naming
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Oct 1, 2024
1 parent affe21b commit b6ca9d2
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 133 deletions.
2 changes: 1 addition & 1 deletion database/redis/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (connector *DbConnector) GetContact(id string) (moira.ContactData, error) {

contact, err := reply.Contact(result)
if err != nil {
return contact, fmt.Errorf("failed to reply contact '%s': %w", id, err)
return contact, fmt.Errorf("failed to deserialize contact '%s': %w", id, err)
}

contact.ID = id
Expand Down
13 changes: 7 additions & 6 deletions database/redis/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/moira-alert/moira/database"
"github.com/moira-alert/moira/datatypes"

"github.com/moira-alert/moira"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
Expand Down Expand Up @@ -191,7 +192,7 @@ func TestContacts(t *testing.T) {

emergencyContact, err = dataBase.GetEmergencyContact(contact2.ID)
So(err, ShouldResemble, database.ErrNil)
So(emergencyContact, ShouldResemble, moira.EmergencyContact{})
So(emergencyContact, ShouldResemble, datatypes.EmergencyContact{})

err = dataBase.SaveContact(contact1)
So(err, ShouldBeNil)
Expand Down Expand Up @@ -346,7 +347,7 @@ func TestContacts(t *testing.T) {

emergencyContact, err = dataBase.GetEmergencyContact(contact2.ID)
So(err, ShouldResemble, database.ErrNil)
So(emergencyContact, ShouldResemble, moira.EmergencyContact{})
So(emergencyContact, ShouldResemble, datatypes.EmergencyContact{})

err = dataBase.SaveContact(contact1)
So(err, ShouldBeNil)
Expand Down Expand Up @@ -541,10 +542,10 @@ var user2Contacts = []*moira.ContactData{
},
}

var user2EmergencyContacts = []moira.EmergencyContact{
var user2EmergencyContacts = []datatypes.EmergencyContact{
{
ContactID: "ContactID-000000000000003",
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff},
},
}

Expand Down Expand Up @@ -602,9 +603,9 @@ var team2Contacts = []*moira.ContactData{
},
}

var team2EmergencyContacts = []moira.EmergencyContact{
var team2EmergencyContacts = []datatypes.EmergencyContact{
{
ContactID: "TeamContactID-000000000000003",
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff},
},
}
22 changes: 11 additions & 11 deletions database/redis/emergency_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (
"strings"

"github.com/go-redis/redis/v8"
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/database"
"github.com/moira-alert/moira/database/redis/reply"
"github.com/moira-alert/moira/datatypes"
)

// GetEmergencyContact method to retrieve an emergency contact from the database.
func (connector *DbConnector) GetEmergencyContact(contactID string) (moira.EmergencyContact, error) {
func (connector *DbConnector) GetEmergencyContact(contactID string) (datatypes.EmergencyContact, error) {
c := *connector.client
ctx := connector.context

cmd := c.Get(ctx, emergencyContactsKey(contactID))

if errors.Is(cmd.Err(), redis.Nil) {
return moira.EmergencyContact{}, database.ErrNil
return datatypes.EmergencyContact{}, database.ErrNil
}

return reply.EmergencyContact(cmd)
}

// GetEmergencyContacts method to retrieve all emergency contacts from the database.
func (connector *DbConnector) GetEmergencyContacts() ([]*moira.EmergencyContact, error) {
func (connector *DbConnector) GetEmergencyContacts() ([]*datatypes.EmergencyContact, error) {
emergencyContactIDs, err := connector.getEmergencyContactIDs()
if err != nil {
return nil, fmt.Errorf("failed to get emergency contact IDs: %w", err)
Expand All @@ -37,7 +37,7 @@ func (connector *DbConnector) GetEmergencyContacts() ([]*moira.EmergencyContact,
}

// GetEmergencyContactsByIDs method to retrieve all emergency contacts from the database by their identifiers.
func (connector *DbConnector) GetEmergencyContactsByIDs(contactIDs []string) ([]*moira.EmergencyContact, error) {
func (connector *DbConnector) GetEmergencyContactsByIDs(contactIDs []string) ([]*datatypes.EmergencyContact, error) {
c := *connector.client
ctx := connector.context

Expand Down Expand Up @@ -86,7 +86,7 @@ func (connector *DbConnector) getEmergencyContactIDs() ([]string, error) {
}

// GetHeartbeatTypeContactIDs a method for obtaining contact IDs by specific emergency type.
func (connector *DbConnector) GetHeartbeatTypeContactIDs(heartbeatType moira.HeartbeatType) ([]string, error) {
func (connector *DbConnector) GetHeartbeatTypeContactIDs(heartbeatType datatypes.HeartbeatType) ([]string, error) {
c := *connector.client
ctx := connector.context

Expand All @@ -98,7 +98,7 @@ func (connector *DbConnector) GetHeartbeatTypeContactIDs(heartbeatType moira.Hea
return contactIDs, nil
}

func (connector *DbConnector) saveEmergencyContacts(emergencyContacts []moira.EmergencyContact) error {
func (connector *DbConnector) saveEmergencyContacts(emergencyContacts []datatypes.EmergencyContact) error {
c := *connector.client
ctx := connector.context

Expand All @@ -117,7 +117,7 @@ func (connector *DbConnector) saveEmergencyContacts(emergencyContacts []moira.Em
}

// SaveEmergencyContact a method for saving emergency contact.
func (connector *DbConnector) SaveEmergencyContact(emergencyContact moira.EmergencyContact) error {
func (connector *DbConnector) SaveEmergencyContact(emergencyContact datatypes.EmergencyContact) error {
c := *connector.client
ctx := connector.context

Expand Down Expand Up @@ -155,7 +155,7 @@ func (connector *DbConnector) RemoveEmergencyContact(contactID string) error {
return nil
}

func addSaveEmergencyContactToPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact moira.EmergencyContact) error {
func addSaveEmergencyContactToPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact datatypes.EmergencyContact) error {
emergencyContactBytes, err := reply.GetEmergencyContactBytes(emergencyContact)
if err != nil {
return fmt.Errorf("failed to get emergency contact '%s' bytes: %w", emergencyContact.ContactID, err)
Expand All @@ -170,7 +170,7 @@ func addSaveEmergencyContactToPipe(ctx context.Context, pipe redis.Pipeliner, em
return nil
}

func addRemoveEmergencyContactToPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact moira.EmergencyContact) {
func addRemoveEmergencyContactToPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact datatypes.EmergencyContact) {
pipe.Del(ctx, emergencyContactsKey(emergencyContact.ContactID))

for _, heartbeatType := range emergencyContact.HeartbeatTypes {
Expand All @@ -182,6 +182,6 @@ func emergencyContactsKey(contactID string) string {
return "moira-emergency-contacts:" + contactID
}

func heartbeatTypeContactsKey(heartbeatType moira.HeartbeatType) string {
func heartbeatTypeContactsKey(heartbeatType datatypes.HeartbeatType) string {
return "moira-heartbeat-type-contacts:" + string(heartbeatType)
}
52 changes: 26 additions & 26 deletions database/redis/emergency_contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
"testing"

"github.com/moira-alert/moira"
moiradb "github.com/moira-alert/moira/database"
"github.com/moira-alert/moira/datatypes"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
Expand All @@ -16,19 +16,19 @@ var (
testContactID2 = "test-contact-id2"
testContactID3 = "test-contact-id3"

testEmergencyContact = moira.EmergencyContact{
testEmergencyContact = datatypes.EmergencyContact{
ContactID: testContactID,
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff},
}

testEmergencyContact2 = moira.EmergencyContact{
testEmergencyContact2 = datatypes.EmergencyContact{
ContactID: testContactID2,
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HeartbeatNotifierOff},
}

testEmergencyContact3 = moira.EmergencyContact{
testEmergencyContact3 = datatypes.EmergencyContact{
ContactID: testContactID3,
HeartbeatTypes: []moira.HeartbeatType{moira.HearbeatTypeNotSet},
HeartbeatTypes: []datatypes.HeartbeatType{datatypes.HearbeatTypeNotSet},
}
)

Expand All @@ -42,7 +42,7 @@ func TestGetEmergencyContact(t *testing.T) {
Convey("With unknown emergency contact", func() {
emergencyContact, err := database.GetEmergencyContact(testContactID)
So(err, ShouldResemble, moiradb.ErrNil)
So(emergencyContact, ShouldResemble, moira.EmergencyContact{})
So(emergencyContact, ShouldResemble, datatypes.EmergencyContact{})
})

Convey("With some emergency contact", func() {
Expand All @@ -66,17 +66,17 @@ func TestGetEmergencyContacts(t *testing.T) {
Convey("Without emergency contacts", func() {
emergencyContacts, err := database.GetEmergencyContacts()
So(err, ShouldBeNil)
So(emergencyContacts, ShouldResemble, []*moira.EmergencyContact{})
So(emergencyContacts, ShouldResemble, []*datatypes.EmergencyContact{})
})

Convey("With some emergency contacts", func() {
database.saveEmergencyContacts([]moira.EmergencyContact{
database.saveEmergencyContacts([]datatypes.EmergencyContact{
testEmergencyContact,
testEmergencyContact2,
testEmergencyContact3,
})

expectedEmergencyContacts := []*moira.EmergencyContact{
expectedEmergencyContacts := []*datatypes.EmergencyContact{
&testEmergencyContact,
&testEmergencyContact2,
&testEmergencyContact3,
Expand All @@ -100,16 +100,16 @@ func TestGetEmergencyContactsByIDs(t *testing.T) {
contactIDs := []string{}
emergencyContacts, err := database.GetEmergencyContactsByIDs(contactIDs)
So(err, ShouldBeNil)
So(emergencyContacts, ShouldResemble, []*moira.EmergencyContact{})
So(emergencyContacts, ShouldResemble, []*datatypes.EmergencyContact{})
})

Convey("With some saved contact ids", func() {
database.saveEmergencyContacts([]moira.EmergencyContact{
database.saveEmergencyContacts([]datatypes.EmergencyContact{
testEmergencyContact,
testEmergencyContact2,
})

expectedEmergencyContacts := []*moira.EmergencyContact{
expectedEmergencyContacts := []*datatypes.EmergencyContact{
&testEmergencyContact,
&testEmergencyContact2,
}
Expand All @@ -122,11 +122,11 @@ func TestGetEmergencyContactsByIDs(t *testing.T) {

Convey("With one saved and one not saved contact ids", func() {
database.Flush()
database.saveEmergencyContacts([]moira.EmergencyContact{
database.saveEmergencyContacts([]datatypes.EmergencyContact{
testEmergencyContact,
})

expectedEmergencyContacts := []*moira.EmergencyContact{
expectedEmergencyContacts := []*datatypes.EmergencyContact{
&testEmergencyContact,
nil,
}
Expand All @@ -147,26 +147,26 @@ func TestGetHeartbeatTypeContactIDs(t *testing.T) {

Convey("Test GetHeartbeatTypeContactIDs", t, func() {
Convey("Without any emergency contacts by heartbeat type", func() {
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff)
So(err, ShouldBeNil)
So(emergencyContactIDs, ShouldBeEmpty)
})

Convey("With some emergency contacts by type", func() {
database.saveEmergencyContacts([]moira.EmergencyContact{
database.saveEmergencyContacts([]datatypes.EmergencyContact{
testEmergencyContact,
testEmergencyContact2,
testEmergencyContact3,
})

emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff)
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContactIDs, []string{
testContactID,
testContactID2,
})

emergencyContactIDs, err = database.GetHeartbeatTypeContactIDs(moira.HearbeatTypeNotSet)
emergencyContactIDs, err = database.GetHeartbeatTypeContactIDs(datatypes.HearbeatTypeNotSet)
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContactIDs, []string{
testContactID3,
Expand All @@ -183,7 +183,7 @@ func TestSaveEmergencyContact(t *testing.T) {

Convey("Test SaveEmergencyContact", t, func() {
Convey("With some emergency contact", func() {
expectedEmergencyContacts := []*moira.EmergencyContact{&testEmergencyContact}
expectedEmergencyContacts := []*datatypes.EmergencyContact{&testEmergencyContact}
expectedEmergencyContactIDs := []string{testContactID}

emergencyContacts, err := database.GetEmergencyContacts()
Expand All @@ -197,7 +197,7 @@ func TestSaveEmergencyContact(t *testing.T) {
So(err, ShouldBeNil)
So(emergencyContacts, ShouldResemble, expectedEmergencyContacts)

emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff)
So(err, ShouldBeNil)
So(emergencyContactIDs, ShouldResemble, expectedEmergencyContactIDs)
})
Expand All @@ -212,14 +212,14 @@ func TestSaveEmergencyContacts(t *testing.T) {

Convey("Test saveEmergencyContacts", t, func() {
Convey("With some emergency contacts", func() {
expectedEmergencyContacts := []*moira.EmergencyContact{&testEmergencyContact, &testEmergencyContact2, &testEmergencyContact3}
expectedEmergencyContacts := []*datatypes.EmergencyContact{&testEmergencyContact, &testEmergencyContact2, &testEmergencyContact3}
expectedEmergencyContactIDs := []string{testContactID, testContactID2}

emergencyContacts, err := database.GetEmergencyContacts()
So(err, ShouldBeNil)
So(emergencyContacts, ShouldBeEmpty)

err = database.saveEmergencyContacts([]moira.EmergencyContact{
err = database.saveEmergencyContacts([]datatypes.EmergencyContact{
testEmergencyContact,
testEmergencyContact2,
testEmergencyContact3,
Expand All @@ -230,7 +230,7 @@ func TestSaveEmergencyContacts(t *testing.T) {
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContacts, expectedEmergencyContacts)

emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(datatypes.HeartbeatNotifierOff)
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContactIDs, expectedEmergencyContactIDs)
})
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestRemoveEmergencyContact(t *testing.T) {

emergencyContact, err = database.GetEmergencyContact(testContactID)
So(errors.Is(err, moiradb.ErrNil), ShouldBeTrue)
So(emergencyContact, ShouldResemble, moira.EmergencyContact{})
So(emergencyContact, ShouldResemble, datatypes.EmergencyContact{})
})
})
}
Loading

0 comments on commit b6ca9d2

Please sign in to comment.