Beispiel #1
0
def test_main(_init_data):
    """
        Integration tests combining add, delete, update, and list
    """

    # Adding some data to start
    for customer in _init_data:
        l.add_customer(customer[0], customer[1], customer[2], customer[3],
                       customer[4], customer[5], customer[6], customer[7])
    assert _init_data[0][1] == l.search_customer(_init_data[0][0])['name']
    assert _init_data[1][2] == l.search_customer(_init_data[1][0])['last_name']

    # Delete a customer

    deleted = l.delete_customer(594)
    assert deleted is True

    # Search for deleted customer, verify empty dict is returned
    gone = l.search_customer(594)
    assert gone == {}

    # Test updating customer credit limit
    updated = l.update_customer_credit(593, 1500)
    assert updated == 1
    new_credit = l.search_customer(593)['credit_limit']
    assert new_credit == 1500

    # Test active customer count
    active = l.list_active_customers()
    assert active == 3
def test_add_customer(_customers):
    """test add customer
    """
    for customer in _customers:

        with pytest.raises(DoesNotExist):
            query = l.db.Customer.get(l.db.Customer.customer_id == customer[0])

        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

        query = l.db.Customer.get(l.db.Customer.customer_id == customer[0])
        assert query.customer_id == customer[0]
        assert query.name == customer[1]
        assert query.last_name == customer[2]
        assert query.home_address == customer[3]
        assert query.phone_number == customer[4]
        assert query.email_address == customer[5]
        assert query.status == customer[6].lower()
        assert query.credit_limit == customer[7]

    for customer in _customers:
        query = l.db.Customer.delete().where(
            l.db.Customer.customer_id == customer[0])
        query.execute()
Beispiel #3
0
def test_delete_customer(_customers):
    """Test delete customer
    """
    for customer in _customers:
        assert l.delete_customer(customer[0]) is False

    for customer in _customers:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    for customer in _customers:
        assert l.delete_customer(customer[0]) is True

    # Verify that the database is again empty.
    for customer in _customers:
        with pytest.raises(NoResultFound):
            query = l.SESSION.query(l.db.Customer).filter(
                l.db.Customer.customer_id == customer[0]
            )
            query.one()
def test_search_customer(_search_customers):
    """Tests search
    """
    for customer in _search_customers[0]:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    result = l.search_customer(_search_customers[1][1])
    assert result == {}

    result = l.search_customer(_search_customers[1][0])
    assert result["name"] == _search_customers[0][1][1]
    assert result["lastname"] == _search_customers[0][1][2]
    assert result["email"] == _search_customers[0][1][5]
    assert result["phone_number"] == _search_customers[0][1][4]

    for customer in _search_customers[0]:
        print(customer[0])
        l.delete_customer(customer[0])
def test_update_customer_credit(_update_customer_credit):
    """Tests update credit
    """
    for customer in _update_customer_credit:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    l.update_customer_credit("798", 0)
    l.update_customer_credit("797", 1000)
    l.update_customer_credit("797", -42)
    l.update_customer_credit("796", 500)
    with pytest.raises(ValueError) as excinfo:
        l.update_customer_credit("00100", 1000)  # error
        assert "NoCustomer" in str(excinfo.value)

    for customer in _update_customer_credit:
        l.delete_customer(customer[0])
def test_add_integrity_error(_customers):
    """Add records already present to throw Integrity Error
    """
    for customer in _customers:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    with pytest.raises(IntegrityError):
        for customer in _customers:
            l.add_customer(
                customer[0],
                customer[1],
                customer[2],
                customer[3],
                customer[4],
                customer[5],
                customer[6],
                customer[7],
            )

    for customer in _customers:
        query = l.db.Customer.delete().where(
            l.db.Customer.customer_id == customer[0])
        query.execute()
Beispiel #7
0
def test_list_active_customers(_customers):
    """Test list active customers
    """
    active = l.list_active_customers()
    assert active == 0

    for customer in _customers:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    active = l.list_active_customers()
    assert active == 6

    # Delete all new entries from the database
    for customer in _customers:
        query = l.CUSTOMERS.delete().where(
            l.CUSTOMERS.c.customer_id == customer[0]
        )
        query.execute()
def test_list_active_customers(_customers):
    """test list active customers
    """
    active = l.list_active_customers()
    assert active == 0

    for customer in _customers:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    active = l.list_active_customers()
    assert active == 6

    for customer in _customers:
        query = l.db.Customer.delete().where(
            l.db.Customer.customer_id == customer[0])
        query.execute()
