Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
tank
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
tank
Commits
f9e13ab6
Commit
f9e13ab6
authored
6 years ago
by
Christian Pointner
Browse files
Options
Downloads
Patches
Plain Diff
added rant about database/sql and gorm ...
parent
9b676607
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
store/store_test.go
+39
-54
39 additions, 54 deletions
store/store_test.go
with
39 additions
and
54 deletions
store/store_test.go
+
39
−
54
View file @
f9e13ab6
...
...
@@ -25,11 +25,12 @@
package
store
import
(
"database/sql"
"fmt"
"os"
"os/user"
"testing"
"github.com/jinzhu/gorm"
)
var
(
...
...
@@ -81,7 +82,7 @@ func TestOpen(t *testing.T) {
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
/// exisiting but non-database file (only possible with sqlite...
/// exisiting but non-database file (only possible with sqlite...
)
// if f, err := os.Create(testDBConnection); err != nil {
// t.Fatalf("unexpected error: %v", err)
// } else {
...
...
@@ -126,60 +127,44 @@ func TestMigrations(t *testing.T) {
// TODO: needs more testing!!!!
}
func
TestDBConstraints
(
t
*
testing
.
T
)
{
db
,
err
:=
sql
.
Open
(
testDBType
,
testDBConnection
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
err
=
db
.
Ping
();
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
res
,
err
:=
db
.
Exec
(
"INSERT INTO groups (name) VALUES ($1)"
,
testGroup1
)
if
err
!=
nil
{
t
.
Fatalf
(
"adding group '%s' shouldn't fail: %v"
,
testGroup1
,
err
)
}
t
.
Fatalf
(
"%v"
,
res
)
// /* should fail */
// insert into files (size) values(100);
// insert into files (group_name, size) values('invalid', 101);
// /* should succeed */
// insert into files (group_name, size) values('test', 101);
// insert into files (group_name, size) values('test', 102);
// select * from files;
// /* should fail */
// insert into playlists (created_at) values ('2018-06-17 02:38:17');
// /* should succeed */
// insert into playlists (created_at, group_name) values('2018-06-17 02:38:17', 'test');
// /* should fail */
// insert into playlist_entries (uri) values ('http://stream.example.com/live.mp3');
// insert into playlist_entries (playlist_id, uri) values (1, 'http://stream.example.com/live.mp3');
// insert into playlist_entries (playlist_id, line_num, uri) values (17, 1, 'http://stream.example.com/live.mp3');
// /* should succeed */
// insert into playlist_entries (playlist_id, line_num, uri) values (1, 1, 'http://stream.example.com/live.mp3');
// /* should fail */
// insert into playlist_entries (playlist_id, line_num, uri) values (1, 1, 'http://stream.example.com/other.mp3');
// insert into playlist_entries (playlist_id, line_num, file_id) values (1, 4, 23);
// /* should succeed */
// insert into playlist_entries (playlist_id, line_num, file_id) values (1, 4, 2);
// delete from files where id = 1;
// /* should fail */
// delete from files where id = 2;
//// Arrrrgh!!!!!
//// both database/sql and gorm are incredibly stupid!!!!
//// using database/sql alone it is needlessly hard to write portable sql queries:
//// postgres: db.Exec("INSERT INTO groups (name) VALUES ($1)", testGroup1)
//// mysql: db.Exec("INSERT INTO groups (name) VALUES (?)", testGroup1)
////
//// using gorm db.Exec() will deal with that but i couldn't find a good way to get
//// the last inserted id without using the model code of gorm and we specifically
//// don't want to use the model code since is not clear whether the constraints
//// are actually enforced by the DBMS or by some gorm callbacks...
////
// func TestDBConstraints(t *testing.T) {
// // we don't want to use the model but hand written SQL commands here since we want to check
// // whether the constraints are really enforced by the DBMS and not by some magic bullshit in gorm!
// // This is even worse since gorm.AutoUpdate will not deal with foreign key constraints and we
// // need do it ourselves at the migration scripts. It would be really nice to be able to check
// // whether the migrations do the right thing!!!
// db, err := gorm.Open(testDBType, testDBConnection)
// if err != nil {
// t.Fatalf("unexpected error: %v", err)
// }
// if err = db.DB().Ping(); err != nil {
// t.Fatalf("unexpected error: %v", err)
// }
// /* should succeed */
// delete from playlists where id = 1;
// delete from files where id = 2;
// res := db.Exec("INSERT INTO groups (name) VALUES (?)", testGroup1)
// if res.Error != nil {
// t.Fatalf("adding group '%s' shouldn't fail: %v", testGroup1, res.Error)
// }
// if res = db.Exec("INSERT INTO groups (name) VALUES (?)", testGroup1); res.Error == nil {
// t.Fatalf("re-adding the same group should fail")
// }
}
// if res = db.Exec("INSERT INTO files (group_name, size) VALUES (?, ?)", testGroup1, 500); res.Error != nil {
// t.Fatalf("re-adding the same group should fail")
// }
// }
// // Groups
// //
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment