From 44e72490d07bd3934726af63c916d6370decec68 Mon Sep 17 00:00:00 2001 From: Stephen Mordue Date: Fri, 4 Aug 2023 16:11:07 -0700 Subject: [PATCH 1/7] Building CRUD functions. Added pytest --- .gitignore | 1 + Pipfile | 12 ++++ Pipfile.lock | 29 ++++++++ app/data.py | 76 +++++++++++++++++++- app/main.py | 5 +- app/templates/home.html | 1 + install.sh | 0 requirements.txt | 1 + run.sh | 2 +- tests/__init__.py | 0 tests/test_data_class.py | 147 +++++++++++++++++++++++++++++++++++++++ tickets/firstTicket.md | 10 +-- 12 files changed, 273 insertions(+), 11 deletions(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock mode change 100644 => 100755 install.sh mode change 100644 => 100755 run.sh create mode 100644 tests/__init__.py create mode 100644 tests/test_data_class.py diff --git a/.gitignore b/.gitignore index 3775733..5814112 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ crashlytics-build.properties fabric.properties .idea/httpRequests .idea/caches/build_file_checksums.ser +.idea/ .DS_Store .AppleDouble .LSOverride diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..55aac82 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +python-dotenv = "*" + +[dev-packages] + +[requires] +python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..1e71e8e --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,29 @@ +{ + "_meta": { + "hash": { + "sha256": "580cb6a9b28d54ce847142d6c2dfcdf68fdc46d1cd31141832b3b21d4776b3fb" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "python-dotenv": { + "hashes": [ + "sha256:a8df96034aae6d2d50a4ebe8216326c61c3eb64836776504fcca410e5937a3ba", + "sha256:f5971a9226b701070a4bf2c38c89e5a3f0d64de8debda981d1db98583009122a" + ], + "index": "pypi", + "version": "==1.0.0" + } + }, + "develop": {} +} diff --git a/app/data.py b/app/data.py index 61e69e5..bf245d5 100644 --- a/app/data.py +++ b/app/data.py @@ -1,4 +1,6 @@ from os import getenv +from typing import Dict, Iterable, Iterator +from random import randrange from certifi import where from dotenv import load_dotenv @@ -8,6 +10,54 @@ class Database: + def __init__(self): + # Load environmental variables + load_dotenv() + + # Create a connection to the MongoDB server + # client = MongoClient(getenv("DB_URL"), tlsCAFile=where())['Database'] + # client = MongoClient('localhost', 27017) + self.client = MongoClient(getenv("DB_URL"), tlsCAFile=where()) + + # Select the database + self.db = self.client['Database'] + + # Select the collection + self.collection = self.db['Monsters'] + + # def __init__(self, collection: str): + # self.collection = self.database[collection] + # + def create_one(self, record: Dict = None) -> bool: + if record is None: + record = Monster().to_dict() + return self.collection.insert_one(record).acknowledged + + def read_one(self, query: Dict = None) -> Dict: + if query is None: + pipeline = [ + {'$sample': {'size': 1}} + ] + record = list(self.collection.aggregate(pipeline)) + return self.collection.find_one(query, {"_id": False}) + + def update_one(self, query: Dict, update: Dict) -> bool: + return self.collection.update_one(query, {"$set": update}).acknowledged + + def delete_one(self, query: Dict) -> bool: + return self.collection.delete_one(query).acknowledged + + def create_many(self, records: Iterable[Dict]) -> bool: + return self.collection.insert_many(records).acknowledged + + # def read_many(self, query: Dict) -> Iterator[Dict]: + # return self.collection.find(query, {"_id": False}) + # + # def update_many(self, query: Dict, update: Dict) -> bool: + # return self.collection.update_many(query, {"$set": update}).acknowledged + # + # def delete_many(self, query: Dict) -> bool: + # return self.collection.delete_many(query).acknowledged def seed(self, amount): pass @@ -16,10 +66,32 @@ def reset(self): pass def count(self) -> int: - pass + # client = MongoClient(getenv("DB_URL"), tlsCAFile=where()) + # db = client['Database'] + # collection = db['Monsters'] + # Use the count_documents method to get the count of documents in the collection + count = self.collection.count_documents({}) # {} means no filter, so it counts all documents + print(f'There are {count} documents in the collection.') + return count def dataframe(self) -> DataFrame: pass def html_table(self) -> str: - pass + return 'here\'s the table' + +if __name__ == '__main__': + #db = Database("Collection") + db = Database() + #db.create_many({"Value": randrange(1, 100)} for _ in range(10)) + #print(DataFrame(db.read_many({}))) + print(db.client) + #databases = db.client.list_database_names() + #print(databases) + + # use list_collection_names method to get a list of all collection names + collections = db.client.list_collection_names() + + # print the collections + for collection in collections: + print(collection) \ No newline at end of file diff --git a/app/main.py b/app/main.py index 1f9e0b0..ebefced 100644 --- a/app/main.py +++ b/app/main.py @@ -10,10 +10,9 @@ from app.graph import chart from app.machine import Machine -SPRINT = 0 +SPRINT = 1 APP = Flask(__name__) - @APP.route("/") def home(): return render_template( @@ -97,4 +96,4 @@ def model(): if __name__ == '__main__': - APP.run() + APP.run(debug=True) diff --git a/app/templates/home.html b/app/templates/home.html index ae82ee4..e809c8d 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -9,6 +9,7 @@

Data Science Team

  • April Fairweather: Database Engineer
  • Thackery Binx: Data Analyst
  • Eugene Albright: Machine Learning Engineer
  • +
  • Stephen Mordue: Data Scientist Student, Bloomtech 2023
  • Tech Stack