def setUp(self):
     ConstSettings.IAASettings.MARKET_TYPE = 2
     self.market1 = OneSidedMarket(name="test_market")
     self.market1.id = "id1"
     self.market2 = OneSidedMarket(name="test_market")
     self.market2.id = "id2"
     self.area = Area(name="test_area")
     self.area._markets.markets = {1: self.market1, 2: self.market2}
     self.publisher = AreaToMarketEventPublisher(self.area)
예제 #2
0
def test_market_acct_simple(market: OneSidedMarket):
    offer = market.offer(20, 10, 'A')
    market.accept_offer(offer, 'B')

    assert market.traded_energy['A'] == offer.energy
    assert market.traded_energy['B'] == -offer.energy
    assert market.bought_energy('A') == 0
    assert market.bought_energy('B') == offer.energy
    assert market.sold_energy('A') == offer.energy
    assert market.sold_energy('B') == 0
예제 #3
0
class MarketStateMachine(RuleBasedStateMachine):
    offers = Bundle('Offers')
    actors = Bundle('Actors')

    def __init__(self):
        self.market = OneSidedMarket(
            area=FakeArea(name='my_fake_house', transfer_fee_pct=0))
        super().__init__()

    @rule(target=actors,
          actor=st.text(min_size=1,
                        max_size=3,
                        alphabet=string.ascii_letters + string.digits))
    def new_actor(self, actor):
        return actor

    @rule(target=offers,
          seller=actors,
          energy=st.integers(min_value=1),
          price=st.integers())
    def offer(self, seller, energy, price):
        return self.market.offer(price, energy, seller)

    @rule(offer=offers, buyer=actors)
    def trade(self, offer, buyer):
        assume(offer.id in self.market.offers)
        self.market.accept_offer(offer, buyer)

    @precondition(lambda self: self.market.offers)
    @rule()
    def check_avg_offer_price(self):
        price = sum(o.price for o in self.market.offers.values())
        energy = sum(o.energy for o in self.market.offers.values())
        assert self.market.avg_offer_price == round(price / energy, 4)

    @precondition(lambda self: self.market.trades)
    @rule()
    def check_avg_trade_price(self):
        price = sum(t.offer.price for t in self.market.trades)
        energy = sum(t.offer.energy for t in self.market.trades)
        assert self.market.avg_trade_price == round(price / energy, 4)

    @precondition(lambda self: self.market.traded_energy)
    @rule()
    def check_acct(self):
        actor_sums = {}
        for t in self.market.trades:
            actor_sums = add_or_create_key(actor_sums, t.seller,
                                           t.offer.energy)
            actor_sums = subtract_or_create_key(actor_sums, t.buyer,
                                                t.offer.energy)
        for actor, sum_ in actor_sums.items():
            assert self.market.traded_energy[actor] == sum_
        assert sum(self.market.traded_energy.values()) == 0
예제 #4
0
파일: test_market.py 프로젝트: xg86/d3a
def test_market_issuance_acct_reverse(last_offer_size, traded_energy):
    market = OneSidedMarket(time_slot=DateTime.now())
    offer1 = market.offer(10, 20, 'A', 'A')
    offer2 = market.offer(10, 10, 'A', 'A')
    offer3 = market.offer(10, last_offer_size, 'D', 'D')

    market.accept_offer(offer1, 'B')
    market.accept_offer(offer2, 'C')
    market.accept_offer(offer3, 'A')
    assert market.traded_energy['A'] == traded_energy
예제 #5
0
def test_market_issuance_acct_reverse(last_offer_size, traded_energy):
    market = OneSidedMarket(bc=NonBlockchainInterface(str(uuid4())), time_slot=now())
    offer1 = market.offer(10, 20, "A", "A")
    offer2 = market.offer(10, 10, "A", "A")
    offer3 = market.offer(10, last_offer_size, "D", "D")

    market.accept_offer(offer1, "B")
    market.accept_offer(offer2, "C")
    market.accept_offer(offer3, "A")
    assert market.traded_energy["A"] == traded_energy
예제 #6
0
파일: test_market.py 프로젝트: xg86/d3a
def test_market_acct_simple(market=OneSidedMarket(time_slot=DateTime.now())):
    offer = market.offer(20, 10, 'A', 'A')
    market.accept_offer(offer, 'B')

    assert market.traded_energy['A'] == offer.energy
    assert market.traded_energy['B'] == -offer.energy
    assert market.bought_energy('A') == 0
    assert market.bought_energy('B') == offer.energy
    assert market.sold_energy('A') == offer.energy
    assert market.sold_energy('B') == 0