def test_update_customer(_customers):
    """test update customer
    """
    for customer in _customers:
        with pytest.raises(ValueError):
            l.update_customer_credit(customer[0], 1000)

    for customer in _customers:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    for customer in _customers:
        query = l.db.Customer.get(l.db.Customer.customer_id == customer[0])
        assert query.credit_limit != 1000
        l.update_customer_credit(customer[0], 1000)
        query = l.db.Customer.get(l.db.Customer.customer_id == customer[0])
        assert query.credit_limit == 1000

    for customer in _customers:
        query = l.db.Customer.delete().where(
            l.db.Customer.customer_id == customer[0])
        query.execute()
def test_search_customer(_customers):
    """test search customer
    """
    for customer in _customers:
        assert l.search_customer(customer[0]) == {}

    for customer in _customers:

        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

        result = l.search_customer(customer[0])
        assert result["customer_id"] == customer[0]
        assert result["name"] == customer[1]
        assert result["lastname"] == customer[2]
        assert result["home_address"] == customer[3]
        assert result["phone_number"] == customer[4]
        assert result["email"] == customer[5]
        assert result["status"] == customer[6].lower()
        assert result["credit_limit"] == customer[7]

    for customer in _customers:
        query = l.db.Customer.delete().where(
            l.db.Customer.customer_id == customer[0])
        query.execute()
Beispiel #11
0
def test_update_customer(_customers):
    """Test update customer
    """
    for customer in _customers:
        with pytest.raises(ValueError):
            l.update_customer_credit(customer[0], 1000)

    for customer in _customers:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    for customer in _customers:
        query = l.SESSION.query(
            l.db.Customer).filter(l.db.Customer.customer_id == customer[0])
        result = query.one()
        assert result.credit_limit != 1000
        l.update_customer_credit(customer[0], "1000")
        query = l.SESSION.query(
            l.db.Customer).filter(l.db.Customer.customer_id == customer[0])
        result = query.one()
        assert result.credit_limit == 1000

    # Delete all new entries from the database
    for customer in _customers:
        query = l.CUSTOMERS.delete().where(
            l.CUSTOMERS.c.customer_id == customer[0])
        query.execute()
Beispiel #12
0
def test_add_integrity_error(_customers):
    """Add records already present to throw Integrity Error
    """
    for customer in _customers:
        l.add_customer(
            customer[0],
            customer[1],
            customer[2],
            customer[3],
            customer[4],
            customer[5],
            customer[6],
            customer[7],
        )

    with pytest.raises(exc.IntegrityError):
        for customer in _customers:
            l.add_customer(
                customer[0],
                customer[1],
                customer[2],
                customer[3],
                customer[4],
                customer[5],
                customer[6],
                customer[7],
            )

    # Delete all new entries from the database
    for customer in _customers:
        query = l.CUSTOMERS.delete().where(
            l.CUSTOMERS.c.customer_id == customer[0])
        query.execute()
def test_delete_customer(_delete_customers):
    """ delete """
    for customer in _delete_customers:
        l.add_customer(*customer)

        response = l.delete_customer(customer[0])
        assert response is True

        deleted = l.search_customer(customer[0])
        assert deleted == {}
Beispiel #14
0
def test_list_active_customers(_list_active_customers):
    """ actives """
    for customer in _list_active_customers:
        l.add_customer(customer[0], customer[1], customer[2], customer[3],
                       customer[4], customer[5], customer[6], customer[7])
    actives = l.list_active_customers()

    assert actives == 4

    for customer in _list_active_customers:
        l.delete_customer(customer[0])
Beispiel #15
0
def test_delete_customer(_delete_customers):
    """ delete """
    for customer in _delete_customers:
        l.add_customer(customer[0], customer[1], customer[2], customer[3],
                       customer[4], customer[5], customer[6], customer[7])

        response = l.delete_customer(customer[0])
        assert response is True

        deleted = l.search_customer(customer[0])
        assert deleted == {}
