def test_grand_total():
    items = [
        LineItem(actual_amount=50, adjustments=50),
        LineItem(actual_amount=100, adjustments=500)
    ]

    assert 700 == grand_total(items)
    def test_get_subtotals_by_campaign(self, invoice_service,
                                       line_item_service):
        campaign_1_items = [LineItem(**item) for item in ITEMS]
        campaign_3_items = [LineItem(**item) for item in CAMPAIGN_3_ITEMS]
        expected_total_1 = grand_total(campaign_1_items)
        expected_total_3 = grand_total(campaign_3_items)
        line_item_service.add_items(campaign_1_items)
        line_item_service.add_items(campaign_3_items)

        totals = invoice_service.get_grouped_totals(group_by="campaign_id")

        assert expected_total_1 == totals[1]
        assert expected_total_3 == totals[3]
示例#3
0
    def test_get_line_items(self, line_item_service):
        line_item_service.add_item(LineItem(**DUMMY_ITEM))

        items = list(line_item_service.get_line_items())

        assert 1 == len(items)
        assert_item_equal(items[0], DUMMY_ITEM)
示例#4
0
def load_data_file(file_path, service_factory):
    full_path = os.path.abspath(".")
    print("Loading data from", full_path)
    with open(file_path, "rt") as f:
        data = json.load(f)
    service_factory.create_proxy_service(LINE_ITEM_SERVICE).add_items(
        LineItem(**item) for item in data)
    print("Added {} items".format(len(data)))
    def test_get_total_with_items(self, invoice_service, line_item_service):
        line_items = [LineItem(**item) for item in ITEMS]
        expected_total = grand_total(line_items)
        line_item_service.add_items(line_items)

        total = invoice_service.get_total()

        assert expected_total == total
示例#6
0
    def test_get_grouped_items(self, line_item_service):
        line_item_service.add_items(LineItem(**item) for item in ITEMS)

        items = line_item_service.get_grouped_items("campaign_id")

        assert isinstance(items, dict)
        assert len(items[1]) == 1
        assert len(items[2]) == 2
    def test_finalize(self, invoice_service, line_item_service):
        line_item_service.add_items(
            LineItem(**item) for item in ITEMS + CAMPAIGN_3_ITEMS)

        return_value = invoice_service.finalize()

        assert {"invoice_id": 1} == return_value
        assert 0 == len(line_item_service.get_line_items())
示例#8
0
    def test_active_filters_applied(self, line_item_service, filter_service):
        items = [LineItem(**item) for item in ITEMS]
        line_item_service.add_items(items)
        filter_service.add_filter("campaign_id", "eq", 1)

        items = line_item_service.get_line_items()

        assert 1 == len(items)
        assert items[0].campaign_id == 1
示例#9
0
    def test_set_invoice(self, line_item_service):
        line_item_service.add_items(LineItem(**item) for item in ITEMS)

        line_item_service.set_invoice(1)
        found_items = line_item_service.get_line_items(
            LineItem.invoice_id == 1)

        assert len(ITEMS) == len(found_items)
        assert 1 == found_items[0].invoice_id
示例#10
0
    def test_get_grouped_items_with_filter(self, line_item_service):
        line_item_service.add_items(LineItem(**item) for item in ITEMS)

        items = line_item_service.get_grouped_items("campaign_id",
                                                    LineItem.campaign_id == 2)

        assert isinstance(items, dict)
        assert 1 not in items
        assert 2 == len(items[2])
示例#11
0
    def test_filter_is_cleared_on_finalize(self, invoice_service,
                                           line_item_service, filter_service):
        line_item_service.add_items(
            LineItem(**item) for item in ITEMS + CAMPAIGN_3_ITEMS)
        filter_service.add_filter("campaign_id", "eq", 1)

        invoice_service.finalize()

        assert 0 == len(filter_service.get_active_filters())
示例#12
0
    def test_serialize_object(self, app_client, dummy_line_item_service):
        data = [LineItem(**item) for item in ITEMS]
        set_data(dummy_line_item_service, data)

        r = app_client.get("/lineitems")
        expected = dict(ITEMS[0])
        expected["invoice_id"] = None

        assert 200 == r.status_code
        assert expected == json.loads(r.data)[0]
示例#13
0
    def test_raise_error_when_updating_readonly(self, line_item_service):
        line_item_service.add_item(LineItem(**DUMMY_ITEM))

        with pytest.raises(ReadOnlyItemValueError):
            line_item_service.update_line_item(1, {
                "adjustments": 123.1,
                "booked_amount": 555.13
            })

        updated_item = line_item_service.get_line_item(1)
        assert_item_equal(updated_item, DUMMY_ITEM)
示例#14
0
    def test_update_line_item(self, line_item_service):
        line_item_service.add_item(LineItem(**DUMMY_ITEM))

        line_item_service.update_line_item(1, {"adjustments": 123.1})

        updated_item = line_item_service.get_line_item(1)

        expected = DUMMY_ITEM.copy()
        expected["adjustments"] = 123.1

        assert_item_equal(updated_item, expected)
示例#15
0
    def test_get_line_items_only_returns_no_invoice_items_by_default(
            self, line_item_service, session_maker):
        line_item_service.add_items(LineItem(**item) for item in ITEMS)
        session = session_maker()
        session.query(LineItem).filter(LineItem.id == 35).update(
            {"invoice_id": 1})

        items = list(line_item_service.get_line_items())

        assert len(items) == 2
        assert 35 not in {item.id for item in items}
示例#16
0
    def test_get_grouped_items_only_returns_no_invoice_items(
            self, line_item_service, session_maker):
        line_item_service.add_items(LineItem(**item) for item in ITEMS)
        session = session_maker()
        session.query(LineItem).filter(LineItem.id == 35).update(
            {"invoice_id": 1})

        items = line_item_service.get_grouped_items("campaign_id")

        assert isinstance(items, dict)
        assert len(items[2]) == 2
        assert 1 not in items
示例#17
0
    def test_raise_error_when_updaing_invalid_props(self, line_item_service):
        line_item_service.add_item(LineItem(**DUMMY_ITEM))

        with pytest.raises(InvalidUpdateError):
            line_item_service.update_line_item(1, {
                "adjustments": 123.1,
                "booked_amount": 555.13,
                "something": 333.2
            })

        updated_item = line_item_service.get_line_item(1)

        assert_item_equal(updated_item, DUMMY_ITEM)
示例#18
0
    def test_get_line_item(self, line_item_service):
        line_item_service.add_item(LineItem(**DUMMY_ITEM))

        item = line_item_service.get_line_item(1)

        assert_item_equal(item, DUMMY_ITEM)
示例#19
0
def test_serialize_item():
    item = LineItem(**ITEM_DATA)

    result = json.loads(dumps(item))

    assert result == ITEM_DATA
示例#20
0
def placement_test_data():
    path = os.path.join(os.path.dirname(__file__), "data",
                        "placements_teaser_data.json")
    with open(path, "rt") as f:
        data = json.load(f)
    return [LineItem(**item) for item in data]
def test_billable_amount():
    item = LineItem(actual_amount=123.4, adjustments=456.7)

    billable = billable_amount(item)

    assert 580.1 == billable