예제 #7
0
def test_market_acct_simple(market=OneSidedMarket(bc=NonBlockchainInterface(str(uuid4())),
                                                  time_slot=now())):
    offer = market.offer(20, 10, "A", "A")
    market.accept_offer(offer, "B")

    assert market.traded_energy["A"] == offer.energy
    assert market.traded_energy["B"] == -offer.energy
    assert market.bought_energy("A") == 0
    assert market.bought_energy("B") == offer.energy
    assert market.sold_energy("A") == offer.energy
    assert market.sold_energy("B") == 0
예제 #8
0
파일: test_market.py 프로젝트: xg86/d3a
def test_market_acct_multiple(market=OneSidedMarket(time_slot=DateTime.now())):
    offer1 = market.offer(10, 20, 'A', 'A')
    offer2 = market.offer(10, 10, 'A', 'A')
    market.accept_offer(offer1, 'B')
    market.accept_offer(offer2, 'C')

    assert market.traded_energy['A'] == offer1.energy + offer2.energy == 30
    assert market.traded_energy['B'] == -offer1.energy == -20
    assert market.traded_energy['C'] == -offer2.energy == -10
    assert market.bought_energy('A') == 0
    assert market.sold_energy('A') == offer1.energy + offer2.energy == 30
    assert market.bought_energy('B') == offer1.energy == 20
    assert market.bought_energy('C') == offer2.energy == 10
예제 #9
0
def test_market_acct_multiple(market=OneSidedMarket(bc=NonBlockchainInterface(str(uuid4())),
                                                    time_slot=now())):
    offer1 = market.offer(10, 20, "A", "A")
    offer2 = market.offer(10, 10, "A", "A")
    market.accept_offer(offer1, "B")
    market.accept_offer(offer2, "C")

    assert market.traded_energy["A"] == offer1.energy + offer2.energy == 30
    assert market.traded_energy["B"] == -offer1.energy == -20
    assert market.traded_energy["C"] == -offer2.energy == -10
    assert market.bought_energy("A") == 0
    assert market.sold_energy("A") == offer1.energy + offer2.energy == 30
    assert market.bought_energy("B") == offer1.energy == 20
    assert market.bought_energy("C") == offer2.energy == 10
예제 #10
0
def test_market_issuance_acct_reverse(market: OneSidedMarket, last_offer_size, traded_energy):
    offer1 = market.offer(10, 20, 'A')
    offer2 = market.offer(10, 10, 'A')
    offer3 = market.offer(10, last_offer_size, 'D')

    market.accept_offer(offer1, 'B')
    market.accept_offer(offer2, 'C')
    market.accept_offer(offer3, 'A')
    assert market.traded_energy['A'] == traded_energy
예제 #11
0
        DeviceRegistry.REGISTRY = device_registry_dict
        ConstSettings.BalancingSettings.ENABLE_BALANCING_MARKET = True


@pytest.yield_fixture
def market():
    return TwoSidedPayAsBid(area=FakeArea("FakeArea"))


def test_device_registry(market=BalancingMarket()):
    with pytest.raises(DeviceNotInRegistryError):
        market.balancing_offer(10, 10, 'noone')


@pytest.mark.parametrize("market, offer", [
    (OneSidedMarket(), "offer"),
    (BalancingMarket(), "balancing_offer")
])
def test_market_offer(market, offer):
    e_offer = getattr(market, offer)(10, 20, 'someone')
    assert market.offers[e_offer.id] == e_offer
    assert e_offer.energy == 20
    assert e_offer.price == 10
    assert e_offer.seller == 'someone'
    assert len(e_offer.id) == 36


def test_market_bid(market: TwoSidedPayAsBid):
    bid = market.bid(1, 2, 'bidder', 'seller')
    assert market.bids[bid.id] == bid
    assert bid.price == 1
예제 #12
0
파일: test_market.py 프로젝트: xg86/d3a
def test_market_offer_invalid(market: OneSidedMarket):
    with pytest.raises(InvalidOffer):
        market.offer(10, -1, 'someone', 'someone')
예제 #13
0
파일: test_market.py 프로젝트: xg86/d3a
    assert len(matched) == 1
    bid, offer = matched[0]
    assert bid.id == 'bid_id3'
    assert bid.price == 12
    assert bid.energy == 10
    assert offer == list(market.offers.values())[0]


def test_device_registry(market=BalancingMarket()):
    with pytest.raises(DeviceNotInRegistryError):
        market.balancing_offer(10, 10, 'noone')


@pytest.mark.parametrize(
    "market, offer",
    [(OneSidedMarket(time_slot=DateTime.now()), "offer"),
     (BalancingMarket(time_slot=DateTime.now()), "balancing_offer")])
def test_market_offer(market, offer):
    DeviceRegistry.REGISTRY = device_registry_dict
    ConstSettings.BalancingSettings.ENABLE_BALANCING_MARKET = True
    e_offer = getattr(market, offer)(10, 20, 'someone', 'someone')
    assert market.offers[e_offer.id] == e_offer
    assert e_offer.energy == 20
    assert e_offer.price == 10
    assert e_offer.seller == 'someone'
    assert len(e_offer.id) == 36


def test_market_bid(market: TwoSidedPayAsBid):
    bid = market.bid(1, 2, 'bidder', 'seller', 'bidder')
    assert market.bids[bid.id] == bid
 def setUp(self):
     self.market = OneSidedMarket(name="test_market", time_slot=now())
     self.market.id = "id"
     self.subscriber = MarketRedisEventSubscriber(self.market)
 def setUp(self):
     ConstSettings.IAASettings.MARKET_TYPE = 2
     self.market = OneSidedMarket(name="test_market", time_slot=now())
     self.market.id = "id"
     self.subscriber = TwoSidedMarketRedisEventSubscriber(self.market)
예제 #16
0
class MarketStateMachine(RuleBasedStateMachine):
    offers = Bundle('Offers')
    actors = Bundle('Actors')

    def __init__(self):
        super().__init__()
        self.market = OneSidedMarket(area=FakeArea(name='my_fake_house'))

    @rule(target=actors, actor=st.text(min_size=1, max_size=3,
                                       alphabet=string.ascii_letters + string.digits))
    def new_actor(self, actor):
        return actor

    @rule(target=offers, seller=actors, energy=st.integers(min_value=1), price=st.integers())
    def offer(self, seller, energy, price):
        return self.market.offer(price, energy, seller)

    @rule(offer=offers, buyer=actors)
    def trade(self, offer, buyer):
        assume(offer.id in self.market.offers)
        self.market.accept_offer(offer, buyer)

    @precondition(lambda self: self.market.offers)
    @rule()
    def check_avg_offer_price(self):
        price = sum(o.price for o in self.market.offers.values())
        energy = sum(o.energy for o in self.market.offers.values())
        assert self.market.avg_offer_price == round(price / energy, 4)

    @precondition(lambda self: self.market.trades)
    @rule()
    def check_avg_trade_price(self):
        price = sum(t.offer.price for t in self.market.trades)
        energy = sum(t.offer.energy for t in self.market.trades)
        assert self.market.avg_trade_price == round(price / energy, 4)

    @precondition(lambda self: self.market.traded_energy)
    @rule()
    def check_acct(self):
        actor_sums = defaultdict(int)
        for t in self.market.trades:
            actor_sums[t.seller] += t.offer.energy
            actor_sums[t.buyer] -= t.offer.energy
        for actor, sum_ in actor_sums.items():
            assert self.market.traded_energy[actor] == sum_
        assert sum(self.market.traded_energy.values()) == 0

    @precondition(lambda self: self.market.traded_energy)
    @rule()
    def check_iou_balance(self):
        seller_ious = defaultdict(int)
        buyer_ious = defaultdict(int)
        for t in self.market.trades:
            seller_ious[t.seller] += t.offer.price
            buyer_ious[t.buyer] += t.offer.price
        trade_sum = sum(t.offer.price for t in self.market.trades)

        for seller, iou in seller_ious.items():
            assert iou == sum(ious[seller] for ious in self.market.ious.values())

        for buyer, iou in buyer_ious.items():
            assert iou == sum(self.market.ious[buyer].values())

        assert trade_sum == sum(sum(i.values()) for i in self.market.ious.values())
예제 #17
0
    assert len(matched) == 1
    bid, offer = matched[0]
    assert bid.id == 'bid_id3'
    assert bid.price == 12
    assert bid.energy == 10
    assert offer == list(market.offers.values())[0]


