diff --git a/go.sum b/go.sum index d99986f650..1ee36db41e 100644 --- a/go.sum +++ b/go.sum @@ -103,6 +103,8 @@ github.com/adlio/schema v1.1.13/go.mod h1:L5Z7tw+7lRK1Fnpi/LT/ooCP1elkXn0krMWBQH github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= +github.com/alecthomas/participle/v2 v2.0.0-alpha7/go.mod h1:NumScqsC42o9x+dGj8/YqsIfhrIQjFEOFovxotbBirA= +github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -191,6 +193,7 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20211130200136-a8f946100490/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E= github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw= +github.com/cockroachdb/apd/v3 v3.1.0/go.mod h1:6qgPBMXjATAdD/VefbRP9NoSLKjbB4LCoA7gN4LpHs4= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= github.com/coinbase/rosetta-sdk-go v0.7.0 h1:lmTO/JEpCvZgpbkOITL95rA80CPKb5CtMzLaqF2mCNg= @@ -243,6 +246,9 @@ github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKY github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/cucumber/gherkin-go/v19 v19.0.3/go.mod h1:jY/NP6jUtRSArQQJ5h1FXOUgk5fZK24qtE7vKi776Vw= +github.com/cucumber/messages-go/v16 v16.0.0/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4= github.com/danieljoos/wincred v1.0.2 h1:zf4bhty2iLuwgjgpraD2E9UbvO+fe54XXGJbOwe23fU= github.com/danieljoos/wincred v1.0.2/go.mod h1:SnuYRW9lp1oJrZX/dXJqr0cPK5gYXqx3EJbmjhLdK9U= @@ -360,6 +366,7 @@ github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/E github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/gateway v1.1.0 h1:u0SuhL9+Il+UbjM9VIE3ntfRujKbvVpFvNB4HbjeVQ0= github.com/gogo/gateway v1.1.0/go.mod h1:S7rR8FRQyG3QFESeSv4l2WnsyzlCLG0CzBbUUo/mbic= github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= @@ -834,6 +841,7 @@ github.com/regen-network/cosmos-proto v0.3.1 h1:rV7iM4SSFAagvy8RiyhiACbWEGotmqzy github.com/regen-network/cosmos-proto v0.3.1/go.mod h1:jO0sVX6a1B36nmE8C9xBFXpNwWejXC7QqCOnH3O0+YM= github.com/regen-network/cosmos-sdk v0.45.0-regen-1 h1:NCXi5HqcIg/dLW6n+niFJ5AmudgWxoODldrzW8fCWrg= github.com/regen-network/cosmos-sdk v0.45.0-regen-1/go.mod h1:3u9K1P67wuSfLNPtKF/E4y01/hb50xhqGYEtobkT56I= +github.com/regen-network/gocuke v0.6.0/go.mod h1:+i/R+pDBMLx1M7rL3fV7FC18gzyVTdGu3rNLUSOzHIo= github.com/regen-network/protobuf v1.3.3-alpha.regen.1 h1:OHEc+q5iIAXpqiqFKeLpu5NwTIkVXUs48vFMwzqpqY4= github.com/regen-network/protobuf v1.3.3-alpha.regen.1/go.mod h1:2DjTFR1HhMQhiWC5sZ4OhQ3+NtdbZ6oBDKQwq5Ou+FI= github.com/regen-network/regen-ledger/orm v1.0.0-beta1 h1:io3MMmqpIy3YCdkGmcsP+xAaq3JZi1/OAWk3ut+Th9s= diff --git a/x/ecocredit/features/basket/create.feature b/x/ecocredit/features/basket/create.feature index c5ecc11ee4..fb3713f25d 100644 --- a/x/ecocredit/features/basket/create.feature +++ b/x/ecocredit/features/basket/create.feature @@ -1,4 +1,4 @@ -Feature: Create Basket +Feature: Create Scenario: basket fee exceeds minimum basket fee When user tries to create a basket diff --git a/x/ecocredit/features/basket/put.feature b/x/ecocredit/features/basket/put.feature index 5c688642a5..aa0b3a3c57 100644 --- a/x/ecocredit/features/basket/put.feature +++ b/x/ecocredit/features/basket/put.feature @@ -1,4 +1,4 @@ -Feature: Put In Basket +Feature: Put Scenario: user provides a valid basket denom Given basket with denom eco.dC.Foo exists @@ -51,32 +51,3 @@ Feature: Put In Basket When user tries to put credits into a basket And user provides credits from a credit class that is not in the list of allowed credit classes Then credits are NOT put into the basket - - Scenario Outline: (block year - credit batch start date year) is more than years into the past - Given: a current block timestamp of - And: a basket with date criteria "years into the past" of - And: a user owns credits from a batch with start date - When: the user attempts to put the credits into the basket - Then: the credits are NOT put into the basket - - Examples: - | x | y | z | - | 2022-04-01 | 10 | 2011-01-01 | - | 2022-04-01 | 10 | 2011-04-01 | - | 2022-04-01 | 10 | 2011-07-01 | - - Scenario Outline: (block year - credit batch start date year) is less than or equal to years into the past - Given: a current block timestamp of - And: a basket with date criteria "years into the past" of - And: a user owns credits from a batch with start date - When: the user attempts to put the credits into the basket - Then: the credits are put into the basket - - Examples: - | x | y | z | - | 2022-04-01 | 10 | 2012-01-01 | - | 2022-04-01 | 10 | 2012-04-01 | - | 2022-04-01 | 10 | 2012-07-01 | - | 2022-04-01 | 10 | 2013-01-01 | - | 2022-04-01 | 10 | 2013-04-01 | - | 2022-04-01 | 10 | 2013-07-01 | diff --git a/x/ecocredit/features/basket/put_date.feature b/x/ecocredit/features/basket/put_date.feature new file mode 100644 index 0000000000..eb9e96acd5 --- /dev/null +++ b/x/ecocredit/features/basket/put_date.feature @@ -0,0 +1,30 @@ +Feature: Put Date + + Scenario Outline: batch start date is more than basket start date calculated from years into the past + Given a current block timestamp of + And a basket with date criteria years into the past of + And a user owns credits from a batch with start date of + When the user attempts to put the credits into the basket + Then the credits are NOT put into the basket + + Examples: + | x | y | z | + | "2022-04-01" | "10" | "2011-01-01" | + | "2022-04-01" | "10" | "2011-04-01" | + | "2022-04-01" | "10" | "2011-07-01" | + + Scenario Outline: batch start date is equal to or less than basket start date calculated from years into the past + Given a current block timestamp of + And a basket with date criteria years into the past of + And a user owns credits from a batch with start date of + When the user attempts to put the credits into the basket + Then the credits are put into the basket + + Examples: + | x | y | z | + | "2022-04-01" | "10" | "2012-01-01" | + | "2022-04-01" | "10" | "2012-04-01" | + | "2022-04-01" | "10" | "2012-07-01" | + | "2022-04-01" | "10" | "2013-01-01" | + | "2022-04-01" | "10" | "2013-04-01" | + | "2022-04-01" | "10" | "2013-07-01" | diff --git a/x/ecocredit/features/basket/take.feature b/x/ecocredit/features/basket/take.feature index 62b5e8d13e..2530e048cb 100644 --- a/x/ecocredit/features/basket/take.feature +++ b/x/ecocredit/features/basket/take.feature @@ -1,4 +1,4 @@ -Feature: Take From Basket +Feature: Take Scenario: user provides a valid basket denom Given basket with denom eco.dC.Foo exists diff --git a/x/ecocredit/go.mod b/x/ecocredit/go.mod index 3a34c8c53e..16172c8528 100644 --- a/x/ecocredit/go.mod +++ b/x/ecocredit/go.mod @@ -14,6 +14,7 @@ require ( github.com/gorilla/mux v1.8.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/pkg/errors v0.9.1 + github.com/regen-network/gocuke v0.6.0 github.com/regen-network/regen-ledger/api v0.8.0 github.com/regen-network/regen-ledger/orm v1.0.0-beta1 github.com/regen-network/regen-ledger/types v1.0.0 @@ -36,6 +37,7 @@ require ( github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect github.com/DataDog/zstd v1.4.5 // indirect github.com/Workiva/go-datastructures v1.0.53 // indirect + github.com/alecthomas/participle/v2 v2.0.0-alpha7 // indirect github.com/armon/go-metrics v0.3.9 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.0 // indirect @@ -43,6 +45,7 @@ require ( github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cockroachdb/apd/v2 v2.0.2 // indirect + github.com/cockroachdb/apd/v3 v3.1.0 // indirect github.com/coinbase/rosetta-sdk-go v0.7.0 // indirect github.com/confio/ics23/go v0.6.6 // indirect github.com/cosmos/btcutil v1.0.4 // indirect @@ -52,6 +55,8 @@ require ( github.com/cosmos/iavl v0.17.3 // indirect github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect github.com/cosmos/ledger-go v0.9.2 // indirect + github.com/cucumber/gherkin-go/v19 v19.0.3 // indirect + github.com/cucumber/messages-go/v16 v16.0.1 // indirect github.com/danieljoos/wincred v1.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect @@ -66,6 +71,7 @@ require ( github.com/go-kit/log v0.2.0 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect + github.com/gofrs/uuid v4.0.0+incompatible // indirect github.com/gogo/gateway v1.1.0 // indirect github.com/golang/snappy v0.0.3 // indirect github.com/google/btree v1.0.0 // indirect diff --git a/x/ecocredit/go.sum b/x/ecocredit/go.sum index 58d7fa152f..831edf7d4b 100644 --- a/x/ecocredit/go.sum +++ b/x/ecocredit/go.sum @@ -96,6 +96,10 @@ github.com/adlio/schema v1.1.13/go.mod h1:L5Z7tw+7lRK1Fnpi/LT/ooCP1elkXn0krMWBQH github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= +github.com/alecthomas/participle/v2 v2.0.0-alpha7 h1:cK4vjj0VSgb3lN1nuKA5F7dw+1s1pWBe5bx7nNCnN+c= +github.com/alecthomas/participle/v2 v2.0.0-alpha7/go.mod h1:NumScqsC42o9x+dGj8/YqsIfhrIQjFEOFovxotbBirA= +github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1 h1:GDQdwm/gAcJcLAKQQZGOJ4knlw+7rfEQQcmwTbt4p5E= +github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -186,6 +190,8 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E= github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw= +github.com/cockroachdb/apd/v3 v3.1.0 h1:MK3Ow7LH0W8zkd5GMKA1PvS9qG3bWFI95WaVNfyZJ/w= +github.com/cockroachdb/apd/v3 v3.1.0/go.mod h1:6qgPBMXjATAdD/VefbRP9NoSLKjbB4LCoA7gN4LpHs4= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= github.com/coinbase/rosetta-sdk-go v0.7.0 h1:lmTO/JEpCvZgpbkOITL95rA80CPKb5CtMzLaqF2mCNg= @@ -233,6 +239,11 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/cucumber/gherkin-go/v19 v19.0.3 h1:mMSKu1077ffLbTJULUfM5HPokgeBcIGboyeNUof1MdE= +github.com/cucumber/gherkin-go/v19 v19.0.3/go.mod h1:jY/NP6jUtRSArQQJ5h1FXOUgk5fZK24qtE7vKi776Vw= +github.com/cucumber/messages-go/v16 v16.0.0/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +github.com/cucumber/messages-go/v16 v16.0.1 h1:fvkpwsLgnIm0qugftrw2YwNlio+ABe2Iu94Ap8GMYIY= +github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4= github.com/danieljoos/wincred v1.0.2 h1:zf4bhty2iLuwgjgpraD2E9UbvO+fe54XXGJbOwe23fU= github.com/danieljoos/wincred v1.0.2/go.mod h1:SnuYRW9lp1oJrZX/dXJqr0cPK5gYXqx3EJbmjhLdK9U= @@ -358,6 +369,8 @@ github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/E github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= +github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/gateway v1.1.0 h1:u0SuhL9+Il+UbjM9VIE3ntfRujKbvVpFvNB4HbjeVQ0= github.com/gogo/gateway v1.1.0/go.mod h1:S7rR8FRQyG3QFESeSv4l2WnsyzlCLG0CzBbUUo/mbic= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= @@ -823,6 +836,8 @@ github.com/regen-network/cosmos-proto v0.3.1 h1:rV7iM4SSFAagvy8RiyhiACbWEGotmqzy github.com/regen-network/cosmos-proto v0.3.1/go.mod h1:jO0sVX6a1B36nmE8C9xBFXpNwWejXC7QqCOnH3O0+YM= github.com/regen-network/cosmos-sdk v0.45.0-regen-1 h1:NCXi5HqcIg/dLW6n+niFJ5AmudgWxoODldrzW8fCWrg= github.com/regen-network/cosmos-sdk v0.45.0-regen-1/go.mod h1:3u9K1P67wuSfLNPtKF/E4y01/hb50xhqGYEtobkT56I= +github.com/regen-network/gocuke v0.6.0 h1:33I/Omwnhav+1gRYJnb82AcYuPXz6/yhk3y//oEnxGw= +github.com/regen-network/gocuke v0.6.0/go.mod h1:+i/R+pDBMLx1M7rL3fV7FC18gzyVTdGu3rNLUSOzHIo= github.com/regen-network/protobuf v1.3.3-alpha.regen.1 h1:OHEc+q5iIAXpqiqFKeLpu5NwTIkVXUs48vFMwzqpqY4= github.com/regen-network/protobuf v1.3.3-alpha.regen.1/go.mod h1:2DjTFR1HhMQhiWC5sZ4OhQ3+NtdbZ6oBDKQwq5Ou+FI= github.com/regen-network/regen-ledger/orm v1.0.0-beta1 h1:io3MMmqpIy3YCdkGmcsP+xAaq3JZi1/OAWk3ut+Th9s= diff --git a/x/ecocredit/server/basket/keeper_test.go b/x/ecocredit/server/basket/keeper_test.go index 93948edece..935c406659 100644 --- a/x/ecocredit/server/basket/keeper_test.go +++ b/x/ecocredit/server/basket/keeper_test.go @@ -2,10 +2,9 @@ package basket_test import ( "context" - "testing" "github.com/golang/mock/gomock" - "github.com/stretchr/testify/require" + "github.com/regen-network/gocuke" "gotest.tools/v3/assert" "github.com/cosmos/cosmos-sdk/orm/model/ormdb" @@ -19,6 +18,7 @@ import ( dbm "github.com/tendermint/tm-db" api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" + ecocreditApi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" "github.com/regen-network/regen-ledger/x/ecocredit" mocks2 "github.com/regen-network/regen-ledger/x/ecocredit/mocks" "github.com/regen-network/regen-ledger/x/ecocredit/server/basket" @@ -26,13 +26,14 @@ import ( ) type baseSuite struct { - t *testing.T + t gocuke.TestingT db ormdb.ModuleDB - stateStore api.StateStore ctx context.Context k basket.Keeper ctrl *gomock.Controller addr sdk.AccAddress + stateStore api.StateStore + ecocreditStore ecocreditApi.StateStore bankKeeper *mocks2.MockBankKeeper ecocreditKeeper *mocks.MockEcocreditKeeper storeKey *sdk.KVStoreKey @@ -40,7 +41,7 @@ type baseSuite struct { distKeeper *mocks2.MockDistributionKeeper } -func setupBase(t *testing.T) *baseSuite { +func setupBase(t gocuke.TestingT) *baseSuite { // prepare database s := &baseSuite{t: t} var err error @@ -48,6 +49,8 @@ func setupBase(t *testing.T) *baseSuite { assert.NilError(t, err) s.stateStore, err = api.NewStateStore(s.db) assert.NilError(t, err) + s.ecocreditStore, err = ecocreditApi.NewStateStore(s.db) + assert.NilError(t, err) db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) @@ -70,9 +73,3 @@ func setupBase(t *testing.T) *baseSuite { return s } - -// this is an example of how we will unit test the basket functionality with mocks -func TestKeeperExample(t *testing.T) { - s := setupBase(t) - require.NotNil(t, s.k) -} diff --git a/x/ecocredit/server/basket/put_test.go b/x/ecocredit/server/basket/put_test.go index 8cc72094ed..0eb226755f 100644 --- a/x/ecocredit/server/basket/put_test.go +++ b/x/ecocredit/server/basket/put_test.go @@ -2,18 +2,23 @@ package basket_test import ( "context" + "strconv" "testing" "time" + "github.com/golang/mock/gomock" + "github.com/regen-network/gocuke" "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/timestamppb" + "gotest.tools/v3/assert" "github.com/cosmos/cosmos-sdk/orm/types/ormerrors" "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" + ecocreditapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" "github.com/regen-network/regen-ledger/types/math" "github.com/regen-network/regen-ledger/x/ecocredit" "github.com/regen-network/regen-ledger/x/ecocredit/basket" @@ -429,3 +434,140 @@ func assertUserSentCredits(t *testing.T, oldBalance math.Dec, amountSent string, require.True(t, checkBalance.Equal(oldBalance)) } + +type putSuite struct { + *baseSuite + basketDenom string + classId string + creditType string + batchDenom string + batchStartDate *timestamppb.Timestamp + tradableCredits string + err error +} + +func TestPutDate(t *testing.T) { + gocuke.NewRunner(t, &putSuite{}).Path("../../features/basket/put_date.feature").Run() +} + +func (s *putSuite) Before(t gocuke.TestingT) { + s.baseSuite = setupBase(t) + s.tradableCredits = "5" + s.classId = "C01" + s.creditType = "C" +} + +func (s *putSuite) ACurrentBlockTimestampOf(a string) { + blockTime, err := time.Parse("2006-01-02", a) + assert.NilError(s.t, err) + + s.sdkCtx = s.sdkCtx.WithBlockTime(blockTime) + s.ctx = sdk.WrapSDKContext(s.sdkCtx) +} + +func (s *putSuite) ABasketWithDateCriteriaYearsIntoThePastOf(a string) { + yearsInThePast, err := strconv.ParseUint(a, 10, 32) + assert.NilError(s.t, err) + + s.basketDenom = "basket-" + a + + basketId, err := s.stateStore.BasketTable().InsertReturningID(s.ctx, &api.Basket{ + BasketDenom: s.basketDenom, + CreditTypeAbbrev: s.creditType, + DateCriteria: &api.DateCriteria{YearsInThePast: uint32(yearsInThePast)}, + }) + assert.NilError(s.t, err) + + err = s.stateStore.BasketClassTable().Insert(s.ctx, &api.BasketClass{ + BasketId: basketId, + ClassId: s.classId, + }) + assert.NilError(s.t, err) +} + +func (s *putSuite) AUserOwnsCreditsFromABatchWithStartDateOf(a string) { + startDate, err := time.Parse("2006-01-02", a) + assert.NilError(s.t, err) + + s.batchDenom = "batch-" + a + s.batchStartDate = timestamppb.New(startDate) + + err = s.ecocreditStore.BatchInfoTable().Insert(s.ctx, &ecocreditapi.BatchInfo{ + BatchDenom: s.batchDenom, + StartDate: s.batchStartDate, + }) + assert.NilError(s.t, err) + + // TODO: migrate from legacy tradable balance store to batch balance table + + //batch, err := s.ecocreditStore.BatchInfoTable().GetByBatchDenom(s.ctx, s.batchDenom) + //assert.NilError(s.t, err) + // + //err = s.ecocreditStore.BatchBalanceTable().Insert(s.ctx, &ecocreditapi.BatchBalance{ + // Address: s.addr, + // BatchId: batch.Id, + // Tradable: s.tradableCredits, + //}) + //assert.NilError(s.t, err) + + legacyStore := s.sdkCtx.KVStore(s.storeKey) + tradKey := ecocredit.TradableBalanceKey(s.addr, ecocredit.BatchDenomT(s.batchDenom)) + userFunds, err := math.NewDecFromString(s.tradableCredits) + require.NoError(s.t, err) + + ecocredit.SetDecimal(legacyStore, tradKey, userFunds) +} + +func (s *putSuite) TheUserAttemptsToPutTheCreditsIntoTheBasket() { + startDate := time.Unix(s.batchStartDate.Seconds, int64(s.batchStartDate.Nanos)) + + s.ecocreditKeeper.EXPECT(). + BatchInfo(s.ctx, &ecocredit.QueryBatchInfoRequest{BatchDenom: s.batchDenom}). + Return(&ecocredit.QueryBatchInfoResponse{Info: &ecocredit.BatchInfo{ + BatchDenom: s.batchDenom, + StartDate: &startDate, + }}, nil).AnyTimes() // only called if valid start date + + s.ecocreditKeeper.EXPECT(). + ProjectInfo(s.ctx, &ecocredit.QueryProjectInfoRequest{}). + Return(&ecocredit.QueryProjectInfoResponse{Info: &ecocredit.ProjectInfo{ + ClassId: s.classId, + }}, nil).AnyTimes() // only called if valid start date + + s.ecocreditKeeper.EXPECT(). + ClassInfo(s.ctx, &ecocredit.QueryClassInfoRequest{ClassId: s.classId}). + Return(&ecocredit.QueryClassInfoResponse{Info: &ecocredit.ClassInfo{ + CreditType: &ecocredit.CreditType{Abbreviation: s.creditType}, + }}, nil).AnyTimes() // only called if valid start date + + any := gomock.Any() + tokenInt, _ := sdk.NewIntFromString(s.tradableCredits) + tokenAmount := sdk.NewCoins(sdk.NewCoin(s.basketDenom, tokenInt)) + + s.bankKeeper.EXPECT(). + MintCoins(any, basket.BasketSubModuleName, tokenAmount). + Return(nil).AnyTimes() // only called if valid start date + + s.bankKeeper.EXPECT(). + SendCoinsFromModuleToAccount(any, basket.BasketSubModuleName, s.addr, tokenAmount). + Return(nil).AnyTimes() // only called if valid start date + + _, s.err = s.k.Put(s.ctx, &basket.MsgPut{ + Owner: s.addr.String(), + BasketDenom: s.basketDenom, + Credits: []*basket.BasketCredit{ + { + BatchDenom: s.batchDenom, + Amount: s.tradableCredits, + }, + }, + }) +} + +func (s *putSuite) TheCreditsArePutIntoTheBasket() { + assert.ErrorIs(s.t, s.err, nil) +} + +func (s *putSuite) TheCreditsAreNotPutIntoTheBasket() { + assert.ErrorContains(s.t, s.err, "cannot put a credit from a batch with start date") +}