Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial generic rethinkdb store #539

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions cmd/metal-api/internal/datastore/ip_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
package datastore

import (
"context"
"log/slog"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/generic-datastore"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata"
)

func TestRethinkStore_FindIPByID(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)

is := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).IP()
tests := []struct {
name string
rs *RethinkStore
is generic.Storage[*metal.IP]
id string
want *metal.IP
wantErr bool
}{
{
name: "TestRethinkStore_FindIP Test 1",
rs: ds,
is: is,
id: "1.2.3.4",
want: &testdata.IP1,
wantErr: false,
},
{
name: "TestRethinkStore_FindIP Test 2",
rs: ds,
is: is,
id: "2.3.4.5",
want: &testdata.IP2,
wantErr: false,
Expand All @@ -37,7 +40,7 @@ func TestRethinkStore_FindIPByID(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.FindIPByID(tt.id)
got, err := tt.is.Get(context.Background(), tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.FindIP() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -52,24 +55,25 @@ func TestRethinkStore_FindIPByID(t *testing.T) {
func TestRethinkStore_ListIPs(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
is := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).IP()

tests := []struct {
name string
rs *RethinkStore
is generic.Storage[*metal.IP]
want metal.IPs
wantErr bool
}{
{
name: "TestRethinkStore_ListIPs Test 1",
rs: ds,
is: is,
want: testdata.TestIPs,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.ListIPs()
got, err := tt.is.List(context.Background())
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.ListIPs() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
45 changes: 27 additions & 18 deletions cmd/metal-api/internal/datastore/partition_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package datastore

import (
"context"
"log/slog"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/generic-datastore"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata"
)

func TestRethinkStore_FindPartition(t *testing.T) {
ds, mock := InitMockDB(t)

ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()
testdata.InitMockDBData(mock)

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
id string
want *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
id: "1",
want: &testdata.Partition1,
wantErr: false,
},
{
name: "Test 2",
rs: ds,
ps: ps,
id: "2",
want: &testdata.Partition2,
wantErr: false,
Expand All @@ -37,7 +42,7 @@ func TestRethinkStore_FindPartition(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.FindPartition(tt.id)
got, err := tt.ps.Get(context.Background(), tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.FindPartition() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -52,24 +57,25 @@ func TestRethinkStore_FindPartition(t *testing.T) {
func TestRethinkStore_ListPartitions(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
want metal.Partitions
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
want: testdata.TestPartitions,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.ListPartitions()
got, err := tt.ps.List(context.Background())
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.ListPartitions() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -84,24 +90,25 @@ func TestRethinkStore_ListPartitions(t *testing.T) {
func TestRethinkStore_CreatePartition(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
p *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
p: &testdata.Partition1,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
if err := tt.rs.CreatePartition(tt.p); (err != nil) != tt.wantErr {
if err := tt.ps.Create(context.Background(), tt.p); (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.CreatePartition() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand All @@ -111,30 +118,31 @@ func TestRethinkStore_CreatePartition(t *testing.T) {
func TestRethinkStore_DeletePartition(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
p *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
p: &testdata.Partition1,
wantErr: false,
},
{
name: "Test 2",
rs: ds,
ps: ps,
p: &testdata.Partition2,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
err := tt.rs.DeletePartition(tt.p)
err := tt.ps.Delete(context.Background(), tt.p)
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.DeletePartition() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -146,24 +154,25 @@ func TestRethinkStore_DeletePartition(t *testing.T) {
func TestRethinkStore_UpdatePartition(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
oldPartition *metal.Partition
newPartition *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
oldPartition: &testdata.Partition1,
newPartition: &testdata.Partition2,
wantErr: false,
},
{
name: "Test 2",
rs: ds,
ps: ps,
oldPartition: &testdata.Partition2,
newPartition: &testdata.Partition1,
wantErr: false,
Expand All @@ -172,7 +181,7 @@ func TestRethinkStore_UpdatePartition(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
if err := tt.rs.UpdatePartition(tt.oldPartition, tt.newPartition); (err != nil) != tt.wantErr {
if err := tt.ps.Update(context.Background(), tt.oldPartition, tt.newPartition); (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.UpdatePartition() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
8 changes: 8 additions & 0 deletions cmd/metal-api/internal/datastore/rethinkdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ tryAgain:
return s
}

func (rs *RethinkStore) DBName() string {
return rs.dbname
majst01 marked this conversation as resolved.
Show resolved Hide resolved
}

func (rs *RethinkStore) QueryExecutor() r.QueryExecutor {
return rs.session
}

func (rs *RethinkStore) findEntityByID(table *r.Term, entity interface{}, id string) error {
res, err := table.Get(id).Run(rs.session)
if err != nil {
Expand Down
Loading
Loading