mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
(Feature) OHLCV data storage and retrieval (#522)
* WIP * end of day WIP started migration of trade history * added kline support to hitbtc huobi lbank * added exchangehistory to all supported exchanges started work on coinbase 300 candles/request method * end of day WIP * removed unused ta and misc changes to flag ready for review * yobit cleanup * revert coinbase changES * general code clean up and added zb support * poloniex support added * renamed method to FormatExchangeKlineInterval other misc fixes * linter fixes * linter fixes * removed verbose * fixed poloniex test coverage * revert poloniex mock data * regenerated poloniex mock data * a very verbose clean up * binance mock clean up * removed unneeded t.Log() * setting verbose to true to debug CI issue * first pass changes addressed * common.ErrNotYetImplemented implemented :D * comments added * WIP-addressed exchange requests and reverted previous GetExchangeHistory changes * WIP-addressed exchange requests and reverted previous GetExchangeHistory changes * increased test coverage added kraken support * OKGroup support completed started work on address GetExchangeHistory feedback and migrating to own PR under https://github.com/xtda/gocryptotrader/tree/exchange_history * convert zb ratelimits * gofmt run on okcoin * increased delay on rate limit * gofmt package * fixed panic with coinbene and bithumb if conversion fails * very broken end of day WIP * added support for GetHistoricCandlesEx to coinbase and binance * gofmt package * coinbase, btcmarkets, zb ex wrapper function added * added all exchange support for ex regenerated mock data * update bithumb to return wrapper method * gofmt package * end of day started work on changes * models created for exchange/asset/currency/currencypair, new seed system created * reworked test coverage added okgroup support general fixes/change requests addressed * Added OneMonth * limit checks on supportedexchanges * reverted getexchangehistory * started work on currency seeding and insertion * reworked binance tesT * added workaround for kraken panic * :D extremely broken WIP * renamed command to extended removed interval check on non-implemented commands * added wrapperconfig back * increased test coverage for FormatExchangeKlineInterval * WIP * increased test coverage for FormatExchangeKlineInterval bitfinex/gateio/huobi * linter fixes * zb kraken lbank coinbene btcmarkets support added * removed verbose * OK group support for other asset types added * swapped margin to use spot endpoint * index support added test coverage added for asset types * added asset type to okcoin test * gofmt * add asset to extended method * removed verbose * Very broken WIP models need to be regenerated * add support for coinbene swap increase test coverage * removed verbose * small clean up of okgroup wrapper functions * verbose to troubleshoot CI issues * removed verbose * added error check reverted coinbasechanges * attempting to fix broken model generation * readme updated * :D i broke so much * model regeneration fixed & complete * candle model filled out * removed unused start/finish started work on decoupling api requests from kline package * restructured coinbene, bithumb methods, added bitstamp support * kraken time fix * BTCMarkets restructure * typo fix * removed test for futures due to contact changing * removed test for futures due to contact changing * added start/end date to extended method over range * WIP * added sync option to candles * converted to assettranslator * removed verbose * removed verbose * removed invalid char * reverted incorrectly removed return * added import * further template updates * macos hates my keyboard :D * misc canges * started work on creating kline from databases eed * x -> i * removed verbose * updated fixCasing to allocate var before checks * sqlite3 supported work started * removed time conversion * further work on tets * sort all outgoing kline candles * fixCasing fix * after/before checks added * added parallel to test * logic check on BTCmarkets * removed unused param, used correct iterator * converted HitBTC to use time.Time * test update * add iszero false check to candle times * Seed exchange & OHCLV data for test usage * updated resultlimit to 5000 * new line added * added comment to exported const * move date forward * use configured ratelimit * fixed pair for test * panic fixed WIP on fixCasing * fixCasing rework, started work on readme docs * enable rate limiter for wrapper issues tool * docs updated * removed unused vars from tests * removed err from return and formatted currency * updated Yobit supported status * Updated HitBTC to use onehour candles due to test exeuction times * added further details to gctcli output * added link to docs * added link to tempalte * disable FTX websocket in config_example * fix poloneix * regenerated poloniex mock data * removed recording flag * format on package * moved exchange var outside of method scope * reworked seeding into package * verbose output improved * removed verbose from candles * Added comments to exported functions * removed verbose output * Reworking of tests * end of day commit * added SQLite migration for asset, test updates for exchange, added support for withdrawal for new exchange_name_id relation * regenerated database models * WIP * test rework, sqlite migration updates for withdrawal * Reworked error returns to stop duplications, format all output to UTC, changed gctscript OHLCV output to be unix timestmap, started work on seeding tool * gofmt * dbseed command for seeding exchanges added, removed seed from dbmigrate, LoadCSV method added for exchange * go mod tidy * added import candle from csv command to dbseed * Removed reset & duplicate migrate command from helper, renamed struct to Item/Candle over Candle/Tick, added test coverage to dbseed, improved withdraw tests * remove broken tests due to ORM generation of Fk, removed go unneeded goroutine for inserting records * removed t.Cleanup usage because appveyor * added test coverage to StoreInDatabase() * removed unneeded data from config for test * added new line * Added down migration support to candle/asset removal, return original error and display rollback error * removed unneeded err assignment, break out of loop on error * add err check to method for test * first pass changes * WIP * Updated migrations for both sqlite3 & postgres to create exchanges if any withdrawal_history records are found, removed migrate command * removed argusage as usage information is provided by flags * added inserted records return count and test coverage * new line added * Database: comment config details out to disable local postgresl testing by default * added asset support * Database: added error return when no exchanges are found, title exchange name across queries * Fixed test data * Database: removed migration bool from ConnectToDatabase(), removed empty line, insert asset on test * Database: verbose linter :D * Database/OHLCV: removed go module from dbseed command and reverted back to main module, converted interval to int64 to match other parts of code base, provided migrations to update database, poloniex fix to skip first candle * dbseed: add completed message to output * Database: added migration to add asset to uniq index for candle table * Database: database -> exchange * Database: add asset to upsert conflict * Poloneix: fix for invalid interval * regenerated poloniex mock data * Database: added down migration for candle interval update * OHCLV/Database: WIP * OHLCV/Datastore: added new ValidateKline() method to check that asset, pair, interval are enabled/supported by exchange and updated tests * revert configtest changes * OHCLV/Kline: pointer assignment to ErrorKline & format pairs on check * goimports * migration updates * Database/Candle: updated tests * revert configtest changes * ZB: updaed defaults to use uppercase pairs * ZB: updaed defaults to use uppercase pairs * revert pair formatting * Switch over to .Cotains() method from pairManagement * Added comment & ftx back 😆 * OHLCV/Datastore: (Candles): added not null to asset, (WithdrawalHistory): added not null to exchange_name_id), reworked KlineError return * set verbose to false * updated btc markets test * Updated readme * removed ability to have blank exchange name as indexing requires it * remove old usedb and empty exchange check, return error on no data instead * Updated kline test to match date range * Renamed candles.exchange_id column to candles.xchange_name_id to match withdrawal table, return err on unwrap, zb fix revert * regenerated sqlite3 models * force UTC for sqlite.... because sqlite * OHLCV/Datastore: upper case pairs on insert for consistency, remove unneeded rollback call on commit failure as it has no effect, move rollback outside of insertsqlite/insertpsql methods * add error check for no candles * nil length/cap is 0 in golang :D * OHLCV/Datastore: updated wording on examples, removed duplicate testfile * OHLCV/Datastore: updated README with links to dbseed * dbtool -> dbseed
This commit is contained in:
123
cmd/dbseed/README.md
Normal file
123
cmd/dbseed/README.md
Normal file
@@ -0,0 +1,123 @@
|
||||
# GoCryptoTrader dbseed tool
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/page-logo.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/portfolio)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This dbseed tool is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## How to use
|
||||
|
||||
#### Prerequisites
|
||||
##### Configuration
|
||||
|
||||
dbseed requires a valid database configuration in your gocryptotrader config
|
||||
|
||||
```sh
|
||||
"database": {
|
||||
"enabled": true,
|
||||
"verbose": true,
|
||||
"driver": "postgres",
|
||||
"connectionDetails": {
|
||||
"host": "localhost",
|
||||
"port": 5432,
|
||||
"username": "gct-dev",
|
||||
"password": "gct-dev",
|
||||
"database": "gct-dev",
|
||||
"sslmode": "disable"
|
||||
}
|
||||
},
|
||||
```
|
||||
|
||||
By default this will load from the default GoCryptoTrader path
|
||||
|
||||
For Windows users this is:
|
||||
```%APPDATA%\GoCryptoTrader```
|
||||
|
||||
For Linux/macOS users this is:
|
||||
```$HOME\.gocryptotrader```
|
||||
|
||||
and can be overridden with the ```-config``` flag
|
||||
|
||||
``` --config value config file to load (default: "~/.gocryptotrader/config.json")```
|
||||
|
||||
#### Usage
|
||||
|
||||
#### Sub Commands
|
||||
##### candle
|
||||
```
|
||||
file seed candle data from a file
|
||||
help, h Shows a list of commands or help for one command
|
||||
```
|
||||
##### command examples
|
||||
```
|
||||
dbseed candle file --exchange=binance --base=BTC --quote=USDT --interval=86400 --asset=spot --filename=../../testdata/binance_BTCUSDT_24h_2019_01_01_2020_01_01.csv
|
||||
```
|
||||
File structure for import contains the following rows with no headers:
|
||||
|
||||
```
|
||||
timestamp, volume, open, high, low, close
|
||||
```
|
||||
An example of this is:
|
||||
```
|
||||
1546300800,23741.687033,3701.23,3797.14,3642,3797.14
|
||||
1546387200,35156.463369,3796.45,3858.56,3750.45,3858.56
|
||||
1546473600,29406.948359,3857.57,3766.78,3730,3766.78
|
||||
1546560000,29519.554671,3767.2,3792.01,3703.57,3792.01
|
||||
1546646400,30490.667751,3790.09,3770.96,3751,3770.96
|
||||
```
|
||||
##### exchange
|
||||
```
|
||||
file seed exchange data from a file
|
||||
add add a single exchange
|
||||
default seed exchange from default list
|
||||
```
|
||||
##### command examples
|
||||
```
|
||||
dbseed exchange add --name=newexchange
|
||||
dbseed exchange file --filename=../../testdata/exchangelist.csv
|
||||
dbseed exchange default
|
||||
```
|
||||
|
||||
File structure for importing contains the following rows with no headers:
|
||||
```
|
||||
exchange
|
||||
```
|
||||
An example of this is:
|
||||
```
|
||||
binance,
|
||||
ftx,
|
||||
btc markets,
|
||||
```
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
|
||||
123
cmd/dbseed/candle.go
Normal file
123
cmd/dbseed/candle.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/candle"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var seedCandleCommand = &cli.Command{
|
||||
Name: "candle",
|
||||
Usage: "seed candle data",
|
||||
Subcommands: []*cli.Command{
|
||||
{
|
||||
Name: "file",
|
||||
Usage: "seed candle data from a file",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "exchange",
|
||||
Usage: "exchange name of supplied candle data",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "base",
|
||||
Usage: "base currency of supplied candle data",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "quote",
|
||||
Usage: "quote currency of supplied candle data",
|
||||
},
|
||||
&cli.Int64Flag{
|
||||
Name: "interval",
|
||||
Usage: "interval of supplied candle data",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "asset",
|
||||
Usage: "asset type of supplied data (spot/margin/futures for example)",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "filename",
|
||||
Usage: "csv file to load candle data from (see readme for formatting details)",
|
||||
TakesFile: true,
|
||||
FilePath: workingDir,
|
||||
},
|
||||
},
|
||||
Action: seedCandleFromFile,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func seedCandleFromFile(c *cli.Context) error {
|
||||
if c.NumFlags() == 0 && c.NArg() == 0 {
|
||||
return cli.ShowSubcommandHelp(c)
|
||||
}
|
||||
|
||||
var exchangeName string
|
||||
if c.IsSet("exchange") {
|
||||
exchangeName = c.String("exchange")
|
||||
} else if c.Args().Get(0) != "" {
|
||||
exchangeName = c.Args().Get(0)
|
||||
}
|
||||
|
||||
var base string
|
||||
if c.IsSet("base") {
|
||||
base = c.String("base")
|
||||
} else if c.Args().Get(1) != "" {
|
||||
base = c.Args().Get(1)
|
||||
}
|
||||
|
||||
var quote string
|
||||
if c.IsSet("quote") {
|
||||
quote = c.String("quote")
|
||||
} else if c.Args().Get(2) != "" {
|
||||
quote = c.Args().Get(2)
|
||||
}
|
||||
|
||||
var interval int64
|
||||
if c.IsSet("interval") {
|
||||
interval = c.Int64("interval")
|
||||
} else if c.Args().Get(3) != "" {
|
||||
var err error
|
||||
interval, err = strconv.ParseInt(c.Args().Get(3), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New("failed to convert interval")
|
||||
}
|
||||
}
|
||||
|
||||
var asset string
|
||||
if c.IsSet("asset") {
|
||||
asset = c.String("asset")
|
||||
} else if c.Args().Get(4) != "" {
|
||||
asset = c.Args().Get(4)
|
||||
}
|
||||
|
||||
var fileName string
|
||||
if c.IsSet("filename") {
|
||||
fileName = c.String("filename")
|
||||
} else if c.Args().Get(5) != "" {
|
||||
fileName = c.Args().Get(5)
|
||||
}
|
||||
|
||||
_, err := os.Stat(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = load(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
totalInserted, err := candle.InsertFromCSV(exchangeName,
|
||||
base, quote, interval, asset,
|
||||
fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Inserted: %v records", totalInserted)
|
||||
return nil
|
||||
}
|
||||
64
cmd/dbseed/database.go
Normal file
64
cmd/dbseed/database.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
dbPSQL "github.com/thrasher-corp/gocryptotrader/database/drivers/postgres"
|
||||
dbsqlite3 "github.com/thrasher-corp/gocryptotrader/database/drivers/sqlite3"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
dbConn *database.Instance
|
||||
)
|
||||
|
||||
func load(c *cli.Context) error {
|
||||
var conf config.Config
|
||||
err := conf.LoadConfig(c.String("config"), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !conf.Database.Enabled {
|
||||
return database.ErrDatabaseSupportDisabled
|
||||
}
|
||||
|
||||
err = openDBConnection(c, conf.Database.Driver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
drv := repository.GetSQLDialect()
|
||||
if drv == database.DBSQLite || drv == database.DBSQLite3 {
|
||||
fmt.Printf("Database file: %s\n", conf.Database.Database)
|
||||
} else {
|
||||
fmt.Printf("Connected to: %s\n", conf.Database.Host)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func openDBConnection(c *cli.Context, driver string) (err error) {
|
||||
if c.IsSet("verbose") {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
if driver == database.DBPostgreSQL {
|
||||
dbConn, err = dbPSQL.Connect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("database failed to connect: %v, some features that utilise a database will be unavailable", err)
|
||||
}
|
||||
return nil
|
||||
} else if driver == database.DBSQLite || driver == database.DBSQLite3 {
|
||||
dbConn, err = dbsqlite3.Connect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("database failed to connect: %v, some features that utilise a database will be unavailable", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("no connection established")
|
||||
}
|
||||
47
cmd/dbseed/dbseed_test.go
Normal file
47
cmd/dbseed/dbseed_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/core"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
testConfig = filepath.Join("..", "..", "testdata", "configtest.json")
|
||||
testApp = &cli.App{
|
||||
Name: "dbseed",
|
||||
Version: core.Version(false),
|
||||
EnableBashCompletion: true,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: testConfig,
|
||||
Usage: "config file to load",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "verbose",
|
||||
Usage: "toggle verbose output",
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
seedExchangeCommand,
|
||||
seedCandleCommand,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
config.TestBypass = true
|
||||
fs := &flag.FlagSet{}
|
||||
fs.String("config", testConfig, "")
|
||||
newCtx := cli.NewContext(testApp, fs, &cli.Context{})
|
||||
err := load(newCtx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
132
cmd/dbseed/exchange.go
Normal file
132
cmd/dbseed/exchange.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
exchangeDB "github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var seedExchangeCommand = &cli.Command{
|
||||
Name: "exchange",
|
||||
Usage: "seed exchange data",
|
||||
Subcommands: []*cli.Command{
|
||||
{
|
||||
Name: "file",
|
||||
Usage: "seed exchange data from a file",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "filename",
|
||||
Usage: "CSV file to load exchanges from",
|
||||
TakesFile: true,
|
||||
FilePath: workingDir,
|
||||
},
|
||||
},
|
||||
Action: seedExchangeFromFile,
|
||||
},
|
||||
{
|
||||
Name: "add",
|
||||
Usage: "add a single exchange",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "name",
|
||||
Usage: "name",
|
||||
},
|
||||
},
|
||||
Action: addSingleExchange,
|
||||
},
|
||||
{
|
||||
Name: "default",
|
||||
Usage: "seed exchange from default list",
|
||||
Action: seedExchangeFromDefaultList,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func seedExchangeFromDefaultList(c *cli.Context) error {
|
||||
err := load(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var allExchanges []exchangeDB.Details
|
||||
for x := range exchange.Exchanges {
|
||||
allExchanges = append(allExchanges, exchangeDB.Details{
|
||||
Name: exchange.Exchanges[x],
|
||||
})
|
||||
}
|
||||
err = exchangeDB.InsertMany(allExchanges)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("command completed successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedExchangeFromFile(c *cli.Context) error {
|
||||
if c.NumFlags() == 0 && c.NArg() == 0 {
|
||||
return cli.ShowSubcommandHelp(c)
|
||||
}
|
||||
|
||||
var fileName string
|
||||
if c.IsSet("filename") {
|
||||
fileName = c.String("filename")
|
||||
} else if c.Args().Get(0) != "" {
|
||||
fileName = c.Args().Get(0)
|
||||
}
|
||||
|
||||
_, err := os.Stat(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = load(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
exchangeList, err := exchangeDB.LoadCSV(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = exchangeDB.InsertMany(exchangeList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for x := range exchangeList {
|
||||
fmt.Printf("Added exchange: %v\n", exchangeList[x].Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func addSingleExchange(c *cli.Context) error {
|
||||
if c.NumFlags() == 0 && c.NArg() == 0 {
|
||||
return cli.ShowSubcommandHelp(c)
|
||||
}
|
||||
|
||||
var exchangeName string
|
||||
if c.IsSet("name") {
|
||||
exchangeName = c.String("name")
|
||||
} else if c.Args().Get(0) != "" {
|
||||
exchangeName = c.Args().Get(0)
|
||||
}
|
||||
|
||||
err := load(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = exchangeDB.Insert(exchangeDB.Details{
|
||||
Name: exchangeName,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Added new exchange: %v", exchangeName)
|
||||
return nil
|
||||
}
|
||||
67
cmd/dbseed/main.go
Normal file
67
cmd/dbseed/main.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/core"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
app = &cli.App{
|
||||
Name: "dbseed",
|
||||
Version: core.Version(false),
|
||||
EnableBashCompletion: true,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: config.DefaultFilePath(),
|
||||
Usage: "config file to load",
|
||||
Destination: &configFile,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "verbose",
|
||||
Usage: "toggle verbose output",
|
||||
Destination: &verbose,
|
||||
},
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
seedExchangeCommand,
|
||||
seedCandleCommand,
|
||||
},
|
||||
}
|
||||
workingDir string
|
||||
configFile string
|
||||
verbose bool
|
||||
)
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
workingDir, err = os.Getwd()
|
||||
if err != nil {
|
||||
log.Println("error getting current working path")
|
||||
workingDir = filepath.Join(".")
|
||||
}
|
||||
|
||||
fmt.Println("GoCryptoTrader database seeding tool")
|
||||
fmt.Println(core.Copyright)
|
||||
fmt.Println()
|
||||
|
||||
err = app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if dbConn != nil {
|
||||
if dbConn.SQL != nil {
|
||||
err = dbConn.SQL.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3905,6 +3905,14 @@ var getHistoricCandlesExtendedCommand = cli.Command{
|
||||
Value: time.Now().Format(common.SimpleTimeFormat),
|
||||
Destination: &endTime,
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "sync",
|
||||
Usage: "<true/false>",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "db",
|
||||
Usage: "source data from database <true/false>",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -3970,6 +3978,16 @@ func getHistoricCandlesExtended(c *cli.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
var sync bool
|
||||
if c.IsSet("sync") {
|
||||
sync = c.Bool("sync")
|
||||
}
|
||||
|
||||
var db bool
|
||||
if c.IsSet("db") {
|
||||
db = c.Bool("db")
|
||||
}
|
||||
|
||||
conn, err := setupClient()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -4006,6 +4024,8 @@ func getHistoricCandlesExtended(c *cli.Context) error {
|
||||
End: e.Unix(),
|
||||
TimeInterval: int64(candleInterval),
|
||||
ExRequest: true,
|
||||
Sync: sync,
|
||||
UseDb: db,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -96,5 +96,5 @@ func convertGCTtoSQLBoilerConfig(c *database.Config) {
|
||||
}
|
||||
|
||||
func convertDBName(in string) string {
|
||||
return filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "/database", in)
|
||||
return filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "database", in)
|
||||
}
|
||||
|
||||
@@ -1362,7 +1362,7 @@ func (c *Config) checkDatabaseConfig() error {
|
||||
}
|
||||
|
||||
if c.Database.Driver == database.DBSQLite || c.Database.Driver == database.DBSQLite3 {
|
||||
databaseDir := filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "/database")
|
||||
databaseDir := filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "database")
|
||||
err := common.CreateDir(databaseDir)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1484,7 +1484,7 @@ func GetFilePath(configfile string) (string, error) {
|
||||
return configfile, nil
|
||||
}
|
||||
|
||||
if flag.Lookup("test.v") != nil && !testBypass {
|
||||
if flag.Lookup("test.v") != nil && !TestBypass {
|
||||
return TestFile, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1717,7 +1717,7 @@ func TestGetFilePath(t *testing.T) {
|
||||
if result != expected {
|
||||
t.Errorf("TestGetFilePath: expected %s got %s", expected, result)
|
||||
}
|
||||
testBypass = true
|
||||
TestBypass = true
|
||||
}
|
||||
|
||||
func TestCheckRemoteControlConfig(t *testing.T) {
|
||||
|
||||
@@ -69,7 +69,7 @@ const (
|
||||
var (
|
||||
Cfg Config
|
||||
IsInitialSetup bool
|
||||
testBypass bool
|
||||
TestBypass bool
|
||||
m sync.Mutex
|
||||
)
|
||||
|
||||
|
||||
@@ -106,6 +106,9 @@ dbmigrate -command "up"
|
||||
|
||||
dbmigrate provides a -migrationdir flag override to tell it what path to look in for migrations
|
||||
|
||||
###### Note: its highly recommended to backup any data before running migrations against a production database especially if you are running SQLite due to alter table limitations
|
||||
|
||||
|
||||
##### Adding a new model
|
||||
Model's are generated using [SQLBoiler](https://github.com/thrasher-corp/sqlboiler)
|
||||
A helper tool has been made located in gen_sqlboiler_config that will parse your GoCryptoTrader config and output a SQLBoiler config
|
||||
@@ -123,7 +126,6 @@ along with the location of the sqlboiler generated config
|
||||
-outdir "~/.gocryptotrader/"
|
||||
```
|
||||
|
||||
|
||||
Generate a new model that gets placed in ./database/models/<databasetype> folder
|
||||
|
||||
Linux:
|
||||
@@ -144,6 +146,10 @@ And in the contrib/sqlboiler.cmd for windows users
|
||||
##### Adding a Repository
|
||||
+ Create Repository directory in github.com/thrasher-corp/gocryptotrader/database/repository/
|
||||
|
||||
|
||||
##### DBSeed helper
|
||||
A helper tool [cmd/dbseed](../cmd/dbseed/README.md) has been created for assisting with data migration
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
@@ -53,4 +53,6 @@ const (
|
||||
DBSQLite3 = "sqlite3"
|
||||
// DBPostgreSQL const string for PostgreSQL across code base
|
||||
DBPostgreSQL = "postgres"
|
||||
// DBInvalidDriver const string for invalid driver
|
||||
DBInvalidDriver = "invalid driver"
|
||||
)
|
||||
|
||||
12
database/migrations/20200518131133_exchange/postgres.sql
Normal file
12
database/migrations/20200518131133_exchange/postgres.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
CREATE TABLE IF NOT EXISTS exchange
|
||||
(
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name varchar(255) NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "index_name_on_exchange" ON exchange using btree(name);
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
DROP TABLE exchange;
|
||||
11
database/migrations/20200518131133_exchange/sqlite3.sql
Normal file
11
database/migrations/20200518131133_exchange/sqlite3.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
CREATE TABLE "exchange" (
|
||||
id text not null primary key,
|
||||
name text not null,
|
||||
unique(name) ON CONFLICT IGNORE
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
DROP TABLE "exchange";
|
||||
22
database/migrations/20200520103816_candle/postgres.sql
Normal file
22
database/migrations/20200520103816_candle/postgres.sql
Normal file
@@ -0,0 +1,22 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
CREATE TABLE IF NOT EXISTS candle
|
||||
(
|
||||
ID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
exchange_id uuid REFERENCES exchange(id) NOT NULL,
|
||||
Base varchar(30) NOT NULL,
|
||||
Quote varchar(30) NOT NULL,
|
||||
Interval varchar(30) NOT NULL,
|
||||
Timestamp TIMESTAMPTZ NOT NULL,
|
||||
Open DOUBLE PRECISION NOT NULL,
|
||||
High DOUBLE PRECISION NOT NULL,
|
||||
Low DOUBLE PRECISION NOT NULL,
|
||||
Close DOUBLE PRECISION NOT NULL,
|
||||
Volume DOUBLE PRECISION NOT NULL,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
DROP TABLE candle;
|
||||
|
||||
19
database/migrations/20200520103816_candle/sqlite3.sql
Normal file
19
database/migrations/20200520103816_candle/sqlite3.sql
Normal file
@@ -0,0 +1,19 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
CREATE TABLE "candle" (
|
||||
id text not null primary key,
|
||||
exchange_id uuid REFERENCES exchange(id),
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval) ON CONFLICT IGNORE
|
||||
);
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
DROP TABLE "candle";
|
||||
6
database/migrations/20200715122118_asset/postgres.sql
Normal file
6
database/migrations/20200715122118_asset/postgres.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
ALTER TABLE candle ADD COLUMN asset VARCHAR(255) NOT NULL;
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
ALTER TABLE candle DROP COLUMN asset;
|
||||
23
database/migrations/20200715122118_asset/sqlite3.sql
Normal file
23
database/migrations/20200715122118_asset/sqlite3.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
ALTER TABLE candle ADD COLUMN asset text;
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
CREATE TABLE "candle_new" (
|
||||
id text not null primary key,
|
||||
exchange_id uuid REFERENCES exchange(id),
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval) ON CONFLICT IGNORE
|
||||
);
|
||||
|
||||
INSERT INTO candle_new SELECT id, exchange_id, Base, Quote, Interval, Timestamp, Open, High, Low, Close, Volume FROM candle;
|
||||
DROP TABLE candle;
|
||||
ALTER TABLE candle_new RENAME TO candle;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
INSERT INTO exchange(name) SELECT exchange from withdrawal_history;
|
||||
ALTER TABLE withdrawal_history ADD COLUMN exchange_name_id UUID REFERENCES exchange(id);
|
||||
UPDATE withdrawal_history SET exchange_name_id = r.ID FROM (SELECT * from exchange) as r WHERE exchange = r.name;
|
||||
ALTER TABLE withdrawal_history DROP COLUMN exchange;
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
ALTER TABLE withdrawal_history ADD COLUMN exchange TEXT;
|
||||
UPDATE withdrawal_history SET exchange = r.name FROM (SELECT * from exchange) as r WHERE exchange_name_id = r.id;
|
||||
ALTER TABLE withdrawal_history ALTER COLUMN exchange SET NOT NULL;
|
||||
ALTER TABLE withdrawal_history DROP CONSTRAINT withdrawal_history_exchange_name_id_fkey;
|
||||
ALTER TABLE withdrawal_history DROP COLUMN exchange_name_id;
|
||||
@@ -0,0 +1,84 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
INSERT INTO exchange(id, name)
|
||||
SELECT
|
||||
lower(hex( randomblob(4)) || '-' || hex( randomblob(2)) || '-' || '4' || substr( hex( randomblob(2)), 2) || '-'
|
||||
|| substr('AB89', 1 + (abs(random()) % 4) , 1) ||
|
||||
substr(hex(randomblob(2)), 2) || '-' || hex(randomblob(6))), exchange
|
||||
from withdrawal_history;
|
||||
|
||||
ALTER TABLE withdrawal_history ADD COLUMN exchange_name_id;
|
||||
|
||||
UPDATE withdrawal_history
|
||||
SET
|
||||
exchange_name_id = (SELECT id FROM exchange WHERE withdrawal_history.exchange = lower(name))
|
||||
WHERE
|
||||
EXISTS(
|
||||
SELECT *
|
||||
FROM exchange
|
||||
WHERE lower("exchange".name) = withdrawal_history.exchange
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS withdrawal_history_new
|
||||
(
|
||||
id text PRIMARY KEY NOT NULL,
|
||||
exchange_name_id text,
|
||||
exchange_id text NOT NULL,
|
||||
status text NOT NULL,
|
||||
currency text NOT NULL,
|
||||
amount real NOT NULL,
|
||||
description text,
|
||||
withdraw_type integer NOT NULL,
|
||||
created_at timestamp NOT NULL default CURRENT_TIMESTAMP,
|
||||
updated_at timestamp NOT NULL default CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(exchange_name_id) REFERENCES exchange(id) ON DELETE RESTRICT
|
||||
);
|
||||
INSERT INTO
|
||||
withdrawal_history_new (id, exchange_name_id, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at)
|
||||
SELECT
|
||||
id, exchange_name_id, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at
|
||||
FROM
|
||||
withdrawal_history;
|
||||
|
||||
DROP TABLE withdrawal_history;
|
||||
|
||||
ALTER TABLE withdrawal_history_new RENAME TO withdrawal_history;
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
ALTER TABLE withdrawal_history ADD COLUMN exchange;
|
||||
|
||||
UPDATE withdrawal_history
|
||||
SET
|
||||
exchange = (SELECT name FROM exchange WHERE withdrawal_history.exchange_name_id = id)
|
||||
WHERE
|
||||
EXISTS(
|
||||
SELECT *
|
||||
FROM exchange
|
||||
WHERE exchange.id = withdrawal_history.exchange_name_id
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS withdrawal_history_new
|
||||
(
|
||||
id text PRIMARY KEY NOT NULL,
|
||||
exchange text NOT NULL,
|
||||
exchange_id text NOT NULL,
|
||||
status text NOT NULL,
|
||||
currency text NOT NULL,
|
||||
amount real NOT NULL,
|
||||
description text,
|
||||
withdraw_type integer NOT NULL,
|
||||
created_at timestamp NOT NULL default CURRENT_TIMESTAMP,
|
||||
updated_at timestamp NOT NULL default CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
withdrawal_history_new (id, exchange, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at)
|
||||
SELECT
|
||||
id, exchange, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at
|
||||
FROM
|
||||
withdrawal_history;
|
||||
|
||||
DROP TABLE withdrawal_history;
|
||||
|
||||
ALTER TABLE withdrawal_history_new RENAME TO withdrawal_history;
|
||||
@@ -0,0 +1,35 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
UPDATE candle SET interval = replace (candle.interval, '15s', '15');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1m', '60');
|
||||
UPDATE candle SET interval = replace (candle.interval, '3m', '180');
|
||||
UPDATE candle SET interval = replace (candle.interval, '5m', '300');
|
||||
UPDATE candle SET interval = replace (candle.interval, '10m', '600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '15m', '900');
|
||||
UPDATE candle SET interval = replace (candle.interval, '30m', '1800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1h', '3600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '6h', '21600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '8h', '28800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '12h', '43200');
|
||||
UPDATE candle SET interval = replace (candle.interval, '24h', '86400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '3d', '259200');
|
||||
UPDATE candle SET interval = replace (candle.interval, '7w', '604800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '15d', '1296000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1w', '604800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '2W', '1209600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1M', '2678400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1Y', '31536000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1d', '86400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '72h', '259200');
|
||||
UPDATE candle SET interval = replace (candle.interval, '168h', '604800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '360h', '1296000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '336h', '1209600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '744h', '2678400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '8760h', '31536000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '4h', '14400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '2h', '7200');
|
||||
alter table candle alter column interval type bigint using interval::bigint;
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
alter table candle alter column interval type varchar(30) using interval::varchar(30);
|
||||
@@ -0,0 +1,34 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
UPDATE candle SET interval = replace (candle.interval, '15s', '15');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1m', '60');
|
||||
UPDATE candle SET interval = replace (candle.interval, '3m', '180');
|
||||
UPDATE candle SET interval = replace (candle.interval, '5m', '300');
|
||||
UPDATE candle SET interval = replace (candle.interval, '10m', '600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '15m', '900');
|
||||
UPDATE candle SET interval = replace (candle.interval, '30m', '1800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1h', '3600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '6h', '21600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '8h', '28800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '12h', '43200');
|
||||
UPDATE candle SET interval = replace (candle.interval, '24h', '86400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '3d', '259200');
|
||||
UPDATE candle SET interval = replace (candle.interval, '7w', '604800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '15d', '1296000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1w', '604800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '2W', '1209600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1M', '2678400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1Y', '31536000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '1d', '86400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '72h', '259200');
|
||||
UPDATE candle SET interval = replace (candle.interval, '168h', '604800');
|
||||
UPDATE candle SET interval = replace (candle.interval, '360h', '1296000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '336h', '1209600');
|
||||
UPDATE candle SET interval = replace (candle.interval, '744h', '2678400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '8760h', '31536000');
|
||||
UPDATE candle SET interval = replace (candle.interval, '4h', '14400');
|
||||
UPDATE candle SET interval = replace (candle.interval, '2h', '7200');
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
-- Nothing to run for sqlite as we leave the type as text
|
||||
@@ -0,0 +1,6 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE candle DROP CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_key;
|
||||
ALTER TABLE candle ADD CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_asset_key UNIQUE(Timestamp, exchange_id, Base, Quote, Interval, asset);
|
||||
-- +goose Down
|
||||
ALTER TABLE candle DROP CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_asset_key;
|
||||
ALTER TABLE candle ADD CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_key UNIQUE(Timestamp, exchange_id, Base, Quote, Interval);
|
||||
@@ -0,0 +1,39 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE "candle_new" (
|
||||
id text not null primary key,
|
||||
exchange_id uuid REFERENCES exchange(id),
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
Asset text,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval, asset) ON CONFLICT IGNORE
|
||||
);
|
||||
INSERT INTO candle_new SELECT id, exchange_id, Base, Quote, Interval, Timestamp, Open, High, Low, Close, Volume, Asset FROM candle;
|
||||
DROP TABLE candle;
|
||||
ALTER TABLE candle_new RENAME TO candle;
|
||||
-- +goose Down
|
||||
CREATE TABLE "candle_new" (
|
||||
id text not null primary key,
|
||||
exchange_id uuid REFERENCES exchange(id),
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
Asset TEXT,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval) ON CONFLICT IGNORE
|
||||
);
|
||||
|
||||
INSERT INTO candle_new SELECT id, exchange_id, Base, Quote, Interval, Timestamp, Open, High, Low, Close, Volume, Asset FROM candle;
|
||||
DROP TABLE candle;
|
||||
ALTER TABLE candle_new RENAME TO candle;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
ALTER TABLE withdrawal_history ALTER COLUMN exchange_name_id SET NOT NULL;
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
ALTER TABLE withdrawal_history ALTER COLUMN exchange_name_id SET NULL;
|
||||
@@ -0,0 +1,51 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
CREATE TABLE IF NOT EXISTS withdrawal_history_new
|
||||
(
|
||||
id text PRIMARY KEY NOT NULL,
|
||||
exchange_name_id text NOT NULL,
|
||||
exchange_id text NOT NULL,
|
||||
status text NOT NULL,
|
||||
currency text NOT NULL,
|
||||
amount real NOT NULL,
|
||||
description text,
|
||||
withdraw_type integer NOT NULL,
|
||||
created_at timestamp NOT NULL default CURRENT_TIMESTAMP,
|
||||
updated_at timestamp NOT NULL default CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(exchange_name_id) REFERENCES exchange(id) ON DELETE RESTRICT
|
||||
);
|
||||
INSERT INTO
|
||||
withdrawal_history_new (id, exchange_name_id, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at)
|
||||
SELECT
|
||||
id, exchange_name_id, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at
|
||||
FROM
|
||||
withdrawal_history;
|
||||
|
||||
DROP TABLE withdrawal_history;
|
||||
ALTER TABLE withdrawal_history_new RENAME TO withdrawal_history;
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
CREATE TABLE IF NOT EXISTS withdrawal_history_new
|
||||
(
|
||||
id text PRIMARY KEY NOT NULL,
|
||||
exchange_name_id text,
|
||||
exchange_id text NOT NULL,
|
||||
status text NOT NULL,
|
||||
currency text NOT NULL,
|
||||
amount real NOT NULL,
|
||||
description text,
|
||||
withdraw_type integer NOT NULL,
|
||||
created_at timestamp NOT NULL default CURRENT_TIMESTAMP,
|
||||
updated_at timestamp NOT NULL default CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
withdrawal_history_new (id, exchange_name_id, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at)
|
||||
SELECT
|
||||
id, exchange_name_id, exchange_id, status, currency, amount, description, withdraw_type, created_at, updated_at
|
||||
FROM
|
||||
withdrawal_history;
|
||||
|
||||
DROP TABLE withdrawal_history;
|
||||
ALTER TABLE withdrawal_history_new RENAME TO withdrawal_history;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- +goose Up
|
||||
-- SQL in this section is executed when the migration is applied.
|
||||
ALTER TABLE candle ALTER COLUMN asset SET NOT NULL;
|
||||
|
||||
-- +goose Down
|
||||
-- SQL in this section is executed when the migration is rolled back.
|
||||
ALTER TABLE candle ALTER COLUMN aseet SET NULL;
|
||||
@@ -0,0 +1,39 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE "candle_new" (
|
||||
id text not null primary key,
|
||||
exchange_id uuid REFERENCES exchange(id),
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
Asset text NOT NULL,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval, Asset) ON CONFLICT IGNORE
|
||||
);
|
||||
INSERT INTO candle_new SELECT id, exchange_id, Base, Quote, Interval, Timestamp, Open, High, Low, Close, Volume, Asset FROM candle;
|
||||
DROP TABLE candle;
|
||||
ALTER TABLE candle_new RENAME TO candle;
|
||||
-- +goose Down
|
||||
CREATE TABLE "candle_new" (
|
||||
id text not null primary key,
|
||||
exchange_id uuid REFERENCES exchange(id),
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
Asset TEXT,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval, Asset) ON CONFLICT IGNORE
|
||||
);
|
||||
|
||||
INSERT INTO candle_new SELECT id, exchange_id, Base, Quote, Interval, Timestamp, Open, High, Low, Close, Volume, Asset FROM candle;
|
||||
DROP TABLE candle;
|
||||
ALTER TABLE candle_new RENAME TO candle;
|
||||
@@ -0,0 +1,10 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE candle DROP CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_asset_key;
|
||||
ALTER TABLE candle RENAME COLUMN exchange_id TO exchange_name_id;
|
||||
ALTER TABLE candle ALTER COLUMN exchange_name_id SET NOT NULL;
|
||||
ALTER TABLE candle ADD CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_asset_key UNIQUE(Timestamp, exchange_name_id, Base, Quote, Interval, asset);
|
||||
-- +goose Down
|
||||
ALTER TABLE candle DROP CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_asset_key;
|
||||
ALTER TABLE candle RENAME COLUMN exchange_name_id TO exchange_id;
|
||||
ALTER TABLE candle ALTER COLUMN exchange_id SET NOT NULL;
|
||||
ALTER TABLE candle ADD CONSTRAINT candle_timestamp_exchange_id_base_quote_interval_asset_key UNIQUE(Timestamp, exchange_id, Base, Quote, Interval);
|
||||
@@ -0,0 +1,39 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE "candle_new" (
|
||||
id text not null primary key,
|
||||
exchange_name_id uuid REFERENCES exchange(id) NOT NULL,
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
Asset text NOT NULL,
|
||||
unique(Timestamp, exchange_name_id, Base, Quote, Interval, Asset) ON CONFLICT IGNORE
|
||||
);
|
||||
INSERT INTO candle_new SELECT id, exchange_id, Base, Quote, Interval, Timestamp, Open, High, Low, Close, Volume, Asset FROM candle;
|
||||
DROP TABLE candle;
|
||||
ALTER TABLE candle_new RENAME TO candle;
|
||||
-- +goose Down
|
||||
CREATE TABLE "candle_new" (
|
||||
id text not null primary key,
|
||||
exchange_id uuid REFERENCES exchange(id),
|
||||
Base text NOT NULL,
|
||||
Quote text NOT NULL,
|
||||
Interval text NOT NULL,
|
||||
Timestamp TIMESTAMP NOT NULL,
|
||||
Open REAL NOT NULL,
|
||||
High REAL NOT NULL,
|
||||
Low REAL NOT NULL,
|
||||
Close REAL NOT NULL,
|
||||
Volume REAL NOT NULL,
|
||||
Asset TEXT,
|
||||
unique(Timestamp, exchange_id, Base, Quote, Interval, Asset) ON CONFLICT IGNORE
|
||||
);
|
||||
|
||||
INSERT INTO candle_new SELECT id, exchange_name_id, Base, Quote, Interval, Timestamp, Open, High, Low, Close, Volume, Asset FROM candle;
|
||||
DROP TABLE candle;
|
||||
ALTER TABLE candle_new RENAME TO candle;
|
||||
@@ -13,77 +13,77 @@ import "testing"
|
||||
// Separating the tests thusly grants avoidance of Postgres deadlocks.
|
||||
func TestParent(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEvents)
|
||||
t.Run("Exchanges", testExchanges)
|
||||
t.Run("Scripts", testScripts)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistories)
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsDelete)
|
||||
t.Run("Exchanges", testExchangesDelete)
|
||||
t.Run("Scripts", testScriptsDelete)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesDelete)
|
||||
}
|
||||
|
||||
func TestQueryDeleteAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsQueryDeleteAll)
|
||||
t.Run("Exchanges", testExchangesQueryDeleteAll)
|
||||
t.Run("Scripts", testScriptsQueryDeleteAll)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesQueryDeleteAll)
|
||||
}
|
||||
|
||||
func TestSliceDeleteAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsSliceDeleteAll)
|
||||
t.Run("Exchanges", testExchangesSliceDeleteAll)
|
||||
t.Run("Scripts", testScriptsSliceDeleteAll)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesSliceDeleteAll)
|
||||
}
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsExists)
|
||||
t.Run("Exchanges", testExchangesExists)
|
||||
t.Run("Scripts", testScriptsExists)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesExists)
|
||||
}
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsFind)
|
||||
t.Run("Exchanges", testExchangesFind)
|
||||
t.Run("Scripts", testScriptsFind)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesFind)
|
||||
}
|
||||
|
||||
func TestBind(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsBind)
|
||||
t.Run("Exchanges", testExchangesBind)
|
||||
t.Run("Scripts", testScriptsBind)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesBind)
|
||||
}
|
||||
|
||||
func TestOne(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsOne)
|
||||
t.Run("Exchanges", testExchangesOne)
|
||||
t.Run("Scripts", testScriptsOne)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesOne)
|
||||
}
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsAll)
|
||||
t.Run("Exchanges", testExchangesAll)
|
||||
t.Run("Scripts", testScriptsAll)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesAll)
|
||||
}
|
||||
|
||||
func TestCount(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsCount)
|
||||
t.Run("Exchanges", testExchangesCount)
|
||||
t.Run("Scripts", testScriptsCount)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesCount)
|
||||
}
|
||||
|
||||
func TestHooks(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsHooks)
|
||||
t.Run("Exchanges", testExchangesHooks)
|
||||
t.Run("Scripts", testScriptsHooks)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesHooks)
|
||||
}
|
||||
|
||||
func TestInsert(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsInsert)
|
||||
t.Run("AuditEvents", testAuditEventsInsertWhitelist)
|
||||
t.Run("Exchanges", testExchangesInsert)
|
||||
t.Run("Exchanges", testExchangesInsertWhitelist)
|
||||
t.Run("Scripts", testScriptsInsert)
|
||||
t.Run("Scripts", testScriptsInsertWhitelist)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesInsert)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesInsertWhitelist)
|
||||
}
|
||||
|
||||
// TestToOne tests cannot be run in parallel
|
||||
@@ -128,30 +128,29 @@ func TestToManyRemove(t *testing.T) {}
|
||||
|
||||
func TestReload(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsReload)
|
||||
t.Run("Scripts", testScriptsReload)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesReload)
|
||||
t.Run("Exchanges", testExchangesReload)
|
||||
}
|
||||
|
||||
func TestReloadAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsReloadAll)
|
||||
t.Run("Exchanges", testExchangesReloadAll)
|
||||
t.Run("Scripts", testScriptsReloadAll)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesReloadAll)
|
||||
}
|
||||
|
||||
func TestSelect(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsSelect)
|
||||
t.Run("Exchanges", testExchangesSelect)
|
||||
t.Run("Scripts", testScriptsSelect)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesSelect)
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsUpdate)
|
||||
t.Run("Exchanges", testExchangesUpdate)
|
||||
t.Run("Scripts", testScriptsUpdate)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesUpdate)
|
||||
}
|
||||
|
||||
func TestSliceUpdateAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsSliceUpdateAll)
|
||||
t.Run("Exchanges", testExchangesSliceUpdateAll)
|
||||
t.Run("Scripts", testScriptsSliceUpdateAll)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesSliceUpdateAll)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ package postgres
|
||||
|
||||
var TableNames = struct {
|
||||
AuditEvent string
|
||||
Candle string
|
||||
Exchange string
|
||||
Script string
|
||||
ScriptExecution string
|
||||
WithdrawalCrypto string
|
||||
@@ -12,6 +14,8 @@ var TableNames = struct {
|
||||
WithdrawalHistory string
|
||||
}{
|
||||
AuditEvent: "audit_event",
|
||||
Candle: "candle",
|
||||
Exchange: "exchange",
|
||||
Script: "script",
|
||||
ScriptExecution: "script_execution",
|
||||
WithdrawalCrypto: "withdrawal_crypto",
|
||||
|
||||
1095
database/models/postgres/candle.go
Normal file
1095
database/models/postgres/candle.go
Normal file
File diff suppressed because it is too large
Load Diff
841
database/models/postgres/candle_test.go
Normal file
841
database/models/postgres/candle_test.go
Normal file
@@ -0,0 +1,841 @@
|
||||
// Code generated by SQLBoiler 3.5.0-gct (https://github.com/thrasher-corp/sqlboiler). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
"github.com/thrasher-corp/sqlboiler/queries"
|
||||
"github.com/thrasher-corp/sqlboiler/randomize"
|
||||
"github.com/thrasher-corp/sqlboiler/strmangle"
|
||||
)
|
||||
|
||||
var (
|
||||
// Relationships sometimes use the reflection helper queries.Equal/queries.Assign
|
||||
// so force a package dependency in case they don't.
|
||||
_ = queries.Equal
|
||||
)
|
||||
|
||||
func testCandles(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
query := Candles()
|
||||
|
||||
if query.Query == nil {
|
||||
t.Error("expected a query, got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rowsAff, err := o.Delete(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesQueryDeleteAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rowsAff, err := Candles().DeleteAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesSliceDeleteAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice := CandleSlice{o}
|
||||
|
||||
if rowsAff, err := slice.DeleteAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesExists(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
e, err := CandleExists(ctx, tx, o.ID)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to check if Candle exists: %s", err)
|
||||
}
|
||||
if !e {
|
||||
t.Errorf("Expected CandleExists to return true, but got false.")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesFind(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
candleFound, err := FindCandle(ctx, tx, o.ID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if candleFound == nil {
|
||||
t.Error("want a record, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesBind(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err = Candles().Bind(ctx, tx, o); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesOne(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if x, err := Candles().One(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if x == nil {
|
||||
t.Error("expected to get a non nil record")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
candleOne := &Candle{}
|
||||
candleTwo := &Candle{}
|
||||
if err = randomize.Struct(seed, candleOne, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
if err = randomize.Struct(seed, candleTwo, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = candleOne.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = candleTwo.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice, err := Candles().All(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(slice) != 2 {
|
||||
t.Error("want 2 records, got:", len(slice))
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var err error
|
||||
seed := randomize.NewSeed()
|
||||
candleOne := &Candle{}
|
||||
candleTwo := &Candle{}
|
||||
if err = randomize.Struct(seed, candleOne, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
if err = randomize.Struct(seed, candleTwo, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = candleOne.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = candleTwo.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 2 {
|
||||
t.Error("want 2 records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func candleBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func testCandlesHooks(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
empty := &Candle{}
|
||||
o := &Candle{}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, false); err != nil {
|
||||
t.Errorf("Unable to randomize Candle object: %s", err)
|
||||
}
|
||||
|
||||
AddCandleHook(boil.BeforeInsertHook, candleBeforeInsertHook)
|
||||
if err = o.doBeforeInsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeInsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeInsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeInsertHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterInsertHook, candleAfterInsertHook)
|
||||
if err = o.doAfterInsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterInsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterInsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterInsertHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterSelectHook, candleAfterSelectHook)
|
||||
if err = o.doAfterSelectHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterSelectHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterSelectHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterSelectHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.BeforeUpdateHook, candleBeforeUpdateHook)
|
||||
if err = o.doBeforeUpdateHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeUpdateHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeUpdateHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeUpdateHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterUpdateHook, candleAfterUpdateHook)
|
||||
if err = o.doAfterUpdateHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterUpdateHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterUpdateHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterUpdateHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.BeforeDeleteHook, candleBeforeDeleteHook)
|
||||
if err = o.doBeforeDeleteHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeDeleteHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeDeleteHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeDeleteHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterDeleteHook, candleAfterDeleteHook)
|
||||
if err = o.doAfterDeleteHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterDeleteHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterDeleteHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterDeleteHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.BeforeUpsertHook, candleBeforeUpsertHook)
|
||||
if err = o.doBeforeUpsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeUpsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeUpsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeUpsertHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterUpsertHook, candleAfterUpsertHook)
|
||||
if err = o.doAfterUpsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterUpsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterUpsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterUpsertHooks = []CandleHook{}
|
||||
}
|
||||
|
||||
func testCandlesInsert(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesInsertWhitelist(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Whitelist(candleColumnsWithoutDefault...)); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandleToOneExchangeUsingExchangeName(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var local Candle
|
||||
var foreign Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err := randomize.Struct(seed, &local, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
if err := randomize.Struct(seed, &foreign, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
local.ExchangeNameID = foreign.ID
|
||||
if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check, err := local.ExchangeName().One(ctx, tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if check.ID != foreign.ID {
|
||||
t.Errorf("want: %v, got %v", foreign.ID, check.ID)
|
||||
}
|
||||
|
||||
slice := CandleSlice{&local}
|
||||
if err = local.L.LoadExchangeName(ctx, tx, false, (*[]*Candle)(&slice), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
|
||||
local.R.ExchangeName = nil
|
||||
if err = local.L.LoadExchangeName(ctx, tx, true, &local, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandleToOneSetOpExchangeUsingExchangeName(t *testing.T) {
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var a Candle
|
||||
var b, c Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, candleDBTypes, false, strmangle.SetComplement(candlePrimaryKeyColumns, candleColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &b, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &c, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, x := range []*Exchange{&b, &c} {
|
||||
err = a.SetExchangeName(ctx, tx, i != 0, x)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if a.R.ExchangeName != x {
|
||||
t.Error("relationship struct not set to correct value")
|
||||
}
|
||||
|
||||
if x.R.ExchangeNameCandles[0] != &a {
|
||||
t.Error("failed to append to foreign relationship struct")
|
||||
}
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID)
|
||||
}
|
||||
|
||||
zero := reflect.Zero(reflect.TypeOf(a.ExchangeNameID))
|
||||
reflect.Indirect(reflect.ValueOf(&a.ExchangeNameID)).Set(zero)
|
||||
|
||||
if err = a.Reload(ctx, tx); err != nil {
|
||||
t.Fatal("failed to reload", err)
|
||||
}
|
||||
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID, x.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesReload(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err = o.Reload(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesReloadAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice := CandleSlice{o}
|
||||
|
||||
if err = slice.ReloadAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesSelect(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice, err := Candles().All(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(slice) != 1 {
|
||||
t.Error("want one record, got:", len(slice))
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
candleDBTypes = map[string]string{`ID`: `uuid`, `ExchangeNameID`: `uuid`, `Base`: `character varying`, `Quote`: `character varying`, `Interval`: `bigint`, `Timestamp`: `timestamp with time zone`, `Open`: `double precision`, `High`: `double precision`, `Low`: `double precision`, `Close`: `double precision`, `Volume`: `double precision`, `Asset`: `character varying`}
|
||||
_ = bytes.MinRead
|
||||
)
|
||||
|
||||
func testCandlesUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if 0 == len(candlePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with no primary key columns")
|
||||
}
|
||||
if len(candleAllColumns) == len(candlePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with only primary key columns")
|
||||
}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candlePrimaryKeyColumns...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
if rowsAff, err := o.Update(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only affect one row but affected", rowsAff)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesSliceUpdateAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if len(candleAllColumns) == len(candlePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with only primary key columns")
|
||||
}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candlePrimaryKeyColumns...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
// Remove Primary keys and unique columns from what we plan to update
|
||||
var fields []string
|
||||
if strmangle.StringSliceMatch(candleAllColumns, candlePrimaryKeyColumns) {
|
||||
fields = candleAllColumns
|
||||
} else {
|
||||
fields = strmangle.SetComplement(
|
||||
candleAllColumns,
|
||||
candlePrimaryKeyColumns,
|
||||
)
|
||||
}
|
||||
|
||||
value := reflect.Indirect(reflect.ValueOf(o))
|
||||
typ := reflect.TypeOf(o).Elem()
|
||||
n := typ.NumField()
|
||||
|
||||
updateMap := M{}
|
||||
for _, col := range fields {
|
||||
for i := 0; i < n; i++ {
|
||||
f := typ.Field(i)
|
||||
if f.Tag.Get("boil") == col {
|
||||
updateMap[col] = value.Field(i).Interface()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slice := CandleSlice{o}
|
||||
if rowsAff, err := slice.UpdateAll(ctx, tx, updateMap); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("wanted one record updated but got", rowsAff)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesUpsert(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if len(candleAllColumns) == len(candlePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with only primary key columns")
|
||||
}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
// Attempt the INSERT side of an UPSERT
|
||||
o := Candle{}
|
||||
if err = randomize.Struct(seed, &o, candleDBTypes, true); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Upsert(ctx, tx, false, nil, boil.Infer(), boil.Infer()); err != nil {
|
||||
t.Errorf("Unable to upsert Candle: %s", err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
|
||||
// Attempt the UPDATE side of an UPSERT
|
||||
if err = randomize.Struct(seed, &o, candleDBTypes, false, candlePrimaryKeyColumns...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
if err = o.Upsert(ctx, tx, true, nil, boil.Infer(), boil.Infer()); err != nil {
|
||||
t.Errorf("Unable to upsert Candle: %s", err)
|
||||
}
|
||||
|
||||
count, err = Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
}
|
||||
1209
database/models/postgres/exchange.go
Normal file
1209
database/models/postgres/exchange.go
Normal file
File diff suppressed because it is too large
Load Diff
1039
database/models/postgres/exchange_test.go
Normal file
1039
database/models/postgres/exchange_test.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,6 @@ import "testing"
|
||||
|
||||
func TestUpsert(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsUpsert)
|
||||
t.Run("Exchanges", testExchangesUpsert)
|
||||
t.Run("Scripts", testScriptsUpsert)
|
||||
t.Run("WithdrawalHistories", testWithdrawalHistoriesUpsert)
|
||||
}
|
||||
|
||||
@@ -50,21 +50,6 @@ var WithdrawalCryptoColumns = struct {
|
||||
|
||||
// Generated where
|
||||
|
||||
type whereHelperfloat64 struct{ field string }
|
||||
|
||||
func (w whereHelperfloat64) EQ(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) }
|
||||
func (w whereHelperfloat64) NEQ(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.NEQ, x)
|
||||
}
|
||||
func (w whereHelperfloat64) LT(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) }
|
||||
func (w whereHelperfloat64) LTE(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.LTE, x)
|
||||
}
|
||||
func (w whereHelperfloat64) GT(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) }
|
||||
func (w whereHelperfloat64) GTE(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.GTE, x)
|
||||
}
|
||||
|
||||
var WithdrawalCryptoWhere = struct {
|
||||
ID whereHelperint64
|
||||
WithdrawalCryptoID whereHelpernull_String
|
||||
|
||||
@@ -111,8 +111,8 @@ type withdrawalFiatL struct{}
|
||||
|
||||
var (
|
||||
withdrawalFiatAllColumns = []string{"id", "withdrawal_fiat_id", "bank_name", "bank_address", "bank_account_name", "bank_account_number", "bsb", "swift_code", "iban", "bank_code"}
|
||||
withdrawalFiatColumnsWithoutDefault = []string{"withdrawal_fiat_id", "bank_name", "bank_address", "bank_account_name", "bank_account_number", "bsb", "swift_code", "iban", "bank_code"}
|
||||
withdrawalFiatColumnsWithDefault = []string{"id"}
|
||||
withdrawalFiatColumnsWithoutDefault = []string{"withdrawal_fiat_id", "bank_name", "bank_address", "bank_account_name", "bank_account_number", "bank_code"}
|
||||
withdrawalFiatColumnsWithDefault = []string{"id", "bsb", "swift_code", "iban"}
|
||||
withdrawalFiatPrimaryKeyColumns = []string{"id"}
|
||||
)
|
||||
|
||||
|
||||
@@ -24,43 +24,43 @@ import (
|
||||
|
||||
// WithdrawalHistory is an object representing the database table.
|
||||
type WithdrawalHistory struct {
|
||||
ID string `boil:"id" json:"id" toml:"id" yaml:"id"`
|
||||
Exchange string `boil:"exchange" json:"exchange" toml:"exchange" yaml:"exchange"`
|
||||
ExchangeID string `boil:"exchange_id" json:"exchange_id" toml:"exchange_id" yaml:"exchange_id"`
|
||||
Status string `boil:"status" json:"status" toml:"status" yaml:"status"`
|
||||
Currency string `boil:"currency" json:"currency" toml:"currency" yaml:"currency"`
|
||||
Amount float64 `boil:"amount" json:"amount" toml:"amount" yaml:"amount"`
|
||||
Description null.String `boil:"description" json:"description,omitempty" toml:"description" yaml:"description,omitempty"`
|
||||
WithdrawType int `boil:"withdraw_type" json:"withdraw_type" toml:"withdraw_type" yaml:"withdraw_type"`
|
||||
CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
|
||||
UpdatedAt time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`
|
||||
ID string `boil:"id" json:"id" toml:"id" yaml:"id"`
|
||||
ExchangeID string `boil:"exchange_id" json:"exchange_id" toml:"exchange_id" yaml:"exchange_id"`
|
||||
Status string `boil:"status" json:"status" toml:"status" yaml:"status"`
|
||||
Currency string `boil:"currency" json:"currency" toml:"currency" yaml:"currency"`
|
||||
Amount float64 `boil:"amount" json:"amount" toml:"amount" yaml:"amount"`
|
||||
Description null.String `boil:"description" json:"description,omitempty" toml:"description" yaml:"description,omitempty"`
|
||||
WithdrawType int `boil:"withdraw_type" json:"withdraw_type" toml:"withdraw_type" yaml:"withdraw_type"`
|
||||
CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
|
||||
UpdatedAt time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`
|
||||
ExchangeNameID string `boil:"exchange_name_id" json:"exchange_name_id" toml:"exchange_name_id" yaml:"exchange_name_id"`
|
||||
|
||||
R *withdrawalHistoryR `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
L withdrawalHistoryL `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
var WithdrawalHistoryColumns = struct {
|
||||
ID string
|
||||
Exchange string
|
||||
ExchangeID string
|
||||
Status string
|
||||
Currency string
|
||||
Amount string
|
||||
Description string
|
||||
WithdrawType string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
ID string
|
||||
ExchangeID string
|
||||
Status string
|
||||
Currency string
|
||||
Amount string
|
||||
Description string
|
||||
WithdrawType string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
ExchangeNameID string
|
||||
}{
|
||||
ID: "id",
|
||||
Exchange: "exchange",
|
||||
ExchangeID: "exchange_id",
|
||||
Status: "status",
|
||||
Currency: "currency",
|
||||
Amount: "amount",
|
||||
Description: "description",
|
||||
WithdrawType: "withdraw_type",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
ID: "id",
|
||||
ExchangeID: "exchange_id",
|
||||
Status: "status",
|
||||
Currency: "currency",
|
||||
Amount: "amount",
|
||||
Description: "description",
|
||||
WithdrawType: "withdraw_type",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
ExchangeNameID: "exchange_name_id",
|
||||
}
|
||||
|
||||
// Generated where
|
||||
@@ -82,40 +82,43 @@ func (w whereHelperint) IN(slice []int) qm.QueryMod {
|
||||
}
|
||||
|
||||
var WithdrawalHistoryWhere = struct {
|
||||
ID whereHelperstring
|
||||
Exchange whereHelperstring
|
||||
ExchangeID whereHelperstring
|
||||
Status whereHelperstring
|
||||
Currency whereHelperstring
|
||||
Amount whereHelperfloat64
|
||||
Description whereHelpernull_String
|
||||
WithdrawType whereHelperint
|
||||
CreatedAt whereHelpertime_Time
|
||||
UpdatedAt whereHelpertime_Time
|
||||
ID whereHelperstring
|
||||
ExchangeID whereHelperstring
|
||||
Status whereHelperstring
|
||||
Currency whereHelperstring
|
||||
Amount whereHelperfloat64
|
||||
Description whereHelpernull_String
|
||||
WithdrawType whereHelperint
|
||||
CreatedAt whereHelpertime_Time
|
||||
UpdatedAt whereHelpertime_Time
|
||||
ExchangeNameID whereHelperstring
|
||||
}{
|
||||
ID: whereHelperstring{field: "\"withdrawal_history\".\"id\""},
|
||||
Exchange: whereHelperstring{field: "\"withdrawal_history\".\"exchange\""},
|
||||
ExchangeID: whereHelperstring{field: "\"withdrawal_history\".\"exchange_id\""},
|
||||
Status: whereHelperstring{field: "\"withdrawal_history\".\"status\""},
|
||||
Currency: whereHelperstring{field: "\"withdrawal_history\".\"currency\""},
|
||||
Amount: whereHelperfloat64{field: "\"withdrawal_history\".\"amount\""},
|
||||
Description: whereHelpernull_String{field: "\"withdrawal_history\".\"description\""},
|
||||
WithdrawType: whereHelperint{field: "\"withdrawal_history\".\"withdraw_type\""},
|
||||
CreatedAt: whereHelpertime_Time{field: "\"withdrawal_history\".\"created_at\""},
|
||||
UpdatedAt: whereHelpertime_Time{field: "\"withdrawal_history\".\"updated_at\""},
|
||||
ID: whereHelperstring{field: "\"withdrawal_history\".\"id\""},
|
||||
ExchangeID: whereHelperstring{field: "\"withdrawal_history\".\"exchange_id\""},
|
||||
Status: whereHelperstring{field: "\"withdrawal_history\".\"status\""},
|
||||
Currency: whereHelperstring{field: "\"withdrawal_history\".\"currency\""},
|
||||
Amount: whereHelperfloat64{field: "\"withdrawal_history\".\"amount\""},
|
||||
Description: whereHelpernull_String{field: "\"withdrawal_history\".\"description\""},
|
||||
WithdrawType: whereHelperint{field: "\"withdrawal_history\".\"withdraw_type\""},
|
||||
CreatedAt: whereHelpertime_Time{field: "\"withdrawal_history\".\"created_at\""},
|
||||
UpdatedAt: whereHelpertime_Time{field: "\"withdrawal_history\".\"updated_at\""},
|
||||
ExchangeNameID: whereHelperstring{field: "\"withdrawal_history\".\"exchange_name_id\""},
|
||||
}
|
||||
|
||||
// WithdrawalHistoryRels is where relationship names are stored.
|
||||
var WithdrawalHistoryRels = struct {
|
||||
ExchangeName string
|
||||
WithdrawalCryptoWithdrawalCryptos string
|
||||
WithdrawalFiatWithdrawalFiats string
|
||||
}{
|
||||
ExchangeName: "ExchangeName",
|
||||
WithdrawalCryptoWithdrawalCryptos: "WithdrawalCryptoWithdrawalCryptos",
|
||||
WithdrawalFiatWithdrawalFiats: "WithdrawalFiatWithdrawalFiats",
|
||||
}
|
||||
|
||||
// withdrawalHistoryR is where relationships are stored.
|
||||
type withdrawalHistoryR struct {
|
||||
ExchangeName *Exchange
|
||||
WithdrawalCryptoWithdrawalCryptos WithdrawalCryptoSlice
|
||||
WithdrawalFiatWithdrawalFiats WithdrawalFiatSlice
|
||||
}
|
||||
@@ -129,8 +132,8 @@ func (*withdrawalHistoryR) NewStruct() *withdrawalHistoryR {
|
||||
type withdrawalHistoryL struct{}
|
||||
|
||||
var (
|
||||
withdrawalHistoryAllColumns = []string{"id", "exchange", "exchange_id", "status", "currency", "amount", "description", "withdraw_type", "created_at", "updated_at"}
|
||||
withdrawalHistoryColumnsWithoutDefault = []string{"exchange", "exchange_id", "status", "currency", "amount", "description", "withdraw_type"}
|
||||
withdrawalHistoryAllColumns = []string{"id", "exchange_id", "status", "currency", "amount", "description", "withdraw_type", "created_at", "updated_at", "exchange_name_id"}
|
||||
withdrawalHistoryColumnsWithoutDefault = []string{"exchange_id", "status", "currency", "amount", "description", "withdraw_type", "exchange_name_id"}
|
||||
withdrawalHistoryColumnsWithDefault = []string{"id", "created_at", "updated_at"}
|
||||
withdrawalHistoryPrimaryKeyColumns = []string{"id"}
|
||||
)
|
||||
@@ -410,6 +413,20 @@ func (q withdrawalHistoryQuery) Exists(ctx context.Context, exec boil.ContextExe
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// ExchangeName pointed to by the foreign key.
|
||||
func (o *WithdrawalHistory) ExchangeName(mods ...qm.QueryMod) exchangeQuery {
|
||||
queryMods := []qm.QueryMod{
|
||||
qm.Where("\"id\" = ?", o.ExchangeNameID),
|
||||
}
|
||||
|
||||
queryMods = append(queryMods, mods...)
|
||||
|
||||
query := Exchanges(queryMods...)
|
||||
queries.SetFrom(query.Query, "\"exchange\"")
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
// WithdrawalCryptoWithdrawalCryptos retrieves all the withdrawal_crypto's WithdrawalCryptos with an executor via withdrawal_crypto_id column.
|
||||
func (o *WithdrawalHistory) WithdrawalCryptoWithdrawalCryptos(mods ...qm.QueryMod) withdrawalCryptoQuery {
|
||||
var queryMods []qm.QueryMod
|
||||
@@ -452,6 +469,107 @@ func (o *WithdrawalHistory) WithdrawalFiatWithdrawalFiats(mods ...qm.QueryMod) w
|
||||
return query
|
||||
}
|
||||
|
||||
// LoadExchangeName allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects. This is for an N-1 relationship.
|
||||
func (withdrawalHistoryL) LoadExchangeName(ctx context.Context, e boil.ContextExecutor, singular bool, maybeWithdrawalHistory interface{}, mods queries.Applicator) error {
|
||||
var slice []*WithdrawalHistory
|
||||
var object *WithdrawalHistory
|
||||
|
||||
if singular {
|
||||
object = maybeWithdrawalHistory.(*WithdrawalHistory)
|
||||
} else {
|
||||
slice = *maybeWithdrawalHistory.(*[]*WithdrawalHistory)
|
||||
}
|
||||
|
||||
args := make([]interface{}, 0, 1)
|
||||
if singular {
|
||||
if object.R == nil {
|
||||
object.R = &withdrawalHistoryR{}
|
||||
}
|
||||
args = append(args, object.ExchangeNameID)
|
||||
|
||||
} else {
|
||||
Outer:
|
||||
for _, obj := range slice {
|
||||
if obj.R == nil {
|
||||
obj.R = &withdrawalHistoryR{}
|
||||
}
|
||||
|
||||
for _, a := range args {
|
||||
if a == obj.ExchangeNameID {
|
||||
continue Outer
|
||||
}
|
||||
}
|
||||
|
||||
args = append(args, obj.ExchangeNameID)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
query := NewQuery(qm.From(`exchange`), qm.WhereIn(`exchange.id in ?`, args...))
|
||||
if mods != nil {
|
||||
mods.Apply(query)
|
||||
}
|
||||
|
||||
results, err := query.QueryContext(ctx, e)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to eager load Exchange")
|
||||
}
|
||||
|
||||
var resultSlice []*Exchange
|
||||
if err = queries.Bind(results, &resultSlice); err != nil {
|
||||
return errors.Wrap(err, "failed to bind eager loaded slice Exchange")
|
||||
}
|
||||
|
||||
if err = results.Close(); err != nil {
|
||||
return errors.Wrap(err, "failed to close results of eager load for exchange")
|
||||
}
|
||||
if err = results.Err(); err != nil {
|
||||
return errors.Wrap(err, "error occurred during iteration of eager loaded relations for exchange")
|
||||
}
|
||||
|
||||
if len(withdrawalHistoryAfterSelectHooks) != 0 {
|
||||
for _, obj := range resultSlice {
|
||||
if err := obj.doAfterSelectHooks(ctx, e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(resultSlice) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if singular {
|
||||
foreign := resultSlice[0]
|
||||
object.R.ExchangeName = foreign
|
||||
if foreign.R == nil {
|
||||
foreign.R = &exchangeR{}
|
||||
}
|
||||
foreign.R.ExchangeNameWithdrawalHistories = append(foreign.R.ExchangeNameWithdrawalHistories, object)
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, local := range slice {
|
||||
for _, foreign := range resultSlice {
|
||||
if local.ExchangeNameID == foreign.ID {
|
||||
local.R.ExchangeName = foreign
|
||||
if foreign.R == nil {
|
||||
foreign.R = &exchangeR{}
|
||||
}
|
||||
foreign.R.ExchangeNameWithdrawalHistories = append(foreign.R.ExchangeNameWithdrawalHistories, local)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadWithdrawalCryptoWithdrawalCryptos allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects. This is for a 1-M or N-M relationship.
|
||||
func (withdrawalHistoryL) LoadWithdrawalCryptoWithdrawalCryptos(ctx context.Context, e boil.ContextExecutor, singular bool, maybeWithdrawalHistory interface{}, mods queries.Applicator) error {
|
||||
@@ -642,6 +760,53 @@ func (withdrawalHistoryL) LoadWithdrawalFiatWithdrawalFiats(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetExchangeName of the withdrawalHistory to the related item.
|
||||
// Sets o.R.ExchangeName to related.
|
||||
// Adds o to related.R.ExchangeNameWithdrawalHistories.
|
||||
func (o *WithdrawalHistory) SetExchangeName(ctx context.Context, exec boil.ContextExecutor, insert bool, related *Exchange) error {
|
||||
var err error
|
||||
if insert {
|
||||
if err = related.Insert(ctx, exec, boil.Infer()); err != nil {
|
||||
return errors.Wrap(err, "failed to insert into foreign table")
|
||||
}
|
||||
}
|
||||
|
||||
updateQuery := fmt.Sprintf(
|
||||
"UPDATE \"withdrawal_history\" SET %s WHERE %s",
|
||||
strmangle.SetParamNames("\"", "\"", 1, []string{"exchange_name_id"}),
|
||||
strmangle.WhereClause("\"", "\"", 2, withdrawalHistoryPrimaryKeyColumns),
|
||||
)
|
||||
values := []interface{}{related.ID, o.ID}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, updateQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
}
|
||||
|
||||
if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil {
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
|
||||
o.ExchangeNameID = related.ID
|
||||
if o.R == nil {
|
||||
o.R = &withdrawalHistoryR{
|
||||
ExchangeName: related,
|
||||
}
|
||||
} else {
|
||||
o.R.ExchangeName = related
|
||||
}
|
||||
|
||||
if related.R == nil {
|
||||
related.R = &exchangeR{
|
||||
ExchangeNameWithdrawalHistories: WithdrawalHistorySlice{o},
|
||||
}
|
||||
} else {
|
||||
related.R.ExchangeNameWithdrawalHistories = append(related.R.ExchangeNameWithdrawalHistories, o)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddWithdrawalCryptoWithdrawalCryptos adds the given related objects to the existing relationships
|
||||
// of the withdrawal_history, optionally inserting them as new records.
|
||||
// Appends related to o.R.WithdrawalCryptoWithdrawalCryptos.
|
||||
|
||||
@@ -1150,6 +1150,115 @@ func testWithdrawalHistoryToManyRemoveOpWithdrawalFiatWithdrawalFiats(t *testing
|
||||
}
|
||||
}
|
||||
|
||||
func testWithdrawalHistoryToOneExchangeUsingExchangeName(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var local WithdrawalHistory
|
||||
var foreign Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err := randomize.Struct(seed, &local, withdrawalHistoryDBTypes, false, withdrawalHistoryColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize WithdrawalHistory struct: %s", err)
|
||||
}
|
||||
if err := randomize.Struct(seed, &foreign, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
local.ExchangeNameID = foreign.ID
|
||||
if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check, err := local.ExchangeName().One(ctx, tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if check.ID != foreign.ID {
|
||||
t.Errorf("want: %v, got %v", foreign.ID, check.ID)
|
||||
}
|
||||
|
||||
slice := WithdrawalHistorySlice{&local}
|
||||
if err = local.L.LoadExchangeName(ctx, tx, false, (*[]*WithdrawalHistory)(&slice), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
|
||||
local.R.ExchangeName = nil
|
||||
if err = local.L.LoadExchangeName(ctx, tx, true, &local, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
}
|
||||
|
||||
func testWithdrawalHistoryToOneSetOpExchangeUsingExchangeName(t *testing.T) {
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var a WithdrawalHistory
|
||||
var b, c Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, withdrawalHistoryDBTypes, false, strmangle.SetComplement(withdrawalHistoryPrimaryKeyColumns, withdrawalHistoryColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &b, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &c, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, x := range []*Exchange{&b, &c} {
|
||||
err = a.SetExchangeName(ctx, tx, i != 0, x)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if a.R.ExchangeName != x {
|
||||
t.Error("relationship struct not set to correct value")
|
||||
}
|
||||
|
||||
if x.R.ExchangeNameWithdrawalHistories[0] != &a {
|
||||
t.Error("failed to append to foreign relationship struct")
|
||||
}
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID)
|
||||
}
|
||||
|
||||
zero := reflect.Zero(reflect.TypeOf(a.ExchangeNameID))
|
||||
reflect.Indirect(reflect.ValueOf(&a.ExchangeNameID)).Set(zero)
|
||||
|
||||
if err = a.Reload(ctx, tx); err != nil {
|
||||
t.Fatal("failed to reload", err)
|
||||
}
|
||||
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID, x.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testWithdrawalHistoriesReload(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -1224,7 +1333,7 @@ func testWithdrawalHistoriesSelect(t *testing.T) {
|
||||
}
|
||||
|
||||
var (
|
||||
withdrawalHistoryDBTypes = map[string]string{`ID`: `uuid`, `Exchange`: `text`, `ExchangeID`: `text`, `Status`: `character varying`, `Currency`: `text`, `Amount`: `double precision`, `Description`: `text`, `WithdrawType`: `integer`, `CreatedAt`: `timestamp without time zone`, `UpdatedAt`: `timestamp without time zone`}
|
||||
withdrawalHistoryDBTypes = map[string]string{`ID`: `uuid`, `ExchangeID`: `text`, `Status`: `character varying`, `Currency`: `text`, `Amount`: `double precision`, `Description`: `text`, `WithdrawType`: `integer`, `CreatedAt`: `timestamp without time zone`, `UpdatedAt`: `timestamp without time zone`, `ExchangeNameID`: `uuid`}
|
||||
_ = bytes.MinRead
|
||||
)
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ import "testing"
|
||||
// Separating the tests thusly grants avoidance of Postgres deadlocks.
|
||||
func TestParent(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEvents)
|
||||
t.Run("Candles", testCandles)
|
||||
t.Run("Exchanges", testExchanges)
|
||||
t.Run("Scripts", testScripts)
|
||||
t.Run("ScriptExecutions", testScriptExecutions)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptos)
|
||||
@@ -22,6 +24,8 @@ func TestParent(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsDelete)
|
||||
t.Run("Candles", testCandlesDelete)
|
||||
t.Run("Exchanges", testExchangesDelete)
|
||||
t.Run("Scripts", testScriptsDelete)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsDelete)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosDelete)
|
||||
@@ -31,6 +35,8 @@ func TestDelete(t *testing.T) {
|
||||
|
||||
func TestQueryDeleteAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsQueryDeleteAll)
|
||||
t.Run("Candles", testCandlesQueryDeleteAll)
|
||||
t.Run("Exchanges", testExchangesQueryDeleteAll)
|
||||
t.Run("Scripts", testScriptsQueryDeleteAll)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsQueryDeleteAll)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosQueryDeleteAll)
|
||||
@@ -40,6 +46,8 @@ func TestQueryDeleteAll(t *testing.T) {
|
||||
|
||||
func TestSliceDeleteAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsSliceDeleteAll)
|
||||
t.Run("Candles", testCandlesSliceDeleteAll)
|
||||
t.Run("Exchanges", testExchangesSliceDeleteAll)
|
||||
t.Run("Scripts", testScriptsSliceDeleteAll)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsSliceDeleteAll)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosSliceDeleteAll)
|
||||
@@ -49,6 +57,8 @@ func TestSliceDeleteAll(t *testing.T) {
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsExists)
|
||||
t.Run("Candles", testCandlesExists)
|
||||
t.Run("Exchanges", testExchangesExists)
|
||||
t.Run("Scripts", testScriptsExists)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsExists)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosExists)
|
||||
@@ -58,6 +68,8 @@ func TestExists(t *testing.T) {
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsFind)
|
||||
t.Run("Candles", testCandlesFind)
|
||||
t.Run("Exchanges", testExchangesFind)
|
||||
t.Run("Scripts", testScriptsFind)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsFind)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosFind)
|
||||
@@ -67,6 +79,8 @@ func TestFind(t *testing.T) {
|
||||
|
||||
func TestBind(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsBind)
|
||||
t.Run("Candles", testCandlesBind)
|
||||
t.Run("Exchanges", testExchangesBind)
|
||||
t.Run("Scripts", testScriptsBind)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsBind)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosBind)
|
||||
@@ -76,6 +90,8 @@ func TestBind(t *testing.T) {
|
||||
|
||||
func TestOne(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsOne)
|
||||
t.Run("Candles", testCandlesOne)
|
||||
t.Run("Exchanges", testExchangesOne)
|
||||
t.Run("Scripts", testScriptsOne)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsOne)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosOne)
|
||||
@@ -85,6 +101,8 @@ func TestOne(t *testing.T) {
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsAll)
|
||||
t.Run("Candles", testCandlesAll)
|
||||
t.Run("Exchanges", testExchangesAll)
|
||||
t.Run("Scripts", testScriptsAll)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsAll)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosAll)
|
||||
@@ -94,6 +112,8 @@ func TestAll(t *testing.T) {
|
||||
|
||||
func TestCount(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsCount)
|
||||
t.Run("Candles", testCandlesCount)
|
||||
t.Run("Exchanges", testExchangesCount)
|
||||
t.Run("Scripts", testScriptsCount)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsCount)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosCount)
|
||||
@@ -103,6 +123,8 @@ func TestCount(t *testing.T) {
|
||||
|
||||
func TestHooks(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsHooks)
|
||||
t.Run("Candles", testCandlesHooks)
|
||||
t.Run("Exchanges", testExchangesHooks)
|
||||
t.Run("Scripts", testScriptsHooks)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsHooks)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosHooks)
|
||||
@@ -113,6 +135,10 @@ func TestHooks(t *testing.T) {
|
||||
func TestInsert(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsInsert)
|
||||
t.Run("AuditEvents", testAuditEventsInsertWhitelist)
|
||||
t.Run("Candles", testCandlesInsert)
|
||||
t.Run("Candles", testCandlesInsertWhitelist)
|
||||
t.Run("Exchanges", testExchangesInsert)
|
||||
t.Run("Exchanges", testExchangesInsertWhitelist)
|
||||
t.Run("Scripts", testScriptsInsert)
|
||||
t.Run("Scripts", testScriptsInsertWhitelist)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsInsert)
|
||||
@@ -128,18 +154,23 @@ func TestInsert(t *testing.T) {
|
||||
// TestToOne tests cannot be run in parallel
|
||||
// or deadlocks can occur.
|
||||
func TestToOne(t *testing.T) {
|
||||
t.Run("CandleToExchangeUsingExchangeName", testCandleToOneExchangeUsingExchangeName)
|
||||
t.Run("ScriptExecutionToScriptUsingScript", testScriptExecutionToOneScriptUsingScript)
|
||||
t.Run("WithdrawalCryptoToWithdrawalHistoryUsingWithdrawalHistory", testWithdrawalCryptoToOneWithdrawalHistoryUsingWithdrawalHistory)
|
||||
t.Run("WithdrawalFiatToWithdrawalHistoryUsingWithdrawalHistory", testWithdrawalFiatToOneWithdrawalHistoryUsingWithdrawalHistory)
|
||||
t.Run("WithdrawalHistoryToExchangeUsingExchangeName", testWithdrawalHistoryToOneExchangeUsingExchangeName)
|
||||
}
|
||||
|
||||
// TestOneToOne tests cannot be run in parallel
|
||||
// or deadlocks can occur.
|
||||
func TestOneToOne(t *testing.T) {}
|
||||
func TestOneToOne(t *testing.T) {
|
||||
t.Run("ExchangeToCandleUsingExchangeNameCandle", testExchangeOneToOneCandleUsingExchangeNameCandle)
|
||||
}
|
||||
|
||||
// TestToMany tests cannot be run in parallel
|
||||
// or deadlocks can occur.
|
||||
func TestToMany(t *testing.T) {
|
||||
t.Run("ExchangeToExchangeNameWithdrawalHistories", testExchangeToManyExchangeNameWithdrawalHistories)
|
||||
t.Run("ScriptToScriptExecutions", testScriptToManyScriptExecutions)
|
||||
t.Run("WithdrawalHistoryToWithdrawalCryptos", testWithdrawalHistoryToManyWithdrawalCryptos)
|
||||
t.Run("WithdrawalHistoryToWithdrawalFiats", testWithdrawalHistoryToManyWithdrawalFiats)
|
||||
@@ -148,9 +179,11 @@ func TestToMany(t *testing.T) {
|
||||
// TestToOneSet tests cannot be run in parallel
|
||||
// or deadlocks can occur.
|
||||
func TestToOneSet(t *testing.T) {
|
||||
t.Run("CandleToExchangeUsingExchangeNameCandle", testCandleToOneSetOpExchangeUsingExchangeName)
|
||||
t.Run("ScriptExecutionToScriptUsingScriptExecutions", testScriptExecutionToOneSetOpScriptUsingScript)
|
||||
t.Run("WithdrawalCryptoToWithdrawalHistoryUsingWithdrawalCryptos", testWithdrawalCryptoToOneSetOpWithdrawalHistoryUsingWithdrawalHistory)
|
||||
t.Run("WithdrawalFiatToWithdrawalHistoryUsingWithdrawalFiats", testWithdrawalFiatToOneSetOpWithdrawalHistoryUsingWithdrawalHistory)
|
||||
t.Run("WithdrawalHistoryToExchangeUsingExchangeNameWithdrawalHistories", testWithdrawalHistoryToOneSetOpExchangeUsingExchangeName)
|
||||
}
|
||||
|
||||
// TestToOneRemove tests cannot be run in parallel
|
||||
@@ -159,7 +192,9 @@ func TestToOneRemove(t *testing.T) {}
|
||||
|
||||
// TestOneToOneSet tests cannot be run in parallel
|
||||
// or deadlocks can occur.
|
||||
func TestOneToOneSet(t *testing.T) {}
|
||||
func TestOneToOneSet(t *testing.T) {
|
||||
t.Run("ExchangeToCandleUsingExchangeNameCandle", testExchangeOneToOneSetOpCandleUsingExchangeNameCandle)
|
||||
}
|
||||
|
||||
// TestOneToOneRemove tests cannot be run in parallel
|
||||
// or deadlocks can occur.
|
||||
@@ -168,6 +203,7 @@ func TestOneToOneRemove(t *testing.T) {}
|
||||
// TestToManyAdd tests cannot be run in parallel
|
||||
// or deadlocks can occur.
|
||||
func TestToManyAdd(t *testing.T) {
|
||||
t.Run("ExchangeToExchangeNameWithdrawalHistories", testExchangeToManyAddOpExchangeNameWithdrawalHistories)
|
||||
t.Run("ScriptToScriptExecutions", testScriptToManyAddOpScriptExecutions)
|
||||
t.Run("WithdrawalHistoryToWithdrawalCryptos", testWithdrawalHistoryToManyAddOpWithdrawalCryptos)
|
||||
t.Run("WithdrawalHistoryToWithdrawalFiats", testWithdrawalHistoryToManyAddOpWithdrawalFiats)
|
||||
@@ -183,6 +219,8 @@ func TestToManyRemove(t *testing.T) {}
|
||||
|
||||
func TestReload(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsReload)
|
||||
t.Run("Candles", testCandlesReload)
|
||||
t.Run("Exchanges", testExchangesReload)
|
||||
t.Run("Scripts", testScriptsReload)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsReload)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosReload)
|
||||
@@ -192,6 +230,8 @@ func TestReload(t *testing.T) {
|
||||
|
||||
func TestReloadAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsReloadAll)
|
||||
t.Run("Candles", testCandlesReloadAll)
|
||||
t.Run("Exchanges", testExchangesReloadAll)
|
||||
t.Run("Scripts", testScriptsReloadAll)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsReloadAll)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosReloadAll)
|
||||
@@ -201,6 +241,8 @@ func TestReloadAll(t *testing.T) {
|
||||
|
||||
func TestSelect(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsSelect)
|
||||
t.Run("Candles", testCandlesSelect)
|
||||
t.Run("Exchanges", testExchangesSelect)
|
||||
t.Run("Scripts", testScriptsSelect)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsSelect)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosSelect)
|
||||
@@ -210,6 +252,8 @@ func TestSelect(t *testing.T) {
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsUpdate)
|
||||
t.Run("Candles", testCandlesUpdate)
|
||||
t.Run("Exchanges", testExchangesUpdate)
|
||||
t.Run("Scripts", testScriptsUpdate)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsUpdate)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosUpdate)
|
||||
@@ -219,6 +263,8 @@ func TestUpdate(t *testing.T) {
|
||||
|
||||
func TestSliceUpdateAll(t *testing.T) {
|
||||
t.Run("AuditEvents", testAuditEventsSliceUpdateAll)
|
||||
t.Run("Candles", testCandlesSliceUpdateAll)
|
||||
t.Run("Exchanges", testExchangesSliceUpdateAll)
|
||||
t.Run("Scripts", testScriptsSliceUpdateAll)
|
||||
t.Run("ScriptExecutions", testScriptExecutionsSliceUpdateAll)
|
||||
t.Run("WithdrawalCryptos", testWithdrawalCryptosSliceUpdateAll)
|
||||
|
||||
@@ -5,6 +5,8 @@ package sqlite3
|
||||
|
||||
var TableNames = struct {
|
||||
AuditEvent string
|
||||
Candle string
|
||||
Exchange string
|
||||
Script string
|
||||
ScriptExecution string
|
||||
WithdrawalCrypto string
|
||||
@@ -12,6 +14,8 @@ var TableNames = struct {
|
||||
WithdrawalHistory string
|
||||
}{
|
||||
AuditEvent: "audit_event",
|
||||
Candle: "candle",
|
||||
Exchange: "exchange",
|
||||
Script: "script",
|
||||
ScriptExecution: "script_execution",
|
||||
WithdrawalCrypto: "withdrawal_crypto",
|
||||
|
||||
996
database/models/sqlite3/candle.go
Normal file
996
database/models/sqlite3/candle.go
Normal file
@@ -0,0 +1,996 @@
|
||||
// Code generated by SQLBoiler 3.5.0-gct (https://github.com/thrasher-corp/sqlboiler). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
"github.com/thrasher-corp/sqlboiler/queries"
|
||||
"github.com/thrasher-corp/sqlboiler/queries/qm"
|
||||
"github.com/thrasher-corp/sqlboiler/queries/qmhelper"
|
||||
"github.com/thrasher-corp/sqlboiler/strmangle"
|
||||
)
|
||||
|
||||
// Candle is an object representing the database table.
|
||||
type Candle struct {
|
||||
ID string `boil:"id" json:"id" toml:"id" yaml:"id"`
|
||||
ExchangeNameID string `boil:"exchange_name_id" json:"exchange_name_id" toml:"exchange_name_id" yaml:"exchange_name_id"`
|
||||
Base string `boil:"Base" json:"Base" toml:"Base" yaml:"Base"`
|
||||
Quote string `boil:"Quote" json:"Quote" toml:"Quote" yaml:"Quote"`
|
||||
Interval string `boil:"Interval" json:"Interval" toml:"Interval" yaml:"Interval"`
|
||||
Timestamp string `boil:"Timestamp" json:"Timestamp" toml:"Timestamp" yaml:"Timestamp"`
|
||||
Open float64 `boil:"Open" json:"Open" toml:"Open" yaml:"Open"`
|
||||
High float64 `boil:"High" json:"High" toml:"High" yaml:"High"`
|
||||
Low float64 `boil:"Low" json:"Low" toml:"Low" yaml:"Low"`
|
||||
Close float64 `boil:"Close" json:"Close" toml:"Close" yaml:"Close"`
|
||||
Volume float64 `boil:"Volume" json:"Volume" toml:"Volume" yaml:"Volume"`
|
||||
Asset string `boil:"Asset" json:"Asset" toml:"Asset" yaml:"Asset"`
|
||||
|
||||
R *candleR `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
L candleL `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
var CandleColumns = struct {
|
||||
ID string
|
||||
ExchangeNameID string
|
||||
Base string
|
||||
Quote string
|
||||
Interval string
|
||||
Timestamp string
|
||||
Open string
|
||||
High string
|
||||
Low string
|
||||
Close string
|
||||
Volume string
|
||||
Asset string
|
||||
}{
|
||||
ID: "id",
|
||||
ExchangeNameID: "exchange_name_id",
|
||||
Base: "Base",
|
||||
Quote: "Quote",
|
||||
Interval: "Interval",
|
||||
Timestamp: "Timestamp",
|
||||
Open: "Open",
|
||||
High: "High",
|
||||
Low: "Low",
|
||||
Close: "Close",
|
||||
Volume: "Volume",
|
||||
Asset: "Asset",
|
||||
}
|
||||
|
||||
// Generated where
|
||||
|
||||
type whereHelperfloat64 struct{ field string }
|
||||
|
||||
func (w whereHelperfloat64) EQ(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) }
|
||||
func (w whereHelperfloat64) NEQ(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.NEQ, x)
|
||||
}
|
||||
func (w whereHelperfloat64) LT(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) }
|
||||
func (w whereHelperfloat64) LTE(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.LTE, x)
|
||||
}
|
||||
func (w whereHelperfloat64) GT(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) }
|
||||
func (w whereHelperfloat64) GTE(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.GTE, x)
|
||||
}
|
||||
|
||||
var CandleWhere = struct {
|
||||
ID whereHelperstring
|
||||
ExchangeNameID whereHelperstring
|
||||
Base whereHelperstring
|
||||
Quote whereHelperstring
|
||||
Interval whereHelperstring
|
||||
Timestamp whereHelperstring
|
||||
Open whereHelperfloat64
|
||||
High whereHelperfloat64
|
||||
Low whereHelperfloat64
|
||||
Close whereHelperfloat64
|
||||
Volume whereHelperfloat64
|
||||
Asset whereHelperstring
|
||||
}{
|
||||
ID: whereHelperstring{field: "\"candle\".\"id\""},
|
||||
ExchangeNameID: whereHelperstring{field: "\"candle\".\"exchange_name_id\""},
|
||||
Base: whereHelperstring{field: "\"candle\".\"Base\""},
|
||||
Quote: whereHelperstring{field: "\"candle\".\"Quote\""},
|
||||
Interval: whereHelperstring{field: "\"candle\".\"Interval\""},
|
||||
Timestamp: whereHelperstring{field: "\"candle\".\"Timestamp\""},
|
||||
Open: whereHelperfloat64{field: "\"candle\".\"Open\""},
|
||||
High: whereHelperfloat64{field: "\"candle\".\"High\""},
|
||||
Low: whereHelperfloat64{field: "\"candle\".\"Low\""},
|
||||
Close: whereHelperfloat64{field: "\"candle\".\"Close\""},
|
||||
Volume: whereHelperfloat64{field: "\"candle\".\"Volume\""},
|
||||
Asset: whereHelperstring{field: "\"candle\".\"Asset\""},
|
||||
}
|
||||
|
||||
// CandleRels is where relationship names are stored.
|
||||
var CandleRels = struct {
|
||||
ExchangeName string
|
||||
}{
|
||||
ExchangeName: "ExchangeName",
|
||||
}
|
||||
|
||||
// candleR is where relationships are stored.
|
||||
type candleR struct {
|
||||
ExchangeName *Exchange
|
||||
}
|
||||
|
||||
// NewStruct creates a new relationship struct
|
||||
func (*candleR) NewStruct() *candleR {
|
||||
return &candleR{}
|
||||
}
|
||||
|
||||
// candleL is where Load methods for each relationship are stored.
|
||||
type candleL struct{}
|
||||
|
||||
var (
|
||||
candleAllColumns = []string{"id", "exchange_name_id", "Base", "Quote", "Interval", "Timestamp", "Open", "High", "Low", "Close", "Volume", "Asset"}
|
||||
candleColumnsWithoutDefault = []string{"id", "exchange_name_id", "Base", "Quote", "Interval", "Timestamp", "Open", "High", "Low", "Close", "Volume", "Asset"}
|
||||
candleColumnsWithDefault = []string{}
|
||||
candlePrimaryKeyColumns = []string{"id"}
|
||||
)
|
||||
|
||||
type (
|
||||
// CandleSlice is an alias for a slice of pointers to Candle.
|
||||
// This should generally be used opposed to []Candle.
|
||||
CandleSlice []*Candle
|
||||
// CandleHook is the signature for custom Candle hook methods
|
||||
CandleHook func(context.Context, boil.ContextExecutor, *Candle) error
|
||||
|
||||
candleQuery struct {
|
||||
*queries.Query
|
||||
}
|
||||
)
|
||||
|
||||
// Cache for insert, update and upsert
|
||||
var (
|
||||
candleType = reflect.TypeOf(&Candle{})
|
||||
candleMapping = queries.MakeStructMapping(candleType)
|
||||
candlePrimaryKeyMapping, _ = queries.BindMapping(candleType, candleMapping, candlePrimaryKeyColumns)
|
||||
candleInsertCacheMut sync.RWMutex
|
||||
candleInsertCache = make(map[string]insertCache)
|
||||
candleUpdateCacheMut sync.RWMutex
|
||||
candleUpdateCache = make(map[string]updateCache)
|
||||
candleUpsertCacheMut sync.RWMutex
|
||||
candleUpsertCache = make(map[string]insertCache)
|
||||
)
|
||||
|
||||
var (
|
||||
// Force time package dependency for automated UpdatedAt/CreatedAt.
|
||||
_ = time.Second
|
||||
// Force qmhelper dependency for where clause generation (which doesn't
|
||||
// always happen)
|
||||
_ = qmhelper.Where
|
||||
)
|
||||
|
||||
var candleBeforeInsertHooks []CandleHook
|
||||
var candleBeforeUpdateHooks []CandleHook
|
||||
var candleBeforeDeleteHooks []CandleHook
|
||||
var candleBeforeUpsertHooks []CandleHook
|
||||
|
||||
var candleAfterInsertHooks []CandleHook
|
||||
var candleAfterSelectHooks []CandleHook
|
||||
var candleAfterUpdateHooks []CandleHook
|
||||
var candleAfterDeleteHooks []CandleHook
|
||||
var candleAfterUpsertHooks []CandleHook
|
||||
|
||||
// doBeforeInsertHooks executes all "before insert" hooks.
|
||||
func (o *Candle) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleBeforeInsertHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doBeforeUpdateHooks executes all "before Update" hooks.
|
||||
func (o *Candle) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleBeforeUpdateHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doBeforeDeleteHooks executes all "before Delete" hooks.
|
||||
func (o *Candle) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleBeforeDeleteHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doBeforeUpsertHooks executes all "before Upsert" hooks.
|
||||
func (o *Candle) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleBeforeUpsertHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doAfterInsertHooks executes all "after Insert" hooks.
|
||||
func (o *Candle) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleAfterInsertHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doAfterSelectHooks executes all "after Select" hooks.
|
||||
func (o *Candle) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleAfterSelectHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doAfterUpdateHooks executes all "after Update" hooks.
|
||||
func (o *Candle) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleAfterUpdateHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doAfterDeleteHooks executes all "after Delete" hooks.
|
||||
func (o *Candle) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleAfterDeleteHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doAfterUpsertHooks executes all "after Upsert" hooks.
|
||||
func (o *Candle) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
||||
if boil.HooksAreSkipped(ctx) {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, hook := range candleAfterUpsertHooks {
|
||||
if err := hook(ctx, exec, o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCandleHook registers your hook function for all future operations.
|
||||
func AddCandleHook(hookPoint boil.HookPoint, candleHook CandleHook) {
|
||||
switch hookPoint {
|
||||
case boil.BeforeInsertHook:
|
||||
candleBeforeInsertHooks = append(candleBeforeInsertHooks, candleHook)
|
||||
case boil.BeforeUpdateHook:
|
||||
candleBeforeUpdateHooks = append(candleBeforeUpdateHooks, candleHook)
|
||||
case boil.BeforeDeleteHook:
|
||||
candleBeforeDeleteHooks = append(candleBeforeDeleteHooks, candleHook)
|
||||
case boil.BeforeUpsertHook:
|
||||
candleBeforeUpsertHooks = append(candleBeforeUpsertHooks, candleHook)
|
||||
case boil.AfterInsertHook:
|
||||
candleAfterInsertHooks = append(candleAfterInsertHooks, candleHook)
|
||||
case boil.AfterSelectHook:
|
||||
candleAfterSelectHooks = append(candleAfterSelectHooks, candleHook)
|
||||
case boil.AfterUpdateHook:
|
||||
candleAfterUpdateHooks = append(candleAfterUpdateHooks, candleHook)
|
||||
case boil.AfterDeleteHook:
|
||||
candleAfterDeleteHooks = append(candleAfterDeleteHooks, candleHook)
|
||||
case boil.AfterUpsertHook:
|
||||
candleAfterUpsertHooks = append(candleAfterUpsertHooks, candleHook)
|
||||
}
|
||||
}
|
||||
|
||||
// One returns a single candle record from the query.
|
||||
func (q candleQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Candle, error) {
|
||||
o := &Candle{}
|
||||
|
||||
queries.SetLimit(q.Query, 1)
|
||||
|
||||
err := q.Bind(ctx, exec, o)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == sql.ErrNoRows {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return nil, errors.Wrap(err, "sqlite3: failed to execute a one query for candle")
|
||||
}
|
||||
|
||||
if err := o.doAfterSelectHooks(ctx, exec); err != nil {
|
||||
return o, err
|
||||
}
|
||||
|
||||
return o, nil
|
||||
}
|
||||
|
||||
// All returns all Candle records from the query.
|
||||
func (q candleQuery) All(ctx context.Context, exec boil.ContextExecutor) (CandleSlice, error) {
|
||||
var o []*Candle
|
||||
|
||||
err := q.Bind(ctx, exec, &o)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "sqlite3: failed to assign all query results to Candle slice")
|
||||
}
|
||||
|
||||
if len(candleAfterSelectHooks) != 0 {
|
||||
for _, obj := range o {
|
||||
if err := obj.doAfterSelectHooks(ctx, exec); err != nil {
|
||||
return o, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return o, nil
|
||||
}
|
||||
|
||||
// Count returns the count of all Candle records in the query.
|
||||
func (q candleQuery) Count(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
|
||||
var count int64
|
||||
|
||||
queries.SetSelect(q.Query, nil)
|
||||
queries.SetCount(q.Query)
|
||||
|
||||
err := q.Query.QueryRowContext(ctx, exec).Scan(&count)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: failed to count candle rows")
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// Exists checks if the row exists in the table.
|
||||
func (q candleQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) {
|
||||
var count int64
|
||||
|
||||
queries.SetSelect(q.Query, nil)
|
||||
queries.SetCount(q.Query)
|
||||
queries.SetLimit(q.Query, 1)
|
||||
|
||||
err := q.Query.QueryRowContext(ctx, exec).Scan(&count)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "sqlite3: failed to check if candle exists")
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// ExchangeName pointed to by the foreign key.
|
||||
func (o *Candle) ExchangeName(mods ...qm.QueryMod) exchangeQuery {
|
||||
queryMods := []qm.QueryMod{
|
||||
qm.Where("\"id\" = ?", o.ExchangeNameID),
|
||||
}
|
||||
|
||||
queryMods = append(queryMods, mods...)
|
||||
|
||||
query := Exchanges(queryMods...)
|
||||
queries.SetFrom(query.Query, "\"exchange\"")
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
// LoadExchangeName allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects. This is for an N-1 relationship.
|
||||
func (candleL) LoadExchangeName(ctx context.Context, e boil.ContextExecutor, singular bool, maybeCandle interface{}, mods queries.Applicator) error {
|
||||
var slice []*Candle
|
||||
var object *Candle
|
||||
|
||||
if singular {
|
||||
object = maybeCandle.(*Candle)
|
||||
} else {
|
||||
slice = *maybeCandle.(*[]*Candle)
|
||||
}
|
||||
|
||||
args := make([]interface{}, 0, 1)
|
||||
if singular {
|
||||
if object.R == nil {
|
||||
object.R = &candleR{}
|
||||
}
|
||||
args = append(args, object.ExchangeNameID)
|
||||
|
||||
} else {
|
||||
Outer:
|
||||
for _, obj := range slice {
|
||||
if obj.R == nil {
|
||||
obj.R = &candleR{}
|
||||
}
|
||||
|
||||
for _, a := range args {
|
||||
if a == obj.ExchangeNameID {
|
||||
continue Outer
|
||||
}
|
||||
}
|
||||
|
||||
args = append(args, obj.ExchangeNameID)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
query := NewQuery(qm.From(`exchange`), qm.WhereIn(`exchange.id in ?`, args...))
|
||||
if mods != nil {
|
||||
mods.Apply(query)
|
||||
}
|
||||
|
||||
results, err := query.QueryContext(ctx, e)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to eager load Exchange")
|
||||
}
|
||||
|
||||
var resultSlice []*Exchange
|
||||
if err = queries.Bind(results, &resultSlice); err != nil {
|
||||
return errors.Wrap(err, "failed to bind eager loaded slice Exchange")
|
||||
}
|
||||
|
||||
if err = results.Close(); err != nil {
|
||||
return errors.Wrap(err, "failed to close results of eager load for exchange")
|
||||
}
|
||||
if err = results.Err(); err != nil {
|
||||
return errors.Wrap(err, "error occurred during iteration of eager loaded relations for exchange")
|
||||
}
|
||||
|
||||
if len(candleAfterSelectHooks) != 0 {
|
||||
for _, obj := range resultSlice {
|
||||
if err := obj.doAfterSelectHooks(ctx, e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(resultSlice) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if singular {
|
||||
foreign := resultSlice[0]
|
||||
object.R.ExchangeName = foreign
|
||||
if foreign.R == nil {
|
||||
foreign.R = &exchangeR{}
|
||||
}
|
||||
foreign.R.ExchangeNameCandle = object
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, local := range slice {
|
||||
for _, foreign := range resultSlice {
|
||||
if local.ExchangeNameID == foreign.ID {
|
||||
local.R.ExchangeName = foreign
|
||||
if foreign.R == nil {
|
||||
foreign.R = &exchangeR{}
|
||||
}
|
||||
foreign.R.ExchangeNameCandle = local
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetExchangeName of the candle to the related item.
|
||||
// Sets o.R.ExchangeName to related.
|
||||
// Adds o to related.R.ExchangeNameCandle.
|
||||
func (o *Candle) SetExchangeName(ctx context.Context, exec boil.ContextExecutor, insert bool, related *Exchange) error {
|
||||
var err error
|
||||
if insert {
|
||||
if err = related.Insert(ctx, exec, boil.Infer()); err != nil {
|
||||
return errors.Wrap(err, "failed to insert into foreign table")
|
||||
}
|
||||
}
|
||||
|
||||
updateQuery := fmt.Sprintf(
|
||||
"UPDATE \"candle\" SET %s WHERE %s",
|
||||
strmangle.SetParamNames("\"", "\"", 0, []string{"exchange_name_id"}),
|
||||
strmangle.WhereClause("\"", "\"", 0, candlePrimaryKeyColumns),
|
||||
)
|
||||
values := []interface{}{related.ID, o.ID}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, updateQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
}
|
||||
|
||||
if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil {
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
|
||||
o.ExchangeNameID = related.ID
|
||||
if o.R == nil {
|
||||
o.R = &candleR{
|
||||
ExchangeName: related,
|
||||
}
|
||||
} else {
|
||||
o.R.ExchangeName = related
|
||||
}
|
||||
|
||||
if related.R == nil {
|
||||
related.R = &exchangeR{
|
||||
ExchangeNameCandle: o,
|
||||
}
|
||||
} else {
|
||||
related.R.ExchangeNameCandle = o
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Candles retrieves all the records using an executor.
|
||||
func Candles(mods ...qm.QueryMod) candleQuery {
|
||||
mods = append(mods, qm.From("\"candle\""))
|
||||
return candleQuery{NewQuery(mods...)}
|
||||
}
|
||||
|
||||
// FindCandle retrieves a single record by ID with an executor.
|
||||
// If selectCols is empty Find will return all columns.
|
||||
func FindCandle(ctx context.Context, exec boil.ContextExecutor, iD string, selectCols ...string) (*Candle, error) {
|
||||
candleObj := &Candle{}
|
||||
|
||||
sel := "*"
|
||||
if len(selectCols) > 0 {
|
||||
sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
|
||||
}
|
||||
query := fmt.Sprintf(
|
||||
"select %s from \"candle\" where \"id\"=?", sel,
|
||||
)
|
||||
|
||||
q := queries.Raw(query, iD)
|
||||
|
||||
err := q.Bind(ctx, exec, candleObj)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == sql.ErrNoRows {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return nil, errors.Wrap(err, "sqlite3: unable to select from candle")
|
||||
}
|
||||
|
||||
return candleObj, nil
|
||||
}
|
||||
|
||||
// Insert a single record using an executor.
|
||||
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
|
||||
func (o *Candle) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error {
|
||||
if o == nil {
|
||||
return errors.New("sqlite3: no candle provided for insertion")
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
if err := o.doBeforeInsertHooks(ctx, exec); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nzDefaults := queries.NonZeroDefaultSet(candleColumnsWithDefault, o)
|
||||
|
||||
key := makeCacheKey(columns, nzDefaults)
|
||||
candleInsertCacheMut.RLock()
|
||||
cache, cached := candleInsertCache[key]
|
||||
candleInsertCacheMut.RUnlock()
|
||||
|
||||
if !cached {
|
||||
wl, returnColumns := columns.InsertColumnSet(
|
||||
candleAllColumns,
|
||||
candleColumnsWithDefault,
|
||||
candleColumnsWithoutDefault,
|
||||
nzDefaults,
|
||||
)
|
||||
|
||||
cache.valueMapping, err = queries.BindMapping(candleType, candleMapping, wl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cache.retMapping, err = queries.BindMapping(candleType, candleMapping, returnColumns)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(wl) != 0 {
|
||||
cache.query = fmt.Sprintf("INSERT INTO \"candle\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
|
||||
} else {
|
||||
cache.query = "INSERT INTO \"candle\" () VALUES ()%s%s"
|
||||
}
|
||||
|
||||
var queryOutput, queryReturning string
|
||||
|
||||
if len(cache.retMapping) != 0 {
|
||||
cache.retQuery = fmt.Sprintf("SELECT \"%s\" FROM \"candle\" WHERE %s", strings.Join(returnColumns, "\",\""), strmangle.WhereClause("\"", "\"", 0, candlePrimaryKeyColumns))
|
||||
}
|
||||
|
||||
cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning)
|
||||
}
|
||||
|
||||
value := reflect.Indirect(reflect.ValueOf(o))
|
||||
vals := queries.ValuesFromMapping(value, cache.valueMapping)
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
||||
fmt.Fprintln(boil.DebugWriter, vals)
|
||||
}
|
||||
|
||||
_, err = exec.ExecContext(ctx, cache.query, vals...)
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "sqlite3: unable to insert into candle")
|
||||
}
|
||||
|
||||
var identifierCols []interface{}
|
||||
|
||||
if len(cache.retMapping) == 0 {
|
||||
goto CacheNoHooks
|
||||
}
|
||||
|
||||
identifierCols = []interface{}{
|
||||
o.ID,
|
||||
}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
||||
}
|
||||
|
||||
err = exec.QueryRowContext(ctx, cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "sqlite3: unable to populate default values for candle")
|
||||
}
|
||||
|
||||
CacheNoHooks:
|
||||
if !cached {
|
||||
candleInsertCacheMut.Lock()
|
||||
candleInsertCache[key] = cache
|
||||
candleInsertCacheMut.Unlock()
|
||||
}
|
||||
|
||||
return o.doAfterInsertHooks(ctx, exec)
|
||||
}
|
||||
|
||||
// Update uses an executor to update the Candle.
|
||||
// See boil.Columns.UpdateColumnSet documentation to understand column list inference for updates.
|
||||
// Update does not automatically update the record in case of default values. Use .Reload() to refresh the records.
|
||||
func (o *Candle) Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error) {
|
||||
var err error
|
||||
if err = o.doBeforeUpdateHooks(ctx, exec); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
key := makeCacheKey(columns, nil)
|
||||
candleUpdateCacheMut.RLock()
|
||||
cache, cached := candleUpdateCache[key]
|
||||
candleUpdateCacheMut.RUnlock()
|
||||
|
||||
if !cached {
|
||||
wl := columns.UpdateColumnSet(
|
||||
candleAllColumns,
|
||||
candlePrimaryKeyColumns,
|
||||
)
|
||||
|
||||
if len(wl) == 0 {
|
||||
return 0, errors.New("sqlite3: unable to update candle, could not build whitelist")
|
||||
}
|
||||
|
||||
cache.query = fmt.Sprintf("UPDATE \"candle\" SET %s WHERE %s",
|
||||
strmangle.SetParamNames("\"", "\"", 0, wl),
|
||||
strmangle.WhereClause("\"", "\"", 0, candlePrimaryKeyColumns),
|
||||
)
|
||||
cache.valueMapping, err = queries.BindMapping(candleType, candleMapping, append(wl, candlePrimaryKeyColumns...))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
}
|
||||
|
||||
var result sql.Result
|
||||
result, err = exec.ExecContext(ctx, cache.query, values...)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to update candle row")
|
||||
}
|
||||
|
||||
rowsAff, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: failed to get rows affected by update for candle")
|
||||
}
|
||||
|
||||
if !cached {
|
||||
candleUpdateCacheMut.Lock()
|
||||
candleUpdateCache[key] = cache
|
||||
candleUpdateCacheMut.Unlock()
|
||||
}
|
||||
|
||||
return rowsAff, o.doAfterUpdateHooks(ctx, exec)
|
||||
}
|
||||
|
||||
// UpdateAll updates all rows with the specified column values.
|
||||
func (q candleQuery) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) {
|
||||
queries.SetUpdate(q.Query, cols)
|
||||
|
||||
result, err := q.Query.ExecContext(ctx, exec)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to update all for candle")
|
||||
}
|
||||
|
||||
rowsAff, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to retrieve rows affected for candle")
|
||||
}
|
||||
|
||||
return rowsAff, nil
|
||||
}
|
||||
|
||||
// UpdateAll updates all rows with the specified column values, using an executor.
|
||||
func (o CandleSlice) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) {
|
||||
ln := int64(len(o))
|
||||
if ln == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if len(cols) == 0 {
|
||||
return 0, errors.New("sqlite3: update all requires at least one column argument")
|
||||
}
|
||||
|
||||
colNames := make([]string, len(cols))
|
||||
args := make([]interface{}, len(cols))
|
||||
|
||||
i := 0
|
||||
for name, value := range cols {
|
||||
colNames[i] = name
|
||||
args[i] = value
|
||||
i++
|
||||
}
|
||||
|
||||
// Append all of the primary key values for each column
|
||||
for _, obj := range o {
|
||||
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), candlePrimaryKeyMapping)
|
||||
args = append(args, pkeyArgs...)
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf("UPDATE \"candle\" SET %s WHERE %s",
|
||||
strmangle.SetParamNames("\"", "\"", 0, colNames),
|
||||
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, candlePrimaryKeyColumns, len(o)))
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, args...)
|
||||
}
|
||||
|
||||
result, err := exec.ExecContext(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to update all in candle slice")
|
||||
}
|
||||
|
||||
rowsAff, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to retrieve rows affected all in update all candle")
|
||||
}
|
||||
return rowsAff, nil
|
||||
}
|
||||
|
||||
// Delete deletes a single Candle record with an executor.
|
||||
// Delete will match against the primary key column to find the record to delete.
|
||||
func (o *Candle) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
|
||||
if o == nil {
|
||||
return 0, errors.New("sqlite3: no Candle provided for delete")
|
||||
}
|
||||
|
||||
if err := o.doBeforeDeleteHooks(ctx, exec); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), candlePrimaryKeyMapping)
|
||||
sql := "DELETE FROM \"candle\" WHERE \"id\"=?"
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, args...)
|
||||
}
|
||||
|
||||
result, err := exec.ExecContext(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to delete from candle")
|
||||
}
|
||||
|
||||
rowsAff, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: failed to get rows affected by delete for candle")
|
||||
}
|
||||
|
||||
if err := o.doAfterDeleteHooks(ctx, exec); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return rowsAff, nil
|
||||
}
|
||||
|
||||
// DeleteAll deletes all matching rows.
|
||||
func (q candleQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
|
||||
if q.Query == nil {
|
||||
return 0, errors.New("sqlite3: no candleQuery provided for delete all")
|
||||
}
|
||||
|
||||
queries.SetDelete(q.Query)
|
||||
|
||||
result, err := q.Query.ExecContext(ctx, exec)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to delete all from candle")
|
||||
}
|
||||
|
||||
rowsAff, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: failed to get rows affected by deleteall for candle")
|
||||
}
|
||||
|
||||
return rowsAff, nil
|
||||
}
|
||||
|
||||
// DeleteAll deletes all rows in the slice, using an executor.
|
||||
func (o CandleSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
|
||||
if len(o) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if len(candleBeforeDeleteHooks) != 0 {
|
||||
for _, obj := range o {
|
||||
if err := obj.doBeforeDeleteHooks(ctx, exec); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var args []interface{}
|
||||
for _, obj := range o {
|
||||
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), candlePrimaryKeyMapping)
|
||||
args = append(args, pkeyArgs...)
|
||||
}
|
||||
|
||||
sql := "DELETE FROM \"candle\" WHERE " +
|
||||
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, candlePrimaryKeyColumns, len(o))
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, args)
|
||||
}
|
||||
|
||||
result, err := exec.ExecContext(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: unable to delete all from candle slice")
|
||||
}
|
||||
|
||||
rowsAff, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "sqlite3: failed to get rows affected by deleteall for candle")
|
||||
}
|
||||
|
||||
if len(candleAfterDeleteHooks) != 0 {
|
||||
for _, obj := range o {
|
||||
if err := obj.doAfterDeleteHooks(ctx, exec); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rowsAff, nil
|
||||
}
|
||||
|
||||
// Reload refetches the object from the database
|
||||
// using the primary keys with an executor.
|
||||
func (o *Candle) Reload(ctx context.Context, exec boil.ContextExecutor) error {
|
||||
ret, err := FindCandle(ctx, exec, o.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = *ret
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReloadAll refetches every row with matching primary key column values
|
||||
// and overwrites the original object slice with the newly updated slice.
|
||||
func (o *CandleSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error {
|
||||
if o == nil || len(*o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
slice := CandleSlice{}
|
||||
var args []interface{}
|
||||
for _, obj := range *o {
|
||||
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), candlePrimaryKeyMapping)
|
||||
args = append(args, pkeyArgs...)
|
||||
}
|
||||
|
||||
sql := "SELECT \"candle\".* FROM \"candle\" WHERE " +
|
||||
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, candlePrimaryKeyColumns, len(*o))
|
||||
|
||||
q := queries.Raw(sql, args...)
|
||||
|
||||
err := q.Bind(ctx, exec, &slice)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "sqlite3: unable to reload all in CandleSlice")
|
||||
}
|
||||
|
||||
*o = slice
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CandleExists checks if the Candle row exists.
|
||||
func CandleExists(ctx context.Context, exec boil.ContextExecutor, iD string) (bool, error) {
|
||||
var exists bool
|
||||
sql := "select exists(select 1 from \"candle\" where \"id\"=? limit 1)"
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, iD)
|
||||
}
|
||||
|
||||
row := exec.QueryRowContext(ctx, sql, iD)
|
||||
|
||||
err := row.Scan(&exists)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "sqlite3: unable to check if candle exists")
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
793
database/models/sqlite3/candle_test.go
Normal file
793
database/models/sqlite3/candle_test.go
Normal file
@@ -0,0 +1,793 @@
|
||||
// Code generated by SQLBoiler 3.5.0-gct (https://github.com/thrasher-corp/sqlboiler). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
"github.com/thrasher-corp/sqlboiler/queries"
|
||||
"github.com/thrasher-corp/sqlboiler/randomize"
|
||||
"github.com/thrasher-corp/sqlboiler/strmangle"
|
||||
)
|
||||
|
||||
var (
|
||||
// Relationships sometimes use the reflection helper queries.Equal/queries.Assign
|
||||
// so force a package dependency in case they don't.
|
||||
_ = queries.Equal
|
||||
)
|
||||
|
||||
func testCandles(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
query := Candles()
|
||||
|
||||
if query.Query == nil {
|
||||
t.Error("expected a query, got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rowsAff, err := o.Delete(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesQueryDeleteAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rowsAff, err := Candles().DeleteAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesSliceDeleteAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice := CandleSlice{o}
|
||||
|
||||
if rowsAff, err := slice.DeleteAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesExists(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
e, err := CandleExists(ctx, tx, o.ID)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to check if Candle exists: %s", err)
|
||||
}
|
||||
if !e {
|
||||
t.Errorf("Expected CandleExists to return true, but got false.")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesFind(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
candleFound, err := FindCandle(ctx, tx, o.ID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if candleFound == nil {
|
||||
t.Error("want a record, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesBind(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err = Candles().Bind(ctx, tx, o); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesOne(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if x, err := Candles().One(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if x == nil {
|
||||
t.Error("expected to get a non nil record")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
candleOne := &Candle{}
|
||||
candleTwo := &Candle{}
|
||||
if err = randomize.Struct(seed, candleOne, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
if err = randomize.Struct(seed, candleTwo, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = candleOne.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = candleTwo.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice, err := Candles().All(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(slice) != 2 {
|
||||
t.Error("want 2 records, got:", len(slice))
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var err error
|
||||
seed := randomize.NewSeed()
|
||||
candleOne := &Candle{}
|
||||
candleTwo := &Candle{}
|
||||
if err = randomize.Struct(seed, candleOne, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
if err = randomize.Struct(seed, candleTwo, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = candleOne.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = candleTwo.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 2 {
|
||||
t.Error("want 2 records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func candleBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candleAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Candle) error {
|
||||
*o = Candle{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func testCandlesHooks(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
empty := &Candle{}
|
||||
o := &Candle{}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, false); err != nil {
|
||||
t.Errorf("Unable to randomize Candle object: %s", err)
|
||||
}
|
||||
|
||||
AddCandleHook(boil.BeforeInsertHook, candleBeforeInsertHook)
|
||||
if err = o.doBeforeInsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeInsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeInsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeInsertHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterInsertHook, candleAfterInsertHook)
|
||||
if err = o.doAfterInsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterInsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterInsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterInsertHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterSelectHook, candleAfterSelectHook)
|
||||
if err = o.doAfterSelectHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterSelectHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterSelectHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterSelectHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.BeforeUpdateHook, candleBeforeUpdateHook)
|
||||
if err = o.doBeforeUpdateHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeUpdateHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeUpdateHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeUpdateHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterUpdateHook, candleAfterUpdateHook)
|
||||
if err = o.doAfterUpdateHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterUpdateHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterUpdateHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterUpdateHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.BeforeDeleteHook, candleBeforeDeleteHook)
|
||||
if err = o.doBeforeDeleteHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeDeleteHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeDeleteHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeDeleteHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterDeleteHook, candleAfterDeleteHook)
|
||||
if err = o.doAfterDeleteHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterDeleteHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterDeleteHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterDeleteHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.BeforeUpsertHook, candleBeforeUpsertHook)
|
||||
if err = o.doBeforeUpsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeUpsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeUpsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleBeforeUpsertHooks = []CandleHook{}
|
||||
|
||||
AddCandleHook(boil.AfterUpsertHook, candleAfterUpsertHook)
|
||||
if err = o.doAfterUpsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterUpsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterUpsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
candleAfterUpsertHooks = []CandleHook{}
|
||||
}
|
||||
|
||||
func testCandlesInsert(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesInsertWhitelist(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Whitelist(candleColumnsWithoutDefault...)); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandleToOneExchangeUsingExchangeName(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var local Candle
|
||||
var foreign Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err := randomize.Struct(seed, &local, candleDBTypes, false, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
if err := randomize.Struct(seed, &foreign, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
local.ExchangeNameID = foreign.ID
|
||||
if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check, err := local.ExchangeName().One(ctx, tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if check.ID != foreign.ID {
|
||||
t.Errorf("want: %v, got %v", foreign.ID, check.ID)
|
||||
}
|
||||
|
||||
slice := CandleSlice{&local}
|
||||
if err = local.L.LoadExchangeName(ctx, tx, false, (*[]*Candle)(&slice), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
|
||||
local.R.ExchangeName = nil
|
||||
if err = local.L.LoadExchangeName(ctx, tx, true, &local, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
}
|
||||
|
||||
func testCandleToOneSetOpExchangeUsingExchangeName(t *testing.T) {
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var a Candle
|
||||
var b, c Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, candleDBTypes, false, strmangle.SetComplement(candlePrimaryKeyColumns, candleColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &b, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &c, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, x := range []*Exchange{&b, &c} {
|
||||
err = a.SetExchangeName(ctx, tx, i != 0, x)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if a.R.ExchangeName != x {
|
||||
t.Error("relationship struct not set to correct value")
|
||||
}
|
||||
|
||||
if x.R.ExchangeNameCandle != &a {
|
||||
t.Error("failed to append to foreign relationship struct")
|
||||
}
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID)
|
||||
}
|
||||
|
||||
zero := reflect.Zero(reflect.TypeOf(a.ExchangeNameID))
|
||||
reflect.Indirect(reflect.ValueOf(&a.ExchangeNameID)).Set(zero)
|
||||
|
||||
if err = a.Reload(ctx, tx); err != nil {
|
||||
t.Fatal("failed to reload", err)
|
||||
}
|
||||
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID, x.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesReload(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err = o.Reload(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesReloadAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice := CandleSlice{o}
|
||||
|
||||
if err = slice.ReloadAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesSelect(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice, err := Candles().All(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(slice) != 1 {
|
||||
t.Error("want one record, got:", len(slice))
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
candleDBTypes = map[string]string{`ID`: `TEXT`, `ExchangeNameID`: `UUID`, `Base`: `TEXT`, `Quote`: `TEXT`, `Interval`: `TEXT`, `Timestamp`: `TIMESTAMP`, `Open`: `REAL`, `High`: `REAL`, `Low`: `REAL`, `Close`: `REAL`, `Volume`: `REAL`, `Asset`: `TEXT`}
|
||||
_ = bytes.MinRead
|
||||
)
|
||||
|
||||
func testCandlesUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if 0 == len(candlePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with no primary key columns")
|
||||
}
|
||||
if len(candleAllColumns) == len(candlePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with only primary key columns")
|
||||
}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candlePrimaryKeyColumns...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
if rowsAff, err := o.Update(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only affect one row but affected", rowsAff)
|
||||
}
|
||||
}
|
||||
|
||||
func testCandlesSliceUpdateAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if len(candleAllColumns) == len(candlePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with only primary key columns")
|
||||
}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Candle{}
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Candles().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
|
||||
if err = randomize.Struct(seed, o, candleDBTypes, true, candlePrimaryKeyColumns...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
|
||||
// Remove Primary keys and unique columns from what we plan to update
|
||||
var fields []string
|
||||
if strmangle.StringSliceMatch(candleAllColumns, candlePrimaryKeyColumns) {
|
||||
fields = candleAllColumns
|
||||
} else {
|
||||
fields = strmangle.SetComplement(
|
||||
candleAllColumns,
|
||||
candlePrimaryKeyColumns,
|
||||
)
|
||||
}
|
||||
|
||||
value := reflect.Indirect(reflect.ValueOf(o))
|
||||
typ := reflect.TypeOf(o).Elem()
|
||||
n := typ.NumField()
|
||||
|
||||
updateMap := M{}
|
||||
for _, col := range fields {
|
||||
for i := 0; i < n; i++ {
|
||||
f := typ.Field(i)
|
||||
if f.Tag.Get("boil") == col {
|
||||
updateMap[col] = value.Field(i).Interface()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slice := CandleSlice{o}
|
||||
if rowsAff, err := slice.UpdateAll(ctx, tx, updateMap); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("wanted one record updated but got", rowsAff)
|
||||
}
|
||||
}
|
||||
1104
database/models/sqlite3/exchange.go
Normal file
1104
database/models/sqlite3/exchange.go
Normal file
File diff suppressed because it is too large
Load Diff
951
database/models/sqlite3/exchange_test.go
Normal file
951
database/models/sqlite3/exchange_test.go
Normal file
@@ -0,0 +1,951 @@
|
||||
// Code generated by SQLBoiler 3.5.0-gct (https://github.com/thrasher-corp/sqlboiler). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
"github.com/thrasher-corp/sqlboiler/queries"
|
||||
"github.com/thrasher-corp/sqlboiler/randomize"
|
||||
"github.com/thrasher-corp/sqlboiler/strmangle"
|
||||
)
|
||||
|
||||
var (
|
||||
// Relationships sometimes use the reflection helper queries.Equal/queries.Assign
|
||||
// so force a package dependency in case they don't.
|
||||
_ = queries.Equal
|
||||
)
|
||||
|
||||
func testExchanges(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
query := Exchanges()
|
||||
|
||||
if query.Query == nil {
|
||||
t.Error("expected a query, got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rowsAff, err := o.Delete(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesQueryDeleteAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rowsAff, err := Exchanges().DeleteAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesSliceDeleteAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice := ExchangeSlice{o}
|
||||
|
||||
if rowsAff, err := slice.DeleteAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only have deleted one row, but affected:", rowsAff)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 0 {
|
||||
t.Error("want zero records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesExists(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
e, err := ExchangeExists(ctx, tx, o.ID)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to check if Exchange exists: %s", err)
|
||||
}
|
||||
if !e {
|
||||
t.Errorf("Expected ExchangeExists to return true, but got false.")
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesFind(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
exchangeFound, err := FindExchange(ctx, tx, o.ID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if exchangeFound == nil {
|
||||
t.Error("want a record, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesBind(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err = Exchanges().Bind(ctx, tx, o); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesOne(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if x, err := Exchanges().One(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
} else if x == nil {
|
||||
t.Error("expected to get a non nil record")
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
exchangeOne := &Exchange{}
|
||||
exchangeTwo := &Exchange{}
|
||||
if err = randomize.Struct(seed, exchangeOne, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
if err = randomize.Struct(seed, exchangeTwo, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = exchangeOne.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = exchangeTwo.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice, err := Exchanges().All(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(slice) != 2 {
|
||||
t.Error("want 2 records, got:", len(slice))
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var err error
|
||||
seed := randomize.NewSeed()
|
||||
exchangeOne := &Exchange{}
|
||||
exchangeTwo := &Exchange{}
|
||||
if err = randomize.Struct(seed, exchangeOne, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
if err = randomize.Struct(seed, exchangeTwo, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = exchangeOne.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = exchangeTwo.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 2 {
|
||||
t.Error("want 2 records, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func exchangeBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exchangeAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Exchange) error {
|
||||
*o = Exchange{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func testExchangesHooks(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
empty := &Exchange{}
|
||||
o := &Exchange{}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, false); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange object: %s", err)
|
||||
}
|
||||
|
||||
AddExchangeHook(boil.BeforeInsertHook, exchangeBeforeInsertHook)
|
||||
if err = o.doBeforeInsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeInsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeInsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeBeforeInsertHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.AfterInsertHook, exchangeAfterInsertHook)
|
||||
if err = o.doAfterInsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterInsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterInsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeAfterInsertHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.AfterSelectHook, exchangeAfterSelectHook)
|
||||
if err = o.doAfterSelectHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterSelectHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterSelectHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeAfterSelectHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.BeforeUpdateHook, exchangeBeforeUpdateHook)
|
||||
if err = o.doBeforeUpdateHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeUpdateHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeUpdateHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeBeforeUpdateHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.AfterUpdateHook, exchangeAfterUpdateHook)
|
||||
if err = o.doAfterUpdateHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterUpdateHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterUpdateHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeAfterUpdateHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.BeforeDeleteHook, exchangeBeforeDeleteHook)
|
||||
if err = o.doBeforeDeleteHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeDeleteHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeDeleteHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeBeforeDeleteHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.AfterDeleteHook, exchangeAfterDeleteHook)
|
||||
if err = o.doAfterDeleteHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterDeleteHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterDeleteHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeAfterDeleteHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.BeforeUpsertHook, exchangeBeforeUpsertHook)
|
||||
if err = o.doBeforeUpsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doBeforeUpsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected BeforeUpsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeBeforeUpsertHooks = []ExchangeHook{}
|
||||
|
||||
AddExchangeHook(boil.AfterUpsertHook, exchangeAfterUpsertHook)
|
||||
if err = o.doAfterUpsertHooks(ctx, nil); err != nil {
|
||||
t.Errorf("Unable to execute doAfterUpsertHooks: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(o, empty) {
|
||||
t.Errorf("Expected AfterUpsertHook function to empty object, but got: %#v", o)
|
||||
}
|
||||
exchangeAfterUpsertHooks = []ExchangeHook{}
|
||||
}
|
||||
|
||||
func testExchangesInsert(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesInsertWhitelist(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Whitelist(exchangeColumnsWithoutDefault...)); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangeOneToOneCandleUsingExchangeNameCandle(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var foreign Candle
|
||||
var local Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err := randomize.Struct(seed, &foreign, candleDBTypes, true, candleColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Candle struct: %s", err)
|
||||
}
|
||||
if err := randomize.Struct(seed, &local, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
foreign.ExchangeNameID = local.ID
|
||||
if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check, err := local.ExchangeNameCandle().One(ctx, tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if check.ExchangeNameID != foreign.ExchangeNameID {
|
||||
t.Errorf("want: %v, got %v", foreign.ExchangeNameID, check.ExchangeNameID)
|
||||
}
|
||||
|
||||
slice := ExchangeSlice{&local}
|
||||
if err = local.L.LoadExchangeNameCandle(ctx, tx, false, (*[]*Exchange)(&slice), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeNameCandle == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
|
||||
local.R.ExchangeNameCandle = nil
|
||||
if err = local.L.LoadExchangeNameCandle(ctx, tx, true, &local, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeNameCandle == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangeOneToOneSetOpCandleUsingExchangeNameCandle(t *testing.T) {
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var a Exchange
|
||||
var b, c Candle
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &b, candleDBTypes, false, strmangle.SetComplement(candlePrimaryKeyColumns, candleColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &c, candleDBTypes, false, strmangle.SetComplement(candlePrimaryKeyColumns, candleColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, x := range []*Candle{&b, &c} {
|
||||
err = a.SetExchangeNameCandle(ctx, tx, i != 0, x)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if a.R.ExchangeNameCandle != x {
|
||||
t.Error("relationship struct not set to correct value")
|
||||
}
|
||||
if x.R.ExchangeName != &a {
|
||||
t.Error("failed to append to foreign relationship struct")
|
||||
}
|
||||
|
||||
if a.ID != x.ExchangeNameID {
|
||||
t.Error("foreign key was wrong value", a.ID)
|
||||
}
|
||||
|
||||
zero := reflect.Zero(reflect.TypeOf(x.ExchangeNameID))
|
||||
reflect.Indirect(reflect.ValueOf(&x.ExchangeNameID)).Set(zero)
|
||||
|
||||
if err = x.Reload(ctx, tx); err != nil {
|
||||
t.Fatal("failed to reload", err)
|
||||
}
|
||||
|
||||
if a.ID != x.ExchangeNameID {
|
||||
t.Error("foreign key was wrong value", a.ID, x.ExchangeNameID)
|
||||
}
|
||||
|
||||
if _, err = x.Delete(ctx, tx); err != nil {
|
||||
t.Fatal("failed to delete x", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangeToManyExchangeNameWithdrawalHistories(t *testing.T) {
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var a Exchange
|
||||
var b, c WithdrawalHistory
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = randomize.Struct(seed, &b, withdrawalHistoryDBTypes, false, withdrawalHistoryColumnsWithDefault...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &c, withdrawalHistoryDBTypes, false, withdrawalHistoryColumnsWithDefault...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b.ExchangeNameID = a.ID
|
||||
c.ExchangeNameID = a.ID
|
||||
|
||||
if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = c.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check, err := a.ExchangeNameWithdrawalHistories().All(ctx, tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bFound, cFound := false, false
|
||||
for _, v := range check {
|
||||
if v.ExchangeNameID == b.ExchangeNameID {
|
||||
bFound = true
|
||||
}
|
||||
if v.ExchangeNameID == c.ExchangeNameID {
|
||||
cFound = true
|
||||
}
|
||||
}
|
||||
|
||||
if !bFound {
|
||||
t.Error("expected to find b")
|
||||
}
|
||||
if !cFound {
|
||||
t.Error("expected to find c")
|
||||
}
|
||||
|
||||
slice := ExchangeSlice{&a}
|
||||
if err = a.L.LoadExchangeNameWithdrawalHistories(ctx, tx, false, (*[]*Exchange)(&slice), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := len(a.R.ExchangeNameWithdrawalHistories); got != 2 {
|
||||
t.Error("number of eager loaded records wrong, got:", got)
|
||||
}
|
||||
|
||||
a.R.ExchangeNameWithdrawalHistories = nil
|
||||
if err = a.L.LoadExchangeNameWithdrawalHistories(ctx, tx, true, &a, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := len(a.R.ExchangeNameWithdrawalHistories); got != 2 {
|
||||
t.Error("number of eager loaded records wrong, got:", got)
|
||||
}
|
||||
|
||||
if t.Failed() {
|
||||
t.Logf("%#v", check)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangeToManyAddOpExchangeNameWithdrawalHistories(t *testing.T) {
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var a Exchange
|
||||
var b, c, d, e WithdrawalHistory
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreigners := []*WithdrawalHistory{&b, &c, &d, &e}
|
||||
for _, x := range foreigners {
|
||||
if err = randomize.Struct(seed, x, withdrawalHistoryDBTypes, false, strmangle.SetComplement(withdrawalHistoryPrimaryKeyColumns, withdrawalHistoryColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = c.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
foreignersSplitByInsertion := [][]*WithdrawalHistory{
|
||||
{&b, &c},
|
||||
{&d, &e},
|
||||
}
|
||||
|
||||
for i, x := range foreignersSplitByInsertion {
|
||||
err = a.AddExchangeNameWithdrawalHistories(ctx, tx, i != 0, x...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
first := x[0]
|
||||
second := x[1]
|
||||
|
||||
if a.ID != first.ExchangeNameID {
|
||||
t.Error("foreign key was wrong value", a.ID, first.ExchangeNameID)
|
||||
}
|
||||
if a.ID != second.ExchangeNameID {
|
||||
t.Error("foreign key was wrong value", a.ID, second.ExchangeNameID)
|
||||
}
|
||||
|
||||
if first.R.ExchangeName != &a {
|
||||
t.Error("relationship was not added properly to the foreign slice")
|
||||
}
|
||||
if second.R.ExchangeName != &a {
|
||||
t.Error("relationship was not added properly to the foreign slice")
|
||||
}
|
||||
|
||||
if a.R.ExchangeNameWithdrawalHistories[i*2] != first {
|
||||
t.Error("relationship struct slice not set to correct value")
|
||||
}
|
||||
if a.R.ExchangeNameWithdrawalHistories[i*2+1] != second {
|
||||
t.Error("relationship struct slice not set to correct value")
|
||||
}
|
||||
|
||||
count, err := a.ExchangeNameWithdrawalHistories().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := int64((i + 1) * 2); count != want {
|
||||
t.Error("want", want, "got", count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesReload(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err = o.Reload(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesReloadAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice := ExchangeSlice{o}
|
||||
|
||||
if err = slice.ReloadAll(ctx, tx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesSelect(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
slice, err := Exchanges().All(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(slice) != 1 {
|
||||
t.Error("want one record, got:", len(slice))
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
exchangeDBTypes = map[string]string{`ID`: `TEXT`, `Name`: `TEXT`}
|
||||
_ = bytes.MinRead
|
||||
)
|
||||
|
||||
func testExchangesUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if 0 == len(exchangePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with no primary key columns")
|
||||
}
|
||||
if len(exchangeAllColumns) == len(exchangePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with only primary key columns")
|
||||
}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangePrimaryKeyColumns...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
if rowsAff, err := o.Update(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("should only affect one row but affected", rowsAff)
|
||||
}
|
||||
}
|
||||
|
||||
func testExchangesSliceUpdateAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if len(exchangeAllColumns) == len(exchangePrimaryKeyColumns) {
|
||||
t.Skip("Skipping table with only primary key columns")
|
||||
}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
var err error
|
||||
o := &Exchange{}
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err = o.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
count, err := Exchanges().Count(ctx, tx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if count != 1 {
|
||||
t.Error("want one record, got:", count)
|
||||
}
|
||||
|
||||
if err = randomize.Struct(seed, o, exchangeDBTypes, true, exchangePrimaryKeyColumns...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
// Remove Primary keys and unique columns from what we plan to update
|
||||
var fields []string
|
||||
if strmangle.StringSliceMatch(exchangeAllColumns, exchangePrimaryKeyColumns) {
|
||||
fields = exchangeAllColumns
|
||||
} else {
|
||||
fields = strmangle.SetComplement(
|
||||
exchangeAllColumns,
|
||||
exchangePrimaryKeyColumns,
|
||||
)
|
||||
}
|
||||
|
||||
value := reflect.Indirect(reflect.ValueOf(o))
|
||||
typ := reflect.TypeOf(o).Elem()
|
||||
n := typ.NumField()
|
||||
|
||||
updateMap := M{}
|
||||
for _, col := range fields {
|
||||
for i := 0; i < n; i++ {
|
||||
f := typ.Field(i)
|
||||
if f.Tag.Get("boil") == col {
|
||||
updateMap[col] = value.Field(i).Interface()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slice := ExchangeSlice{o}
|
||||
if rowsAff, err := slice.UpdateAll(ctx, tx, updateMap); err != nil {
|
||||
t.Error(err)
|
||||
} else if rowsAff != 1 {
|
||||
t.Error("wanted one record updated but got", rowsAff)
|
||||
}
|
||||
}
|
||||
@@ -72,21 +72,6 @@ func (w whereHelpernull_String) GTE(x null.String) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.GTE, x)
|
||||
}
|
||||
|
||||
type whereHelperfloat64 struct{ field string }
|
||||
|
||||
func (w whereHelperfloat64) EQ(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) }
|
||||
func (w whereHelperfloat64) NEQ(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.NEQ, x)
|
||||
}
|
||||
func (w whereHelperfloat64) LT(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) }
|
||||
func (w whereHelperfloat64) LTE(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.LTE, x)
|
||||
}
|
||||
func (w whereHelperfloat64) GT(x float64) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) }
|
||||
func (w whereHelperfloat64) GTE(x float64) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.GTE, x)
|
||||
}
|
||||
|
||||
var WithdrawalCryptoWhere = struct {
|
||||
ID whereHelperint64
|
||||
Address whereHelperstring
|
||||
|
||||
@@ -109,8 +109,8 @@ type withdrawalFiatL struct{}
|
||||
|
||||
var (
|
||||
withdrawalFiatAllColumns = []string{"id", "bank_name", "bank_address", "bank_account_name", "bank_account_number", "bsb", "swift_code", "iban", "bank_code", "withdrawal_history_id"}
|
||||
withdrawalFiatColumnsWithoutDefault = []string{"bank_name", "bank_address", "bank_account_name", "bank_account_number", "bsb", "swift_code", "iban", "bank_code", "withdrawal_history_id"}
|
||||
withdrawalFiatColumnsWithDefault = []string{"id"}
|
||||
withdrawalFiatColumnsWithoutDefault = []string{"bank_name", "bank_address", "bank_account_name", "bank_account_number", "bank_code", "withdrawal_history_id"}
|
||||
withdrawalFiatColumnsWithDefault = []string{"id", "bsb", "swift_code", "iban"}
|
||||
withdrawalFiatPrimaryKeyColumns = []string{"id"}
|
||||
)
|
||||
|
||||
|
||||
@@ -23,82 +23,85 @@ import (
|
||||
|
||||
// WithdrawalHistory is an object representing the database table.
|
||||
type WithdrawalHistory struct {
|
||||
ID string `boil:"id" json:"id" toml:"id" yaml:"id"`
|
||||
Exchange string `boil:"exchange" json:"exchange" toml:"exchange" yaml:"exchange"`
|
||||
ExchangeID string `boil:"exchange_id" json:"exchange_id" toml:"exchange_id" yaml:"exchange_id"`
|
||||
Status string `boil:"status" json:"status" toml:"status" yaml:"status"`
|
||||
Currency string `boil:"currency" json:"currency" toml:"currency" yaml:"currency"`
|
||||
Amount float64 `boil:"amount" json:"amount" toml:"amount" yaml:"amount"`
|
||||
Description null.String `boil:"description" json:"description,omitempty" toml:"description" yaml:"description,omitempty"`
|
||||
WithdrawType int64 `boil:"withdraw_type" json:"withdraw_type" toml:"withdraw_type" yaml:"withdraw_type"`
|
||||
CreatedAt string `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
|
||||
UpdatedAt string `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`
|
||||
ID string `boil:"id" json:"id" toml:"id" yaml:"id"`
|
||||
ExchangeNameID string `boil:"exchange_name_id" json:"exchange_name_id" toml:"exchange_name_id" yaml:"exchange_name_id"`
|
||||
ExchangeID string `boil:"exchange_id" json:"exchange_id" toml:"exchange_id" yaml:"exchange_id"`
|
||||
Status string `boil:"status" json:"status" toml:"status" yaml:"status"`
|
||||
Currency string `boil:"currency" json:"currency" toml:"currency" yaml:"currency"`
|
||||
Amount float64 `boil:"amount" json:"amount" toml:"amount" yaml:"amount"`
|
||||
Description null.String `boil:"description" json:"description,omitempty" toml:"description" yaml:"description,omitempty"`
|
||||
WithdrawType int64 `boil:"withdraw_type" json:"withdraw_type" toml:"withdraw_type" yaml:"withdraw_type"`
|
||||
CreatedAt string `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
|
||||
UpdatedAt string `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`
|
||||
|
||||
R *withdrawalHistoryR `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
L withdrawalHistoryL `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
var WithdrawalHistoryColumns = struct {
|
||||
ID string
|
||||
Exchange string
|
||||
ExchangeID string
|
||||
Status string
|
||||
Currency string
|
||||
Amount string
|
||||
Description string
|
||||
WithdrawType string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
ID string
|
||||
ExchangeNameID string
|
||||
ExchangeID string
|
||||
Status string
|
||||
Currency string
|
||||
Amount string
|
||||
Description string
|
||||
WithdrawType string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}{
|
||||
ID: "id",
|
||||
Exchange: "exchange",
|
||||
ExchangeID: "exchange_id",
|
||||
Status: "status",
|
||||
Currency: "currency",
|
||||
Amount: "amount",
|
||||
Description: "description",
|
||||
WithdrawType: "withdraw_type",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
ID: "id",
|
||||
ExchangeNameID: "exchange_name_id",
|
||||
ExchangeID: "exchange_id",
|
||||
Status: "status",
|
||||
Currency: "currency",
|
||||
Amount: "amount",
|
||||
Description: "description",
|
||||
WithdrawType: "withdraw_type",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
}
|
||||
|
||||
// Generated where
|
||||
|
||||
var WithdrawalHistoryWhere = struct {
|
||||
ID whereHelperstring
|
||||
Exchange whereHelperstring
|
||||
ExchangeID whereHelperstring
|
||||
Status whereHelperstring
|
||||
Currency whereHelperstring
|
||||
Amount whereHelperfloat64
|
||||
Description whereHelpernull_String
|
||||
WithdrawType whereHelperint64
|
||||
CreatedAt whereHelperstring
|
||||
UpdatedAt whereHelperstring
|
||||
ID whereHelperstring
|
||||
ExchangeNameID whereHelperstring
|
||||
ExchangeID whereHelperstring
|
||||
Status whereHelperstring
|
||||
Currency whereHelperstring
|
||||
Amount whereHelperfloat64
|
||||
Description whereHelpernull_String
|
||||
WithdrawType whereHelperint64
|
||||
CreatedAt whereHelperstring
|
||||
UpdatedAt whereHelperstring
|
||||
}{
|
||||
ID: whereHelperstring{field: "\"withdrawal_history\".\"id\""},
|
||||
Exchange: whereHelperstring{field: "\"withdrawal_history\".\"exchange\""},
|
||||
ExchangeID: whereHelperstring{field: "\"withdrawal_history\".\"exchange_id\""},
|
||||
Status: whereHelperstring{field: "\"withdrawal_history\".\"status\""},
|
||||
Currency: whereHelperstring{field: "\"withdrawal_history\".\"currency\""},
|
||||
Amount: whereHelperfloat64{field: "\"withdrawal_history\".\"amount\""},
|
||||
Description: whereHelpernull_String{field: "\"withdrawal_history\".\"description\""},
|
||||
WithdrawType: whereHelperint64{field: "\"withdrawal_history\".\"withdraw_type\""},
|
||||
CreatedAt: whereHelperstring{field: "\"withdrawal_history\".\"created_at\""},
|
||||
UpdatedAt: whereHelperstring{field: "\"withdrawal_history\".\"updated_at\""},
|
||||
ID: whereHelperstring{field: "\"withdrawal_history\".\"id\""},
|
||||
ExchangeNameID: whereHelperstring{field: "\"withdrawal_history\".\"exchange_name_id\""},
|
||||
ExchangeID: whereHelperstring{field: "\"withdrawal_history\".\"exchange_id\""},
|
||||
Status: whereHelperstring{field: "\"withdrawal_history\".\"status\""},
|
||||
Currency: whereHelperstring{field: "\"withdrawal_history\".\"currency\""},
|
||||
Amount: whereHelperfloat64{field: "\"withdrawal_history\".\"amount\""},
|
||||
Description: whereHelpernull_String{field: "\"withdrawal_history\".\"description\""},
|
||||
WithdrawType: whereHelperint64{field: "\"withdrawal_history\".\"withdraw_type\""},
|
||||
CreatedAt: whereHelperstring{field: "\"withdrawal_history\".\"created_at\""},
|
||||
UpdatedAt: whereHelperstring{field: "\"withdrawal_history\".\"updated_at\""},
|
||||
}
|
||||
|
||||
// WithdrawalHistoryRels is where relationship names are stored.
|
||||
var WithdrawalHistoryRels = struct {
|
||||
ExchangeName string
|
||||
WithdrawalCryptos string
|
||||
WithdrawalFiats string
|
||||
}{
|
||||
ExchangeName: "ExchangeName",
|
||||
WithdrawalCryptos: "WithdrawalCryptos",
|
||||
WithdrawalFiats: "WithdrawalFiats",
|
||||
}
|
||||
|
||||
// withdrawalHistoryR is where relationships are stored.
|
||||
type withdrawalHistoryR struct {
|
||||
ExchangeName *Exchange
|
||||
WithdrawalCryptos WithdrawalCryptoSlice
|
||||
WithdrawalFiats WithdrawalFiatSlice
|
||||
}
|
||||
@@ -112,8 +115,8 @@ func (*withdrawalHistoryR) NewStruct() *withdrawalHistoryR {
|
||||
type withdrawalHistoryL struct{}
|
||||
|
||||
var (
|
||||
withdrawalHistoryAllColumns = []string{"id", "exchange", "exchange_id", "status", "currency", "amount", "description", "withdraw_type", "created_at", "updated_at"}
|
||||
withdrawalHistoryColumnsWithoutDefault = []string{"id", "exchange", "exchange_id", "status", "currency", "amount", "description", "withdraw_type"}
|
||||
withdrawalHistoryAllColumns = []string{"id", "exchange_name_id", "exchange_id", "status", "currency", "amount", "description", "withdraw_type", "created_at", "updated_at"}
|
||||
withdrawalHistoryColumnsWithoutDefault = []string{"id", "exchange_name_id", "exchange_id", "status", "currency", "amount", "description", "withdraw_type"}
|
||||
withdrawalHistoryColumnsWithDefault = []string{"created_at", "updated_at"}
|
||||
withdrawalHistoryPrimaryKeyColumns = []string{"id"}
|
||||
)
|
||||
@@ -393,6 +396,20 @@ func (q withdrawalHistoryQuery) Exists(ctx context.Context, exec boil.ContextExe
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// ExchangeName pointed to by the foreign key.
|
||||
func (o *WithdrawalHistory) ExchangeName(mods ...qm.QueryMod) exchangeQuery {
|
||||
queryMods := []qm.QueryMod{
|
||||
qm.Where("\"id\" = ?", o.ExchangeNameID),
|
||||
}
|
||||
|
||||
queryMods = append(queryMods, mods...)
|
||||
|
||||
query := Exchanges(queryMods...)
|
||||
queries.SetFrom(query.Query, "\"exchange\"")
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
// WithdrawalCryptos retrieves all the withdrawal_crypto's WithdrawalCryptos with an executor.
|
||||
func (o *WithdrawalHistory) WithdrawalCryptos(mods ...qm.QueryMod) withdrawalCryptoQuery {
|
||||
var queryMods []qm.QueryMod
|
||||
@@ -435,6 +452,107 @@ func (o *WithdrawalHistory) WithdrawalFiats(mods ...qm.QueryMod) withdrawalFiatQ
|
||||
return query
|
||||
}
|
||||
|
||||
// LoadExchangeName allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects. This is for an N-1 relationship.
|
||||
func (withdrawalHistoryL) LoadExchangeName(ctx context.Context, e boil.ContextExecutor, singular bool, maybeWithdrawalHistory interface{}, mods queries.Applicator) error {
|
||||
var slice []*WithdrawalHistory
|
||||
var object *WithdrawalHistory
|
||||
|
||||
if singular {
|
||||
object = maybeWithdrawalHistory.(*WithdrawalHistory)
|
||||
} else {
|
||||
slice = *maybeWithdrawalHistory.(*[]*WithdrawalHistory)
|
||||
}
|
||||
|
||||
args := make([]interface{}, 0, 1)
|
||||
if singular {
|
||||
if object.R == nil {
|
||||
object.R = &withdrawalHistoryR{}
|
||||
}
|
||||
args = append(args, object.ExchangeNameID)
|
||||
|
||||
} else {
|
||||
Outer:
|
||||
for _, obj := range slice {
|
||||
if obj.R == nil {
|
||||
obj.R = &withdrawalHistoryR{}
|
||||
}
|
||||
|
||||
for _, a := range args {
|
||||
if a == obj.ExchangeNameID {
|
||||
continue Outer
|
||||
}
|
||||
}
|
||||
|
||||
args = append(args, obj.ExchangeNameID)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
query := NewQuery(qm.From(`exchange`), qm.WhereIn(`exchange.id in ?`, args...))
|
||||
if mods != nil {
|
||||
mods.Apply(query)
|
||||
}
|
||||
|
||||
results, err := query.QueryContext(ctx, e)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to eager load Exchange")
|
||||
}
|
||||
|
||||
var resultSlice []*Exchange
|
||||
if err = queries.Bind(results, &resultSlice); err != nil {
|
||||
return errors.Wrap(err, "failed to bind eager loaded slice Exchange")
|
||||
}
|
||||
|
||||
if err = results.Close(); err != nil {
|
||||
return errors.Wrap(err, "failed to close results of eager load for exchange")
|
||||
}
|
||||
if err = results.Err(); err != nil {
|
||||
return errors.Wrap(err, "error occurred during iteration of eager loaded relations for exchange")
|
||||
}
|
||||
|
||||
if len(withdrawalHistoryAfterSelectHooks) != 0 {
|
||||
for _, obj := range resultSlice {
|
||||
if err := obj.doAfterSelectHooks(ctx, e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(resultSlice) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if singular {
|
||||
foreign := resultSlice[0]
|
||||
object.R.ExchangeName = foreign
|
||||
if foreign.R == nil {
|
||||
foreign.R = &exchangeR{}
|
||||
}
|
||||
foreign.R.ExchangeNameWithdrawalHistories = append(foreign.R.ExchangeNameWithdrawalHistories, object)
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, local := range slice {
|
||||
for _, foreign := range resultSlice {
|
||||
if local.ExchangeNameID == foreign.ID {
|
||||
local.R.ExchangeName = foreign
|
||||
if foreign.R == nil {
|
||||
foreign.R = &exchangeR{}
|
||||
}
|
||||
foreign.R.ExchangeNameWithdrawalHistories = append(foreign.R.ExchangeNameWithdrawalHistories, local)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadWithdrawalCryptos allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects. This is for a 1-M or N-M relationship.
|
||||
func (withdrawalHistoryL) LoadWithdrawalCryptos(ctx context.Context, e boil.ContextExecutor, singular bool, maybeWithdrawalHistory interface{}, mods queries.Applicator) error {
|
||||
@@ -625,6 +743,53 @@ func (withdrawalHistoryL) LoadWithdrawalFiats(ctx context.Context, e boil.Contex
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetExchangeName of the withdrawalHistory to the related item.
|
||||
// Sets o.R.ExchangeName to related.
|
||||
// Adds o to related.R.ExchangeNameWithdrawalHistories.
|
||||
func (o *WithdrawalHistory) SetExchangeName(ctx context.Context, exec boil.ContextExecutor, insert bool, related *Exchange) error {
|
||||
var err error
|
||||
if insert {
|
||||
if err = related.Insert(ctx, exec, boil.Infer()); err != nil {
|
||||
return errors.Wrap(err, "failed to insert into foreign table")
|
||||
}
|
||||
}
|
||||
|
||||
updateQuery := fmt.Sprintf(
|
||||
"UPDATE \"withdrawal_history\" SET %s WHERE %s",
|
||||
strmangle.SetParamNames("\"", "\"", 0, []string{"exchange_name_id"}),
|
||||
strmangle.WhereClause("\"", "\"", 0, withdrawalHistoryPrimaryKeyColumns),
|
||||
)
|
||||
values := []interface{}{related.ID, o.ID}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, updateQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
}
|
||||
|
||||
if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil {
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
|
||||
o.ExchangeNameID = related.ID
|
||||
if o.R == nil {
|
||||
o.R = &withdrawalHistoryR{
|
||||
ExchangeName: related,
|
||||
}
|
||||
} else {
|
||||
o.R.ExchangeName = related
|
||||
}
|
||||
|
||||
if related.R == nil {
|
||||
related.R = &exchangeR{
|
||||
ExchangeNameWithdrawalHistories: WithdrawalHistorySlice{o},
|
||||
}
|
||||
} else {
|
||||
related.R.ExchangeNameWithdrawalHistories = append(related.R.ExchangeNameWithdrawalHistories, o)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddWithdrawalCryptos adds the given related objects to the existing relationships
|
||||
// of the withdrawal_history, optionally inserting them as new records.
|
||||
// Appends related to o.R.WithdrawalCryptos.
|
||||
|
||||
@@ -800,6 +800,114 @@ func testWithdrawalHistoryToManyAddOpWithdrawalFiats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func testWithdrawalHistoryToOneExchangeUsingExchangeName(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var local WithdrawalHistory
|
||||
var foreign Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err := randomize.Struct(seed, &local, withdrawalHistoryDBTypes, false, withdrawalHistoryColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize WithdrawalHistory struct: %s", err)
|
||||
}
|
||||
if err := randomize.Struct(seed, &foreign, exchangeDBTypes, false, exchangeColumnsWithDefault...); err != nil {
|
||||
t.Errorf("Unable to randomize Exchange struct: %s", err)
|
||||
}
|
||||
|
||||
if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
local.ExchangeNameID = foreign.ID
|
||||
if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check, err := local.ExchangeName().One(ctx, tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if check.ID != foreign.ID {
|
||||
t.Errorf("want: %v, got %v", foreign.ID, check.ID)
|
||||
}
|
||||
|
||||
slice := WithdrawalHistorySlice{&local}
|
||||
if err = local.L.LoadExchangeName(ctx, tx, false, (*[]*WithdrawalHistory)(&slice), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
|
||||
local.R.ExchangeName = nil
|
||||
if err = local.L.LoadExchangeName(ctx, tx, true, &local, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.ExchangeName == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
}
|
||||
|
||||
func testWithdrawalHistoryToOneSetOpExchangeUsingExchangeName(t *testing.T) {
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
tx := MustTx(boil.BeginTx(ctx, nil))
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var a WithdrawalHistory
|
||||
var b, c Exchange
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, withdrawalHistoryDBTypes, false, strmangle.SetComplement(withdrawalHistoryPrimaryKeyColumns, withdrawalHistoryColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &b, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &c, exchangeDBTypes, false, strmangle.SetComplement(exchangePrimaryKeyColumns, exchangeColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, x := range []*Exchange{&b, &c} {
|
||||
err = a.SetExchangeName(ctx, tx, i != 0, x)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if a.R.ExchangeName != x {
|
||||
t.Error("relationship struct not set to correct value")
|
||||
}
|
||||
|
||||
if x.R.ExchangeNameWithdrawalHistories[0] != &a {
|
||||
t.Error("failed to append to foreign relationship struct")
|
||||
}
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID)
|
||||
}
|
||||
|
||||
zero := reflect.Zero(reflect.TypeOf(a.ExchangeNameID))
|
||||
reflect.Indirect(reflect.ValueOf(&a.ExchangeNameID)).Set(zero)
|
||||
|
||||
if err = a.Reload(ctx, tx); err != nil {
|
||||
t.Fatal("failed to reload", err)
|
||||
}
|
||||
|
||||
if a.ExchangeNameID != x.ID {
|
||||
t.Error("foreign key was wrong value", a.ExchangeNameID, x.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testWithdrawalHistoriesReload(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -875,7 +983,7 @@ func testWithdrawalHistoriesSelect(t *testing.T) {
|
||||
}
|
||||
|
||||
var (
|
||||
withdrawalHistoryDBTypes = map[string]string{`ID`: `TEXT`, `Exchange`: `TEXT`, `ExchangeID`: `TEXT`, `Status`: `TEXT`, `Currency`: `TEXT`, `Amount`: `REAL`, `Description`: `TEXT`, `WithdrawType`: `INTEGER`, `CreatedAt`: `TIMESTAMP`, `UpdatedAt`: `TIMESTAMP`}
|
||||
withdrawalHistoryDBTypes = map[string]string{`ID`: `TEXT`, `ExchangeNameID`: `TEXT`, `ExchangeID`: `TEXT`, `Status`: `TEXT`, `Currency`: `TEXT`, `Amount`: `REAL`, `Description`: `TEXT`, `WithdrawType`: `INTEGER`, `CreatedAt`: `TIMESTAMP`, `UpdatedAt`: `TIMESTAMP`}
|
||||
_ = bytes.MinRead
|
||||
)
|
||||
|
||||
|
||||
@@ -56,10 +56,6 @@ func Event(id, msgtype, message string) {
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
log.Errorf(log.Global, "Event Transaction commit failed: %v", err)
|
||||
err = tx.Rollback()
|
||||
if err != nil {
|
||||
log.Errorf(log.Global, "Event Transaction rollback failed: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,16 +4,13 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
|
||||
"github.com/thrasher-corp/goose"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
@@ -26,7 +23,6 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
t := m.Run()
|
||||
|
||||
err = os.RemoveAll(testhelpers.TempDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to remove temp db file: %v", err)
|
||||
@@ -93,12 +89,6 @@ func TestAudit(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
path := filepath.Join("..", "..", "migrations")
|
||||
err = goose.Run("up", dbConn.SQL, repository.GetSQLDialect(), path, "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to run migrations %v", err)
|
||||
}
|
||||
|
||||
if test.runner != nil {
|
||||
test.runner(t)
|
||||
}
|
||||
|
||||
270
database/repository/candle/candle.go
Normal file
270
database/repository/candle/candle.go
Normal file
@@ -0,0 +1,270 @@
|
||||
package candle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
modelPSQL "github.com/thrasher-corp/gocryptotrader/database/models/postgres"
|
||||
modelSQLite "github.com/thrasher-corp/gocryptotrader/database/models/sqlite3"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
"github.com/thrasher-corp/sqlboiler/queries/qm"
|
||||
)
|
||||
|
||||
// Series returns candle data
|
||||
func Series(exchangeName, base, quote string, interval int64, asset string, start, end time.Time) (out Item, err error) {
|
||||
if exchangeName == "" || base == "" || quote == "" || asset == "" || interval <= 0 {
|
||||
return out, errInvalidInput
|
||||
}
|
||||
|
||||
queries := []qm.QueryMod{
|
||||
qm.Where("base = ?", strings.ToUpper(base)),
|
||||
qm.Where("quote = ?", strings.ToUpper(quote)),
|
||||
qm.Where("interval = ?", interval),
|
||||
qm.Where("asset = ?", asset),
|
||||
qm.Where("timestamp between ? and ?", start.UTC(), end.UTC()),
|
||||
}
|
||||
|
||||
exchangeUUID, errS := exchange.UUIDByName(exchangeName)
|
||||
if errS != nil {
|
||||
return out, errS
|
||||
}
|
||||
queries = append(queries, qm.Where("exchange_name_id = ?", exchangeUUID.String()))
|
||||
|
||||
if repository.GetSQLDialect() == database.DBSQLite3 {
|
||||
retCandle, errC := modelSQLite.Candles(queries...).All(context.Background(), database.DB.SQL)
|
||||
if errC != nil {
|
||||
return out, errC
|
||||
}
|
||||
for x := range retCandle {
|
||||
t, errT := time.Parse(time.RFC3339, retCandle[x].Timestamp)
|
||||
if errT != nil {
|
||||
return out, errT
|
||||
}
|
||||
out.Candles = append(out.Candles, Candle{
|
||||
Timestamp: t,
|
||||
Open: retCandle[x].Open,
|
||||
High: retCandle[x].High,
|
||||
Low: retCandle[x].Low,
|
||||
Close: retCandle[x].Close,
|
||||
Volume: retCandle[x].Volume,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
retCandle, errC := modelPSQL.Candles(queries...).All(context.Background(), database.DB.SQL)
|
||||
if errC != nil {
|
||||
return out, errC
|
||||
}
|
||||
|
||||
for x := range retCandle {
|
||||
out.Candles = append(out.Candles, Candle{
|
||||
Timestamp: retCandle[x].Timestamp,
|
||||
Open: retCandle[x].Open,
|
||||
High: retCandle[x].High,
|
||||
Low: retCandle[x].Low,
|
||||
Close: retCandle[x].Close,
|
||||
Volume: retCandle[x].Volume,
|
||||
})
|
||||
}
|
||||
}
|
||||
if len(out.Candles) < 1 {
|
||||
return out, fmt.Errorf(errNoCandleDataFound, exchangeName, base, quote, interval, asset)
|
||||
}
|
||||
|
||||
out.ExchangeID = exchangeName
|
||||
out.Interval = interval
|
||||
out.Base = base
|
||||
out.Quote = quote
|
||||
out.Asset = asset
|
||||
return out, err
|
||||
}
|
||||
|
||||
// Insert series of candles
|
||||
func Insert(in *Item) (uint64, error) {
|
||||
if database.DB.SQL == nil {
|
||||
return 0, database.ErrDatabaseSupportDisabled
|
||||
}
|
||||
|
||||
if len(in.Candles) < 1 {
|
||||
return 0, errNoCandleData
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx, err := database.DB.SQL.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var totalInserted uint64
|
||||
if repository.GetSQLDialect() == database.DBSQLite3 {
|
||||
totalInserted, err = insertSQLite(ctx, tx, in)
|
||||
} else {
|
||||
totalInserted, err = insertPostgresSQL(ctx, tx, in)
|
||||
}
|
||||
if err != nil {
|
||||
errRB := tx.Rollback()
|
||||
if errRB != nil {
|
||||
log.Errorln(log.DatabaseMgr, errRB)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return totalInserted, nil
|
||||
}
|
||||
|
||||
func insertSQLite(ctx context.Context, tx *sql.Tx, in *Item) (uint64, error) {
|
||||
var totalInserted uint64
|
||||
for x := range in.Candles {
|
||||
var tempCandle = modelSQLite.Candle{
|
||||
ExchangeNameID: in.ExchangeID,
|
||||
Base: strings.ToUpper(in.Base),
|
||||
Quote: strings.ToUpper(in.Quote),
|
||||
Interval: strconv.FormatInt(in.Interval, 10),
|
||||
Asset: in.Asset,
|
||||
Timestamp: in.Candles[x].Timestamp.UTC().Format(time.RFC3339),
|
||||
Open: in.Candles[x].Open,
|
||||
High: in.Candles[x].High,
|
||||
Low: in.Candles[x].Low,
|
||||
Close: in.Candles[x].Close,
|
||||
Volume: in.Candles[x].Volume,
|
||||
}
|
||||
tempUUID, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
tempCandle.ID = tempUUID.String()
|
||||
err = tempCandle.Insert(ctx, tx, boil.Infer())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if totalInserted < math.MaxUint64 {
|
||||
totalInserted++
|
||||
}
|
||||
}
|
||||
return totalInserted, nil
|
||||
}
|
||||
|
||||
func insertPostgresSQL(ctx context.Context, tx *sql.Tx, in *Item) (uint64, error) {
|
||||
var totalInserted uint64
|
||||
for x := range in.Candles {
|
||||
var tempCandle = modelPSQL.Candle{
|
||||
ExchangeNameID: in.ExchangeID,
|
||||
Base: strings.ToUpper(in.Base),
|
||||
Quote: strings.ToUpper(in.Quote),
|
||||
Interval: in.Interval,
|
||||
Asset: in.Asset,
|
||||
Timestamp: in.Candles[x].Timestamp,
|
||||
Open: in.Candles[x].Open,
|
||||
High: in.Candles[x].High,
|
||||
Low: in.Candles[x].Low,
|
||||
Close: in.Candles[x].Close,
|
||||
Volume: in.Candles[x].Volume,
|
||||
}
|
||||
err := tempCandle.Upsert(ctx, tx, true, []string{"timestamp", "exchange_name_id", "base", "quote", "interval", "asset"}, boil.Infer(), boil.Infer())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if totalInserted < math.MaxUint64 {
|
||||
totalInserted++
|
||||
}
|
||||
}
|
||||
return totalInserted, nil
|
||||
}
|
||||
|
||||
// InsertFromCSV load a CSV list of candle data and insert into database
|
||||
func InsertFromCSV(exchangeName, base, quote string, interval int64, asset, file string) (uint64, error) {
|
||||
csvFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = csvFile.Close()
|
||||
if err != nil {
|
||||
log.Errorln(log.Global, err)
|
||||
}
|
||||
}()
|
||||
|
||||
csvData := csv.NewReader(csvFile)
|
||||
|
||||
exchangeUUID, err := exchange.UUIDByName(exchangeName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
tempCandle := &Item{
|
||||
ExchangeID: exchangeUUID.String(),
|
||||
Base: base,
|
||||
Quote: quote,
|
||||
Interval: interval,
|
||||
Asset: asset,
|
||||
}
|
||||
|
||||
for {
|
||||
row, errCSV := csvData.Read()
|
||||
if errCSV != nil {
|
||||
if errCSV == io.EOF {
|
||||
break
|
||||
}
|
||||
return 0, errCSV
|
||||
}
|
||||
|
||||
tempTick := Candle{}
|
||||
v, errParse := strconv.ParseInt(row[0], 10, 32)
|
||||
if errParse != nil {
|
||||
return 0, errParse
|
||||
}
|
||||
tempTick.Timestamp = time.Unix(v, 0).UTC()
|
||||
if tempTick.Timestamp.IsZero() {
|
||||
err = fmt.Errorf("invalid timestamp received on row %v", row)
|
||||
break
|
||||
}
|
||||
|
||||
tempTick.Volume, err = strconv.ParseFloat(row[1], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempTick.Open, err = strconv.ParseFloat(row[2], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempTick.High, err = strconv.ParseFloat(row[3], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempTick.Low, err = strconv.ParseFloat(row[4], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempTick.Close, err = strconv.ParseFloat(row[5], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
tempCandle.Candles = append(tempCandle.Candles, tempTick)
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return Insert(tempCandle)
|
||||
}
|
||||
306
database/repository/candle/candle_test.go
Normal file
306
database/repository/candle/candle_test.go
Normal file
@@ -0,0 +1,306 @@
|
||||
package candle
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
|
||||
)
|
||||
|
||||
var (
|
||||
verbose = false
|
||||
testExchanges = []exchange.Details{
|
||||
{
|
||||
Name: "one",
|
||||
},
|
||||
{
|
||||
Name: "two",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if verbose {
|
||||
testhelpers.EnableVerboseTestOutput()
|
||||
}
|
||||
|
||||
var err error
|
||||
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
|
||||
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create temp file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
t := m.Run()
|
||||
|
||||
err = os.RemoveAll(testhelpers.TempDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to remove temp db file: %v", err)
|
||||
}
|
||||
|
||||
os.Exit(t)
|
||||
}
|
||||
|
||||
func TestInsert(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func(includeOHLCVData bool) error
|
||||
runner func(t *testing.T)
|
||||
closer func(dbConn *database.Instance) error
|
||||
}{
|
||||
{
|
||||
name: "postgresql",
|
||||
config: testhelpers.PostgresTestDatabase,
|
||||
seedDB: seedDB,
|
||||
},
|
||||
{
|
||||
name: "SQLite",
|
||||
config: &database.Config{
|
||||
Driver: database.DBSQLite3,
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
|
||||
},
|
||||
seedDB: seedDB,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB(false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
data, err := genOHCLVData()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := Insert(&data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if r != 365 {
|
||||
t.Fatalf("unexpected number inserted: %v", r)
|
||||
}
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertFromCSV(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func(includeOHLCVData bool) error
|
||||
runner func(t *testing.T)
|
||||
closer func(dbConn *database.Instance) error
|
||||
}{
|
||||
{
|
||||
name: "postgresql",
|
||||
config: testhelpers.PostgresTestDatabase,
|
||||
seedDB: seedDB,
|
||||
},
|
||||
{
|
||||
name: "SQLite",
|
||||
config: &database.Config{
|
||||
Driver: database.DBSQLite3,
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
|
||||
},
|
||||
seedDB: seedDB,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB(false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
exchange.ResetExchangeCache()
|
||||
testFile := filepath.Join("..", "..", "..", "testdata", "binance_BTCUSDT_24h_2019_01_01_2020_01_01.csv")
|
||||
count, err := InsertFromCSV(testExchanges[0].Name, "BTC", "USDT", 86400, "spot", testFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 365 {
|
||||
t.Fatalf("expected 365 results to be inserted received: %v", count)
|
||||
}
|
||||
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeries(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func(includeOHLCVData bool) error
|
||||
runner func(t *testing.T)
|
||||
closer func(dbConn *database.Instance) error
|
||||
}{
|
||||
{
|
||||
name: "postgresql",
|
||||
config: testhelpers.PostgresTestDatabase,
|
||||
seedDB: seedDB,
|
||||
},
|
||||
{
|
||||
name: "SQLite",
|
||||
config: &database.Config{
|
||||
Driver: database.DBSQLite3,
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
|
||||
},
|
||||
seedDB: seedDB,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB(true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
end := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
ret, err := Series(testExchanges[0].Name,
|
||||
"BTC", "USDT",
|
||||
86400, "spot",
|
||||
start, end)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(ret.Candles) != 365 {
|
||||
t.Errorf("unexpected number of results received: %v", len(ret.Candles))
|
||||
}
|
||||
|
||||
ret, err = Series("", "", "", 0, "", start, end)
|
||||
if err != nil {
|
||||
if !errors.Is(err, errInvalidInput) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
ret, err = Series(testExchanges[0].Name,
|
||||
"BTC", "MOON",
|
||||
864000, "spot",
|
||||
start, end)
|
||||
if err != nil {
|
||||
if !errors.Is(err, errInvalidInput) {
|
||||
if err.Error() != fmt.Errorf(errNoCandleDataFound, testExchanges[0].Name,
|
||||
"BTC", "MOON",
|
||||
"864000", "spot").Error() {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func seedDB(includeOHLCVData bool) error {
|
||||
err := exchange.InsertMany(testExchanges)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if includeOHLCVData {
|
||||
exchange.ResetExchangeCache()
|
||||
data, err := genOHCLVData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = Insert(&data)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func genOHCLVData() (out Item, err error) {
|
||||
exchangeUUID, err := exchange.UUIDByName(testExchanges[0].Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
out.ExchangeID = exchangeUUID.String()
|
||||
out.Base = currency.BTC.String()
|
||||
out.Quote = currency.USDT.String()
|
||||
out.Interval = 86400
|
||||
out.Asset = "spot"
|
||||
|
||||
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
for x := 0; x < 365; x++ {
|
||||
out.Candles = append(out.Candles, Candle{
|
||||
Timestamp: start.Add(time.Hour * 24 * time.Duration(x)),
|
||||
Open: 1000,
|
||||
High: 1000,
|
||||
Low: 1000,
|
||||
Close: 1000,
|
||||
Volume: 1000,
|
||||
})
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
36
database/repository/candle/candle_types.go
Normal file
36
database/repository/candle/candle_types.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package candle
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
errNoCandleDataFound = "no candle data found: %v %v %v %v %v"
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidInput = errors.New("exchange, base , quote, asset, interval, start & end cannot be empty")
|
||||
errNoCandleData = errors.New("no candle data provided")
|
||||
)
|
||||
|
||||
// Item generic candle holder for modelPSQL & modelSQLite
|
||||
type Item struct {
|
||||
ID string
|
||||
ExchangeID string
|
||||
Base string
|
||||
Quote string
|
||||
Interval int64
|
||||
Asset string
|
||||
Candles []Candle
|
||||
}
|
||||
|
||||
// Candle holds each interval
|
||||
type Candle struct {
|
||||
Timestamp time.Time
|
||||
Open float64
|
||||
High float64
|
||||
Low float64
|
||||
Close float64
|
||||
Volume float64
|
||||
}
|
||||
229
database/repository/exchange/exchange.go
Normal file
229
database/repository/exchange/exchange.go
Normal file
@@ -0,0 +1,229 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/common/cache"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
modelPSQL "github.com/thrasher-corp/gocryptotrader/database/models/postgres"
|
||||
modelSQLite "github.com/thrasher-corp/gocryptotrader/database/models/sqlite3"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
"github.com/thrasher-corp/sqlboiler/queries/qm"
|
||||
)
|
||||
|
||||
// One returns one exchange by Name
|
||||
func One(in string) (Details, error) {
|
||||
return one(in, "name")
|
||||
}
|
||||
|
||||
// OneByUUID returns one exchange by UUID
|
||||
func OneByUUID(in uuid.UUID) (Details, error) {
|
||||
return one(in.String(), "id")
|
||||
}
|
||||
|
||||
// one returns one exchange by clause
|
||||
func one(in, clause string) (out Details, err error) {
|
||||
if database.DB.SQL == nil {
|
||||
return out, database.ErrDatabaseSupportDisabled
|
||||
}
|
||||
|
||||
whereQM := qm.Where(clause+"= ?", in)
|
||||
if repository.GetSQLDialect() == database.DBSQLite3 {
|
||||
ret, errS := modelSQLite.Exchanges(whereQM).One(context.Background(), database.DB.SQL)
|
||||
if errS != nil {
|
||||
return out, errS
|
||||
}
|
||||
out.Name = ret.Name
|
||||
out.UUID, errS = uuid.FromString(ret.ID)
|
||||
if errS != nil {
|
||||
return out, errS
|
||||
}
|
||||
} else {
|
||||
ret, errS := modelPSQL.Exchanges(whereQM).One(context.Background(), database.DB.SQL)
|
||||
if errS != nil {
|
||||
return out, errS
|
||||
}
|
||||
out.Name = ret.Name
|
||||
out.UUID, errS = uuid.FromString(ret.ID)
|
||||
if errS != nil {
|
||||
return out, errS
|
||||
}
|
||||
}
|
||||
|
||||
return out, err
|
||||
}
|
||||
|
||||
// Insert writes a single entry into database
|
||||
func Insert(in Details) error {
|
||||
if database.DB.SQL == nil {
|
||||
return database.ErrDatabaseSupportDisabled
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx, err := database.DB.SQL.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if repository.GetSQLDialect() == database.DBSQLite3 {
|
||||
err = insertSQLite(ctx, tx, []Details{in})
|
||||
} else {
|
||||
err = insertPostgresql(ctx, tx, []Details{in})
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errRB := tx.Rollback()
|
||||
if errRB != nil {
|
||||
log.Errorln(log.DatabaseMgr, errRB)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertMany writes multiple entries into database
|
||||
func InsertMany(in []Details) error {
|
||||
if database.DB.SQL == nil {
|
||||
return database.ErrDatabaseSupportDisabled
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx, err := database.DB.SQL.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if repository.GetSQLDialect() == database.DBSQLite3 {
|
||||
err = insertSQLite(ctx, tx, in)
|
||||
} else {
|
||||
err = insertPostgresql(ctx, tx, in)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errRB := tx.Rollback()
|
||||
if errRB != nil {
|
||||
log.Errorln(log.DatabaseMgr, errRB)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
errRB := tx.Rollback()
|
||||
if errRB != nil {
|
||||
log.Errorln(log.DatabaseMgr, errRB)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertSQLite(ctx context.Context, tx *sql.Tx, in []Details) (err error) {
|
||||
for x := range in {
|
||||
tempUUID, errUUID := uuid.NewV4()
|
||||
if errUUID != nil {
|
||||
return errUUID
|
||||
}
|
||||
var tempInsert = modelSQLite.Exchange{
|
||||
Name: in[x].Name,
|
||||
ID: tempUUID.String(),
|
||||
}
|
||||
|
||||
err = tempInsert.Insert(ctx, tx, boil.Infer())
|
||||
if err != nil {
|
||||
errRB := tx.Rollback()
|
||||
if errRB != nil {
|
||||
log.Errorln(log.DatabaseMgr, errRB)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertPostgresql(ctx context.Context, tx *sql.Tx, in []Details) (err error) {
|
||||
for x := range in {
|
||||
var tempInsert = modelPSQL.Exchange{
|
||||
Name: in[x].Name,
|
||||
}
|
||||
|
||||
err = tempInsert.Upsert(ctx, tx, true, []string{"name"}, boil.Infer(), boil.Infer())
|
||||
if err != nil {
|
||||
errRB := tx.Rollback()
|
||||
if errRB != nil {
|
||||
log.Errorln(log.DatabaseMgr, errRB)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UUIDByName returns UUID of exchange
|
||||
func UUIDByName(exchange string) (uuid.UUID, error) {
|
||||
exchange = strings.ToLower(exchange)
|
||||
v := exchangeCache.Get(exchange)
|
||||
if v != nil {
|
||||
return v.(uuid.UUID), nil
|
||||
}
|
||||
ret, err := One(exchange)
|
||||
if err != nil {
|
||||
if err != sql.ErrNoRows {
|
||||
return uuid.UUID{}, err
|
||||
}
|
||||
return uuid.UUID{}, ErrNoExchangeFound
|
||||
}
|
||||
|
||||
exchangeCache.Add(exchange, ret.UUID)
|
||||
return ret.UUID, nil
|
||||
}
|
||||
|
||||
// ResetExchangeCache reinitialise cache to blank state used to clear cache for testing
|
||||
func ResetExchangeCache() {
|
||||
exchangeCache = cache.New(10)
|
||||
}
|
||||
|
||||
// LoadCSV loads & parses a CSV list of exchanges
|
||||
func LoadCSV(file string) (out []Details, err error) {
|
||||
csvFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = csvFile.Close()
|
||||
if err != nil {
|
||||
log.Errorln(log.Global, err)
|
||||
}
|
||||
}()
|
||||
|
||||
csvData := csv.NewReader(csvFile)
|
||||
for {
|
||||
row, errCSV := csvData.Read()
|
||||
if errCSV != nil {
|
||||
if errCSV == io.EOF {
|
||||
return out, err
|
||||
}
|
||||
return out, errCSV
|
||||
}
|
||||
|
||||
out = append(out, Details{
|
||||
Name: row[0],
|
||||
})
|
||||
}
|
||||
}
|
||||
184
database/repository/exchange/exchange_test.go
Normal file
184
database/repository/exchange/exchange_test.go
Normal file
@@ -0,0 +1,184 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
|
||||
)
|
||||
|
||||
var (
|
||||
verbose = false
|
||||
|
||||
testExchanges = []Details{
|
||||
{
|
||||
Name: "one",
|
||||
},
|
||||
{
|
||||
Name: "two",
|
||||
},
|
||||
{
|
||||
Name: "three",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if verbose {
|
||||
testhelpers.EnableVerboseTestOutput()
|
||||
}
|
||||
|
||||
var err error
|
||||
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
|
||||
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create temp file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
t := m.Run()
|
||||
|
||||
err = os.RemoveAll(testhelpers.TempDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to remove temp db file: %v", err)
|
||||
}
|
||||
|
||||
os.Exit(t)
|
||||
}
|
||||
|
||||
func TestInsertMany(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func() error
|
||||
runner func(t *testing.T)
|
||||
closer func(dbConn *database.Instance) error
|
||||
}{
|
||||
{
|
||||
name: "postgresql",
|
||||
config: testhelpers.PostgresTestDatabase,
|
||||
seedDB: seed,
|
||||
},
|
||||
{
|
||||
name: "SQLite",
|
||||
config: &database.Config{
|
||||
Driver: database.DBSQLite3,
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
|
||||
},
|
||||
seedDB: seed,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
err = InsertMany(testExchanges)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneAndOneByUUID(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func() error
|
||||
runner func(t *testing.T)
|
||||
closer func(dbConn *database.Instance) error
|
||||
}{
|
||||
{
|
||||
name: "postgresql",
|
||||
config: testhelpers.PostgresTestDatabase,
|
||||
seedDB: seed,
|
||||
},
|
||||
{
|
||||
name: "SQLite",
|
||||
config: &database.Config{
|
||||
Driver: database.DBSQLite3,
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
|
||||
},
|
||||
seedDB: seed,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
ret, err := One("one")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ret2, err := OneByUUID(ret.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if ret.Name != ret2.Name {
|
||||
t.Fatalf("unexpected value received: %v", ret2.Name)
|
||||
}
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func seed() error {
|
||||
return InsertMany(testExchanges)
|
||||
}
|
||||
|
||||
func TestLoadCSV(t *testing.T) {
|
||||
testData := filepath.Join("..", "..", "..", "testdata", "exchangelist.csv")
|
||||
_, err := LoadCSV(testData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
19
database/repository/exchange/exchange_types.go
Normal file
19
database/repository/exchange/exchange_types.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/common/cache"
|
||||
)
|
||||
|
||||
var (
|
||||
exchangeCache = cache.New(10)
|
||||
ErrNoExchangeFound = errors.New("exchange not found")
|
||||
)
|
||||
|
||||
// Details holds exchange information such as Name
|
||||
type Details struct {
|
||||
UUID uuid.UUID
|
||||
Name string
|
||||
}
|
||||
44
database/repository/repository_test.go
Normal file
44
database/repository/repository_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
)
|
||||
|
||||
func TestGetSQLDialect(t *testing.T) {
|
||||
testCases := []struct {
|
||||
driver string
|
||||
expectedReturn string
|
||||
}{
|
||||
{
|
||||
"postgresql",
|
||||
database.DBPostgreSQL,
|
||||
},
|
||||
{
|
||||
"sqlite",
|
||||
database.DBSQLite3,
|
||||
},
|
||||
{
|
||||
"sqlite3",
|
||||
database.DBSQLite3,
|
||||
},
|
||||
{
|
||||
"invalid",
|
||||
database.DBInvalidDriver,
|
||||
},
|
||||
}
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.driver, func(t *testing.T) {
|
||||
database.DB.Config = &database.Config{
|
||||
Driver: test.driver,
|
||||
}
|
||||
ret := GetSQLDialect()
|
||||
if ret != test.expectedReturn {
|
||||
t.Fatalf("unexpected return: %v", ret)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -118,9 +118,5 @@ func Event(id, name, path string, data null.Bytes, executionType, status string,
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "Event Transaction commit failed: %v", err)
|
||||
err = tx.Rollback()
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "Event Transaction rollback failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,19 +4,20 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
|
||||
"github.com/thrasher-corp/goose"
|
||||
"github.com/volatiletech/null"
|
||||
)
|
||||
|
||||
var (
|
||||
verbose = false
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
|
||||
@@ -26,6 +27,10 @@ func TestMain(m *testing.M) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if verbose {
|
||||
testhelpers.EnableVerboseTestOutput()
|
||||
}
|
||||
|
||||
t := m.Run()
|
||||
|
||||
err = os.RemoveAll(testhelpers.TempDir)
|
||||
@@ -63,8 +68,8 @@ func TestScript(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for _, tests := range testCases {
|
||||
test := tests
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
@@ -75,12 +80,6 @@ func TestScript(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
path := filepath.Join("..", "..", "migrations")
|
||||
err = goose.Run("up", dbConn.SQL, repository.GetSQLDialect(), path, "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to run migrations %v", err)
|
||||
}
|
||||
|
||||
if test.runner != nil {
|
||||
test.runner()
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
modelPSQL "github.com/thrasher-corp/gocryptotrader/database/models/postgres"
|
||||
modelSQLite "github.com/thrasher-corp/gocryptotrader/database/models/sqlite3"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
exchangeDB "github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
@@ -33,6 +34,13 @@ func Event(res *withdraw.Response) {
|
||||
ctx := context.Background()
|
||||
ctx = boil.SkipTimestamps(ctx)
|
||||
|
||||
exchangeUUID, err := exchangeDB.UUIDByName(res.Exchange.Name)
|
||||
if err != nil {
|
||||
log.Error(log.DatabaseMgr, err)
|
||||
return
|
||||
}
|
||||
|
||||
res.Exchange.Name = exchangeUUID.String()
|
||||
tx, err := database.DB.SQL.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "Event transaction being failed: %v", err)
|
||||
@@ -56,22 +64,18 @@ func Event(res *withdraw.Response) {
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "Event Transaction commit failed: %v", err)
|
||||
err = tx.Rollback()
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "Event Transaction rollback failed: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func addPSQLEvent(ctx context.Context, tx *sql.Tx, res *withdraw.Response) (err error) {
|
||||
var tempEvent = modelPSQL.WithdrawalHistory{
|
||||
Exchange: res.Exchange.Name,
|
||||
ExchangeID: res.Exchange.ID,
|
||||
Status: res.Exchange.Status,
|
||||
Currency: res.RequestDetails.Currency.String(),
|
||||
Amount: res.RequestDetails.Amount,
|
||||
WithdrawType: int(res.RequestDetails.Type),
|
||||
ExchangeNameID: res.Exchange.Name,
|
||||
ExchangeID: res.Exchange.ID,
|
||||
Status: res.Exchange.Status,
|
||||
Currency: res.RequestDetails.Currency.String(),
|
||||
Amount: res.RequestDetails.Amount,
|
||||
WithdrawType: int(res.RequestDetails.Type),
|
||||
}
|
||||
|
||||
if res.RequestDetails.Description != "" {
|
||||
@@ -146,13 +150,13 @@ func addSQLiteEvent(ctx context.Context, tx *sql.Tx, res *withdraw.Response) (er
|
||||
}
|
||||
|
||||
var tempEvent = modelSQLite.WithdrawalHistory{
|
||||
ID: newUUID.String(),
|
||||
Exchange: res.Exchange.Name,
|
||||
ExchangeID: res.Exchange.ID,
|
||||
Status: res.Exchange.Status,
|
||||
Currency: res.RequestDetails.Currency.String(),
|
||||
Amount: res.RequestDetails.Amount,
|
||||
WithdrawType: int64(res.RequestDetails.Type),
|
||||
ID: newUUID.String(),
|
||||
ExchangeNameID: res.Exchange.Name,
|
||||
ExchangeID: res.Exchange.ID,
|
||||
Status: res.Exchange.Status,
|
||||
Currency: res.RequestDetails.Currency.String(),
|
||||
Amount: res.RequestDetails.Amount,
|
||||
WithdrawType: int64(res.RequestDetails.Type),
|
||||
}
|
||||
|
||||
if res.RequestDetails.Description != "" {
|
||||
@@ -227,12 +231,22 @@ func GetEventByUUID(id string) (*withdraw.Response, error) {
|
||||
|
||||
// GetEventsByExchange returns all withdrawal requests by exchange
|
||||
func GetEventsByExchange(exchange string, limit int) ([]*withdraw.Response, error) {
|
||||
return getByColumns(generateWhereQuery([]string{"exchange"}, []string{exchange}, limit))
|
||||
exch, err := exchangeDB.UUIDByName(exchange)
|
||||
if err != nil {
|
||||
log.Error(log.DatabaseMgr, err)
|
||||
return nil, err
|
||||
}
|
||||
return getByColumns(generateWhereQuery([]string{"exchange_name_id"}, []string{exch.String()}, limit))
|
||||
}
|
||||
|
||||
// GetEventByExchangeID return requested withdraw information by Exchange ID
|
||||
func GetEventByExchangeID(exchange, id string) (*withdraw.Response, error) {
|
||||
resp, err := getByColumns(generateWhereQuery([]string{"exchange", "exchange_id"}, []string{exchange, id}, 1))
|
||||
exch, err := exchangeDB.UUIDByName(exchange)
|
||||
if err != nil {
|
||||
log.Error(log.DatabaseMgr, err)
|
||||
return nil, err
|
||||
}
|
||||
resp, err := getByColumns(generateWhereQuery([]string{"exchange_name_id", "exchange_id"}, []string{exch.String(), id}, 1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -245,7 +259,12 @@ func GetEventsByDate(exchange string, start, end time.Time, limit int) ([]*withd
|
||||
if exchange == "" {
|
||||
return getByColumns(betweenQuery)
|
||||
}
|
||||
return getByColumns(append(generateWhereQuery([]string{"exchange"}, []string{exchange}, 0), betweenQuery...))
|
||||
exch, err := exchangeDB.UUIDByName(exchange)
|
||||
if err != nil {
|
||||
log.Error(log.DatabaseMgr, err)
|
||||
return nil, err
|
||||
}
|
||||
return getByColumns(append(generateWhereQuery([]string{"exchange_name_id"}, []string{exch.String()}, 0), betweenQuery...))
|
||||
}
|
||||
|
||||
func generateWhereQuery(columns, id []string, limit int) []qm.QueryMod {
|
||||
@@ -284,7 +303,6 @@ func getByColumns(q []qm.QueryMod) ([]*withdraw.Response, error) {
|
||||
tempResp.ID = newUUID
|
||||
tempResp.Exchange = new(withdraw.ExchangeResponse)
|
||||
tempResp.Exchange.ID = v[x].ExchangeID
|
||||
tempResp.Exchange.Name = v[x].Exchange
|
||||
tempResp.Exchange.Status = v[x].Status
|
||||
tempResp.RequestDetails = new(withdraw.Request)
|
||||
tempResp.RequestDetails = &withdraw.Request{
|
||||
@@ -294,6 +312,19 @@ func getByColumns(q []qm.QueryMod) ([]*withdraw.Response, error) {
|
||||
Type: withdraw.RequestType(v[x].WithdrawType),
|
||||
}
|
||||
|
||||
exchangeName, err := v[x].ExchangeName().One(ctx, database.DB.SQL)
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "Unable to get exchange name")
|
||||
tempUUID, errUUID := uuid.FromString(v[x].ExchangeNameID)
|
||||
if errUUID != nil {
|
||||
log.Errorf(log.DatabaseMgr, "invalid exchange name UUID for record %v", v[x].ID)
|
||||
} else {
|
||||
tempResp.Exchange.UUID = tempUUID
|
||||
}
|
||||
} else {
|
||||
tempResp.Exchange.Name = exchangeName.Name
|
||||
}
|
||||
|
||||
createdAtTime, err := time.Parse(time.RFC3339, v[x].CreatedAt)
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "record: %v has an incorrect time format ( %v ) - defaulting to empty time: %v", tempResp.ID, v[x].CreatedAt, err)
|
||||
@@ -346,7 +377,6 @@ func getByColumns(q []qm.QueryMod) ([]*withdraw.Response, error) {
|
||||
tempResp.ID = newUUID
|
||||
tempResp.Exchange = new(withdraw.ExchangeResponse)
|
||||
tempResp.Exchange.ID = v[x].ExchangeID
|
||||
tempResp.Exchange.Name = v[x].Exchange
|
||||
tempResp.Exchange.Status = v[x].Status
|
||||
tempResp.RequestDetails = new(withdraw.Request)
|
||||
tempResp.RequestDetails = &withdraw.Request{
|
||||
@@ -358,6 +388,19 @@ func getByColumns(q []qm.QueryMod) ([]*withdraw.Response, error) {
|
||||
tempResp.CreatedAt = v[x].CreatedAt
|
||||
tempResp.UpdatedAt = v[x].UpdatedAt
|
||||
|
||||
exchangeName, err := v[x].ExchangeName().One(ctx, database.DB.SQL)
|
||||
if err != nil {
|
||||
log.Errorf(log.DatabaseMgr, "Unable to get exchange name")
|
||||
tempUUID, errUUID := uuid.FromString(v[x].ExchangeNameID)
|
||||
if errUUID != nil {
|
||||
log.Errorf(log.DatabaseMgr, "invalid exchange name UUID for record %v", v[x].ID)
|
||||
} else {
|
||||
tempResp.Exchange.UUID = tempUUID
|
||||
}
|
||||
} else {
|
||||
tempResp.Exchange.Name = exchangeName.Name
|
||||
}
|
||||
|
||||
if withdraw.RequestType(v[x].WithdrawType) == withdraw.Crypto {
|
||||
tempResp.RequestDetails.Crypto = new(withdraw.CryptoRequest)
|
||||
x, err := v[x].WithdrawalCryptoWithdrawalCryptos().One(ctx, database.DB.SQL)
|
||||
|
||||
@@ -6,22 +6,32 @@ import (
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
"github.com/thrasher-corp/goose"
|
||||
)
|
||||
|
||||
var (
|
||||
verbose = false
|
||||
testExchanges = []exchange.Details{
|
||||
{
|
||||
Name: "one",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if verbose {
|
||||
testhelpers.EnableVerboseTestOutput()
|
||||
}
|
||||
|
||||
var err error
|
||||
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
|
||||
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
|
||||
@@ -79,10 +89,9 @@ func TestWithdraw(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
path := filepath.Join("..", "..", "migrations")
|
||||
err = goose.Run("up", dbConn.SQL, repository.GetSQLDialect(), path, "")
|
||||
err = exchange.InsertMany(testExchanges)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to run migrations %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.runner != nil {
|
||||
@@ -99,45 +108,42 @@ func TestWithdraw(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func withdrawHelper(t *testing.T) {
|
||||
t.Helper()
|
||||
var wg sync.WaitGroup
|
||||
func seedWithdrawData() {
|
||||
for x := 0; x < 20; x++ {
|
||||
wg.Add(1)
|
||||
go func(x int) {
|
||||
defer wg.Done()
|
||||
test := fmt.Sprintf("test-%v", x)
|
||||
resp := &withdraw.Response{
|
||||
Exchange: &withdraw.ExchangeResponse{
|
||||
Name: test,
|
||||
ID: test,
|
||||
Status: test,
|
||||
},
|
||||
RequestDetails: &withdraw.Request{
|
||||
Exchange: test,
|
||||
Description: test,
|
||||
Amount: 1.0,
|
||||
},
|
||||
}
|
||||
rnd := rand.Intn(2)
|
||||
if rnd == 0 {
|
||||
resp.RequestDetails.Currency = currency.AUD
|
||||
resp.RequestDetails.Type = 1
|
||||
resp.RequestDetails.Fiat = new(withdraw.FiatRequest)
|
||||
resp.RequestDetails.Fiat.Bank = new(banking.Account)
|
||||
} else {
|
||||
resp.RequestDetails.Currency = currency.BTC
|
||||
resp.RequestDetails.Type = 0
|
||||
resp.RequestDetails.Crypto = new(withdraw.CryptoRequest)
|
||||
resp.RequestDetails.Crypto.Address = test
|
||||
resp.RequestDetails.Crypto.FeeAmount = 0
|
||||
resp.RequestDetails.Crypto.AddressTag = test
|
||||
}
|
||||
Event(resp)
|
||||
}(x)
|
||||
test := fmt.Sprintf("test-%v", x)
|
||||
resp := &withdraw.Response{
|
||||
Exchange: &withdraw.ExchangeResponse{
|
||||
Name: testExchanges[0].Name,
|
||||
ID: test,
|
||||
Status: test,
|
||||
},
|
||||
RequestDetails: &withdraw.Request{
|
||||
Exchange: testExchanges[0].Name,
|
||||
Description: test,
|
||||
Amount: 1.0,
|
||||
},
|
||||
}
|
||||
rnd := rand.Intn(2)
|
||||
if rnd == 0 {
|
||||
resp.RequestDetails.Currency = currency.AUD
|
||||
resp.RequestDetails.Type = 1
|
||||
resp.RequestDetails.Fiat = new(withdraw.FiatRequest)
|
||||
resp.RequestDetails.Fiat.Bank = new(banking.Account)
|
||||
} else {
|
||||
resp.RequestDetails.Currency = currency.BTC
|
||||
resp.RequestDetails.Type = 0
|
||||
resp.RequestDetails.Crypto = new(withdraw.CryptoRequest)
|
||||
resp.RequestDetails.Crypto.Address = test
|
||||
resp.RequestDetails.Crypto.FeeAmount = 0
|
||||
resp.RequestDetails.Crypto.AddressTag = test
|
||||
}
|
||||
exchange.ResetExchangeCache()
|
||||
Event(resp)
|
||||
}
|
||||
}
|
||||
func withdrawHelper(t *testing.T) {
|
||||
seedWithdrawData()
|
||||
|
||||
wg.Wait()
|
||||
_, err := GetEventByUUID(withdraw.DryRunID.String())
|
||||
if err != nil {
|
||||
if !errors.Is(err, ErrNoResults) {
|
||||
@@ -145,27 +151,31 @@ func withdrawHelper(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
v, err := GetEventsByExchange("test-1", 10)
|
||||
v, err := GetEventsByExchange(testExchanges[0].Name, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = GetEventByExchangeID("test-1", "test-1")
|
||||
if v[0].Exchange.Name != testExchanges[0].Name {
|
||||
t.Fatalf("expected name to be translated to valid string instead received: %v", v[0].Exchange.Name)
|
||||
}
|
||||
|
||||
_, err = GetEventByExchangeID(testExchanges[0].Name, "test-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
_, err = GetEventByUUID(v[0].ID.String())
|
||||
if err != nil {
|
||||
if !errors.Is(err, ErrNoResults) {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = GetEventsByDate("test-1", time.Now().UTC().Add(-time.Minute), time.Now().UTC(), 5)
|
||||
_, err = GetEventsByDate(testExchanges[0].Name, time.Now().UTC().Add(-time.Minute), time.Now().UTC(), 5)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package testhelpers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
psqlConn "github.com/thrasher-corp/gocryptotrader/database/drivers/postgres"
|
||||
sqliteConn "github.com/thrasher-corp/gocryptotrader/database/drivers/sqlite3"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
"github.com/thrasher-corp/goose"
|
||||
"github.com/thrasher-corp/sqlboiler/boil"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -15,6 +21,8 @@ var (
|
||||
TempDir string
|
||||
// PostgresTestDatabase postgresql database config details
|
||||
PostgresTestDatabase *database.Config
|
||||
// MigrationDir default folder for migration's
|
||||
MigrationDir = filepath.Join("..", "..", "migrations")
|
||||
)
|
||||
|
||||
// GetConnectionDetails returns connection details for CI or test db instances
|
||||
@@ -68,7 +76,6 @@ func GetConnectionDetails() *database.Config {
|
||||
// ConnectToDatabase opens connection to database and returns pointer to instance of database.DB
|
||||
func ConnectToDatabase(conn *database.Config) (dbConn *database.Instance, err error) {
|
||||
database.DB.Config = conn
|
||||
|
||||
if conn.Driver == database.DBPostgreSQL {
|
||||
dbConn, err = psqlConn.Connect()
|
||||
if err != nil {
|
||||
@@ -77,12 +84,16 @@ func ConnectToDatabase(conn *database.Config) (dbConn *database.Instance, err er
|
||||
} else if conn.Driver == database.DBSQLite3 || conn.Driver == database.DBSQLite {
|
||||
database.DB.DataPath = TempDir
|
||||
dbConn, err = sqliteConn.Connect()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
database.DB.Connected = true
|
||||
|
||||
err = migrateDB(database.DB.SQL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -98,3 +109,18 @@ func CloseDatabase(conn *database.Instance) (err error) {
|
||||
func CheckValidConfig(config *drivers.ConnectionDetails) bool {
|
||||
return !reflect.DeepEqual(drivers.ConnectionDetails{}, *config)
|
||||
}
|
||||
|
||||
func migrateDB(db *sql.DB) error {
|
||||
return goose.Run("up", db, repository.GetSQLDialect(), MigrationDir, "")
|
||||
}
|
||||
|
||||
// EnableVerboseTestOutput enables debug output for SQL queries
|
||||
func EnableVerboseTestOutput() {
|
||||
c := log.GenDefaultSettings()
|
||||
log.GlobalLogConfig = &c
|
||||
log.SetupGlobalLogger()
|
||||
|
||||
DBLogger := database.Logger{}
|
||||
boil.DebugMode = true
|
||||
boil.DebugWriter = DBLogger
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
@@ -19,6 +20,7 @@ func TestMain(m *testing.M) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
MigrationDir = filepath.Join("..", "migrations")
|
||||
t := m.Run()
|
||||
|
||||
err = os.RemoveAll(TempDir)
|
||||
@@ -63,8 +65,8 @@ func TestDatabaseConnect(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for _, tests := range testCases {
|
||||
test := tests
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
|
||||
@@ -58,6 +58,10 @@ type Candle struct {
|
||||
}
|
||||
```
|
||||
|
||||
### DBSeed helper
|
||||
|
||||
A helper tool [cmd/dbseed](../cmd/dbseed/README.md) has been created for assisting with candle data migration
|
||||
|
||||
## Exchange status
|
||||
| Exchange | Supported |
|
||||
|----------------|-------------|
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/database/models/postgres"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/models/sqlite3"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/audit"
|
||||
exchangeDB "github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
@@ -1607,47 +1608,58 @@ func (s *RPCServer) GetHistoricCandles(_ context.Context, req *gctrpc.GetHistori
|
||||
return nil, errors.New(errCurrencyPairUnset)
|
||||
}
|
||||
|
||||
exchange := GetExchangeByName(req.Exchange)
|
||||
if exchange == nil {
|
||||
return nil, errors.New("Exchange " + req.Exchange + " not found")
|
||||
}
|
||||
|
||||
var candles kline.Item
|
||||
var err error
|
||||
if req.ExRequest {
|
||||
candles, err = exchange.GetHistoricCandlesExtended(currency.Pair{
|
||||
Delimiter: req.Pair.Delimiter,
|
||||
Base: currency.NewCode(req.Pair.Base),
|
||||
Quote: currency.NewCode(req.Pair.Quote),
|
||||
},
|
||||
asset.Item(strings.ToLower(req.AssetType)),
|
||||
time.Unix(req.Start, 0),
|
||||
time.Unix(req.End, 0),
|
||||
kline.Interval(req.TimeInterval))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
candles, err = exchange.GetHistoricCandles(currency.Pair{
|
||||
Delimiter: req.Pair.Delimiter,
|
||||
Base: currency.NewCode(req.Pair.Base),
|
||||
Quote: currency.NewCode(req.Pair.Quote),
|
||||
},
|
||||
asset.Item(strings.ToLower(req.AssetType)),
|
||||
time.Unix(req.Start, 0),
|
||||
time.Unix(req.End, 0),
|
||||
kline.Interval(req.TimeInterval))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
resp := gctrpc.GetHistoricCandlesResponse{
|
||||
Exchange: exchange.GetName(),
|
||||
Interval: kline.Interval(req.TimeInterval).Short(),
|
||||
Pair: req.Pair,
|
||||
Start: req.Start,
|
||||
End: req.End,
|
||||
}
|
||||
|
||||
if req.UseDb {
|
||||
candles, err = kline.LoadFromDatabase(req.Exchange,
|
||||
currency.Pair{
|
||||
Delimiter: req.Pair.Delimiter,
|
||||
Base: currency.NewCode(req.Pair.Base),
|
||||
Quote: currency.NewCode(req.Pair.Quote),
|
||||
},
|
||||
asset.Item(strings.ToLower(req.AssetType)),
|
||||
kline.Interval(req.TimeInterval),
|
||||
time.Unix(req.Start, 0),
|
||||
time.Unix(req.End, 0),
|
||||
)
|
||||
} else {
|
||||
exchangeEngine := GetExchangeByName(req.Exchange)
|
||||
if exchangeEngine == nil {
|
||||
return nil, errors.New("Exchange " + req.Exchange + " not found")
|
||||
}
|
||||
if req.ExRequest {
|
||||
candles, err = exchangeEngine.GetHistoricCandlesExtended(currency.Pair{
|
||||
Delimiter: req.Pair.Delimiter,
|
||||
Base: currency.NewCode(req.Pair.Base),
|
||||
Quote: currency.NewCode(req.Pair.Quote),
|
||||
},
|
||||
asset.Item(strings.ToLower(req.AssetType)),
|
||||
time.Unix(req.Start, 0),
|
||||
time.Unix(req.End, 0),
|
||||
kline.Interval(req.TimeInterval))
|
||||
} else {
|
||||
candles, err = exchangeEngine.GetHistoricCandles(currency.Pair{
|
||||
Delimiter: req.Pair.Delimiter,
|
||||
Base: currency.NewCode(req.Pair.Base),
|
||||
Quote: currency.NewCode(req.Pair.Quote),
|
||||
},
|
||||
asset.Item(strings.ToLower(req.AssetType)),
|
||||
time.Unix(req.Start, 0),
|
||||
time.Unix(req.End, 0),
|
||||
kline.Interval(req.TimeInterval))
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Exchange = candles.Exchange
|
||||
for i := range candles.Candles {
|
||||
resp.Candle = append(resp.Candle, &gctrpc.Candle{
|
||||
Time: candles.Candles[i].Time.Unix(),
|
||||
@@ -1658,6 +1670,16 @@ func (s *RPCServer) GetHistoricCandles(_ context.Context, req *gctrpc.GetHistori
|
||||
Volume: candles.Candles[i].Volume,
|
||||
})
|
||||
}
|
||||
|
||||
if req.Sync && !req.UseDb {
|
||||
_, err = kline.StoreInDatabase(&candles)
|
||||
if err != nil {
|
||||
if errors.Is(err, exchangeDB.ErrNoExchangeFound) {
|
||||
return nil, errors.New("exchange was not found in database, you can seed existing data or insert a new exchange via the dbseed")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ func parseMultipleEvents(ret []*withdraw.Response) *gctrpc.WithdrawalEventsByExc
|
||||
Id: ret[x].ID.String(),
|
||||
Exchange: &gctrpc.WithdrawlExchangeEvent{
|
||||
Name: ret[x].Exchange.Name,
|
||||
Id: ret[x].Exchange.Name,
|
||||
Id: ret[x].Exchange.ID,
|
||||
Status: ret[x].Exchange.Status,
|
||||
},
|
||||
Request: &gctrpc.WithdrawalRequestEvent{
|
||||
|
||||
@@ -905,7 +905,7 @@ func TestExecutionTypeToOrderStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSDT")
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -923,7 +923,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSDT")
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -761,10 +761,8 @@ func (b *Binance) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (b *Binance) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
if kline.TotalCandlesPerInterval(start, end, interval) > b.Features.Enabled.Kline.ResultLimit {
|
||||
@@ -812,10 +810,8 @@ func (b *Binance) GetHistoricCandles(pair currency.Pair, a asset.Item, start, en
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (b *Binance) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
|
||||
@@ -1253,7 +1253,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("TBTCUSD")
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -821,10 +821,8 @@ func (b *Bitfinex) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (b *Bitfinex) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
if kline.TotalCandlesPerInterval(start, end, interval) > b.Features.Enabled.Kline.ResultLimit {
|
||||
@@ -866,10 +864,8 @@ func (b *Bitfinex) GetHistoricCandles(pair currency.Pair, a asset.Item, start, e
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (b *Bitfinex) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
|
||||
@@ -552,7 +552,7 @@ func TestGetCandleStick(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTC_KRW")
|
||||
currencyPair, err := currency.NewPairFromString("BTCKRW")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -564,7 +564,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTC_KRW")
|
||||
currencyPair, err := currency.NewPairFromString("BTCKRW")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -606,10 +606,8 @@ func (b *Bithumb) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (b *Bithumb) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
formattedPair, err := b.FormatExchangeCurrency(pair, a)
|
||||
|
||||
@@ -678,7 +678,7 @@ func TestBitstamp_OHLC(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBitstamp_GetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("btcusd")
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -692,7 +692,7 @@ func TestBitstamp_GetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBitstamp_GetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("btcusd")
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -674,10 +674,8 @@ func (b *Bitstamp) ValidateCredentials() error {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (b *Bitstamp) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
@@ -725,10 +723,8 @@ func (b *Bitstamp) GetHistoricCandles(pair currency.Pair, a asset.Item, start, e
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (b *Bitstamp) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package btcmarkets
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@@ -729,7 +728,7 @@ func TestBTCMarkets_GetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
_, err = b.GetHistoricCandles(p, asset.Spot, time.Now().Add(-time.Hour*24).UTC(), time.Now().UTC(), kline.FifteenMin)
|
||||
if err != nil {
|
||||
if !errors.As(err, &kline.ErrorKline{}) {
|
||||
if err.Error() != "interval not supported" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -800,10 +800,8 @@ func (b *BTCMarkets) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (b *BTCMarkets) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
if kline.TotalCandlesPerInterval(start, end, interval) > b.Features.Enabled.Kline.ResultLimit {
|
||||
@@ -870,10 +868,8 @@ func (b *BTCMarkets) GetHistoricCandles(pair currency.Pair, a asset.Item, start,
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (b *BTCMarkets) GetHistoricCandlesExtended(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !b.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := b.ValidateKline(p, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
|
||||
@@ -81,7 +81,7 @@ func TestGetTrades(t *testing.T) {
|
||||
func TestGetHistoricRatesGranularityCheck(t *testing.T) {
|
||||
end := time.Now()
|
||||
start := end.Add(-time.Hour * 2)
|
||||
p := currency.NewPair(currency.BTC, currency.USD)
|
||||
p := currency.NewPairWithDelimiter("BTC", "USD", "-")
|
||||
_, err := c.GetHistoricCandles(p, asset.Spot, start, end, kline.OneHour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -92,7 +92,7 @@ func TestCoinbasePro_GetHistoricCandlesExtended(t *testing.T) {
|
||||
start := time.Unix(1546300800, 0)
|
||||
end := time.Unix(1577836799, 0)
|
||||
|
||||
p := currency.NewPair(currency.BTC, currency.USD)
|
||||
p := currency.NewPairWithDelimiter("BTC", "USD", "-")
|
||||
_, err := c.GetHistoricCandlesExtended(p, asset.Spot, start, end, kline.OneDay)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -762,10 +762,8 @@ func checkInterval(i time.Duration) (int64, error) {
|
||||
// GetHistoricCandles returns a set of candle between two time periods for a
|
||||
// designated time period
|
||||
func (c *CoinbasePro) GetHistoricCandles(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !c.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := c.ValidateKline(p, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
if kline.TotalCandlesPerInterval(start, end, interval) > c.Features.Enabled.Kline.ResultLimit {
|
||||
@@ -814,10 +812,8 @@ func (c *CoinbasePro) GetHistoricCandles(p currency.Pair, a asset.Item, start, e
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (c *CoinbasePro) GetHistoricCandlesExtended(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !c.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := c.ValidateKline(p, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
|
||||
@@ -708,7 +708,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
currencyPairSwap, err := currency.NewPairFromString(swapTestPair)
|
||||
currencyPairSwap, err := currency.NewPairFromString(spotTestPair)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -776,10 +776,8 @@ func (c *Coinbene) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (c *Coinbene) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !c.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := c.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
formattedPair, err := c.FormatExchangeCurrency(pair, asset.PerpetualSwap)
|
||||
@@ -805,6 +803,7 @@ func (c *Coinbene) GetHistoricCandles(pair currency.Pair, a asset.Item, start, e
|
||||
Exchange: c.Name,
|
||||
Pair: pair,
|
||||
Interval: interval,
|
||||
Asset: a,
|
||||
}
|
||||
|
||||
for x := range candles.Data {
|
||||
|
||||
@@ -1063,7 +1063,7 @@ func (e *Base) AuthenticateWebsocket() error {
|
||||
}
|
||||
|
||||
// KlineIntervalEnabled returns if requested interval is enabled on exchange
|
||||
func (e *Base) KlineIntervalEnabled(in kline.Interval) bool {
|
||||
func (e *Base) klineIntervalEnabled(in kline.Interval) bool {
|
||||
return e.Features.Enabled.Kline.Intervals[in.Word()]
|
||||
}
|
||||
|
||||
@@ -1072,3 +1072,28 @@ func (e *Base) KlineIntervalEnabled(in kline.Interval) bool {
|
||||
func (e *Base) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
return strconv.FormatFloat(in.Duration().Seconds(), 'f', 0, 64)
|
||||
}
|
||||
|
||||
// ValidateKline confirms that the requested pair, asset & interval are supported and/or enabled by the requested exchange
|
||||
func (e *Base) ValidateKline(pair currency.Pair, a asset.Item, interval kline.Interval) error {
|
||||
var errorList []string
|
||||
var err kline.ErrorKline
|
||||
if e.CurrencyPairs.IsAssetEnabled(a) != nil {
|
||||
err.Asset = a
|
||||
errorList = append(errorList, "asset not enabled")
|
||||
} else if !e.CurrencyPairs.Pairs[a].Enabled.Contains(pair, true) {
|
||||
err.Pair = pair
|
||||
errorList = append(errorList, "pair not enabled")
|
||||
}
|
||||
|
||||
if !e.klineIntervalEnabled(interval) {
|
||||
err.Interval = interval
|
||||
errorList = append(errorList, "interval not supported")
|
||||
}
|
||||
|
||||
if len(errorList) > 0 {
|
||||
err.Err = errors.New(strings.Join(errorList, ","))
|
||||
return &err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1872,6 +1872,54 @@ func Test_FormatExchangeKlineInterval(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBase_ValidateKline(t *testing.T) {
|
||||
pairs := currency.Pairs{
|
||||
currency.Pair{Base: currency.BTC, Quote: currency.USDT},
|
||||
}
|
||||
|
||||
availablePairs := currency.Pairs{
|
||||
currency.Pair{Base: currency.BTC, Quote: currency.USDT},
|
||||
currency.Pair{Base: currency.BTC, Quote: currency.AUD},
|
||||
}
|
||||
|
||||
b := Base{
|
||||
Name: "TESTNAME",
|
||||
CurrencyPairs: currency.PairsManager{
|
||||
Pairs: map[asset.Item]*currency.PairStore{
|
||||
asset.Spot: {
|
||||
AssetEnabled: convert.BoolPtr(true),
|
||||
Enabled: pairs,
|
||||
Available: availablePairs,
|
||||
},
|
||||
},
|
||||
},
|
||||
Features: Features{
|
||||
Enabled: FeaturesEnabled{
|
||||
Kline: kline.ExchangeCapabilitiesEnabled{
|
||||
Intervals: map[string]bool{
|
||||
kline.OneMin.Word(): true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := b.ValidateKline(availablePairs[0], asset.Spot, kline.OneMin)
|
||||
if err != nil {
|
||||
t.Fatalf("expected validation to pass received error: %v", err)
|
||||
}
|
||||
|
||||
err = b.ValidateKline(availablePairs[1], asset.Spot, kline.OneYear)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation to fail")
|
||||
}
|
||||
|
||||
err = b.ValidateKline(availablePairs[1], asset.Index, kline.OneYear)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckTransientError(t *testing.T) {
|
||||
b := Base{}
|
||||
err := b.CheckTransientError(nil)
|
||||
@@ -1993,7 +2041,7 @@ func TestAuthenticateWebsocket(t *testing.T) {
|
||||
|
||||
func TestKlineIntervalEnabled(t *testing.T) {
|
||||
b := Base{}
|
||||
if b.KlineIntervalEnabled(kline.EightHour) {
|
||||
if b.klineIntervalEnabled(kline.EightHour) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -855,7 +855,7 @@ func TestGetFundingHistory(t *testing.T) {
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
t.Parallel()
|
||||
currencyPair, err := currency.NewPairFromString(spotPair)
|
||||
currencyPair, err := currency.NewPairFromString("BTC/USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -869,7 +869,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
t.Parallel()
|
||||
currencyPair, err := currency.NewPairFromString(spotPair)
|
||||
currencyPair, err := currency.NewPairFromString("BTC/USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -931,10 +931,8 @@ func (f *FTX) ValidateCredentials() error {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (f *FTX) GetHistoricCandles(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !f.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := f.ValidateKline(p, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
formattedPair, err := f.FormatExchangeCurrency(p, a)
|
||||
@@ -972,10 +970,8 @@ func (f *FTX) GetHistoricCandles(p currency.Pair, a asset.Item, start, end time.
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (f *FTX) GetHistoricCandlesExtended(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !f.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := f.ValidateKline(p, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
|
||||
@@ -733,10 +733,8 @@ func (g *Gateio) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (g *Gateio) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !g.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := g.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
hours := end.Sub(start).Hours()
|
||||
|
||||
@@ -76,7 +76,7 @@ func TestGetChartCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSD")
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSD")
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -700,10 +700,8 @@ func (h *HitBTC) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (h *HitBTC) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !h.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := h.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
formattedPair, err := h.FormatExchangeCurrency(pair, a)
|
||||
@@ -742,12 +740,9 @@ func (h *HitBTC) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (h *HitBTC) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !h.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := h.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
Exchange: h.Name,
|
||||
Pair: pair,
|
||||
|
||||
@@ -90,7 +90,7 @@ func TestGetSpotKline(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSDT")
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("BTCUSDT")
|
||||
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -988,10 +988,8 @@ func (h *HUOBI) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (h *HUOBI) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !h.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := h.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
formattedPair, err := h.FormatExchangeCurrency(pair, a)
|
||||
|
||||
@@ -104,7 +104,7 @@ func CreateKline(trades []order.TradeHistory, interval Interval, p currency.Pair
|
||||
return candles, nil
|
||||
}
|
||||
|
||||
// validatData checks for zero values on data and sorts before turning
|
||||
// validateData checks for zero values on data and sorts before turning
|
||||
// converting into OHLC
|
||||
func validateData(trades []order.TradeHistory) error {
|
||||
if len(trades) < 2 {
|
||||
@@ -281,7 +281,7 @@ func CalcDateRanges(start, end time.Time, interval Interval, limit uint32) (out
|
||||
}
|
||||
y++
|
||||
}
|
||||
if allDateIntervals != nil {
|
||||
if allDateIntervals != nil && lastNum+1 < len(allDateIntervals) {
|
||||
out = append(out, DateRange{
|
||||
Start: allDateIntervals[lastNum+1],
|
||||
End: allDateIntervals[len(allDateIntervals)-1],
|
||||
@@ -299,3 +299,10 @@ func (k *Item) SortCandlesByTimestamp(asc bool) {
|
||||
return k.Candles[i].Time.Before(k.Candles[j].Time)
|
||||
})
|
||||
}
|
||||
|
||||
// FormatDates converts all date to UTC time
|
||||
func (k *Item) FormatDates() {
|
||||
for x := range k.Candles {
|
||||
k.Candles[x].Time = k.Candles[x].Time.UTC()
|
||||
}
|
||||
}
|
||||
|
||||
155
exchanges/kline/kline_datastorage.go
Normal file
155
exchanges/kline/kline_datastorage.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package kline
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/candle"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// LoadFromDatabase returns Item from database seeded data
|
||||
func LoadFromDatabase(exchange string, pair currency.Pair, a asset.Item, interval Interval, start, end time.Time) (Item, error) {
|
||||
retCandle, err := candle.Series(exchange,
|
||||
pair.Base.String(), pair.Quote.String(),
|
||||
int64(interval.Duration().Seconds()), a.String(), start, end)
|
||||
if err != nil {
|
||||
return Item{}, err
|
||||
}
|
||||
|
||||
ret := Item{
|
||||
Exchange: exchange,
|
||||
Pair: pair,
|
||||
Interval: interval,
|
||||
}
|
||||
|
||||
for x := range retCandle.Candles {
|
||||
ret.Candles = append(ret.Candles, Candle{
|
||||
Time: retCandle.Candles[x].Timestamp,
|
||||
Open: retCandle.Candles[x].Open,
|
||||
High: retCandle.Candles[x].High,
|
||||
Low: retCandle.Candles[x].Low,
|
||||
Close: retCandle.Candles[x].Close,
|
||||
Volume: retCandle.Candles[x].Volume,
|
||||
})
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// StoreInDatabase returns Item from database seeded data
|
||||
func StoreInDatabase(in *Item) (uint64, error) {
|
||||
if in.Exchange == "" {
|
||||
return 0, errors.New("name cannot be blank")
|
||||
}
|
||||
|
||||
if (in.Pair == currency.Pair{}) {
|
||||
return 0, errors.New("currency pair cannot be empty")
|
||||
}
|
||||
|
||||
if in.Asset == "" {
|
||||
return 0, errors.New("asset cannot be blank")
|
||||
}
|
||||
|
||||
if len(in.Candles) < 1 {
|
||||
return 0, errors.New("candle data is empty")
|
||||
}
|
||||
|
||||
exchangeUUID, err := exchange.UUIDByName(in.Exchange)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
databaseCandles := candle.Item{
|
||||
ExchangeID: exchangeUUID.String(),
|
||||
Base: in.Pair.Base.Upper().String(),
|
||||
Quote: in.Pair.Quote.Upper().String(),
|
||||
Interval: int64(in.Interval.Duration().Seconds()),
|
||||
Asset: in.Asset.String(),
|
||||
}
|
||||
|
||||
for x := range in.Candles {
|
||||
databaseCandles.Candles = append(databaseCandles.Candles, candle.Candle{
|
||||
Timestamp: in.Candles[x].Time,
|
||||
Open: in.Candles[x].Open,
|
||||
High: in.Candles[x].High,
|
||||
Low: in.Candles[x].Low,
|
||||
Close: in.Candles[x].Close,
|
||||
Volume: in.Candles[x].Volume,
|
||||
})
|
||||
}
|
||||
return candle.Insert(&databaseCandles)
|
||||
}
|
||||
|
||||
// LoadFromGCTScriptCSV loads kline data from a CSV file
|
||||
func LoadFromGCTScriptCSV(file string) (out []Candle, errRet error) {
|
||||
csvFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = csvFile.Close()
|
||||
if err != nil {
|
||||
log.Errorln(log.Global, err)
|
||||
}
|
||||
}()
|
||||
|
||||
csvData := csv.NewReader(csvFile)
|
||||
|
||||
for {
|
||||
row, errCSV := csvData.Read()
|
||||
if errCSV != nil {
|
||||
if errCSV == io.EOF {
|
||||
break
|
||||
}
|
||||
return out, errCSV
|
||||
}
|
||||
|
||||
tempCandle := Candle{}
|
||||
v, errParse := strconv.ParseInt(row[0], 10, 32)
|
||||
if errParse != nil {
|
||||
err = errParse
|
||||
break
|
||||
}
|
||||
tempCandle.Time = time.Unix(v, 0).UTC()
|
||||
if tempCandle.Time.IsZero() {
|
||||
err = fmt.Errorf("invalid timestamp received on row %v", row)
|
||||
break
|
||||
}
|
||||
|
||||
tempCandle.Volume, err = strconv.ParseFloat(row[1], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempCandle.Open, err = strconv.ParseFloat(row[2], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempCandle.High, err = strconv.ParseFloat(row[3], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempCandle.Low, err = strconv.ParseFloat(row[4], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
tempCandle.Close, err = strconv.ParseFloat(row[5], 64)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
out = append(out, tempCandle)
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
@@ -1,17 +1,35 @@
|
||||
package kline
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/common/crypto"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/candle"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
var (
|
||||
verbose = false
|
||||
testExchanges = []exchange.Details{
|
||||
{
|
||||
Name: "one",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestValidateData(t *testing.T) {
|
||||
err := validateData(nil)
|
||||
if err == nil {
|
||||
@@ -86,7 +104,7 @@ func TestCreateKline(t *testing.T) {
|
||||
trades = append(trades, order.TradeHistory{
|
||||
Timestamp: time.Now().Add((time.Duration(rand.Intn(10)) * time.Minute) +
|
||||
(time.Duration(rand.Intn(10)) * time.Second)),
|
||||
TID: crypto.HexEncodeToString([]byte(string(i))),
|
||||
TID: crypto.HexEncodeToString([]byte(string(rune(i)))),
|
||||
Amount: float64(rand.Intn(20)) + 1,
|
||||
Price: 1000 + float64(rand.Intn(1000)),
|
||||
})
|
||||
@@ -237,14 +255,24 @@ func TestDurationToWord(t *testing.T) {
|
||||
func TestKlineErrors(t *testing.T) {
|
||||
v := ErrorKline{
|
||||
Interval: OneYear,
|
||||
Pair: currency.NewPair(currency.BTC, currency.AUD),
|
||||
Err: errors.New("hello world"),
|
||||
}
|
||||
|
||||
if v.Error() != "oneyear interval unsupported by exchange" {
|
||||
t.Fatal("unexpected error returned")
|
||||
if v.Interval != OneYear {
|
||||
t.Fatalf("expected OneYear received %v:", v.Interval)
|
||||
}
|
||||
|
||||
if v.Unwrap().Error() != "8760h0m0s interval unsupported by exchange" {
|
||||
t.Fatal("unexpected error returned")
|
||||
if v.Pair != currency.NewPair(currency.BTC, currency.AUD) {
|
||||
t.Fatalf("expected OneYear received %v:", v.Pair)
|
||||
}
|
||||
|
||||
if v.Error() != "hello world" {
|
||||
t.Fatal("expected error return received empty value")
|
||||
}
|
||||
|
||||
if v.Unwrap().Error() != "hello world" {
|
||||
t.Fatal("expected error return received empty value")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,7 +409,7 @@ func TestCalcDateRanges(t *testing.T) {
|
||||
|
||||
v = CalcDateRanges(time.Now(), time.Now().AddDate(0, 0, 1), OneDay, 100)
|
||||
if len(v) != 1 {
|
||||
t.Fatal("expected CalcDateRanges() with a Candle count lower than limit to return 1 result")
|
||||
t.Fatal("expected CalcDateRanges() with a Item count lower than limit to return 1 result")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -394,7 +422,7 @@ func TestItem_SortCandlesByTimestamp(t *testing.T) {
|
||||
}
|
||||
|
||||
for x := 0; x < 100; x++ {
|
||||
y := rand.Float64()
|
||||
y := rand.Float64() // nolint gosec: used for generating test data no need to import crypo/rand
|
||||
tempKline.Candles = append(tempKline.Candles,
|
||||
Candle{
|
||||
Time: time.Now().AddDate(0, 0, -x),
|
||||
@@ -416,3 +444,240 @@ func TestItem_SortCandlesByTimestamp(t *testing.T) {
|
||||
t.Fatal("expected kline.Candles to be in ascending order")
|
||||
}
|
||||
}
|
||||
|
||||
func setupTest(t *testing.T) {
|
||||
if verbose {
|
||||
testhelpers.EnableVerboseTestOutput()
|
||||
}
|
||||
|
||||
var err error
|
||||
testhelpers.MigrationDir = filepath.Join("..", "..", "database", "migrations")
|
||||
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
|
||||
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreInDatabase(t *testing.T) {
|
||||
setupTest(t)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func(bool) error
|
||||
runner func(t *testing.T)
|
||||
closer func(dbConn *database.Instance) error
|
||||
}{
|
||||
{
|
||||
name: "postgresql",
|
||||
config: testhelpers.PostgresTestDatabase,
|
||||
seedDB: seedDB,
|
||||
},
|
||||
{
|
||||
name: "SQLite",
|
||||
config: &database.Config{
|
||||
Driver: database.DBSQLite3,
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
|
||||
},
|
||||
seedDB: seedDB,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB(false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, ohlcvData, err := genOHCLVData()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := StoreInDatabase(&ohlcvData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if r != 365 {
|
||||
t.Fatalf("unexpected number inserted: %v", r)
|
||||
}
|
||||
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
err := os.RemoveAll(testhelpers.TempDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to remove temp db file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromDatabase(t *testing.T) {
|
||||
setupTest(t)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func(bool) error
|
||||
runner func(t *testing.T)
|
||||
closer func(dbConn *database.Instance) error
|
||||
}{
|
||||
{
|
||||
name: "postgresql",
|
||||
config: testhelpers.PostgresTestDatabase,
|
||||
seedDB: seedDB,
|
||||
},
|
||||
{
|
||||
name: "SQLite",
|
||||
config: &database.Config{
|
||||
Driver: database.DBSQLite3,
|
||||
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
|
||||
},
|
||||
seedDB: seedDB,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB(true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
p, err := currency.NewPairFromString("BTCUSDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
end := start.AddDate(1, 0, 0)
|
||||
ret, err := LoadFromDatabase(testExchanges[0].Name, p, asset.Spot, OneDay, start, end)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ret.Exchange != testExchanges[0].Name {
|
||||
t.Fatalf("uncorrect data returned: %v", ret.Exchange)
|
||||
}
|
||||
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
err := os.RemoveAll(testhelpers.TempDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to remove temp db file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: find a better way to handle this to remove duplication between candle test
|
||||
func seedDB(includeOHLCVData bool) error {
|
||||
err := exchange.InsertMany(testExchanges)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if includeOHLCVData {
|
||||
data, _, err := genOHCLVData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = candle.Insert(&data)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func genOHCLVData() (out candle.Item, outItem Item, err error) {
|
||||
exchangeUUID, err := exchange.UUIDByName(testExchanges[0].Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
out.ExchangeID = exchangeUUID.String()
|
||||
out.Base = currency.BTC.String()
|
||||
out.Quote = currency.USDT.String()
|
||||
out.Interval = 86400
|
||||
out.Asset = "spot"
|
||||
|
||||
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
for x := 0; x < 365; x++ {
|
||||
out.Candles = append(out.Candles, candle.Candle{
|
||||
Timestamp: start.Add(time.Hour * 24 * time.Duration(x)),
|
||||
Open: 1000,
|
||||
High: 1000,
|
||||
Low: 1000,
|
||||
Close: 1000,
|
||||
Volume: 1000,
|
||||
})
|
||||
}
|
||||
|
||||
outItem.Interval = OneDay
|
||||
outItem.Asset = asset.Spot
|
||||
outItem.Pair = currency.NewPair(currency.BTC, currency.USDT)
|
||||
outItem.Exchange = testExchanges[0].Name
|
||||
|
||||
for x := 0; x < 365; x++ {
|
||||
outItem.Candles = append(outItem.Candles, Candle{
|
||||
Time: start.Add(time.Hour * 24 * time.Duration(x)),
|
||||
Open: 1000,
|
||||
High: 1000,
|
||||
Low: 1000,
|
||||
Close: 1000,
|
||||
Volume: 1000,
|
||||
})
|
||||
}
|
||||
|
||||
return out, outItem, nil
|
||||
}
|
||||
|
||||
func TestLoadCSV(t *testing.T) {
|
||||
v, err := LoadFromGCTScriptCSV(filepath.Join("..", "..", "testdata", "binance_BTCUSDT_24h_2019_01_01_2020_01_01.csv"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if v[0].Time.UTC() != time.Unix(1546300800, 0).UTC() {
|
||||
t.Fatalf("unexpected value received: %v", v[0].Time)
|
||||
}
|
||||
|
||||
if v[269].Close != 8177.91 {
|
||||
t.Fatalf("unexpected value received: %v", v[269].Close)
|
||||
}
|
||||
|
||||
if v[364].Open != 7246 {
|
||||
t.Fatalf("unexpected value received: %v", v[364].Open)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package kline
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
@@ -34,8 +33,6 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
// ErrUnsupportedInterval locale for an unsupported interval
|
||||
ErrUnsupportedInterval = "%s interval unsupported by exchange"
|
||||
// ErrRequestExceedsExchangeLimits locale for exceeding rate limits message
|
||||
ErrRequestExceedsExchangeLimits = "requested data would exceed exchange limits please lower range or use GetHistoricCandlesEx"
|
||||
)
|
||||
@@ -76,17 +73,20 @@ type Interval time.Duration
|
||||
|
||||
// ErrorKline struct to hold kline interval errors
|
||||
type ErrorKline struct {
|
||||
Asset asset.Item
|
||||
Pair currency.Pair
|
||||
Interval Interval
|
||||
Err error
|
||||
}
|
||||
|
||||
// Error returns short interval unsupported message
|
||||
func (k ErrorKline) Error() string {
|
||||
return fmt.Sprintf(ErrUnsupportedInterval, k.Interval.Word())
|
||||
func (k *ErrorKline) Error() string {
|
||||
return k.Err.Error()
|
||||
}
|
||||
|
||||
// Unwrap returns interval unsupported message
|
||||
func (k *ErrorKline) Unwrap() error {
|
||||
return fmt.Errorf(ErrUnsupportedInterval, k.Interval)
|
||||
return k.Err
|
||||
}
|
||||
|
||||
// DateRange holds a start and end date for kline usage
|
||||
|
||||
@@ -1410,7 +1410,7 @@ func TestParseTime(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("XBTUSD")
|
||||
currencyPair, err := currency.NewPairFromString("XBT-USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1426,7 +1426,7 @@ func TestGetHistoricCandles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
currencyPair, err := currency.NewPairFromString("XBTUSD")
|
||||
currencyPair, err := currency.NewPairFromString("XBT-USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -863,10 +863,8 @@ func (k *Kraken) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (k *Kraken) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !k.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := k.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
@@ -909,12 +907,9 @@ func (k *Kraken) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (k *Kraken) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if !k.KlineIntervalEnabled(interval) {
|
||||
return kline.Item{}, kline.ErrorKline{
|
||||
Interval: interval,
|
||||
}
|
||||
if err := k.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
|
||||
ret := kline.Item{
|
||||
Exchange: k.Name,
|
||||
Pair: pair,
|
||||
|
||||
@@ -410,7 +410,7 @@ func TestGetOrderHistory(t *testing.T) {
|
||||
|
||||
func TestGetHistoricCandles(t *testing.T) {
|
||||
t.Parallel()
|
||||
pair, err := currency.NewPairFromString(testCurrencyPair)
|
||||
pair, err := currency.NewPairFromString("eth_btc")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -430,7 +430,7 @@ func TestGetHistoricCandlesExtended(t *testing.T) {
|
||||
|
||||
startTime := time.Now().Add(-time.Hour)
|
||||
end := time.Now()
|
||||
pair, err := currency.NewPairFromString(testCurrencyPair)
|
||||
pair, err := currency.NewPairFromString("eth_btc")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user