def test_add_customer(_add_customers):
    """ additions """
    for customer in _add_customers:
        l.add_customer(*customer)
        added = l.search_customer(customer[0])
        assert added["name"] == customer[1]
        assert added["last_name"] == customer[2]
        assert added["email_address"] == customer[5]
        assert added["phone_number"] == customer[4]

    for customer in _add_customers:
        l.delete_customer(customer[0])
    def test_list_active_customers(self):
        """Test that the function lists all active customers"""
        # Given
        basic_operations.add_customer(*CUSTOMER2)
        basic_operations.add_customer(*CUSTOMER3)

        # When

        # Then
        self.assertEqual(2, basic_operations.list_active_customers())

        basic_operations.delete_customer(CUSTOMER2[0])
        basic_operations.delete_customer(CUSTOMER3[0])
def test_list_active_customers(_list_active_customers):
    """ actives """
    for customer in _list_active_customers:
        l.add_customer(*customer)

    actives = l.list_active_customers()

    assert actives == 4

    for customer in _list_active_customers:
        l.delete_customer(customer[0])

    assert l.list_active_customers() == 0
Beispiel #19
0
def test_add_customer(_add_customers):
    """ additions """
    for customer in _add_customers:
        l.add_customer(customer[0], customer[1], customer[2], customer[3],
                       customer[4], customer[5], customer[6], customer[7])
        added = l.search_customer(customer[0])
        assert added["name"] == customer[1]
        assert added["lastname"] == customer[2]
        assert added["email"] == customer[5]
        assert added["phone_number"] == customer[4]

    for customer in _add_customers:
        l.delete_customer(customer[0])
Beispiel #20
0
    def test_search_customer(self):
        bo.add_customer(1, 'Lady', 'Gaga', '453 Hollywood Blvd',
                        555334290, '*****@*****.**', True, 20000)

        customer_dict = bo.search_customer(1)

        test_customer = {
            'first_name': 'Lady',
            'last_name': 'Gaga',
            'email': '*****@*****.**',
            'phone_number': 555334290
        }

        self.assertDictEqual(test_customer, customer_dict)
Beispiel #21
0
def test_update_customer_credit(_update_customer_credit):
    """ update """
    for customer in _update_customer_credit:
        l.add_customer(customer[0], customer[1], customer[2], customer[3],
                       customer[4], customer[5], customer[6], customer[7])

    l.update_customer_credit("798", 0)
    l.update_customer_credit("797", 1000)
    l.update_customer_credit("797", -42)
    l.update_customer_credit("796", 500)
    #with pytest.raises(Exception) as excinfo:
    dne = l.update_customer_credit("00100", 1000)  # error
    assert '<Model: Customer> instance matching query does not exist:' in str(
        dne)
Beispiel #22
0
    def test_search_customer(self):
        bo.add_customer('C00002', 'Lady', 'Gaga', '453 Hollywood Blvd',
                        '555334290', '*****@*****.**', 'Active', '20000')

        customer_dict = bo.search_customer('C00002')

        test_customer = {
            'first_name': 'Lady',
            'last_name': 'Gaga',
            'email': '*****@*****.**',
            'phone_number': '555334290'
        }

        self.assertDictEqual(test_customer, customer_dict)
def test_add_customer(_add_customers):
    """ additions """
    while True:
        try:
            customer = next(_add_customers)
        except StopIteration:
            break
        l.add_customer(customer[0], customer[1], customer[2], customer[3],
                       customer[4], customer[5], customer[6], customer[7])
        added = l.search_customer(customer[0])
        assert added["name"] == customer[1]
        assert added["lastname"] == customer[2]
        assert added["email"] == customer[5]
        assert added["phone_number"] == customer[4]
        l.delete_customer(customer[0])
