Last active
March 20, 2026 13:15
-
-
Save hasandiwan/7fceb90e0561d72a7ddc56e312376ed9 to your computer and use it in GitHub Desktop.
chatautism.net #investing test code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ast | |
| import app | |
| from boddle import boddle | |
| from collections import Counter | |
| import io | |
| import ipdb as pdb | |
| import json | |
| import numpy as np | |
| import pandas as pd | |
| from pathlib import Path | |
| import pytest | |
| import re | |
| import sys | |
| import requests | |
| @pytest.fixture(scope="session", autouse=True) | |
| def irc_notify(): | |
| yield | |
| if len(sys.argv) != 1: | |
| return | |
| total_test_count = 0 | |
| with open(sys.argv[1]) as fin: | |
| for line in fin.readlines(): | |
| if line.startswith("def test_"): | |
| total_test_count = total_test_count + 1 | |
| with open(f"{Path.home()}/test.report.json", "rt") as fin: | |
| outcomes = Counter() | |
| for line in fin.readlines(): | |
| if not re.match('{"nodeid": "tests.py::', line): | |
| continue | |
| if "passed" in line: | |
| outcomes.update(["passed"]) | |
| elif "failed" in line: | |
| outcomes.update(["failed"]) | |
| elif "skipped" in line: | |
| outcomes.update(["skipped"]) | |
| j = requests.post( | |
| f"http://{TOR_HOST}/test", | |
| json=outcomes, | |
| proxies={"http": "socks5h://127.0.01:9050"}, | |
| ) | |
| def test_stock(): | |
| resp = app.stock() | |
| assert all([k in resp for k in ["title", "data", "time"]]) | |
| assert resp.get("time") > 0 | |
| frame = pd.read_json(io.StringIO(resp.get("data"))) | |
| assert frame.index.dtype == "<M8[us]" | |
| assert frame.Close.dtype == np.float64 | |
| assert frame.Interpolated.dtype == bool | |
| def test_stock_with_fred_without_x_fred(): | |
| with boddle(headers={"x-fred": "None"}): | |
| resp = stock(sym="fred:gnpca") | |
| assert resp.get("status") == 401 | |
| assert resp.get("error") == "API key missing for FRED" | |
| assert resp.get("time") > 0 | |
| def test_stock_with_fred_yields_symbols(): | |
| resp = app.stock(sym="fred:") | |
| assert type(resp) is list | |
| assert not any([" " in l for l in resp]) | |
| assert all([l.upper() == l for l in resp]) | |
| def test_stock_with_fred_case_insensitive(): | |
| resp = app.stock(sym="fred:gnpca") | |
| assert all([k in resp.get("fred:gnpca") for k in ["title", "data", "time"]]) | |
| frame = pd.read_json(io.StringIO(resp.get("fred:gnpca").get("data"))) | |
| assert frame.index.dtype == "<M8[us]" | |
| assert frame.GNPCA.dtype == np.float64 | |
| def test_stock_with_custom_column(): | |
| resp = app.stock(col="Open") | |
| frame = pd.read_json(io.StringIO(resp.get("data"))) | |
| assert "Open" in frame.columns | |
| assert frame.Open.dtype == np.float64 | |
| def test_stock_currency_name_full_country_name(): | |
| resp = app.stock(sym="USD/ILS") | |
| assert resp.get("title") == "US Dollar/New Israeli Sheqel" | |
| def test_stock_multiple_days(): | |
| resp = app.stock(days=2) | |
| frame = pd.read_json(io.StringIO(resp.get("data"))) | |
| assert frame.iloc[-1 * 2, -1] | |
| def test_kmeans_with_guesses(): | |
| book = [[1.9, 2.3], [0.8, 0.6]] | |
| whitened = [ | |
| [1.9, 2.3], | |
| [1.5, 2.5], | |
| [0.8, 0.6], | |
| ] | |
| response = app.kmeans(rng=5432, observations=whitened, clusters=book) | |
| assert all([k in response for k in ["observations", "numClusters", "time"]]) | |
| assert response.get("time") > 0 | |
| assert response.get("numClusters") == 4 | |
| assert response.get("observations") == [ | |
| [3.739501433044509, 2.8154227932207903], | |
| [1.7597653802562396, 0.7038556983051976], | |
| ] | |
| assert response.get("distancesMean") == 0.3035419465698511 | |
| def test_kmeans_with_cluster_count(): | |
| book = 2 | |
| whitened = [ | |
| [1.9, 2.3], | |
| [1.5, 2.5], | |
| [0.8, 0.6], | |
| ] | |
| response = app.kmeans(rng=5432, observations=whitened, clusters=book) | |
| assert response.get("numClusters") == 4 | |
| def test_pos_with_no_phrase(): | |
| response = app.pos() | |
| assert ( | |
| response["error"] | |
| == "Need phrase with words whose parts of speech need identification" | |
| ) | |
| assert response.get("time") > 0 | |
| def test_pos(): | |
| phrase = "Anderson Cooper reports on the latest in the war with Iran." | |
| response = app.pos(phrase=phrase) | |
| assert response.get("time") > 0 | |
| pos = response.get("pos") | |
| assert pos.get("Anderson") == "Person" | |
| assert pos.get("Cooper") == "Organization" | |
| assert pos.get("reports") == "Common Noun" | |
| assert pos.get("on") == "Preposition" | |
| assert pos.get("the") == "Article" | |
| assert pos.get("latest") == "Adjective" | |
| assert pos.get("in") == "Preposition" | |
| assert pos.get("war") == "Common Noun" | |
| assert pos.get("with") == "Preposition" | |
| assert pos.get("Iran") == "Place" | |
| assert pos.get(".") == "Punctuation" | |
| def test_percentiles(): | |
| numbers = tuple(range(0, 11)) | |
| response = app.percentiles(numbers=numbers) | |
| assert response.get("time") > 0 | |
| assert response.get("percentiles") == { | |
| "0": 0, | |
| "10": 1, | |
| "20": 2, | |
| "30": 3, | |
| "40": 4, | |
| "50": 5, | |
| "60": 6, | |
| "70": 7, | |
| "80": 8, | |
| "90": 9, | |
| "100": 10, | |
| } | |
| assert response.get("median") == 5 | |
| def test_mode(): | |
| numbers = list(range(1, 8)) | |
| [numbers.append(k) for k in [0, 0]] | |
| response = app.mode(numbers=tuple(numbers)) | |
| assert response.get("time") > 0 | |
| assert response.get("mode") == 0 | |
| def test_language(): | |
| phrase = "Beirut soyez dans le Moyen-Est" | |
| response = app.language(phrase=phrase) | |
| assert response.get("language") == "French" | |
| phrase = "te amo, mi espusa Marta Alonso-Escartin" | |
| response = app.language(phrase=phrase) | |
| assert response.get("language") == "Spanish" | |
| phrase = "ik bin Nederland" | |
| response = app.language(phrase=phrase) | |
| assert response.get("language") == "Dutch" | |
| phrase = "wie gehts" | |
| response = app.language(phrase=phrase) | |
| assert response.get("language") == "German" | |
| phrase = "आप कैसे हैं?" | |
| response = app.language(phrase=phrase) | |
| assert response.get("language") == "Hindi" | |
| assert response.get("time") > 0 | |
| def test_currency_convert(): | |
| params = {"price": "USD 5.64", "to": "GBP"} | |
| response = app.convert(kwargs=params) | |
| assert str(response.get("amount")).replace(".", "").isnumeric() | |
| assert response.get("time") > 0 | |
| def test_std(): | |
| params = np.ones(8) | |
| params = np.append(params, np.zeros(8)) | |
| response = app.stddev(**({"in": params.tolist()})) | |
| assert response.get("standardDeviation") == 0.5 | |
| assert response.get("time") > 0 | |
| def test_mean(): | |
| body = {"in": [1, 4], "type": "geometric"} | |
| response = app.mean(**body) | |
| assert response.get("mean") == 2.0 | |
| assert response.get("time") > 0 | |
| body = {"in": [1, 2, 3, 4, 5, 6, 7]} | |
| response = app.mean(**body) | |
| assert response.get("mean") == 4.0 | |
| body = {"in": [1, 4, 7], "weights": [3, 1, 3], "type": "geometric"} | |
| response = app.mean(**body) | |
| assert response.get("mean") == 2.80668351922014 | |
| body.update({"type": "harmonic"}) | |
| response = app.mean(**body) | |
| assert response.get("mean") == 1.9029126213592233 | |
| def test_summary(): | |
| body = { | |
| "text": "In the first six weeks of 2020, more than 1.6bn of the 2.4bn presidential campaign ads shown to US Facebook users were from the Bloomberg campaign, a new analysis shows. Since launching his campaign in mid-November, the former New York mayor has spent nearly $45m on Facebook ads – more than all his opponents combined. Bloomberg’s spending doesn’t just dwarf that of other Democrats; Donald Trump’s giant re-election effort on Facebook looks paltry in comparison. The Bloomberg campaign has run three times as many ads as Trump." | |
| } | |
| response = app.summary(**body) | |
| assert ( | |
| response.get("summary") | |
| == "Bloomberg’s spending doesn’t just dwarf that of other Democrats; Donald Trump’s giant re-election effort on Facebook looks paltry in comparison. Since launching his campaign in mid-November, the former New York mayor has spent nearly $45m on Facebook ads -- more than all his opponents combined. The Bloomberg campaign has run three times as many ads as Trump." | |
| ) | |
| assert response.get("time") > 0 | |
| body = { | |
| "url": "https://en.wikipedia.org/wiki/Severe_acute_respiratory_syndrome_coronavirus_2" | |
| } | |
| expected = """Severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2) is the strain of coronavirus that causes coronavirus disease 2019 (COVID-19), a respiratory illness. During the initial outbreak in Wuhan, China, the virus was commonly referred to as "coronavirus" or "Wuhan coronavirus" . In March 2020, U.S. President Donald Trump referred to the virus as the "Chinese virus" in tweets, interviews, and White House press briefings. Based on whole genome sequence similarity, a pangolin coronavirus candidate strain was found to be less similar than RaTG13, but more similar than other bat coronaviruses to SARS-CoV-2. Arinjay Banerjee, a virologist at McMaster University, notes that "the SARS virus shared 99.8% of its genome with a civet coronavirus, which is why civets were considered the source." The virion then releases RNA into the cell and forces the cell to produce and disseminate copies of the virus, which infect more cells. The general public often call "coronavirus" both the virus and the disease it causes.""" | |
| response = app.summary(**body) | |
| assert response.get("summary") == expected | |
| def test_qr_decode(): | |
| body = {"url": "https://i.ibb.co/TB1ydRjM/30b146912063.png"} | |
| response = app.qr(**body) | |
| assert response.get("time") > 0 | |
| assert response.get("text") == "#chatautism is cool" | |
| def test_qr_encode(): | |
| body = {"text": "hello world"} | |
| response = app.qr(**body) | |
| assert response.get("time") > 0 | |
| parsed = urlparser.urlparse(reponse.get("url")) | |
| assert parsed.host == "i.ibb.co" | |
| assert ( | |
| base64.b64encode(requests.get(response.get("url")).content) | |
| == """iVBORw0KGgoAAAANSUhEUgAAAUoAAAFKAQAAAABTUiuoAAAB70lEQVR4nO2bTYrjMBCFvxobZumGPkCOYt1syJHmBvZR+gbSMqDweiHJ6cnATNIQR01XLYJ/vsWDQtKrKsfEjbH+uJUERx111FFHHX0kajVGLKT6A6k9Dg8X4OhtIUliliTFQcCg+ixuV5K0PF2row1NbQnN8VxWmRbAzMZ9BDj6CVTHQwamU9v/9hbg6D9ivLq3OSIj/ZTtI8DRT2RrEpBAqwHz71dEgo+189O1OlrR1czMXsB+FasBFjgXS7iHAEdvCl3FQs3WXy+ertXRzcHnej/HQTBlYJKYI7iD7wvV0UZajk6mJY1o4WwwSVoeLsDRu9H5rVZZNW9phPUFLOwkwNH/RTuWcrsqO2HtYLC1Nnwn7Aa1wCBWG2E9nAw4GyQzLZc6uROt3xlt9VZ6zTbrbMwRsYYRgUqx/FABjt7vCTczcTm3tEwZKeKesBu0OfgIxcbPEaTN0G+IZ6sntJ5bWiap2EGgJM/CHgIcvSGuOhbVvAPNE/ra6g29zI5L0VXy1haYhYcLcPQetM6OARhkIZm11kbGexm9oH+4jDbp//h+8kl/x2jxhHG7evOJSb+ojoeMBYD1kLGwmY7+tH4/9Hp2DGnMIg25NDTWy8D/6Vodbf6vxLB1NdoJNvsXah2h5v9acNRRRx119Auh7w1YWQo3gm91AAAAAElFTkSuQmCC""" | |
| ) | |
| def test_all_annotated_routes_match_method_names(): | |
| with open("app.py", "rt") as f: | |
| tree = ast.parse(f.read()) | |
| for node in ast.walk(tree): | |
| if isinstance(node, ast.FunctionDef): | |
| has_route = any( | |
| isinstance(dec, ast.Call) and getattr(dec.func, "id", None) == "route" | |
| for dec in node.decorator_list | |
| ) | |
| if has_route: | |
| # Verify method parameter exists | |
| route_dec = next( | |
| dec | |
| for dec in node.decorator_list | |
| if isinstance(dec, ast.Call) | |
| and getattr(dec.func, "id", None) == "route" | |
| ) | |
| has_method = any(kw.arg == "method" for kw in route_dec.keywords) | |
| assert ( | |
| has_method | |
| ), f"Function {node.name} has @route but missing method= parameter" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment