-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f01064
commit bc19b15
Showing
19 changed files
with
1,998 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package db | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
/* | ||
Copyright (C) 2023 Ulbora Labs LLC. (www.ulboralabs.com) | ||
All rights reserved. | ||
Copyright (C) 2023 Ken Williamson | ||
All rights reserved. | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
// AddHome AddHome | ||
func (d *MyBlogDB) AddHome(ab *Home) (bool, int64) { | ||
var suc bool | ||
var id int64 | ||
if !d.testConnection() { | ||
d.DB.Connect() | ||
} | ||
if ab != nil { | ||
var a []any | ||
a = append(a, ab.Content) | ||
suc, id = d.DB.Insert(insertHome, a...) | ||
d.Log.Debug("suc in add Home", suc) | ||
d.Log.Debug("id in add Home", id) | ||
} | ||
return suc, id | ||
} | ||
|
||
// UpdateHome UpdateHome | ||
func (d *MyBlogDB) UpdateHome(ab *Home) bool { | ||
var suc bool | ||
if !d.testConnection() { | ||
d.DB.Connect() | ||
} | ||
if ab != nil { | ||
var a []any | ||
a = append(a, ab.Content, ab.ID) | ||
suc = d.DB.Update(updateHome, a...) | ||
d.Log.Debug("suc in update Home", suc) | ||
} | ||
return suc | ||
} | ||
|
||
// GetHome GetHome | ||
func (d *MyBlogDB) GetHome() *[]Home { | ||
if !d.testConnection() { | ||
d.DB.Connect() | ||
} | ||
var rtn = []Home{} | ||
var a []any | ||
a = append(a) | ||
rows := d.DB.GetList(selectHome, a...) | ||
if rows != nil && len(rows.Rows) != 0 { | ||
foundRows := rows.Rows | ||
for r := range foundRows { | ||
foundRow := foundRows[r] | ||
rowContent := d.parseHomeRow(&foundRow) | ||
rtn = append(rtn, *rowContent) | ||
} | ||
} | ||
return &rtn | ||
} | ||
|
||
func (d *MyBlogDB) parseHomeRow(foundRow *[]string) *Home { | ||
var rtn Home | ||
d.Log.Debug("foundRow in Home", *foundRow) | ||
if len(*foundRow) > 0 { | ||
id, err := strconv.ParseInt((*foundRow)[0], 10, 64) | ||
d.Log.Debug("id err in get Home", err) | ||
if err == nil { | ||
rtn.ID = id | ||
rtn.Content = (*foundRow)[1] | ||
} | ||
} | ||
return &rtn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package db | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
lg "github.com/GolangToolKits/go-level-logger" | ||
gdb "github.com/GolangToolKits/go-mysql" | ||
) | ||
|
||
func TestMyBlogDB_AddHome(t *testing.T) { | ||
|
||
// db := gdb.MyDB{ | ||
// Host: "localhost:3306", | ||
// User: "admin", | ||
// Password: "admin", | ||
// Database: "go_micro_blog", | ||
// } | ||
|
||
db := gdb.MyDBMock{ | ||
Host: "localhost:3306", | ||
User: "admin", | ||
Password: "admin", | ||
Database: "go_micro_blog", | ||
} | ||
db.MockTestRow = &gdb.DbRow{ | ||
//Row: []string{"0"}, | ||
Row: []string{}, | ||
} | ||
db.MockConnectSuccess = true | ||
db.MockInsertID1 = 1 | ||
db.MockInsertSuccess1 = true | ||
|
||
var l lg.Logger | ||
log := l.New() | ||
log.SetLogLevel(lg.AllLevel) | ||
|
||
type fields struct { | ||
DB gdb.Database | ||
Log lg.Log | ||
} | ||
type args struct { | ||
r *Home | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
want1 int64 | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "test 1", | ||
fields: fields{ | ||
DB: db.New(), | ||
Log: log, | ||
}, | ||
args: args{ | ||
r: &Home{ | ||
Content: "test Home content", | ||
}, | ||
}, | ||
want: true, | ||
want1: 1, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
d := &MyBlogDB{ | ||
DB: tt.fields.DB, | ||
Log: tt.fields.Log, | ||
} | ||
d.DB.Connect() | ||
got, got1 := d.AddHome(tt.args.r) | ||
if got != tt.want { | ||
t.Errorf("MyBlogDB.AddHome() got = %v, want %v", got, tt.want) | ||
} | ||
if got1 != tt.want1 { | ||
t.Errorf("MyBlogDB.AddHome() got1 = %v, want %v", got1, tt.want1) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMyBlogDB_UpdateHome(t *testing.T) { | ||
|
||
// db := gdb.MyDB{ | ||
// Host: "localhost:3306", | ||
// User: "admin", | ||
// Password: "admin", | ||
// Database: "go_micro_blog", | ||
// } | ||
|
||
db := gdb.MyDBMock{ | ||
Host: "localhost:3306", | ||
User: "admin", | ||
Password: "admin", | ||
Database: "go_micro_blog", | ||
} | ||
db.MockTestRow = &gdb.DbRow{ | ||
//Row: []string{"0"}, | ||
Row: []string{}, | ||
} | ||
db.MockConnectSuccess = true | ||
db.MockUpdateSuccess1 = true | ||
|
||
var l lg.Logger | ||
log := l.New() | ||
log.SetLogLevel(lg.AllLevel) | ||
|
||
type fields struct { | ||
DB gdb.Database | ||
Log lg.Log | ||
} | ||
type args struct { | ||
r *Home | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "test 1", | ||
fields: fields{ | ||
DB: db.New(), | ||
Log: log, | ||
}, | ||
args: args{ | ||
r: &Home{ | ||
ID: 1, | ||
Content: "test home content 1", | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
d := &MyBlogDB{ | ||
DB: tt.fields.DB, | ||
Log: tt.fields.Log, | ||
} | ||
d.DB.Connect() | ||
if got := d.UpdateHome(tt.args.r); got != tt.want { | ||
t.Errorf("MyBlogDB.UpdateHome() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMyBlogDB_GetHome(t *testing.T) { | ||
// db := gdb.MyDB{ | ||
// Host: "localhost:3306", | ||
// User: "admin", | ||
// Password: "admin", | ||
// Database: "go_micro_blog", | ||
// } | ||
|
||
db := gdb.MyDBMock{ | ||
Host: "localhost:3306", | ||
User: "admin", | ||
Password: "admin", | ||
Database: "go_micro_blog", | ||
} | ||
db.MockTestRow = &gdb.DbRow{ | ||
//Row: []string{"0"}, | ||
Row: []string{}, | ||
} | ||
|
||
db.MockRows1 = &gdb.DbRows{ | ||
Rows: [][]string{ | ||
{"1", "test home content 1"}, | ||
//{"2", "test content 3"}, | ||
}, | ||
} | ||
|
||
var l lg.Logger | ||
log := l.New() | ||
log.SetLogLevel(lg.AllLevel) | ||
|
||
type fields struct { | ||
DB gdb.Database | ||
Log lg.Log | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want *[]Home | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "test 1", | ||
fields: fields{ | ||
DB: db.New(), | ||
Log: log, | ||
}, | ||
want: &[]Home{ | ||
{1, "test home content 1"}, | ||
// {3, "test Content 3"}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
d := &MyBlogDB{ | ||
DB: tt.fields.DB, | ||
Log: tt.fields.Log, | ||
} | ||
d.DB.Connect() | ||
if got := d.GetHome(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("MyBlogDB.GetHome() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.