Skip to content

Commit

Permalink
feat: create firestore session test assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
paigecox committed Oct 12, 2023
1 parent b552acb commit 335944b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions manager/store/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type Engine interface {
CertificateStore
OcpiStore
LocationStore
SessionStore
}
1 change: 1 addition & 0 deletions manager/store/firestore/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func cleanupAllCollections(t *testing.T, gcloudProject string) {
cleanupCollection(t, gcloudProject, "Location")
cleanupCollection(t, gcloudProject, "OcpiParty")
cleanupCollection(t, gcloudProject, "OcpiRegistration")
cleanupCollection(t, gcloudProject, "Session")
cleanupCollection(t, gcloudProject, "Token")
cleanupCollection(t, gcloudProject, "Transaction")
}
Expand Down
19 changes: 19 additions & 0 deletions manager/store/firestore/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package firestore

import (
"context"
"github.com/thoughtworks/maeve-csms/manager/store"
)

func (s *Store) SetSession(ctx context.Context, session *store.Session) error {

return nil
}

func (s *Store) LookupSession(ctx context.Context, sessionId string) (*store.Session, error) {
return nil, nil
}

func (s *Store) ListSessions(context context.Context, offset int, limit int) ([]*store.Session, error) {
return nil, nil
}
48 changes: 48 additions & 0 deletions manager/store/firestore/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build integration

package firestore_test

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thoughtworks/maeve-csms/manager/store"
"github.com/thoughtworks/maeve-csms/manager/store/firestore"
"golang.org/x/net/context"
"k8s.io/utils/clock"
"testing"
)

func TestSetAndLookupSession(t *testing.T) {
defer cleanupAllCollections(t, "myproject")

ctx := context.Background()
sessionStore, err := firestore.NewStore(ctx, "myproject", clock.RealClock{})
require.NoError(t, err)

want := &store.Session{
CountryCode: "BEL",
PartyId: "TWK",
Id: "s001",
StartDateTime: "", //Look at
EndDateTime: "",
Kwh: 5,
CdrToken: store.CdrToken{
ContractId: "GBTWK012345678V",
Type: "RFID",
Uid: "MYRFIDTAG",
},
AuthMethod: "AUTH_REQUEST", //may cause issue
Currency: "GBP",
Status: "ACTIVE",
LastUpdated: "2019-08-24T14:15:22Z",
}
err = sessionStore.SetSession(ctx, want)
require.NoError(t, err)

got, err := sessionStore.LookupSession(ctx, "s001")
require.NoError(t, err)

//assert.Regexp(t, `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z`)

assert.Equal(t, want, got)
}
35 changes: 35 additions & 0 deletions manager/store/inmemory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Store struct {
registrations map[string]*store.OcpiRegistration
partyDetails map[string]*store.OcpiParty
locations map[string]*store.Location
sessions map[string]*store.Session
}

func NewStore(clock clock.PassiveClock) *Store {
Expand All @@ -51,6 +52,7 @@ func NewStore(clock clock.PassiveClock) *Store {
registrations: make(map[string]*store.OcpiRegistration),
partyDetails: make(map[string]*store.OcpiParty),
locations: make(map[string]*store.Location),
sessions: make(map[string]*store.Session),
}
}

Expand Down Expand Up @@ -462,3 +464,36 @@ func (s *Store) ListLocations(_ context.Context, offset int, limit int) ([]*stor
}
return locations, nil
}

func (s *Store) SetSession(_ context.Context, session *store.Session) error {
s.Lock()
defer s.Unlock()

s.sessions[session.Id] = session

return nil
}

func (s *Store) LookupSession(_ context.Context, SessionId string) (*store.Session, error) {
s.Lock()
defer s.Unlock()

return s.sessions[SessionId], nil
}

func (s *Store) ListSessions(_ context.Context, offset int, limit int) ([]*store.Session, error) {
s.Lock()
defer s.Unlock()
var sessions []*store.Session
count := 0
for _, session := range s.sessions {
if count >= offset && count < offset+limit {
sessions = append(sessions, session)
}
count++
}
if sessions == nil {
sessions = make([]*store.Session, 0)
}
return sessions, nil
}
6 changes: 4 additions & 2 deletions manager/store/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ type Session struct {
LocationId *string
EvseId *string
ConnectorId *string
Currency *string
TotalCost Price //can be omitted
Currency string
TotalCost Price //can be omitted/optional
Status string
LastUpdated string
}

type SessionStore interface {
SetSession(ctx context.Context, session *Session) error
LookupSession(ctx context.Context, sessionId string) (*Session, error)
ListSessions(ctx context.Context, offset int, limit int) ([]*Session, error)
}

0 comments on commit 335944b

Please sign in to comment.