Skip to content

Commit

Permalink
Implement rpc image service (#14)
Browse files Browse the repository at this point in the history
* #13 setup Makefile to generate proto

* #13 rename file to image

* #13 implement GetBoolEnv

* #13 create pkg and move env loader

* #13 create request validations

* #13 implement error convertors

* #13 change service name

* #13 add uuid

* #13 fix test rule

* #13 remove .gitkeep

* #13 separate required value getter

* #13 ignore env files

* #13 implement repository and setup migration

* #13 implement upload image function

* #13 setup local and test database

* #13 refresh mod file

* #13 import driver library

* #13 add device id to request

* #13 add device id to request

* #13 fix failed to use transaction

* #13 change pb package name

* #13 use yaml for config

* #13 use yaml for config

* #13 setup ci

* #13 fix ci config

* #13 down go version on ci

* #13 upgrade actions/setup-go

* #13 down go version on ci

* #13 change ci config

* #13 change ci config

* #13 change ci config

* #13 change ci config

* #13 change ci config

* #13 change ci config

* #13 change ci config

* #13 change ci config
  • Loading branch information
CityBear3 authored Jul 28, 2023
1 parent 98aa86c commit 0e55369
Show file tree
Hide file tree
Showing 70 changed files with 7,297 additions and 886 deletions.
28 changes: 23 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,47 @@ name: CI

on:
push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19

- name: Build
run: |
go build -v ./...
go build -v
test:
runs-on: ubuntu-latest
services:
mysql:
image: mysql
env:
MYSQL_DATABASE: satellite
MYSQL_USER: satellite
MYSQL_PASSWORD: satellite
MYSQL_ROOT_USER: satellite
MYSQL_ROOT_PASSWORD: satellite
ports:
- "3307:3306"
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19

- name: Test
env:
MYSQL_DATABASE: satellite
MYSQL_USER: satellite
MYSQL_PASSWORD: satellite
MYSQL_HOST: localhost
MYSQL_PORT: 3307
run: |
go install -tags 'mysql' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
sleep 20
make migrate-up
go test -v $(go list ./... | grep -v pb)
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@
go.work

# ide
.idea
.idea

# dotenv
.env
.envrc

docker/minio/data/**
docker/mysql/db/**

22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIGRATION = ""

gen-proto:
protoc --proto_path=protoc --go_out=./pb --go_opt=paths=source_relative --go-grpc_out=./pb --go-grpc_opt=paths=source_relative protoc/**/v1/*.proto

test:
go test -v ./...

gen-schema:
sqlboiler mysql -c database.toml -o ./adaptor/repository/mysql/shcema -p schema --no-tests --wipe

gen-migration:
migrate create -ext sql -dir ./db/migration -seq ${MIGRATION}

migrate-up:
migrate --path db/migration --database "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}" --verbose up

migrate-down:
migrate --path db/migration --database "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}" --verbose down

migrate-drop:
migrate --path db/migration --database "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}" --verbose drop
Empty file removed adaptor/repository/.gitkeep
Empty file.
38 changes: 38 additions & 0 deletions adaptor/repository/mysql/archive_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mysql

import (
"context"
"github.com/CityBear3/satellite/adaptor/repository/mysql/shcema"
"github.com/CityBear3/satellite/domain/model"
"github.com/oklog/ulid/v2"
"github.com/volatiletech/sqlboiler/v4/boil"
)

type ArchiveRepository struct{}

func NewArchiveRepository() *ArchiveRepository {
return &ArchiveRepository{}
}

func (i *ArchiveRepository) Save(ctx context.Context, archive model.Archive) error {
tx, err := GetTransaction(ctx)
if err != nil {
return err
}

archiveSchema := schema.Archive{
ID: archive.Id.String(),
Size: archive.Size,
Ext: archive.Ext,
DeviceID: archive.DeviceId.String(),
}
if err := archiveSchema.Upsert(ctx, tx, boil.Infer(), boil.Infer()); err != nil {
return err
}
return nil
}

func (i *ArchiveRepository) GetArchive(ctx context.Context, imageId ulid.ULID) (*model.Archive, error) {
//TODO implement me
panic("implement me")
}
121 changes: 121 additions & 0 deletions adaptor/repository/mysql/archive_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package mysql

import (
"context"
"database/sql"
"github.com/CityBear3/satellite/domain/model"
"github.com/CityBear3/satellite/tests/helper"
"github.com/CityBear3/satellite/tests/table"
_ "github.com/go-sql-driver/mysql"
"github.com/oklog/ulid/v2"
"github.com/stretchr/testify/assert"
"github.com/volatiletech/sqlboiler/v4/boil"
"testing"
)

func setTx(ctx context.Context, tx *sql.Tx) context.Context {
return context.WithValue(ctx, "tx", tx)
}

func TestArchiveRepository_Save(t *testing.T) {
type args struct {
ctx context.Context
archive model.Archive
}
type test struct {
name string
args args
wantErr bool
expectedErr string
tables []helper.TableOperator
queries []string
}

db, err := helper.GetTestDB()
if err != nil {
t.Fatal(err)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
panic(err)
}
}(db)
boil.SetDB(db)

sut := NewArchiveRepository()

clientId := ulid.Make()
deviceId := ulid.Make()
archiveId := ulid.Make()

tests := []test{
{
name: "insert image",
args: args{
ctx: context.Background(),
archive: model.Archive{
Id: archiveId,
Size: 1024,
Ext: "jpg",
DeviceId: deviceId,
},
},
wantErr: false,
tables: []helper.TableOperator{
table.ClientTable{
Id: clientId.String(),
Name: "test",
Description: "test",
Secret: "",
},
table.DeviceTable{
Id: deviceId.String(),
Name: "test",
Description: "test",
Secret: "",
ClientId: clientId.String(),
},
},
queries: []string{
"SELECT * FROM `archive` WHERE `id`=?",
},
},
}

for _, tt := range tests {
tx, err := boil.BeginTx(tt.args.ctx, nil)
if err != nil {
t.Error(err)
}
ctx := setTx(tt.args.ctx, tx)
for _, operator := range tt.tables {
if err := operator.Insert(ctx, tx); err != nil {
t.Error(err)
return
}
}

savedResult := table.ArchiveTable{}
t.Run(tt.name, func(t *testing.T) {
if err = sut.Save(ctx, tt.args.archive); err != nil {
t.Error(err)
return
}
err := tx.QueryRowContext(ctx, tt.queries[0], tt.args.archive.Id.String()).Scan(&savedResult.Id, &savedResult.DeviceId, &savedResult.Size, &savedResult.Ext, &savedResult.CreatedAt, &savedResult.UpdatedAt)
if err != nil {
t.Error(err)
} else {
assert.Equal(t, tt.args.archive.Id.String(), savedResult.Id)
assert.Equal(t, tt.args.archive.Size, savedResult.Size)
assert.Equal(t, tt.args.archive.Ext, savedResult.Ext)
}

t.Cleanup(func() {
if err = tx.Rollback(); err != nil {
t.Fatal(err)
}
})
})
}
}
Loading

0 comments on commit 0e55369

Please sign in to comment.