def get_liabilities(): # expecting query arguments "/api/[email protected]" email = request.args.get("email") store = InMemoryDataStore() liabilities = store.get_user_liabilities( email) # will raise an error if not found resulting in a 500 return jsonify(liabilities)
def test_server_get_user_assets_endpoint(self): server = BackendServer(host=self.host, port=self.port) server.start() # make request with no portfolio leading to a none resp = requests.get(f"http://{self.host}:{self.port}/api/get_assets", params={"email": "*****@*****.**"}) self.assertEqual(resp.status_code, 200) self.assertIsNone(resp.json()) # mock portfolio mock_port = utils.get_test_portfolio() InMemoryDataStore.update_user_portfolio("*****@*****.**", mock_port) # test resp = requests.get(f"http://{self.host}:{self.port}/api/get_assets", params={"email": "*****@*****.**"}) self.assertEqual(resp.status_code, 200) resp = resp.json() self.assertEqual(mock_port.cash_assets.total, resp["cash"]) self.assertEqual(mock_port.invested_assets.total, resp["invested"]) self.assertEqual(mock_port.use_assets.total, resp["use"]) server.stop()
def get_net_worth(): # expecting query arguments "/api/[email protected]" email = request.args.get("email") store = InMemoryDataStore() u = store.get_user( email) # will raise an error if not found resulting in a 500 return jsonify(u.net_worth)
def update_user_portfolio(): store = InMemoryDataStore() json_data = request.get_json(force=True) cash_json = json_data["cashAssets"] cash = CashAssets( cash_json["checkingAcs"], cash_json["savingsAcs"], cash_json["moneyMarketAccounts"], cash_json["savingsBonds"], cash_json["cds"], cash_json["lifeInsurance"], Other(cash_json["other"]["title"], cash_json["other"]["amount"])) invested_json = json_data["investedAssets"] invested = InvestedAssets( invested_json["brokerage"], Other(invested_json["otherTax"]["title"], invested_json["otherTax"]["amount"]), invested_json["ira"], invested_json["rothIra"], invested_json["k401"], invested_json["sepIra"], invested_json["keogh"], invested_json["pension"], invested_json["annuity"], invested_json["realEstate"], invested_json["soleProp"], invested_json["partnership"], invested_json["cCorp"], invested_json["sCorp"], invested_json["limitedLiabilityCompany"], Other(invested_json["otherBusiness"]["title"], invested_json["otherBusiness"]["amount"])) use_json = json_data["useAssets"] use = UseAssets( use_json["principalHome"], use_json["vacationHome"], use_json["vehicles"], use_json["homeFurnishings"], use_json["artsAndAntiques"], use_json["jewelryAndFurs"], Other(use_json["other"]["title"], use_json["other"]["amount"])) current_json = json_data["currentLiabilities"] current = CurrentLiabilities( current_json["creditCardBalance"], current_json["incomeTaxOwed"], Other(current_json["other"]["title"], current_json["other"]["amount"])) long_json = json_data["longTermLiabilities"] long = LongTermLiabilities( long_json["homeMortgage"], long_json["homeEquityLoan"], long_json["rentPropertiesMortgage"], long_json["carLoans"], long_json["studentLoans"], long_json["lifeInsurancePolicyLoans"], Other(long_json["other"]["title"], long_json["other"]["amount"])) currency = Currencies[json_data["currency"]] email = json_data["email"] store.update_user_portfolio(email=email, portfolio=Portfolio( currency, cash, invested, use, current, long)) return jsonify("Success")
def add_user(): store = InMemoryDataStore() user_json = request.get_json(force=True) store.add_user(firstname=user_json["firstname"], lastname=user_json["lastname"], age=user_json["age"], email=user_json["email"]) return jsonify("Success")
def tearDown(self): if os.path.isdir(self.TEST_STORAGE_PATH): for file in os.listdir(self.TEST_STORAGE_PATH): os.remove(os.path.join(self.TEST_STORAGE_PATH, file)) os.rmdir(self.TEST_STORAGE_PATH) # clear the contents after a test InMemoryDataStore.clear()
def test_update_user_portfolio_raises_error_for_invalid_user(self): users_file = os.path.join(self.TEST_STORAGE_PATH, "users.pck") store = InMemoryDataStore(users_file) cash, use, invested, current, long = utils.get_test_assets_and_liabilities( ) with self.assertRaises(ValueError): store.update_user_portfolio( "*****@*****.**", Portfolio(Currencies.GBP, cash, invested, use, current, long))
def test_server_get_user_liabilities_endpoint(self): server = BackendServer(host=self.host, port=self.port) server.start() # make request with no portfolio leading to a none resp = requests.get( f"http://{self.host}:{self.port}/api/get_liabilities", params={"email": "*****@*****.**"}) self.assertEqual(resp.status_code, 200) self.assertIsNone(resp.json()) mock_port = utils.get_test_portfolio() InMemoryDataStore().update_user_portfolio("*****@*****.**", mock_port) # test resp = requests.get( f"http://{self.host}:{self.port}/api/get_liabilities", params={"email": "*****@*****.**"}) self.assertEqual(resp.status_code, 200) resp = resp.json() self.assertEqual(mock_port.current_liabilities.total, resp["current"]) self.assertEqual(mock_port.long_term_liabilities.total, resp["long"]) server.stop()
def test_datastore_loads_pck_file(self): InMemoryDataStore( users_pck_file=os.path.join(self.TEST_STORAGE_PATH, "users.pck")) self.assertEqual(InMemoryDataStore._USERS[0].first_name, self.TEST_USER.first_name) self.assertEqual(InMemoryDataStore._USERS[0].last_name, self.TEST_USER.last_name) self.assertEqual(InMemoryDataStore._USERS[0].age, self.TEST_USER.age) self.assertEqual(InMemoryDataStore._USERS[0].email, self.TEST_USER.email)
def test_datastore_add_user(self): # create store with default data store = InMemoryDataStore( users_pck_file=os.path.join(self.TEST_STORAGE_PATH, "users.pck")) # add a new user store.add_user("Jane", "Doe", 18, "*****@*****.**") self.assertEqual(InMemoryDataStore._USERS[1].email, "*****@*****.**") # add an existing email with self.assertRaises(ValueError): store.add_user(self.TEST_USER.first_name, self.TEST_USER.last_name, self.TEST_USER.age, self.TEST_USER.email)
def test_datastore_get_user_returns_the_right_values(self): users_file = os.path.join(self.TEST_STORAGE_PATH, "users.pck") # check for no user in database with self.assertRaises(ValueError): store = InMemoryDataStore() store.get_user(self.TEST_USER.email) # check the right user is returned store = InMemoryDataStore(users_file) u = store.get_user(self.TEST_USER.email) self.assertEqual(u.first_name, self.TEST_USER.first_name) self.assertEqual(u.last_name, self.TEST_USER.last_name) self.assertEqual(u.age, self.TEST_USER.age) # check that editing the user doesn't change the database user u.first_name = "YuGiOh" self.assertEqual(InMemoryDataStore._USERS[0].first_name, self.TEST_USER.first_name)
def test_update_user_portfolio_persists_change(self): cash, use, invested, current, long = utils.get_test_assets_and_liabilities( ) p = Portfolio(Currencies.GBP, cash, invested, use, current, long) users_file = os.path.join(self.TEST_STORAGE_PATH, "users.pck") store = InMemoryDataStore(users_pck_file=users_file) self.assertIsNone(None, InMemoryDataStore._USERS[0].portfolio) store.update_user_portfolio("*****@*****.**", p) self.assertEqual(Currencies.GBP, InMemoryDataStore._USERS[0].portfolio.currency) p.currency = Currencies.USD store.update_user_portfolio("*****@*****.**", p) self.assertEqual(Currencies.USD, InMemoryDataStore._USERS[0].portfolio.currency)
def test_datastore_persists_to_disk(self): users_file = os.path.join(self.TEST_STORAGE_PATH, "users.pck") # get contents of the current file with open(users_file, "rb") as fp: orig_users = pickle.load(fp) # create store and populate with file, then add user store = InMemoryDataStore(users_pck_file=users_file) store.add_user("Jane", "Doe", 18, "*****@*****.**") # persist to disk store.save_to_disk(loc=self.TEST_STORAGE_PATH) # get contents and check that list is appended with open(users_file, "rb") as fp: new_users = pickle.load(fp) self.assertGreater(len(new_users), len(orig_users)) self.assertIsNone(new_users[1].portfolio)
import os from backend import STORAGE_PATH from backend.models import CashAssets, UseAssets, InvestedAssets, Other, Portfolio from backend.models import CurrentLiabilities, LongTermLiabilities, Currencies from backend.server import BackendServer from backend.datastore import InMemoryDataStore if __name__ == "__main__": # figure out if a folder exists - if it doesn't then create one if not os.path.isdir(STORAGE_PATH): os.makedirs(STORAGE_PATH) if "users.pck" in os.listdir(STORAGE_PATH): # load data into memory InMemoryDataStore( users_pck_file=os.path.join(STORAGE_PATH, "users.pck")) else: InMemoryDataStore().add_user("John", "Doe", 29, "*****@*****.**") cash = CashAssets(1, 2, 3, 4, 5, 6, Other("Test", 7)) use = UseAssets(10, 20, 30, 40, 50, 60, Other("TestUse", 70)) invested = InvestedAssets(1.5, Other("TestInvest", 2.5), 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, Other("TestInvest", 16.5)) current = CurrentLiabilities(0, 111.1, Other("TestCurrent", 21.3)) long = LongTermLiabilities(10, 20, 30, 40, 50, 60, Other(amount=70)) p = Portfolio(Currencies.GBP, cash, invested, use, current, long) InMemoryDataStore().update_user_portfolio("*****@*****.**", p)
def tearDown(self): store = InMemoryDataStore() store.clear()
def setUp(self): self.host = "localhost" self.port = 50123 # add to the datastore store = InMemoryDataStore() store.add_user("John", "Doe", 23, "*****@*****.**")