def test_device_registry(market=BalancingMarket(area=FakeArea("FakeArea"))):
    with pytest.raises(DeviceNotInRegistryError):
        market.balancing_offer(10, 10, 'noone')


@pytest.mark.parametrize(
    "market, offer",
    [(OneSidedMarket(area=FakeArea("FakeArea", transfer_fee_pct=0)), "offer"),
     (BalancingMarket(area=FakeArea("FakeArea", transfer_fee_pct=0)),
      "balancing_offer")])
def test_market_offer(market, offer):
    e_offer = getattr(market, offer)(10, 20, 'someone')
    assert market.offers[e_offer.id] == e_offer
    assert e_offer.energy == 20
    assert e_offer.price == 10
    assert e_offer.seller == 'someone'
    assert len(e_offer.id) == 36


def test_market_bid(market: TwoSidedPayAsBid):
    bid = market.bid(1, 2, 'bidder', 'seller')
    assert market.bids[bid.id] == bid
    assert bid.price == 1
예제 #18
0
    yield
    DeviceRegistry.REGISTRY = {}


@pytest.fixture
def market():
    return TwoSidedMarket(time_slot=now())


def test_device_registry(market=BalancingMarket()):
    with pytest.raises(DeviceNotInRegistryError):
        market.balancing_offer(10, 10, "noone")


@pytest.mark.parametrize("market, offer", [
    (OneSidedMarket(bc=NonBlockchainInterface(str(uuid4())), time_slot=now()), "offer"),
    (BalancingMarket(bc=NonBlockchainInterface(str(uuid4())), time_slot=now()), "balancing_offer")
])
def test_market_offer(market, offer):
    ConstSettings.BalancingSettings.ENABLE_BALANCING_MARKET = True
    e_offer = getattr(market, offer)(10, 20, "someone", "someone")
    assert market.offers[e_offer.id] == e_offer
    assert e_offer.energy == 20
    assert e_offer.price == 10
    assert e_offer.seller == "someone"
    assert len(e_offer.id) == 36


def test_market_offer_invalid(market: OneSidedMarket):
    with pytest.raises(InvalidOffer):
        market.offer(10, -1, "someone", "someone")
예제 #19
0
    matched = list(market._perform_pay_as_bid_matching())
    assert len(matched) == 1
    bid, offer = matched[0]
    assert bid.id == 'bid_id3'
    assert bid.price == 12
    assert bid.energy == 10
    assert offer == list(market.offers.values())[0]


def test_device_registry(market=BalancingMarket()):
    with pytest.raises(DeviceNotInRegistryError):
        market.balancing_offer(10, 10, 'noone')


@pytest.mark.parametrize("market, offer", [
    (OneSidedMarket(time_slot=now()), "offer"),
    (BalancingMarket(time_slot=now()), "balancing_offer")
])
def test_market_offer(market, offer):
    DeviceRegistry.REGISTRY = device_registry_dict
    ConstSettings.BalancingSettings.ENABLE_BALANCING_MARKET = True
    e_offer = getattr(market, offer)(10, 20, 'someone', 'someone')
    assert market.offers[e_offer.id] == e_offer
    assert e_offer.energy == 20
    assert e_offer.price == 10
    assert e_offer.seller == 'someone'
    assert len(e_offer.id) == 36


def test_market_bid(market: TwoSidedPayAsBid):
    bid = market.bid(1, 2, 'bidder', 'seller', 'bidder')
예제 #20
0
 def __init__(self):
     super().__init__()
     self.market = OneSidedMarket(area=FakeArea(name='my_fake_house'))
예제 #21
0
 def __init__(self):
     self.market = OneSidedMarket(
         area=FakeArea(name='my_fake_house', transfer_fee_pct=0))
     super().__init__()
예제 #22
0
 def __init__(self):
     self.market = OneSidedMarket(bc=NonBlockchainInterface(str(uuid4())), time_slot=now())
     super().__init__()
예제 #23
0
파일: test_market.py 프로젝트: xg86/d3a
 def __init__(self):
     self.market = OneSidedMarket(time_slot=DateTime.now())
     super().__init__()
예제 #24
0
def test_market_iou(market: OneSidedMarket):
    offer = market.offer(10, 20, 'A')
    market.accept_offer(offer, 'B')

    assert market.ious['B']['A'] == 10