diff options
author | Cypher <cypher@server.ky> | 2023-09-28 12:57:52 -0500 |
---|---|---|
committer | Cypher <cypher@server.ky> | 2023-09-28 12:57:52 -0500 |
commit | 32f71e7c0c30b4e4c96c8f817811dee42cf872a6 (patch) | |
tree | f86f23ee7500d0d7c23ecd4f9e39a1ff15a5ec2a | |
parent | 310e50d7be93eb7352121f6809b28b37c348dbd5 (diff) | |
download | librefund-32f71e7c0c30b4e4c96c8f817811dee42cf872a6.tar.xz |
Improve service feature tests
Make the feature tests easier to understand.
-rw-r--r-- | config.go | 12 | ||||
-rw-r--r-- | service/service_feature_test.go | 247 |
2 files changed, 102 insertions, 157 deletions
@@ -173,6 +173,13 @@ func ApplyFileConfig(cfg *Config, filePath string) error { return nil } +func LoadConfigFromFile(filePath string) Config { + var cfg Config + ApplyFileConfig(&cfg, filePath) + cfg.Apply(DefaultConfig) + return cfg +} + func LoadTestConfig() Config { cfg := Config{ TestPaymentGatewayEnable: BoolRef(true), @@ -189,10 +196,7 @@ func LoadTestConfig() Config { // LoadConfigFromEnv load configuration from the file defined by 'FUNDING_CONFIG'. func LoadConfigFromEnv() Config { - var cfg Config - ApplyFileConfig(&cfg, os.Getenv("FUNDING_CONFIG")) - cfg.Apply(DefaultConfig) - return cfg + return LoadConfigFromFile(os.Getenv("FUNDING_CONFIG")) } func SetBitcoinTestConfig(cfg *Config) { diff --git a/service/service_feature_test.go b/service/service_feature_test.go index 680b05f..5430113 100644 --- a/service/service_feature_test.go +++ b/service/service_feature_test.go @@ -3,8 +3,8 @@ package service import ( + "os" "os/exec" - "path" "testing" "git.server.ky/cypher/librefund" @@ -12,198 +12,139 @@ import ( "github.com/stretchr/testify/require" ) -// There is no clear way to do good feature tests, but it is also clear they -// are desired as a good stepping stone. -// -// The happy medium is to keep features tests small and have things -// maintainable. Until a solid foundation can be implemented. - -func configTempDirectory(t *testing.T, cfg *librefund.Config) { - projRoot := t.TempDir() - cfg.AgreementsFile = librefund.StringRef(path.Join(projRoot, "var", "agreements.json")) - cfg.TransactionFile = librefund.StringRef(path.Join(projRoot, "var", "transactions.json")) - cfg.ProjectDirectory = librefund.StringRef(path.Join(projRoot, "var", "project")) - - // CP command can be replaced with an API to add/remove entities. It - // is a long term objective. - err := exec.Command("cp", "--recursive", "../testdata/project_root/var", projRoot).Run() +func TestContributingAndCancellingAnObjective(t *testing.T) { + projectRoot := t.TempDir() + err := exec.Command("cp", "--recursive", "../testdata/service/.", projectRoot).Run() require.NoError(t, err, "copying project files") -} - -// isBetween is a transaction predicate to find transactions between accounts. -type isBetween struct { - from string - to string -} - -func (s isBetween) True(tr librefund.Transaction) bool { - return tr.From == s.from && tr.To == s.to -} - -func TestCancelObjective(t *testing.T) { - objectiveName := "documentation and examples" - projectName := "funding" - contributions := librefund.Monies{ - librefund.MustMoney("0.05", "KUDOS"), - librefund.MustMoney("0.05", "KUDOS"), - librefund.MustMoney("0.05", "KUDOS"), - librefund.MustMoney("0.05", "KUDOS"), - } - cfg := librefund.LoadTestConfig() - configTempDirectory(t, &cfg) + testDir, err := os.Getwd() + require.NoError(t, err) + err = os.Chdir(projectRoot) + require.NoError(t, err) + defer os.Chdir(testDir) + cfg := librefund.LoadConfigFromFile(ConfigPath) srv, err := NewService(cfg) require.NoError(t, err) - go func() { _ = srv.Start() }() defer srv.Stop() - var payments []string - for _, amount := range contributions { - obj, err := srv.GetObjective(projectName, objectiveName) - require.NoError(t, err) - - ctr, err := srv.Contribute( - projectName, - obj.Version, - nil, - ContributionReq{ - AgreeToTerms: true, - Amount: amount.AmountString(), - TransferMethod: payment.TestTransferMethod, - }, - ) - require.NoError(t, err, "contributing") - paymentID := string(ctr.TransactionID) - payments = append(payments, paymentID) - } - for _, p := range payments { - payment.RequirePaymentStatus(t, srv.testPG, p, payment.PaymentPaid) - } + projectName := "funding" + objectiveName := "documentation and examples" + obj, err := srv.GetObjective(projectName, objectiveName) + require.NoError(t, err) - err = srv.importPaymentsFrom(srv.testPG) + contributions := []*Contribution{} + contribution, err := srv.Contribute( + projectName, + obj.Version, + nil, + ContributionReq{ + AgreeToTerms: true, + Amount: "0.1", + TransferMethod: payment.TestTransferMethod, + }, + ) require.NoError(t, err) - obj, err := srv.GetObjective(projectName, objectiveName) - require.NoError(t, err, "objective should be open") - total, err := contributions.Sum() + contributions = append(contributions, contribution) + contribution, err = srv.Contribute( + projectName, + obj.Version, + nil, + ContributionReq{ + AgreeToTerms: true, + Amount: "0.10", + TransferMethod: payment.TestTransferMethod, + }, + ) require.NoError(t, err) - require.Equal(t, total.String(), obj.Funding.String(), "objective should show money") + contributions = append(contributions, contribution) + + // check contributions + err = srv.importPaymentsFrom(srv.testPG) + require.NoError(t, err) + + listedContributions, err := srv.GetProjectContributions(projectName, 100, 0) + require.NoError(t, err, "new contributions should be listed") + require.Len(t, listedContributions, len(contributions)) - err = srv.CancelObjective(projectName, objectiveName) + err = srv.CancelObjective(projectName, obj.Name) require.NoError(t, err) - for _, p := range payments { - payment.RequirePaymentStatus(t, srv.testPG, p, payment.PaymentRefunded) - } _, err = srv.GetObjective(projectName, objectiveName) require.Error(t, err, "objective should be closed") - - acc := librefund.NewObjectiveAccount(obj) - balance, err := srv.transactions.GetAccountBalance(acc) - require.NoError(t, err, "objective account") - require.Equal(t, - librefund.MustMoney("0", total.Currency().Code).String(), - balance.String(), - "objective account should be empty", - ) } -func TestClaimObjective(t *testing.T) { - objectiveName := "documentation and examples" - projectName := "funding" - contributions := librefund.Monies{ - librefund.MustMoney("0.05", "KUDOS"), - librefund.MustMoney("0.05", "KUDOS"), - librefund.MustMoney("0.05", "KUDOS"), - librefund.MustMoney("0.05", "KUDOS"), - } - refunded := librefund.Monies{ - librefund.MustMoney("0.02", "KUDOS"), - librefund.MustMoney("0.02", "KUDOS"), - librefund.MustMoney("0.02", "KUDOS"), - librefund.MustMoney("0.02", "KUDOS"), - } +func TestContributingAndClaimingAnObjective(t *testing.T) { + projectRoot := t.TempDir() + output, err := exec.Command("cp", "--recursive", "../testdata/service/.", projectRoot).CombinedOutput() + require.NoErrorf(t, err, "copying project files: %s", string(output)) - totalContributed, err := contributions.Sum() - require.NoError(t, err) - totalRefunded, err := refunded.Sum() + testDir, err := os.Getwd() require.NoError(t, err) - maintainerKepts, err := totalContributed.Subtract(totalRefunded) + err = os.Chdir(projectRoot) require.NoError(t, err) + defer os.Chdir(testDir) - cfg := librefund.LoadTestConfig() - configTempDirectory(t, &cfg) - + cfg := librefund.LoadConfigFromFile(ConfigPath) srv, err := NewService(cfg) require.NoError(t, err) - go func() { _ = srv.Start() }() defer srv.Stop() - var payments []string - for _, amount := range contributions { - obj, err := srv.GetObjective(projectName, objectiveName) - require.NoError(t, err) - - ctr, err := srv.Contribute( - projectName, - obj.Version, - nil, - ContributionReq{ - AgreeToTerms: true, - Amount: amount.AmountString(), - TransferMethod: payment.TestTransferMethod, - }, - ) - require.NoError(t, err, "contributing") - paymentID := string(ctr.TransactionID) - payments = append(payments, paymentID) - } - for _, p := range payments { - payment.RequirePaymentStatus(t, srv.testPG, p, payment.PaymentPaid) - } - err = srv.importPaymentsFrom(srv.testPG) + projectName := "funding" + objectiveName := "documentation and examples" + obj, err := srv.GetObjective(projectName, objectiveName) require.NoError(t, err) - obj, err := srv.GetObjective(projectName, objectiveName) - require.NoError(t, err, "objective should be open") - total, err := contributions.Sum() + contributions := []*Contribution{} + contribution, err := srv.Contribute( + projectName, + obj.Version, + nil, + ContributionReq{ + AgreeToTerms: true, + Amount: "0.1", + TransferMethod: payment.TestTransferMethod, + }, + ) + require.NoError(t, err) + + contributions = append(contributions, contribution) + contribution, err = srv.Contribute( + projectName, + obj.Version, + nil, + ContributionReq{ + AgreeToTerms: true, + Amount: "0.10", + TransferMethod: payment.TestTransferMethod, + }, + ) require.NoError(t, err) - require.Equal(t, total.String(), obj.Funding.String(), "objective should show money") + contributions = append(contributions, contribution) - err = srv.ClaimObjective(projectName, objectiveName) + // check contributions + err = srv.importPaymentsFrom(srv.testPG) require.NoError(t, err) - for i, id := range payments { - if refunded[i].Amount() == 0 { - continue - } - payment.RequirePaymentStatus(t, srv.testPG, id, payment.PaymentRefunded) - p, err := srv.testPG.GetPayment(id) - require.NoError(t, err) - require.Equal(t, refunded[i].String(), p.RefundAmount.String()) - } + listedContributions, err := srv.GetProjectContributions(projectName, 100, 0) + require.NoError(t, err, "new contributions should be listed") + require.Len(t, listedContributions, len(contributions)) + + err = srv.ClaimObjective(projectName, obj.Name) + require.NoError(t, err) _, err = srv.GetObjective(projectName, objectiveName) require.Error(t, err, "objective should be closed") - acc := librefund.NewObjectiveAccount(obj) - balance, err := srv.transactions.GetAccountBalance(acc) - require.NoError(t, err, "objective account") - require.Equal(t, - librefund.MustMoney("0", total.Currency().Code).String(), - balance.String(), - "objective account should be empty", - ) - - acc = librefund.NewMaintainerAccount(obj.Currency().Code) - balance, err = srv.transactions.GetAccountBalance(acc) - require.NoError(t, err, "maintainer account") - require.Equal(t, balance.String(), maintainerKepts.String(), "maintainer amount") + maintainerAccount := librefund.NewMaintainerAccount(obj.Currency().Code) + balance, err := srv.transactions.GetAccountBalance(maintainerAccount) + require.NoError(t, err) + require.Equal(t, balance.String(), "0.12 KUDOS", "maintainer keeps some funds") } |