From cdc88752da37d0b08098bd2327d4c8ffdb7180ba Mon Sep 17 00:00:00 2001 From: Shuhrat Tultaganov Date: Tue, 13 Dec 2022 19:24:42 +0300 Subject: [PATCH] add test for Func Create tables --- internal/bd/bd.go | 17 +++++++++++++++-- internal/bd/bd_test.go | 19 +++++++++++++++++++ internal/bd/filmrepo_test.go | 22 ++++++++++++++++++++++ internal/bd/filmsrepo.go | 24 ++++++++++++++++++++++++ internal/bd/model/films.go | 6 +++--- internal/bd/repositoryes/filmsrepo.go | 18 ------------------ internal/bd/testhelper.go | 26 ++++++++++++++++++++++++++ 7 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 internal/bd/bd_test.go create mode 100644 internal/bd/filmrepo_test.go create mode 100644 internal/bd/filmsrepo.go delete mode 100644 internal/bd/repositoryes/filmsrepo.go create mode 100644 internal/bd/testhelper.go diff --git a/internal/bd/bd.go b/internal/bd/bd.go index dcd178d..afa3f87 100644 --- a/internal/bd/bd.go +++ b/internal/bd/bd.go @@ -7,8 +7,9 @@ import ( ) type Bd struct { - config *ConfigBD - db *sql.DB + config *ConfigBD + db *sql.DB + filmsrepo *Filmsrepo } func New(config *ConfigBD) *Bd { @@ -32,3 +33,15 @@ func (b *Bd) Open() error { func (b *Bd) Close() { b.db.Close() } + +func (b *Bd) Films() *Filmsrepo { + if b.filmsrepo != nil { + return b.filmsrepo + } + + b.filmsrepo = &Filmsrepo{ + db: *b, + } + + return b.filmsrepo +} diff --git a/internal/bd/bd_test.go b/internal/bd/bd_test.go new file mode 100644 index 0000000..6e15cc8 --- /dev/null +++ b/internal/bd/bd_test.go @@ -0,0 +1,19 @@ +package bd_test + +import ( + "os" + "testing" +) + +var ( + database string +) + +func TestMain(m *testing.M) { + database := os.Getenv("DATABASE_URL") + if database == "" { + database = "host=localhost user=admin password=root dbname=postgres sslmode=disable" + } + + os.Exit(m.Run()) +} diff --git a/internal/bd/filmrepo_test.go b/internal/bd/filmrepo_test.go new file mode 100644 index 0000000..5682e39 --- /dev/null +++ b/internal/bd/filmrepo_test.go @@ -0,0 +1,22 @@ +package bd_test + +import ( + "testing" + + "git.ukamnya.ru/stulyaganov/RestApi/internal/bd" + "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" + "github.com/stretchr/testify/assert" +) + +func TestFilmRepo_Create(t *testing.T) { + + s, teardown := bd.Testdb(t, "host=localhost user=admin password=root dbname=postgres sslmode=disable") + defer teardown("films") + u, err := s.Films().Create(&model.Films{ + Id: 0, + Ru_title: "sdasdasda", + }) + + assert.NoError(t, err) + assert.NotNil(t, u) +} diff --git a/internal/bd/filmsrepo.go b/internal/bd/filmsrepo.go new file mode 100644 index 0000000..143c2b3 --- /dev/null +++ b/internal/bd/filmsrepo.go @@ -0,0 +1,24 @@ +package bd + +import ( + "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" +) + +type Filmsrepo struct { + db Bd +} + +func (f *Filmsrepo) Create(m *model.Films) (*model.Films, error) { + if err := f.db.db.QueryRow( + "INSERT INTO films (id, ru_title, orig_title) VALUES($1, $2, $3) RETURNING id", + m.Id, m.Ru_title, m.Orig_title).Scan(&m.Id); err != nil { + return nil, err + } + + return m, nil + +} + +func FindDyId(id int) (*model.Films, error) { + return nil, nil +} diff --git a/internal/bd/model/films.go b/internal/bd/model/films.go index 1aa2e97..386ce53 100644 --- a/internal/bd/model/films.go +++ b/internal/bd/model/films.go @@ -1,9 +1,9 @@ package model type Films struct { - id int - ru_title string - orig_title string + Id int + Ru_title string + Orig_title string imdb_id string kinopoisk_id string posterUrl string diff --git a/internal/bd/repositoryes/filmsrepo.go b/internal/bd/repositoryes/filmsrepo.go deleted file mode 100644 index e0b64de..0000000 --- a/internal/bd/repositoryes/filmsrepo.go +++ /dev/null @@ -1,18 +0,0 @@ -package repositoryes - -import ( - "git.ukamnya.ru/stulyaganov/RestApi/internal/bd" - "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" -) - -type Filmsrepo struct { - DB *bd.Bd -} - -func Create(model.Films) (*model.Films, error) { - return nil, nil -} - -func FindDyId(id int) (*model.Films, error) { - return nil, nil -} diff --git a/internal/bd/testhelper.go b/internal/bd/testhelper.go new file mode 100644 index 0000000..5587718 --- /dev/null +++ b/internal/bd/testhelper.go @@ -0,0 +1,26 @@ +package bd + +import ( + "fmt" + "strings" + "testing" +) + +func Testdb(t *testing.T, databaseurl string) (*Bd, func(...string)) { + t.Helper() + config := NewConfig() + config.BaseUrlBd = databaseurl + s := New(config) + + if err := s.Open(); err != nil { + t.Fatal(err) + } + return s, func(tables ...string) { + if len(tables) > 0 { + if _, err := s.db.Exec(fmt.Sprintf("TRUNCATE %s CASCADE", strings.Join(tables, ", "))); err != nil { + t.Fatal(err) + } + } + s.Close() + } +}