コード例 #1
0
 def test_customer_model_basic(self):
     new_customer = Customer().import_data({
         'name': 'Robin',
         'surname': 'Staunton-Collins'
     })
     assert new_customer.name == 'Robin'
     assert new_customer.surname == 'Staunton-Collins'
     customer_data = new_customer.export_data()
     assert customer_data == {
         'name': 'Robin',
         'surname': 'Staunton-Collins'
     }
コード例 #2
0
    def test_open_account_zero_initial_credit(self, test_client):
        new_customer = Customer(name="Robin", surname="Staunton-Collins")
        db.session.add(new_customer)
        db.session.commit()
        c = Customer.query.first()

        response = test_client.post('/api/v1/openAccount',
                                    json={
                                        'customerID': str(c.id),
                                        'initialCredit': str(0)
                                    })
        assert response.status_code == 200
        json = response.get_json()
        assert len(str(json['account']['account_number'])) == 20
        assert json['account']['uri'] == '/api/v1/getCustomerInfo'
        assert json['account']['balance'] == 0.0
        assert json['account']['customer_id'] == c.id

        a = Account.query.first()

        assert Transaction.query.all() == []
        assert a.customer_id == c.id
        assert a.balance == 0.0
        assert a.owner.id == c.id
        assert a.transactions.all() == []
コード例 #3
0
    def test_open_account(self, test_client, url, data):
        new_customer = Customer(name="Robin", surname="Staunton-Collins")
        db.session.add(new_customer)
        db.session.commit()
        c = Customer.query.first()

        response = test_client.post(url, json=data)
        assert response.status_code == 200
        json = response.get_json()
        assert len(str(json['account']['account_number'])) == 20
        assert json['account']['uri'] == '/api/v1/getCustomerInfo'
        assert json['account']['balance'] == 50.0
        assert json['account']['customer_id'] == c.id

        t = Transaction.query.first()
        a = Account.query.first()
        print(t)
        print(a)

        assert a.customer_id == c.id
        assert a.balance == 50.0

        assert t.account_id == a.id
        assert t.amount == a.balance

        assert a.balance == 50.0
        assert a.owner.id == c.id
        assert a.transactions.first() == t
コード例 #4
0
    def test_get_customer_info_data_pagination(self, test_client, url, data,
                                               lots_of_transactions):
        c = Customer(name="Monty", surname="Python")
        db.session.add(c)
        db.session.commit()

        a1 = Account(account_number=1234, balance=100, customer_id=c.id)
        a2 = Account(account_number=4321, balance=100, customer_id=c.id)
        db.session.add(a1)
        db.session.add(a2)
        db.session.commit()

        db.session.add_all(lots_of_transactions)
        db.session.commit()

        response = test_client.get(url, json=data)

        assert response.status_code == 200
        json = response.get_json()
        assert json['customer'] == {
            'name':
            "Monty",
            'surname':
            "Python",
            'uri':
            '/api/v1/customers/1',
            'accounts': [{
                'uri':
                '/api/v1/accounts/1',
                'account_number':
                1234,
                'balance':
                100.0,
                'customer_id':
                c.id,
                'transactions': [{
                    'amount': -20.0,
                    'account_id': 1,
                    'time': datetime(2020, 4, 20, 21).isoformat(),
                    'uri': '/api/v1/transactions/1'
                }]
            }, {
                'uri':
                '/api/v1/accounts/2',
                'account_number':
                1234,
                'balance':
                100.0,
                'customer_id':
                c.id,
                'transactions': [{
                    'amount': 50.0,
                    'account_id': 2,
                    'time': datetime(2020, 4, 19, 15).isoformat(),
                    'uri': '/api/v1/transactions/2'
                }]
            }]
        }
コード例 #5
0
 def test_customer_model_db(self, test_client):
     new_customer = Customer().import_data({
         'name': 'Robin',
         'surname': 'Staunton-Collins'
     })
     db.session.add(new_customer)
     db.session.commit()
     customer = Customer.query.first()
     assert customer == new_customer
コード例 #6
0
 def test_repr(self, test_client):
     new_customer = Customer().import_data({
         'name': 'Robin',
         'surname': 'Staunton-Collins'
     })
     db.session.add(new_customer)
     db.session.commit()
     c = Customer.query.first()
     assert repr(c) == "<Customer Robin Staunton-Collins>"
コード例 #7
0
 def test_account_owner(self, test_client):
     customer = Customer(name="Robin", surname="Staunton-Collins")
     db.session.add(customer)
     c = Customer.query.first()
     new_account = Account(account_number=1234, balance=50, owner=c)
     db.session.add(new_account)
     db.session.commit()
     acc = Account.query.first()
     assert acc.owner == c
