Skip to content

Commit

Permalink
Merge pull request #11 from harakeishi/add_test_and_comment
Browse files Browse the repository at this point in the history
Add test and comment
  • Loading branch information
harakeishi authored Feb 12, 2023
2 parents 96d85b3 + 5eee457 commit 902faf8
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 66 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
push:
branches:
- main
pull_request:
jobs:
golang-test:
strategy:
matrix:
go-version: [1.20.x]
os: [ubuntu-latest]
name: lint
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
- name: testing
run: go test ./... -coverprofile=coverage.out
- name: create report
uses: k1LoW/octocov-action@v0
20 changes: 20 additions & 0 deletions .octocov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
coverage:
paths:
- coverage.out
codeToTestRatio:
code:
- '**/*.go'
- '!**/*_test.go'
test:
- '**/*_test.go'
testExecutionTime:
if: true
comment:
if: is_pull_request
report:
if: is_default_branch
datastores:
- artifact://${GITHUB_REPOSITORY}
diff:
datastores:
- artifact://${GITHUB_REPOSITORY}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ go 1.18

require (
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/google/go-cmp v0.5.2
github.com/google/go-github v17.0.0+incompatible
github.com/harakeishi/curver v0.1.2
github.com/mattn/go-runewidth v0.0.13
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8
github.com/spf13/cobra v1.4.0
golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/genkiroid/cert v0.0.0-20191007122723-897560fbbe50 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 h1:QqwPZCwh/k1uYqq6uXSb9TRDhTkfQbO80v8zhnIe5zM=
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04=
github.com/genkiroid/cert v0.0.0-20191007122723-897560fbbe50 h1:vLwmYBduhnWWqShoUGbVgDulhcLdanoYtCQxYMzwaqQ=
github.com/genkiroid/cert v0.0.0-20191007122723-897560fbbe50/go.mod h1:Pb7nyGYAfDyE/IkU6AJeRshIFko0wJC9cOqeYzYQffk=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down
9 changes: 8 additions & 1 deletion trv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
Source []Source `json:"source"`
}

