Example #1
0
def test_insert():

    c1 = {'Name': name_generator(), 'AccountNumber': '123321'}
    c2 = {'Name': name_generator(), 'AccountNumber': account_generator()}
    c3 = {
        'Name': name_generator(),
        'AccountNumber': account_generator(),
        'Phone': '2313211232'
    }

    cust = Customer(**c1)

    cust1 = Customer()
    cust1.Name = c2.get('Name')
    cust1.AccountNumber = c2.get('AccountNumber')

    cust2 = Customer()
    [setattr(cust2, k, v) for k, v in c3.items()]

    customers = [cust, cust1, cust2]

    assert type(cust.Name) == str
    assert type(cust.AccountNumber) == str
    assert cust.AccountNumber == "123321"

    # Create a session object - session object gets passed to the table object
    session = QuickBooks(TestConf.DSN)
    session.connect()

    session.query('select * from customer')
    session.cursor.execute('select * from customer')

    # Manually create a customer - need to manually retreive the result
    resp = session.cursor.execute(
        """
        Insert Into Customer (name, accountnumber)
            VALUES (?, ?)
        """,
        ['McHarry' + datetime.now().strftime('%y%m%d%H%M%S'), '123321123'])

    with pytest.raises(ProgrammingError):
        resp.fetchval()

    # ListId of the last created object
    resp = session.last_insert('Customer')
    assert isinstance(resp, str)

    # Pass all args as strings
    session.cursor.execute(
        f'insert into customer ({cust2.to_names()}) values ({cust2.to_params()})',
        cust2.to_values())
    last_insert = session.last_insert('customer')
    assert isinstance(last_insert, str)

    # When using the save method the last insert and primary key are returned
    resp_list = [c.save(qb=session) for c in customers]

    session.close()
Example #2
0
def test_pre_insert():

    session = QuickBooks(TestConf.DSN)
    session.connect()

    items = session.sql('select FullName from Item where Type = ?',
                        ['ItemService']).fetchall()
    vendor = session.sql('select Name from vendor').fetchall()
    customer = session.sql('select FullName from Customer').fetchall()
    cogs_account = session.sql(
        'select Name from account where AccountType = ?',
        ['CostOfGoodsSold']).fetchall()
    class_name = session.sql('select FullName from class').fetchall()

    bill_test = Bill(
        VendorRefFullName=vendor[15][0],
        APAccountRefFullName="Accounts Payable",
        RefNumber="1233-1231",
        Memo="PO123312",
        # ItemLineClassRefFullName =
        TxnDate=date(2021, 1, 1))

    line_1 = BillItemLine(
        Item=items[2][0],
        Desc='OverideAccount',
        Quantity=2,
        ItemLineCustomerRefFullName=customer[2][0],
        ItemLineOverrideItemAccountRefFullName=cogs_account[2][0],
        Amount=Decimal('20.21'))

    line_2 = BillItemLine(Item=items[2][0],
                          Desc='Customer with normal account',
                          Quantity=2,
                          ItemLineCustomerRefFullName=customer[2][0],
                          Amount=Decimal('10.21'))

    line_3 = BillItemLine(Item=items[2].FullName,
                          Desc='Normal with class with amount = None',
                          Quantity=2,
                          ItemLineClassRefFullName=class_name[2].FullName,
                          Amount=None)

    lines = [line_1, line_2, line_3]
    # Add list of items and single item
    bill_test.add_item(line_1)
    bill_test.add_item(lines)

    assert len(bill_test._line_item) == 4

    # Create the bill
    resp = bill_test.save(qb=session)
    last_insert = session.last_insert('BillItemLine')
    assert last_insert == resp.get('id')
Example #3
0
def test_connection():
    session = QuickBooks(TestConf.DSN)
    session.connect()

    customer = session.query('SELECT * FROM Customer')
    assert isinstance(customer, pd.DataFrame)
    account = session.query('SELECT * FROM Account')
    assert isinstance(account, pd.DataFrame)

    with pytest.raises(QBMissingTable) as e_info:
        error = session.query("SELECT * FROM Missing")

    session.close()
Example #4
0
def test_query():
    session = QuickBooks(remote_dsn=TestConf.DSN, ip=TestConf.IP)
    session.connect()

    resp = session.sql('select * from account', [])
    resp.columns().fetchone()
    resp_all = resp.fetchall()

    assert isinstance(resp_all, list)
    assert isinstance(resp.columns().fetchone(), Row)
    session.close()
Example #5
0
def test_expense_query():
    session = QuickBooks(TestConf.DSN)
    session.connect()

    resp = session.sql('select * from account where accounttype = ?',
                       ['expense'])

    assert isinstance(resp.fetchone(), Row)
    expense_row = resp.fetchone()
    assert expense_row.AccountType == 'Expense'
    assert isinstance(expense_row.ListID, str)
    session.close()
Example #6
0
def test_methods():
    session = QuickBooks(TestConf.DSN)
    session.connect()

    tables = session.tables()

    session.cursor.execute('SELECT * FROM Customer')

    resp = session.sql('select * from Customer where isactive = ?', [True])
    row_customer = resp.fetchone()
    assert row_customer.IsActive

    resp = session.sql('select * from Customer where isactive = ?', [1])
    row_customer = resp.fetchone()
    assert row_customer.IsActive
Example #7
0
def test_call():
    con = QuickBooks(TestConf.DSN)
    con.connect()
    account = con.query("SELECT * FROM Account")
    assert len(account) > 1
    con.close()
Example #8
0
def test_session():
    session = QuickBooks(TestConf.DSN)
    session.connect()
    assert isinstance(session.cnxn, Connection)
    assert isinstance(session.cursor, Cursor)
    session.close()
Example #9
0
def test_connection_error():
    with pytest.raises(QBConnectionError):
        error_con = QuickBooks("MissingFile").connect()