[sales] Clean up tests

This commit is contained in:
Mark Spanbroek 2022-03-31 11:34:33 +02:00 committed by markspanbroek
parent 3810889fcb
commit 11f7b4b4d8
1 changed files with 20 additions and 36 deletions

View File

@ -7,12 +7,19 @@ import ./examples
suite "Sales": suite "Sales":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
let request = StorageRequest(duration: 60.u256, size: 100.u256, maxPrice:42.u256)
var sales: Sales var sales: Sales
var market: MockMarket var market: MockMarket
setup: setup:
market = MockMarket.new() market = MockMarket.new()
sales = Sales.new(market) sales = Sales.new(market)
sales.start()
teardown:
sales.stop()
test "has no availability initially": test "has no availability initially":
check sales.available.len == 0 check sales.available.len == 0
@ -27,7 +34,6 @@ suite "Sales":
check sales.available.contains(availability2) check sales.available.contains(availability2)
test "can remove available storage": test "can remove available storage":
let availability = Availability.example
sales.add(availability) sales.add(availability)
sales.remove(availability) sales.remove(availability)
check sales.available.len == 0 check sales.available.len == 0
@ -38,68 +44,46 @@ suite "Sales":
check availability1.id != availability2.id check availability1.id != availability2.id
test "offers available storage when matching request comes in": test "offers available storage when matching request comes in":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
sales.add(availability) sales.add(availability)
sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
discard await market.requestStorage(request) discard await market.requestStorage(request)
check market.offered.len == 1 check market.offered.len == 1
check market.offered[0].price == 42.u256 check market.offered[0].price == 42.u256
sales.stop()
test "ignores request when no matching storage is available": test "ignores request when no matching storage is available":
let availability = Availability.init(size=99, duration=60, minPrice=42.u256)
sales.add(availability) sales.add(availability)
sales.start() var tooBig = request
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256) tooBig.size = request.size + 1
discard await market.requestStorage(request) discard await market.requestStorage(tooBig)
check market.offered.len == 0 check market.offered.len == 0
sales.stop()
test "makes storage unavailable when offer is submitted": test "makes storage unavailable when offer is submitted":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
sales.add(availability) sales.add(availability)
sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
discard await market.requestStorage(request) discard await market.requestStorage(request)
check sales.available.len == 0 check sales.available.len == 0
sales.stop()
test "sets expiry time of offer": test "sets expiry time of offer":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
sales.add(availability) sales.add(availability)
sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
let now = getTime().toUnix().u256 let now = getTime().toUnix().u256
discard await market.requestStorage(request) discard await market.requestStorage(request)
check market.offered[0].expiry == now + sales.offerExpiryInterval check market.offered[0].expiry == now + sales.offerExpiryInterval
sales.stop()
test "call onSale when offer is selected": test "calls onSale when offer is selected":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256) var sold: StorageOffer
sales.add(availability)
var selectedOffer: StorageOffer
sales.onSale = proc(offer: StorageOffer) = sales.onSale = proc(offer: StorageOffer) =
selectedOffer = offer sold = offer
sales.start() sales.add(availability)
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
discard await market.requestStorage(request) discard await market.requestStorage(request)
let offer = market.offered[0] let offer = market.offered[0]
await market.selectOffer(offer.id) await market.selectOffer(offer.id)
check selectedOffer == offer check sold == offer
sales.stop()
test "does not call onSale when a different offer is selected": test "does not call onSale when a different offer is selected":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256) var didSell: bool
sales.add(availability)
var onSaleWasCalled: bool
sales.onSale = proc(offer: StorageOffer) = sales.onSale = proc(offer: StorageOffer) =
onSaleWasCalled = true didSell = true
sales.start() sales.add(availability)
var request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256) let request = await market.requestStorage(request)
request = await market.requestStorage(request)
var otherOffer = StorageOffer(requestId: request.id, price: 1.u256) var otherOffer = StorageOffer(requestId: request.id, price: 1.u256)
otherOffer = await market.offerStorage(otherOffer) otherOffer = await market.offerStorage(otherOffer)
await market.selectOffer(otherOffer.id) await market.selectOffer(otherOffer.id)
check not onSaleWasCalled check not didSell
sales.stop()