def test_charge_sum_zero (self): allocation = Allocation(None, None, 0, datetime(2000, 1, 1), datetime(2001, 1, 1)) allocation.project_id = "project" allocation.resource_id = "resource" Session.add(allocation) Session.commit() Session.close() allocation = Session.query(Allocation).one() assert_equal(allocation._charge_sum, 0)
def test_resource (self): allocation = Allocation( None, None, None, None, None) assert_equal(allocation.resource, None) resource = Mock(['id']) resource.id = sentinel.resource_id allocation.resource = resource assert_equal(allocation.resource, sentinel.resource) Resource.cached.assert_called_with(sentinel.resource_id)
def test_project (self): allocation = Allocation( None, None, None, None, None) assert_equal(allocation.project, None) project = Mock(['id']) project.id = sentinel.project_id allocation.project = project assert_equal(allocation.project, sentinel.project) Project.cached.assert_called_with(sentinel.project_id)
def test_charge_sum_one (self): allocation = Allocation(None, None, 0, datetime(2000, 1, 1), datetime(2001, 1, 1)) allocation.project_id = "project" allocation.resource_id = "resource" charge = Charge(allocation, 1) Session.add_all([allocation, charge]) Session.commit() Session.close() allocation = Session.query(Allocation).one() assert_equal(allocation._charge_sum, 1)
def test_hold_sum_one (self): allocation = Allocation(None, None, 0, datetime(2000, 1, 1), datetime(2001, 1, 1)) allocation.project_id = "project" allocation.resource_id = "resource" hold = Hold(allocation, 1) Session.add_all([allocation, hold]) Session.commit() Session.close() allocation = Session.query(Allocation).one() assert_equal(allocation._active_hold_sum, 1)
def test_refund_sum_zero (self): allocation = Allocation(None, None, 0, datetime(2000, 1, 1), datetime(2001, 1, 1)) allocation.project_id = "project" allocation.resource_id = "resource" charge = Charge(allocation, 0) Session.add_all([allocation, charge]) Session.commit() Session.close() charge = Session.query(Charge).one() assert_equal(charge._refund_sum, 0)
def test_amount_charged (self): allocation = Allocation(None, None, None, None, None) assert_equal(allocation.amount_charged(), 0) charge_1 = Mock(['effective_amount']) charge_1.effective_amount = Mock(return_value=1) allocation.charges.append(charge_1) assert_equal(allocation.amount_charged(recalculate=True), 1) charge_2 = Mock(['effective_amount']) charge_2.effective_amount = Mock(return_value=2) allocation.charges.append(charge_2) assert_equal(allocation.amount_charged(recalculate=True), 3)
def test_hold_sum_two_with_one_inactive (self): allocation = Allocation(None, None, 0, datetime(2000, 1, 1), datetime(2001, 1, 1)) allocation.project_id = "project" allocation.resource_id = "resource" hold_1 = Hold(allocation, 1) hold_1.active = False hold_2 = Hold(allocation, 2) Session.add_all([allocation, hold_1, hold_2]) Session.commit() Session.close() allocation = Session.query(Allocation).one() assert_equal(allocation._active_hold_sum, 2)
def test_refund_sum_two (self): allocation = Allocation(None, None, 0, datetime(2000, 1, 1), datetime(2001, 1, 1)) allocation.project_id = "project" allocation.resource_id = "resource" charge = Charge(allocation, 0) refund_1 = Refund(charge, 1) refund_2 = Refund(charge, 2) Session.add_all([allocation, charge, refund_1, refund_2]) Session.commit() Session.close() allocation = Session.query(Allocation).one() assert_equal(allocation._refund_sum, 3)
def test_active (self): start = datetime(2000, 1, 1) end = datetime(2000, 1, 3) allocation = Allocation(None, None, None, start, end) assert allocation.active() assert not allocation.active(start-timedelta(hours=1)) assert allocation.active(start) assert not allocation.active(end) assert not allocation.active(end+timedelta(hours=1))
def test_amount_held (self): allocation = Allocation(None, None, None, None, None) assert_equal(allocation.amount_held(), 0) hold_1 = Mock(['amount', 'active']) hold_1.amount = 1 hold_1.active = True allocation.holds.append(hold_1) assert_equal(allocation.amount_held(recalculate=True), 1) hold_2 = Mock(['amount', 'active']) hold_2.amount = 2 hold_2.active = True allocation.holds.append(hold_2) assert_equal(allocation.amount_held(recalculate=True), 3) hold_1.active = False assert_equal(allocation.amount_held(recalculate=True), 2) hold_2.active = False assert_equal(allocation.amount_held(recalculate=True), 0)
def test_amount_available (self): allocation = Allocation(None, None, 0, None, None) assert_equal(allocation.amount_available(), 0) allocation.amount = 10 assert_equal(allocation.amount_available(), 10) hold_1 = Mock(['amount', 'active']) hold_1.amount = 1 hold_1.active = True allocation.holds.append(hold_1) assert_equal(allocation.amount_available(recalculate=True), 9) hold_2 = Mock(['amount', 'active']) hold_2.amount = 2 hold_2.active = True allocation.holds.append(hold_2) assert_equal(allocation.amount_available(recalculate=True), 7) hold_1.active = False assert_equal(allocation.amount_available(recalculate=True), 8) hold_2.active = False assert_equal(allocation.amount_available(recalculate=True), 10) charge_1 = Mock(['effective_amount']) charge_1.effective_amount = Mock(return_value=1) allocation.charges.append(charge_1) assert_equal(allocation.amount_available(recalculate=True), 9) charge_2 = Mock(['effective_amount']) charge_2.effective_amount = Mock(return_value=2) allocation.charges.append(charge_2) assert_equal(allocation.amount_available(recalculate=True), 7) charge_3 = Mock(['effective_amount']) charge_3.effective_amount = Mock(return_value=8) allocation.charges.append(charge_3) assert_equal(allocation.amount_available(recalculate=True), 0)
def test_hold (self): allocation = Allocation(None, None, 1, None, None) hold = allocation.hold(1) cbank.model.entities.Hold.assert_called_with( allocation, 1) assert_equal(hold, sentinel.hold)