import pytest

from domain.batch import Batch
from domain.order_line import OrderLine
from datetime import date
from tests.common_test import get_session, clear_all


def test_orderline_mapper_can_load_lines(session):
    query = """
        INSERT INTO order_lines (ref, sku, qty) VALUES
        ('order1', 'CHAIR', 12),
        ('order2', 'TABLE', 13),
        ('order3', 'LIPSTICK', 14)
    """
    session.execute(query)
    expected = [
        OrderLine("order1", "CHAIR", 12),
        OrderLine("order2", "TABLE", 13),
        OrderLine("order3", "LIPSTICK", 14),
    ]
    obtained = session.query(OrderLine).all()
    assert  obtained == expected

def test_orderline_mapper_can_save_lines(session):
    new_line = OrderLine("order1", "DECORATION", 12)
    session.add(new_line)
    session.commit()
    rows = list(session.execute("SELECT ref,sku,qty from 'order_lines'"))
    assert rows == [("order1", "DECORATION",12)]

def test_retrieving_batches(session):
    query = """
        INSERT INTO batches(reference, sku, _purchased_quantity, eta)
        VALUES 
        ('batch1', "sku1", 100, null),
        ('batch2', "sku2", 200, '2011-04-11');
    """
    session.execute(query)
    expected = [
        Batch("batch1", "sku1", 100, eta=None),
        Batch("batch2", "sku2", 200, eta=date(2011,4,11)),
    ]
    obtained = session.query(Batch).all()
    assert obtained == expected


def test_saving_batches(session):   
    batch = Batch("batch1", "sku1", 100, eta=None)
    session.add(batch)
    session.commit()
    rows = list(session.execute(
        "SELECT reference, sku, _purchased_quantity, eta from 'batches'"
    ))
    assert rows == [('batch1', 'sku1', 100, None)]

def test_saving_allocations(session):
    batch = Batch("batch1", "sku1", 100, eta=None)
    line = OrderLine("order1", "sku1", 10)
    batch.allocate(line)
    session.add(batch)
    session.commit()
    query = """
        SELECT ol.ref, b.reference 
        FROM allocations a
        INNER JOIN order_lines ol on ol.id = a.id
        INNER JOIN batches b on b.id = a.id
    """
    rows = list(session.execute(query))
    assert rows == [(line.ref, batch.reference)]


def test_retrieveing_allocations(session):
    query = """
        INSERT INTO order_lines(ref, sku, qty)
        VALUES
        ('order1', 'sku1', 12);
    """
    session.execute(query)
    [[olid]] = session.execute(
        'SELECT id FROM order_lines WHERE ref=:ref AND sku=:sku',
        dict(ref='order1', sku='sku1')
    )
    query = """
        INSERT INTO batches(reference, sku, _purchased_quantity, eta)
        VALUES
        ('batch1', 'sku1', 100, null);
    """
    session.execute(query)
    [[bid]] = session.execute(
        'SELECT id FROM batches WHERE reference=:ref AND sku=:sku',
        dict(ref='batch1', sku='sku1')
    )
    query = """
        INSERT INTO allocations(orderline_id, batch_id)
        VALUES
        ({olid}, {bid})
    """.format(olid=olid, bid=bid)
    session.execute(query)
    batch = session.query(Batch).one()
    assert batch._allocations == {
        OrderLine("order1", "sku1", 12)
    }