// Check if there is a config file
func (c Config) Exists() (bool, error) {
home, err := os.UserHomeDir()
if err != nil {
Expand All @@ -28,6 +29,8 @@ func (c Config) Exists() (bool, error) {
return true, nil
}
}

// Load the config file
func (c *Config) loadConfig() error {
home, err := os.UserHomeDir()
if err != nil {
Expand All @@ -43,18 +46,21 @@ func (c *Config) loadConfig() error {
return nil
}

// Retrieve the source list from the config.
func (c Config) getSourceList() []string {
var sourceList []string
for _, v := range c.Source {
sourceList = append(sourceList, fmt.Sprintf("%s/%s", v.Repo, v.Path))
}
return sourceList
}

// Add the source to the config.
func (c *Config) addSource(s Source) {
c.Source = append(c.Source, s)
c.saveConfig()
}

// save the config.
func (c Config) saveConfig() {
home, _ := os.UserHomeDir()
file, _ := json.MarshalIndent(c, "", " ")
Expand All @@ -65,6 +71,7 @@ func (c Config) saveConfig() {
_ = ioutil.WriteFile(filepath.Join(dir, "config.json"), file, 0644)
}

// Generate GitHub client
func (s Source) NewClient() (*github.Client, context.Context, error) {
var client *github.Client
var ts oauth2.TokenSource
Expand Down
92 changes: 92 additions & 0 deletions trv/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package trv

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestConfig_getSourceList(t *testing.T) {
tests := []struct {
name string
c Config
want []string
}{
{
name: "The correct listing can be obtained from the source",
c: Config{
Source: []Source{
{
Repo: "test1",
Path: "testPath1",
},
{
Repo: "test2",
Path: "testPath2",
},
},
},
want: []string{
"test1/testPath1",
"test2/testPath2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.c.getSourceList(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Config.getSourceList() = %v, want %v", got, tt.want)
}
})
}
}

func TestConfig_addSource(t *testing.T) {
type args struct {
s Source
}
tests := []struct {
name string
c *Config
args args
want Config
}{
{
name: "Correct source is added.",
c: &Config{},
args: args{
s: Source{
Owner: "test",
Repo: "test",
Path: "test",
IsEnterprise: false,
Token: "test",
BaseURL: "",
UploadURL: "",
},
},
want: Config{
Source: []Source{
{
Owner: "test",
Repo: "test",
Path: "test",
IsEnterprise: false,
Token: "test",
BaseURL: "",
UploadURL: "",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.c.addSource(tt.args.s)
if diff := cmp.Diff(tt.c.Source, tt.want.Source); diff != "" {
t.Errorf("Source value is mismatch (-get +want):\n%s", diff)
}
})
}
}
19 changes: 15 additions & 4 deletions trv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ type DB struct {
}

// If there is DB data locally, load it and return it.
func (d *DB) loadData(repo, path string) {
func (d *DB) loadData(repo, path string) error {
home, err := os.UserHomeDir()
if err != nil {
log.Printf("loadData fail:%s", err)
return fmt.Errorf("loadData fail:%w", err)
}
raw, _ := ioutil.ReadFile(fmt.Sprintf("%s/.trv/%s-%s.json", home, repo, strings.Replace(path, "/", "-", -1)))
json.Unmarshal(raw, &d.Tables)
if err := json.Unmarshal(raw, &d.Tables); err != nil {
return fmt.Errorf("loadData fail:%w", err)
}
return nil
}

// Store DB data locally.
Expand All @@ -48,6 +51,12 @@ func (d *DB) saveData(repo, path string) {
}
}

/*
Get DB schema information from GitHub.
If there is `schema.json` in GitHub, retrieve it.
Otherwise, get all markdowns.
In that case, skip this process if the information is already stored locally.
*/
func (d *DB) fetchDBInfo(client *github.Client, ctx context.Context, source Source) error {
content, _, _, _ := client.Repositories.GetContents(ctx, source.Owner, source.Repo, fmt.Sprintf("%s/schema.json", source.Path), nil)
if content != nil {
Expand Down Expand Up @@ -77,9 +86,11 @@ func (d *DB) fetchDBInfo(client *github.Client, ctx context.Context, source Sour
continue
}
var table Table
if err := table.fetchTableInfoFromMarkdown(client, ctx, source.Owner, source.Repo, path); err != nil {
schema, err := table.fetchTableInfoInMarkdownFromGitHub(client.Repositories, ctx, source.Owner, source.Repo, path)
if err != nil {
return fmt.Errorf("fech DB info fail:%w", err)
}
table.Description, table.Columns = schema.parse()
d.Tables = append(d.Tables, table)
}
}
Expand Down
51 changes: 51 additions & 0 deletions trv/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package trv

import "strings"

type schema string

// Parses schema information output in markdown from tbls
func (s schema) parse() (Description string, columns []Column) {
tmp := strings.Split(string(s), "#")
d := strings.Split(tmp[3], "\n")
Description = d[2]

var columIndex int
tmp = strings.Split(string(s), "#")
for i, v := range tmp {
if strings.Contains(v, "Columns") {
columIndex = i
}
}

rows := strings.Split(tmp[columIndex], "\n")
header := strings.Split(rows[2], "|")
nameIndex := index(header, "Name")
typeIndex := index(header, "Type")
commentIndex := index(header, "Comment")

for i, v := range rows {
if i < 4 {
continue
}
colum := strings.Split(v, "|")
if len(colum) < 8 {
return
}
columns = append(columns, Column{
Name: strings.TrimSpace(colum[nameIndex]),
Type: strings.TrimSpace(colum[typeIndex]),
Comment: strings.TrimSpace(colum[commentIndex]),
})
}
return
}

func index(a []string, query string) int {
for i, v := range a {
if strings.TrimSpace(v) == query {
return i
}
}
return -1
}
66 changes: 66 additions & 0 deletions trv/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package trv

import (
"reflect"
"testing"
)

func Test_schema_parse(t *testing.T) {
tests := []struct {
name string
s schema
wantDescription string
wantColumns []Column
}{
{
name: "",
s: `
# users
## Description
ユーザーを管理するテーブル
## Columns
| Name | Type | Default | Nullable | Extra Definition | Children | Parents | Comment | Labels |
| ---- | ---- | ------- | -------- | ---------------- | -------- | ------- | ------- | ------ |
| id | int | | false | auto_increment | [comment_stars](comment_stars.md) [comments](comments.md) [posts](posts.md) [user_options](user_options.md) [logs](logs.md) | | ユーザーID | |
## Constraints
| Name | Type | Definition |
| ---- | ---- | ---------- |
| email | UNIQUE | UNIQUE KEY email (email) |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |
| username | UNIQUE | UNIQUE KEY username (username) |
## Indexes
| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (id) USING BTREE |
| email | UNIQUE KEY email (email) USING BTREE |
| username | UNIQUE KEY username (username) USING BTREE |
## Relations
![er](users.svg)
---
> Generated by [tbls](https://github.com/k1LoW/tbls)
`,
wantDescription: "ユーザーを管理するテーブル",
wantColumns: []Column{
{
Name: "id",
Type: "int",
Comment: "ユーザーID",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotDescription, gotColumns := tt.s.parse()
if gotDescription != tt.wantDescription {
t.Errorf("schema.parse() gotDescription = %v, want %v", gotDescription, tt.wantDescription)
}
if !reflect.DeepEqual(gotColumns, tt.wantColumns) {
t.Errorf("schema.parse() gotColumns = %v, want %v", gotColumns, tt.wantColumns)
}
})
}
}
4 changes: 3 additions & 1 deletion trv/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ If not, retrieve it from a remote location.
func (s Source) setDbData() (DB, error) {
var db DB

db.loadData(s.Repo, s.Path)
if err := db.loadData(s.Repo, s.Path); err != nil {
return DB{}, fmt.Errorf("DB loadData fail:%w", err)
}

client, ctx, err := s.NewClient()
if err != nil {
Expand Down
Loading

0 comments on commit 902faf8

Please sign in to comment.