Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 1851ea1

Browse files
authored
Merge pull request #1829 from OpenBazaar/cointypefix
(#1810) Correctly set coinType db return only when CryptoListing
2 parents f0a381a + ee8d0aa commit 1851ea1

File tree

6 files changed

+122
-19
lines changed

6 files changed

+122
-19
lines changed

repo/db/cases.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ func (c *CasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByA
274274
thumbnail = contract.VendorListings[0].Item.Images[0].Tiny
275275
}
276276
}
277+
278+
if contract.VendorListings[0].Metadata != nil && contract.VendorListings[0].Metadata.ContractType != pb.Listing_Metadata_CRYPTOCURRENCY {
279+
coinType = ""
280+
}
277281
}
278282
if contract.BuyerOrder != nil {
279283
slug = contract.VendorListings[0].Slug

repo/db/cases_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,25 +1062,51 @@ func TestCasesDB_Put_PaymentCoin(t *testing.T) {
10621062

10631063
func TestCasesDB_Put_CoinType(t *testing.T) {
10641064
var (
1065-
testsCoins = []string{"TBTC", "TETH"}
1066-
contract = factory.NewContract()
1065+
contract = factory.NewContract()
1066+
testCoins = []struct {
1067+
coinType string
1068+
cryptoListing bool
1069+
}{
1070+
{
1071+
"",
1072+
true,
1073+
},
1074+
{
1075+
"TBTC",
1076+
true,
1077+
},
1078+
{
1079+
"TETH",
1080+
true,
1081+
},
1082+
{
1083+
"TBCH",
1084+
false,
1085+
},
1086+
}
10671087
)
10681088

1069-
for _, testCoin := range testsCoins {
1089+
for _, test := range testCoins {
10701090
var casesdb, teardown, err = buildNewCaseStore()
10711091
if err != nil {
10721092
t.Fatal(err)
10731093
}
10741094

10751095
//contract.VendorListings[0].Metadata.CoinType = testCoin
1076-
paymentCoin := repo.CurrencyCode(testCoin)
1096+
paymentCoin := repo.CurrencyCode(test.coinType)
1097+
1098+
if test.cryptoListing {
1099+
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_CRYPTOCURRENCY
1100+
} else {
1101+
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_PHYSICAL_GOOD
1102+
}
10771103

10781104
err = casesdb.PutRecord(&repo.DisputeCaseRecord{
10791105
CaseID: "paymentCoinTest",
10801106
BuyerContract: contract,
10811107
VendorContract: contract,
10821108
IsBuyerInitiated: true,
1083-
CoinType: testCoin,
1109+
CoinType: test.coinType,
10841110
PaymentCoin: &paymentCoin,
10851111
})
10861112
if err != nil {
@@ -1098,8 +1124,10 @@ func TestCasesDB_Put_CoinType(t *testing.T) {
10981124
if count != 1 {
10991125
t.Errorf(`Expected %d record got %d`, 1, count)
11001126
}
1101-
if cases[0].CoinType != testCoin {
1102-
t.Errorf(`Expected %s got %s`, testCoin, cases[0].CoinType)
1127+
if test.cryptoListing && cases[0].CoinType != test.coinType {
1128+
t.Errorf(`Expected %s got %s`, test.coinType, cases[0].CoinType)
1129+
} else if !test.cryptoListing && cases[0].CoinType != "" {
1130+
t.Errorf(`Expected "" got %s`, cases[0].CoinType)
11031131
}
11041132
err = casesdb.Delete(cases[0].CaseId)
11051133
if err != nil {

repo/db/purchases.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ func (p *PurchasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sor
198198
moderated = true
199199
}
200200

201+
if len(rc.VendorListings) > 0 && rc.VendorListings[0].Metadata != nil && rc.VendorListings[0].Metadata.ContractType != pb.Listing_Metadata_CRYPTOCURRENCY {
202+
coinType = ""
203+
}
204+
201205
ret = append(ret, repo.Purchase{
202206
OrderId: orderID,
203207
Slug: slug,

repo/db/purchases_test.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -969,17 +969,45 @@ func TestPurchasesDB_Put_PaymentCoin(t *testing.T) {
969969
func TestPurchasesDB_Put_CoinType(t *testing.T) {
970970
var (
971971
contract = factory.NewContract()
972-
testsCoins = []string{"", "TBTC", "TETH"}
972+
testsCoins = []struct {
973+
coinType string
974+
cryptoListing bool
975+
}{
976+
{
977+
"",
978+
true,
979+
},
980+
{
981+
"TBTC",
982+
true,
983+
},
984+
{
985+
"TETH",
986+
true,
987+
},
988+
{
989+
"TBCH",
990+
false,
991+
},
992+
}
973993
)
974994

975-
for _, testCoin := range testsCoins {
995+
for _, test := range testsCoins {
976996
var purdb, teardown, err = buildNewPurchaseStore()
977997
if err != nil {
978998
t.Fatal(err)
979999
}
9801000

981-
contract.VendorListings[0].Metadata.CryptoCurrencyCode = testCoin
982-
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_CRYPTOCURRENCY
1001+
contract.VendorListings[0].Item.PriceCurrency = &pb.CurrencyDefinition{
1002+
Code: test.coinType,
1003+
Divisibility: 8,
1004+
}
1005+
if test.cryptoListing {
1006+
contract.VendorListings[0].Metadata.CryptoCurrencyCode = test.coinType
1007+
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_CRYPTOCURRENCY
1008+
} else {
1009+
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_PHYSICAL_GOOD
1010+
}
9831011

9841012
err = purdb.Put("orderID", *contract, 0, false)
9851013
if err != nil {
@@ -993,8 +1021,10 @@ func TestPurchasesDB_Put_CoinType(t *testing.T) {
9931021
if count != 1 {
9941022
t.Errorf(`Expected %d record got %d`, 1, count)
9951023
}
996-
if purchases[0].CoinType != testCoin {
997-
t.Errorf(`Expected %s got %s`, testCoin, purchases[0].CoinType)
1024+
if test.cryptoListing && purchases[0].CoinType != test.coinType {
1025+
t.Errorf(`Expected %s got %s`, test.coinType, purchases[0].CoinType)
1026+
} else if !test.cryptoListing && purchases[0].CoinType != "" {
1027+
t.Errorf(`Expected "" got %s`, purchases[0].CoinType)
9981028
}
9991029
teardown()
10001030
}

repo/db/sales.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ func (s *SalesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByA
197197
moderated = true
198198
}
199199

200+
if len(rc.VendorListings) > 0 && rc.VendorListings[0].Metadata != nil && rc.VendorListings[0].Metadata.ContractType != pb.Listing_Metadata_CRYPTOCURRENCY {
201+
coinType = ""
202+
}
203+
200204
ret = append(ret, repo.Sale{
201205
OrderId: orderID,
202206
Slug: slug,

repo/db/sales_test.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -771,16 +771,47 @@ func TestSalesDB_Put_PaymentCoin(t *testing.T) {
771771
}
772772

773773
func TestSalesDB_Put_CoinType(t *testing.T) {
774-
var contract = factory.NewContract()
774+
var (
775+
contract = factory.NewContract()
776+
testCoins = []struct {
777+
coinType string
778+
cryptoListing bool
779+
}{
780+
{
781+
"",
782+
true,
783+
},
784+
{
785+
"TBTC",
786+
true,
787+
},
788+
{
789+
"TETH",
790+
true,
791+
},
792+
{
793+
"TBCH",
794+
false,
795+
},
796+
}
797+
)
775798

776-
for _, testCoin := range []string{"", "TBTC", "TETH"} {
799+
for _, test := range testCoins {
777800
var saldb, teardown, err = buildNewSaleStore()
778801
if err != nil {
779802
t.Fatal(err)
780803
}
781804

782-
contract.VendorListings[0].Metadata.CryptoCurrencyCode = testCoin
783-
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_CRYPTOCURRENCY
805+
contract.VendorListings[0].Item.PriceCurrency = &pb.CurrencyDefinition{
806+
Code: test.coinType,
807+
Divisibility: 8,
808+
}
809+
if test.cryptoListing {
810+
contract.VendorListings[0].Metadata.CryptoCurrencyCode = test.coinType
811+
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_CRYPTOCURRENCY
812+
} else {
813+
contract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_PHYSICAL_GOOD
814+
}
784815

785816
err = saldb.Put("orderID", *contract, 0, false)
786817
if err != nil {
@@ -794,8 +825,10 @@ func TestSalesDB_Put_CoinType(t *testing.T) {
794825
if count != 1 {
795826
t.Errorf(`Expected %d record got %d`, 1, count)
796827
}
797-
if sales[0].CoinType != testCoin {
798-
t.Errorf(`Expected %s got %s`, testCoin, sales[0].CoinType)
828+
if test.cryptoListing && sales[0].CoinType != test.coinType {
829+
t.Errorf(`Expected %s got %s`, test.coinType, sales[0].CoinType)
830+
} else if !test.cryptoListing && sales[0].CoinType != "" {
831+
t.Errorf(`Expected "" got %s`, sales[0].CoinType)
799832
}
800833
teardown()
801834
}

0 commit comments

Comments
 (0)