Skip to content

Commit

Permalink
Merge pull request #180 from k1LoW/config-name
Browse files Browse the repository at this point in the history
Add `name:` for specifing database name of document
  • Loading branch information
k1LoW authored Mar 12, 2020
2 parents f897afd + 0b5dbf3 commit a119009
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Key features of `tbls` are:
- [Lint a database](#lint-a-database)
- [Continuous Integration](#continuous-integration)
- [Configuration](#configration)
- [Name](#name)
- [DSN](#dsn)
- [Support Database](#support-database)
- [Document path](#document-path)
Expand Down Expand Up @@ -223,9 +224,18 @@ script:

## Configuration

### Name

`name:` is used to specify the database name of the document.

``` yaml
# .tbls.yml
name: mydatabase
```

### DSN

`DSN:` (Data Source Name) is used to connect to database.
`dsn:` (Data Source Name) is used to connect to database.

``` yaml
# .tbls.yml
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var DefaultDistance = 1

// Config is tbls config
type Config struct {
Name string `yaml:"name"`
DSN string `yaml:"dsn"`
DocPath string `yaml:"docPath"`
Format Format `yaml:"format"`
Expand Down Expand Up @@ -239,6 +240,9 @@ func (c *Config) LoadConfigFile(path string) error {

// ModifySchema modify schema.Schema by config
func (c *Config) ModifySchema(s *schema.Schema) error {
if c.Name != "" {
s.Name = c.Name
}
err := c.MergeAdditionalData(s)
if err != nil {
return err
Expand Down
83 changes: 44 additions & 39 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ func TestLoadDefault(t *testing.T) {
}
want := ""
if config.DSN != want {
t.Errorf("actual %v\nwant %v", config.DSN, want)
t.Errorf("got %v\nwant %v", config.DSN, want)
}
want2 := "dbdoc"
if config.DocPath != want2 {
t.Errorf("actual %v\nwant %v", config.DocPath, want2)
t.Errorf("got %v\nwant %v", config.DocPath, want2)
}
want3 := "png"
if config.ER.Format != want3 {
t.Errorf("actual %v\nwant %v", config.ER.Format, want3)
t.Errorf("got %v\nwant %v", config.ER.Format, want3)
}
want4 := 1
if *config.ER.Distance != want4 {
t.Errorf("actual %v\nwant %v", config.ER.Distance, want4)
t.Errorf("got %v\nwant %v", config.ER.Distance, want4)
}
}

Expand All @@ -48,19 +48,19 @@ func TestLoadConfigFile(t *testing.T) {
if err != nil {
t.Fatal(err)
}
expected := "pg://root:pgpass@localhost:55432/testdb?sslmode=disable"
if config.DSN != expected {
t.Errorf("actual %v\nwant %v", config.DSN, expected)
want := "pg://root:pgpass@localhost:55432/testdb?sslmode=disable"
if config.DSN != want {
t.Errorf("got %v\nwant %v", config.DSN, want)
}
expected2 := "sample/pg"
if config.DocPath != expected2 {
t.Errorf("actual %v\nwant %v", config.DocPath, expected2)
want2 := "sample/pg"
if config.DocPath != want2 {
t.Errorf("got %v\nwant %v", config.DocPath, want2)
}
}

var tests = []struct {
value string
expected string
value string
want string
}{
{"${TBLS_ONE}/${TBLS_TWO}", "one/two"},
{"${TBLS_ONE}/${TBLS_TWO}/${TBLS_NONE}", "one/two/"},
Expand All @@ -72,12 +72,12 @@ func TestParseWithEnvirion(t *testing.T) {
_ = os.Setenv("TBLS_ONE", "one")
_ = os.Setenv("TBLS_TWO", "two")
for _, tt := range tests {
actual, err := parseWithEnviron(tt.value)
got, err := parseWithEnviron(tt.value)
if err != nil {
t.Fatal(err)
}
if actual != tt.expected {
t.Errorf("actual %v\nwant %v", actual, tt.expected)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
}
}
Expand Down Expand Up @@ -132,17 +132,17 @@ func TestMergeAditionalData(t *testing.T) {
if err != nil {
t.Error(err)
}
expected := 1
actual := len(s.Relations)
if actual != expected {
t.Errorf("actual %v\nwant %v", actual, expected)
want := 1
got := len(s.Relations)
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
posts, _ := s.FindTableByName("posts")
title, _ := posts.FindColumnByName("title")
expected2 := "post title"
actual2 := title.Comment
if actual2 != expected2 {
t.Errorf("actual %v\nwant %v", actual2, expected2)
want2 := "post title"
got2 := title.Comment
if got2 != want2 {
t.Errorf("got %v\nwant %v", got2, want2)
}
}

Expand Down Expand Up @@ -227,10 +227,10 @@ func TestFilterTables(t *testing.T) {
if err != nil {
t.Error(err)
}
expected := 2
actual := len(s.Tables)
if actual != expected {
t.Errorf("actual %v\nwant %v", actual, expected)
want := 2
got := len(s.Tables)
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
}

Expand Down Expand Up @@ -297,22 +297,27 @@ func TestModifySchema(t *testing.T) {
if err != nil {
t.Error(err)
}
expected := 1
actual := len(s.Relations)
if actual != expected {
t.Errorf("actual %v\nwant %v", actual, expected)
want := 1
got := len(s.Relations)
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
posts, _ := s.FindTableByName("posts")
title, _ := posts.FindColumnByName("title")
expected2 := "post title"
actual2 := title.Comment
if actual2 != expected2 {
t.Errorf("actual %v\nwant %v", actual2, expected2)
want2 := "post title"
got2 := title.Comment
if got2 != want2 {
t.Errorf("got %v\nwant %v", got2, want2)
}
want3 := 2
got3 := len(s.Tables)
if got3 != want3 {
t.Errorf("got %v\nwant %v", got3, want3)
}
expected3 := 2
actual3 := len(s.Tables)
if actual3 != expected3 {
t.Errorf("actual %v\nwant %v", actual, expected)
want4 := "mydatabase"
got4 := s.Name
if got4 != want4 {
t.Errorf("got %v\nwant %v", got4, want4)
}
}

Expand Down
1 change: 1 addition & 0 deletions testdata/config_test.yml.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: ""
dsn: ""
docPath: dbdoc
format:
Expand Down
1 change: 1 addition & 0 deletions testdata/config_test_tbls.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
name: mydatabase
include:
- users
- posts
Expand Down

0 comments on commit a119009

Please # to comment.