def test_list_active_customers(_list_active_customers):
    """ actives """
    active_count = 0

    for customer in _list_active_customers:
        if customer[-2] == 'active':
            active_count += 1
        l.add_customer(customer[0], customer[1], customer[2], customer[3],
                       customer[4], customer[5], customer[6], customer[7])
    actives = l.list_active_customers()

    assert actives == active_count

    for customer in _list_active_customers:
        l.delete_customer(customer[0])
    def test_add_customer_exisiting(self):
        """Testing adding existing customer to database"""
        # Given

        # When
        basic_operations.add_customer(*CUSTOMER1)

        # Then
        try:
            basic_operations.add_customer(*CUSTOMER1)
        except Exception as e_val:
            self.fail(e_val)

        db_customer1 = Customers.get(Customers.customer_id == "FY2020-001")
        db_customer1.delete_instance()
    def test_full_integration(self):
        """Integrates all methods into one test"""
        basic_operations.add_customer('004', 'Joey', 'Smith',
                                      '123 Jackson Street', 1234567890,
                                      '*****@*****.**', 'active', 1000.00)
        basic_operations.update_customer_credit('004', 2000)
        basic_operations.update_status('004', 'inactive')
        expected_customer = {
            'customer_id': '004',
            'name': 'Joey',
            'lastname': 'Smith',
            'home_address': '123 Jackson Street',
            'phone_number': '1234567890',
            'email_address': '*****@*****.**',
            'status': 'inactive',
            'credit_limit': 2000.00
        }
        actual_searched_customer = basic_operations.search_customer('004')
        self.assertEqual(actual_searched_customer, expected_customer)
        actual_customer_count = basic_operations.list_active_customers()
        self.assertEqual(actual_customer_count, 0)
        basic_operations.delete_customer('004')

        try:
            DATABASE.connect()
            DATABASE.execute_sql('PRAGMA foreign_keys = ON;')
            LOGGER.info('Successfully connected to the database')
            searched_customer = Customers.get(Customers.customer_id == '004')
            LOGGER.info('Customer Found!')
            deleted_customer = {
                'customer_id': searched_customer.customer_id,
                'name': searched_customer.name,
                'lastname': searched_customer.lastname,
                'home_address': searched_customer.home_address,
                'phone_number': searched_customer.phone_number,
                'email_address': searched_customer.email_address,
                'status': searched_customer.status,
                'credit_limit': searched_customer.credit_limit
            }

        except Exception as ex:
            LOGGER.info('Error finding customer 004')
            LOGGER.info(ex)
            deleted_customer = {}

        self.assertEqual(deleted_customer, {})
        LOGGER.info('Closing database')
        DATABASE.close()
Beispiel #27
0
def test_load_data(datafiles):
    """ verify that data load from file works """
    # make sure dir has at least one file
    assert len(os.listdir(datafiles)) == 1
    file = os.listdir(datafiles)[0]
    iterable = l.load_data(f"{datafiles}/{file}")
    count = 0
    while count < 10:  # take 10 values from iterator
        iter_value = next(iterable)
        l.add_customer(*iter_value)
        count += 1
    actives = l.list_active_customers()
    assert actives == 10  # check for actives
    added = l.search_customer(iter_value[0])
    assert added["name"] == iter_value[1]
    assert added["home_address"] == iter_value[3]
Beispiel #28
0
    def test_add_customer_exisiting(self):
        """Testing adding existing customer to database"""
        LOGGER.info("*** TEST ***: add_customer with existing customer_id")
        # Given

        # When
        basic_operations.add_customer(*CUSTOMER1)

        # Then
        try:
            basic_operations.add_customer(*CUSTOMER1)
        except IntegrityError as e_val:
            self.fail(e_val)

        db_customer1 = Customers.get(Customers.customer_id == "FY2020-001")
        db_customer1.delete_instance()
def test_search_customer(_search_customers):
    """ search """
    for customer in _search_customers[0]:
        l.add_customer(*customer)

    result = l.search_customer(_search_customers[1][1])
    assert result == {}

    result = l.search_customer(_search_customers[1][0])
    assert result["name"] == _search_customers[0][1][1]
    assert result["last_name"] == _search_customers[0][1][2]
    assert result["email_address"] == _search_customers[0][1][5]
    assert result["phone_number"] == _search_customers[0][1][4]

    for customer in _search_customers[0]:
        l.delete_customer(customer[0])
Beispiel #30
0
    def test_add_customer(self):

        bo.add_customer(1, 'Lady', 'Gaga', '453 Hollywood Blvd',
                     555334290, '*****@*****.**', True, 20000)

        test_customer = [1, 'Lady', 'Gaga', '453 Hollywood Blvd',
                         555334290, '*****@*****.**', True, 20000]

        self.assertEqual(test_customer[0], Customer.customer_id)
        self.assertEqual(test_customer[1], Customer.first_name)
        self.assertEqual(test_customer[2], Customer.last_name)
        self.assertEqual(test_customer[3], Customer.home_address)
        self.assertEqual(test_customer[4], Customer.phone_number)
        self.assertEqual(test_customer[5], Customer.email_address)
        self.assertEqual(test_customer[6], Customer.status)
        self.assertEqual(test_customer[7], Customer.credit_limit)