Skip to content

Commit

Permalink
Make the cards a fixture to have them available when starting the app
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhiek187 committed Mar 29, 2021
1 parent 4e89d67 commit 80abd85
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ After cloning this repo and installing [Django](https://docs.djangoproject.com/e
3. Install all python dependencies: `pip3 install -r requirements.txt`
4. Head into the stockhelper directory: `cd stockhelper`
5. Create the SQLite databases: `python3 manage.py migrate`
6. Run the Django server: `python3 manage.py runserver`
7. Open `localhost:8000/stockapp` in your browser.
8. When done, press `CTRL/CMD-C` and deactivate the virtual environment: `deactivate`
6. Load the flashcards data: `python3 manage.py loaddata cards.json`
7. Run the Django server: `python3 manage.py runserver`
8. Open `localhost:8000/stockapp` in your browser.
9. When done, press `CTRL/CMD-C` and deactivate the virtual environment: `deactivate`
1 change: 1 addition & 0 deletions stockhelper/stockapp/fixtures/cards.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"model": "stockapp.card", "pk": "014474e4-b1ff-40b2-98e0-33fed80515cc", "fields": {"word": "Market Order", "definition": "A trade that's executed immediately after a deal is made"}}, {"model": "stockapp.card", "pk": "029e04db-5f99-4933-9283-03756d1bc980", "fields": {"word": "Mutual Fund", "definition": "An active, or managed, investment to purchase stocks from a fund manager"}}, {"model": "stockapp.card", "pk": "03f227c4-2ccf-46e8-945d-d74d91319bf3", "fields": {"word": "Liquidity", "definition": "A measure of cash a company has"}}, {"model": "stockapp.card", "pk": "0e497e7b-c8f5-4962-a01a-88b7d1b8e315", "fields": {"word": "Stop-Loss Order", "definition": "A trade that becomes a market order once a certain price is reached"}}, {"model": "stockapp.card", "pk": "14f20f26-5cee-4fe6-93db-407edc4b0f77", "fields": {"word": "Risk", "definition": "The probability of incurring a loss from an investment"}}, {"model": "stockapp.card", "pk": "2ee73373-177f-4fed-8d70-c24bfbfb07ad", "fields": {"word": "ETF", "definition": "Also known as an exchange-traded fund, an investment similar to mutual funds, except that stocks can be traded throughout the day, instead of at the end of the day"}}, {"model": "stockapp.card", "pk": "3a82e23c-6b9c-46ca-b32f-19320d830ff1", "fields": {"word": "Market Exchange", "definition": "A system where goods and services are exchanged"}}, {"model": "stockapp.card", "pk": "3b860f2f-3792-4c36-a482-c0863fc0cbe5", "fields": {"word": "Stop-Limit Order", "definition": "A trade that becomes a limit order once a certain price is reached"}}, {"model": "stockapp.card", "pk": "455c89b1-7726-4b39-bf03-5431307cceae", "fields": {"word": "Dividend Yield", "definition": "The ratio of a company's payment to shareholders (dividend) over the share price"}}, {"model": "stockapp.card", "pk": "4cad2432-36b5-41fa-b4fd-c2f21d691b6b", "fields": {"word": "ROI", "definition": "Also known as return on investment, the amount of profit made on an investment"}}, {"model": "stockapp.card", "pk": "52acdb71-b0aa-4fff-a8a4-2b22fab8ccbe", "fields": {"word": "P/E Ratio", "definition": "Also known as the price-to-earnings ratio, the ratio of a company's earning to the share price (share price / EPS)"}}, {"model": "stockapp.card", "pk": "54ccdee3-5701-4bfe-8823-5751e5162945", "fields": {"word": "Equity", "definition": "Also known as stocks, a measure of ownership of a company's assets"}}, {"model": "stockapp.card", "pk": "7b8a8999-b872-485a-913b-6997932072be", "fields": {"word": "Beta", "definition": "A measure of how much a stock is correlated with the market"}}, {"model": "stockapp.card", "pk": "85124f77-812d-4d0c-99e0-160b1addb8ed", "fields": {"word": "Volume", "definition": "A measure of how much a stock has been traded after a certain period of time"}}, {"model": "stockapp.card", "pk": "87a55b36-cb70-4d7b-83ff-5c1befa0ad28", "fields": {"word": "EPS", "definition": "Also known as earnings per share, the amount a company earns per outstanding share"}}, {"model": "stockapp.card", "pk": "8a8f87ab-3b71-4dcd-94d7-8e0644bda8e0", "fields": {"word": "Debt", "definition": "Also known as bonds, a loan given to a company that must be paid back at some point"}}, {"model": "stockapp.card", "pk": "8e75c51e-1522-4732-956d-57e5f6de0514", "fields": {"word": "Volatility", "definition": "A measure of fluctuations in the stock price"}}, {"model": "stockapp.card", "pk": "99e54934-08d1-4505-97b0-789850494b7f", "fields": {"word": "Portfolio", "definition": "A collection of investments held by a trader"}}, {"model": "stockapp.card", "pk": "a5a74ce2-b312-4d04-8927-c6450996b360", "fields": {"word": "Share Price", "definition": "The value of one stock"}}, {"model": "stockapp.card", "pk": "bb94ff5b-958a-4b36-beca-e52de2685bf4", "fields": {"word": "Market Cap", "definition": "The value of a company's shares (share price * # of shares)"}}, {"model": "stockapp.card", "pk": "bd043b9a-25ce-4d7f-94e8-48f799f3b6a2", "fields": {"word": "Trader", "definition": "A person requesting to buy or sell assets"}}, {"model": "stockapp.card", "pk": "c756e5a9-09d1-4432-ab3a-07b98c680620", "fields": {"word": "Index", "definition": "A measure of how a group of stocks are doing"}}, {"model": "stockapp.card", "pk": "d91a33f2-80eb-4d5d-8341-f7e9e0db6f0e", "fields": {"word": "Limit Order", "definition": "A trade that's executed at a certain price or better"}}, {"model": "stockapp.card", "pk": "dc658d39-0844-4de9-91aa-6b02a845c1b2", "fields": {"word": "Index Fund", "definition": "A passive investment to purchase stocks that follow a certain index"}}, {"model": "stockapp.card", "pk": "e4783829-8dda-45b2-8a08-14aff5dab429", "fields": {"word": "Broker", "definition": "A person that handles transactions between the trader and the exchange"}}, {"model": "stockapp.card", "pk": "f5d4ba1c-fc3e-4de0-8c99-2c4a9997c874", "fields": {"word": "Futures Contract", "definition": "An agreement to buy or sell an asset at a specified price and future date"}}]
21 changes: 18 additions & 3 deletions stockhelper/stockapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

