"""
Nose tests for acp_times based on 
rules from here https://rusa.org/pages/acp-brevet-control-times-calculator
"""
import os 

from pymongo import MongoClient
client = MongoClient('mongodb://' + os.environ['MONGODB_HOSTNAME'], 27017)
db = client.testdb

# Two test lists of dictionaries, three entries each?
# Order of insertion: brevDist, beginDate, dist, openTime, closeTime

test_list1 = [ {"brevDist" : "400", "beginDate": "2021-01-01T00:00", "dist": "100", 
				 "openTime": "2021-01-01T02:56", "closeTime": "2021-01-01T06:40"},
			   {"brevDist" : "400", "beginDate": "2021-01-01T00:00", "dist": "200", 
			   "openTime": "2021-01-01T05:53", "closeTime": "2021-01-01T13:20"},
			   {"brevDist" : "400", "beginDate": "2021-01-01T00:00", "dist": "300",  
			    "openTime": "2021-01-01T09:00" ,"closeTime": "2021-01-01T20:00"} 
			]

test_list2 = [ {"brevDist" : "600", "beginDate": "2021-05-01T12:00", "dist": "170", 
				 "openTime": "2021-05-01T17:00", "closeTime": "2021-05-01T23:20"},
			   {"brevDist" : "600", "beginDate": "2021-05-01T12:00", "dist": "340", 
			   "openTime": "2021-05-01T22:15", "closeTime": "2021-05-02T10:40"},
			   {"brevDist" : "600", "beginDate": "2021-05-01T12:00", "dist": "510",  
			   "openTime": "2021-05-02T03:48" , "closeTime": "2021-05-02T22:00"} 
			]

# You can test inserting times, and then 
# immediately try finding them (if you want to, maybe even delete afterwards?), does that make sense?

def test_submit_one():
	# https://www.geeksforgeeks.org/difference-between-insert-insertone-and-insertmany-in-pymongo/
	# insert() seems to be deprecated so using insert_many()

	# Inserting the documents in test_list1 into the test collection
	db.test.insert_many(test_list1)

	# TODO: Convert into for loops

	# find_one() - function returns only one value if the data is found in the database; else returns None
	assert db.test.find_one( {"dist" : "100"} ) != None
	assert db.test.find_one( {"dist" : "200"} ) != None
	assert db.test.find_one( {"dist" : "300"} ) != None

	assert db.test.find_one( {"dist" : "450"} ) == None

	# Opening Times
	assert db.test.find_one( {"openTime" : "2021-01-01T02:56"} ) != None
	assert db.test.find_one( {"openTime" : "2021-01-01T05:53"} ) != None
	assert db.test.find_one( {"openTime" : "2021-01-01T09:00"} ) != None

	assert db.test.find_one( {"openTime" : "2020-01-01T01:00"} ) == None

	# Closing Times
	assert db.test.find_one( {"closeTime" : "2021-01-01T06:40"} ) != None
	assert db.test.find_one( {"closeTime": "2021-01-01T13:20"} ) != None
	assert db.test.find_one( {"closeTime" : "2021-01-01T20:00"} ) != None

	assert db.test.find_one( {"closeTime" : "2020-01-01T:08:00"} ) == None

	# Clear the entire "test" collection
	db.test.delete_many({})

	# These should return None now
	assert db.test.find_one( {"dist" : "200"} ) == None
	assert db.test.find_one( {"openTime" : "2021-01-01T02:56"} ) == None
	assert db.test.find_one( {"closeTime" : "2021-01-01T20:00"} ) == None


def test_submit_two():
 	# Inserting the documents in test_list1 into the test collection
	db.test.insert_many(test_list2)

	# TODO: Convert into for loops

	# find_one() - function returns only one value if the data is found in the database; else returns None
	assert db.test.find_one( {"dist" : "170"} ) != None
	assert db.test.find_one( {"dist" : "340"} ) != None
	assert db.test.find_one( {"dist" : "510"} ) != None

	assert db.test.find_one( {"dist" : "650"} ) == None


	# Opening Times
	assert db.test.find_one( {"openTime" : "2021-05-01T17:00"} ) != None
	assert db.test.find_one( {"openTime" : "2021-05-01T22:15"} ) != None
	assert db.test.find_one( {"openTime" : "2021-05-02T03:48"} ) != None

	assert db.test.find_one( {"openTime" : "2010-01-01T01:00"} ) == None

	# Closing Times
	assert db.test.find_one( {"closeTime" : "2021-05-01T23:20"} ) != None
	assert db.test.find_one( {"closeTime" : "2021-05-02T10:40"} ) != None
	assert db.test.find_one( {"closeTime" : "2021-05-02T22:00"} ) != None

	assert db.test.find_one( {"closeTime" : "2020-01-01T:08:00"} ) == None


	# Clear the entire "test" collection
	db.test.delete_many({})

	# These should return None now
	assert db.test.find_one( {"dist" : "170"} ) == None
	assert db.test.find_one( {"openTime" : "2021-05-01T22:15"} ) == None
	assert db.test.find_one( {"closeTime" : "2021-01-01T20:00"} ) == None

