from .repository_base import RepositoryBase
import sys
import traceback

# Get database instance
db = RepositoryBase()
collection = "trainingdata"

######################################################
# Test all basic methods/queries from RepositoryBase #
######################################################

# NOTE, RepositoryBase was created with the purpose of adding methods 
# with basic queries 


print("\n\nTesting 'find_one' query...")
find_one_result = db.find_one(collection, {"question_id": "0", "topic": "passage"})
print(find_one_result)


print("\n\nTesting 'find' query...")
find_results = db.find(collection, {"question_id": "0"})
for result in find_results:
    print(result)


print("\n\nTesting 'insert_one' query...")
try:
    trainingentry1 = {'question_id': "1000",'topic': "topicTest",'answer': "answerTest"}
    db.insert_one(collection, trainingentry1)
    print("Data was inserted successfully to database.")
except:
    sys.stderr.write('ERROR, record was not inserted successfully')
    print(traceback.format_exc())
print("Retrieving inserted data...")
find_one_result = db.find_one(collection, {"question_id": "0", "topic": "passage"})
print(find_one_result)


print("\n\nTesting 'delete_one' query...")
trainingentry1 = {"question_id": "1000", "topic": "topicTest","answer": "answerTest"}
try:
    db.delete_one(collection, trainingentry1)
    print("Data was deleted successfully")
except:
    print("ERROR, Data was not deleted bc it does not exist, or an error occurred")