# Create tests for each view
class HomeViewTests(TestCase):
# Load all the card data
fixtures = ["cards.json"]

# All test functions must start with test*
def test_view_renders(self):
# Check that the view renders properly
Expand All @@ -18,14 +21,14 @@ def test_view_renders(self):
self.assertContains(response, "navbar")
self.assertContains(response, "<a class=\"link-home nav-link active\" aria-current=\"page\"")
# Check that the relevant content on the home page is present
self.assertContains(response, "Welcome!")
self.assertContains(response, "Welcome to How to Stock!")
self.assertContains(response, "<strong>Home</strong>")
self.assertContains(response, "<strong>Screener</strong>")
self.assertContains(response, "<strong>Flashcards</strong>")
self.assertContains(response, "<strong>Portfolio</strong>")
self.assertContains(response, "Data provided by Financial Modeling Prep")
# Check that the correct context data is passed
self.assertQuerysetEqual(response.context["dummy_list"], [])
self.assertIn("equity", response.context["terms"])


class ScreenerViewTests(TestCase):
Expand All @@ -41,6 +44,10 @@ def test_view_renders(self):
# Check that the correct context data is passed
self.assertIsNotNone(response.context["form"])
self.assertEqual(response.context["results"], None)
self.assertEqual({
"beta", "dividendYield", "etf", "index", "indexFund", "marketCap", "marketExchange",
"mutualFunds", "sharePrice", "volume"
}, response.context["terms"].keys())

def test_post_form(self):
# Check that submitting the form produces results
Expand Down Expand Up @@ -112,6 +119,10 @@ def test_view_renders(self):
self.assertIsNotNone(response.context["profile"])
self.assertIsNotNone(response.context["history"])
self.assertEqual(response.context["balance"], self.session["balance"])
self.assertEqual({
"beta", "broker", "dividendYield", "marketCap", "marketExchange", "marketOrder", "risk",
"sharePrice", "trader", "volatility", "volume"
}, response.context["terms"].keys())

def test_buy_new_stock(self):
# Check that buying a stock with 0 shares creates a new Stock object
Expand Down Expand Up @@ -235,6 +246,7 @@ def test_view_renders(self):
self.assertEqual(response.context["balance"], session["balance"])
self.assertEqual(response.context["net_worth"], response.context["balance"])
self.assertQuerysetEqual(response.context["stocks"], [])
self.assertEqual({"portfolio", "roi", "sharePrice"}, response.context["terms"].keys())

def test_stocks_show(self):
# Check that the following Stock objects are rendered and the net worth changes
Expand All @@ -244,8 +256,11 @@ def test_stocks_show(self):
price=3000.00, change=-35.91)
old_stock3 = Stock.objects.create(ticker="GOOGL", name="Alphabet Inc", shares=2,
price=2000.00, change=-3.73)

response = self.client.get(reverse("stockapp:portfolio"))
session = self.client.session
session["balance"] = 10000
session.save()
new_stock1 = Stock.objects.get(pk=old_stock1.ticker)
new_stock2 = Stock.objects.get(pk=old_stock2.ticker)
new_stock3 = Stock.objects.get(pk=old_stock3.ticker)
Expand All @@ -272,7 +287,7 @@ def test_stocks_show(self):
net_worth = (session["balance"] + old_stock1.shares * new_stock1.price + old_stock2.shares
* new_stock2.price + old_stock3.shares * new_stock3.price)
# Convert the Decimal to a float
self.assertEqual(response.context["net_worth"], float(net_worth))
self.assertAlmostEqual(response.context["net_worth"], float(net_worth))
stock_list = [
f"<Stock: {new_stock1.ticker} - {new_stock1.name}>",
f"<Stock: {new_stock2.ticker} - {new_stock2.name}>",
Expand Down

0 comments on commit 80abd85

Please sign in to comment.