-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recovered removed helper files and removed unnecessary endpoints
- Loading branch information
1 parent
364c20b
commit a725a85
Showing
4 changed files
with
129 additions
and
63 deletions.
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,94 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
_ "database/sql" | ||
"embed" | ||
"fmt" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
|
||
sqle "github.com/dolthub/go-mysql-server" | ||
"github.com/dolthub/go-mysql-server/memory" | ||
"github.com/dolthub/go-mysql-server/server" | ||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/matheusgomes28/urchin/common" | ||
"github.com/matheusgomes28/urchin/database" | ||
) | ||
|
||
//go:generate ../../../migrations ./migrations | ||
|
||
//go:embed migrations/*.sql | ||
var EmbedMigrations embed.FS | ||
|
||
func RunDatabaseServer(app_settings common.AppSettings) { | ||
pro := CreateTestDatabase(app_settings.DatabaseName) | ||
engine := sqle.NewDefault(pro) | ||
engine.Analyzer.Catalog.MySQLDb.AddRootAccount() | ||
|
||
session := memory.NewSession(sql.NewBaseSession(), pro) | ||
ctx := sql.NewContext(context.Background(), sql.WithSession(session)) | ||
ctx.SetCurrentDatabase(app_settings.DatabaseName) | ||
|
||
config := server.Config{ | ||
Protocol: "tcp", | ||
Address: fmt.Sprintf("%s:%d", app_settings.DatabaseAddress, app_settings.DatabasePort), | ||
} | ||
s, err := server.NewServer(config, engine, memory.NewSessionBuilder(pro), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err = s.Start(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func CreateTestDatabase(name string) *memory.DbProvider { | ||
db := memory.NewDatabase(name) | ||
db.BaseDatabase.EnablePrimaryKeyIndexes() | ||
|
||
pro := memory.NewDBProvider(db) | ||
return pro | ||
} | ||
|
||
func WaitForDb(app_settings common.AppSettings) (database.SqlDatabase, error) { | ||
|
||
for range 400 { | ||
database, err := database.MakeSqlConnection( | ||
app_settings.DatabaseUser, | ||
app_settings.DatabasePassword, | ||
app_settings.DatabaseAddress, | ||
app_settings.DatabasePort, | ||
app_settings.DatabaseName, | ||
) | ||
|
||
if err == nil { | ||
return database, nil | ||
} | ||
|
||
time.Sleep(25 * time.Millisecond) | ||
} | ||
|
||
return database.SqlDatabase{}, fmt.Errorf("database did not start") | ||
} | ||
|
||
// GetAppSettings gets the settings for the http servers | ||
// taking into account a unique port. Very hacky way to | ||
// get a unique port: manually have to pass a new number | ||
// for every test... | ||
// TODO : Find a way to assign a unique port at compile | ||
// | ||
// time | ||
func GetAppSettings(app_num int) common.AppSettings { | ||
app_settings := common.AppSettings{ | ||
DatabaseAddress: "localhost", | ||
DatabasePort: 3336 + app_num, // Initial port | ||
DatabaseUser: "root", | ||
DatabasePassword: "", | ||
DatabaseName: "urchin", | ||
WebserverPort: 8080, | ||
} | ||
|
||
return app_settings | ||
} |
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,35 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
"io" | ||
"mime/multipart" | ||
"net/textproto" | ||
) | ||
|
||
func CreateFormImagePart(writer *multipart.Writer, fieldname string, filename string, contentType string) (io.Writer, error) { | ||
h := make(textproto.MIMEHeader) | ||
h.Set("Content-Disposition", | ||
fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldname, filename)) | ||
h.Set("Content-Type", contentType) | ||
return writer.CreatePart(h) | ||
} | ||
|
||
func CreateTextFormHeader(writer *multipart.Writer, fieldname string) (io.Writer, error) { | ||
h := make(textproto.MIMEHeader) | ||
h.Set("Content-Disposition", | ||
fmt.Sprintf(`form-data; name="%s"`, fieldname)) | ||
return writer.CreatePart(h) | ||
} | ||
|
||
// Creating an image in memory for testing: https://yourbasic.org/golang/create-image/ | ||
func CreateImage() image.Image { | ||
width := 1 | ||
height := 1 | ||
|
||
upLeft := image.Point{0, 0} | ||
lowRight := image.Point{width, height} | ||
|
||
return image.NewRGBA(image.Rectangle{upLeft, lowRight}) | ||
} |