コード例 #8
0
 def test_post_accounts(self, test_client):
     c = Customer(name="Robin", surname="Staunton-Collins")
     db.session.add(c)
     db.session.commit()
     c = Customer.query.first()
     response = test_client.post('/api/v1/accounts',
                                 json={
                                     'balance': 300_000,
                                     'customer_id': c.id
                                 })
コード例 #9
0
 def test_customer_accounts(self, test_client):
     customer = Customer(name="Robin", surname="Staunton-Collins")
     first_account = Account(account_number=1234,
                             balance=50,
                             owner=customer)
     second_account = Account(account_number=2345,
                              balance=14000,
                              owner=customer)
     db.session.add_all([customer, first_account, second_account])
     db.session.commit()
     c = Customer.query.first()
     assert c.accounts.all() == [first_account, second_account]
コード例 #10
0
def test_customers():
    customers = [
        {
            'name': "Robin",
            'surname': "Staunton-Collins"
        },
        {
            'name': "Jerry",
            'surname': "Seinfeld"
        },
    ]
    return [Customer().import_data(customer) for customer in customers]
コード例 #11
0
    def test_get_customer_info_data(self, test_client, url, data):
        customer = Customer(name="Monty", surname="Python")
        db.session.add(customer)
        db.session.commit()
        c = Customer.query.first()

        account = Account(account_number=1234, balance=100, customer_id=c.id)
        db.session.add(account)
        db.session.commit()
        a = Account.query.first()

        t1 = Transaction(amount=-20,
                         time=datetime(2020, 4, 20, 21),
                         account_id=a.id)
        t2 = Transaction(amount=50,
                         time=datetime(2020, 4, 19, 15),
                         account_id=a.id)
        db.session.add_all([t1, t2])
        db.session.commit()

        response = test_client.get(url, json=data)

        assert response.status_code == 200
        json = response.get_json()
        assert json['customer'] == {
            'name':
            "Monty",
            'surname':
            "Python",
            'accounts': [{
                'account_number':
                1234,
                'balance':
                100.0,
                'customer_id':
                c.id,
                'transactions': [
                    {
                        'amount': -20.0,
                        'account_id': 1,
                        'time': datetime(2020, 4, 20, 21).isoformat(),
                    },
                    {
                        'amount': 50,
                        'account_id': 1,
                        'time': datetime(2020, 4, 19, 15).isoformat(),
                    },
                ]
            }]
        }
コード例 #12
0
    def test_delete_customer(self, test_client):
        c = Customer(name="Robin", surname="Staunton-Collins")
        db.session.add(c)
        db.session.commit()

        c = Customer.query.first()
        assert c is not None

        response = test_client.delete(
            'api/v1/customers/1'
        )
        assert response.status_code == 200
        assert response.json == {'result': True}

        assert Customer.query.all() == []
コード例 #13
0
    def test_get_customer(self, test_client):
        c = Customer(name="Monty", surname="Python")
        db.session.add(c)
        db.session.commit()

        response = test_client.get(
            'api/v1/customers/1'
        )
        assert response.status_code == 200
        assert response.json == {
            'customer': {
                'name': "Monty",
                'surname': "Python",
                'uri': '/api/v1/customers/1'
            }
        }
コード例 #14
0
    def test_open_account_collision(self, test_client,
                                    mock_get_account_number):
        c = Customer(name="Robin", surname="Staunton-Collins")
        db.session.add(c)
        db.session.commit()
        c = Customer.query.first()
        a = Account(account_number='0123456789', customer_id=c.id)
        db.session.add(a)
        db.session.commit()

        response = test_client.post('/api/v1/openAccount',
                                    json={'customerID': str(c.id)})
        assert response.status_code == 403
        assert response.json == {
            'message': 'An account with that number already exists',
            'status_code': 403
        }
コード例 #15
0
    def test_put_customer(self, test_client):
        c = Customer(name="Robin", surname="Staunton-Collins")
        db.session.add(c)
        db.session.commit()

        response = test_client.put(
            'api/v1/customers/1',
            json={
                'name': "Monty",
                'surname': "Python"
            }
        )
        assert response.status_code == 200
        assert response.json == {
            'customer': {
                'name': "Monty",
                'surname': "Python",
                'uri': '/api/v1/customers/1'
            }
        }
コード例 #16
0
 def test_invalid_data(self):
     with pytest.raises(ValueError):
         Customer().import_data({
             'name': 0,
         })