"""
TODO: use
import cgi
cgi.test()
to test script with HTTP headers and HTML. See docs.python.org/3/library/cgi.html
"""
import unittest, unittest.mock

""" create connection to API, test data service and query function """
import iteratequestionnaire as iq
iqcxn = iq.InfusionQuery()

class TestInfusionQuery(unittest.TestCase):
    ''' this class may be just to test InfusionQuery() class, with new test class for each component class '''

    def sampleQuery( ):
        """ makes 4 queries returning date created, leadsource and tags (groups) """
        table = 'Contact'
        returnFields = ['DateCreated', 'Leadsource']
        query = {'ContactType' : '%'}
        limit = 10
        page = 0
        datesource = iqcxn.infusionsoft.DataService('query', 'Contact',
                    10, 0, {'ContactType' : '%'}, ['DateCreated','Leadsource'])

        print(iqcxn.infusionsoft.DataService( 'query', table, limit,
                page, query, returnFields))
    
    def fullStringQuery_a():
    
        print(iqcxn.infusionsoft.DataService('query', 'Contact', 10, 0,
                {'ContactType' : '%'}, ['DateCreated','Leadsource'])) # returns an array of dicts
    
    def fullStringQuery_b():
    
        print(iqcxn.infusionsoft.DataService('query', 'Contact', 10, 0,
                {'ContactType' : '%'}, ['Groups'])) # get tags with 'Groups' field from 'Contact' table
    
    
    def main():
    
        tests = {'sample' : sampleQuery, 'query_a' : fullStringQuery_a, 'query_b' : fullStringQuery_b}
    
        for test in tests.keys():
            try:
                tests[test]()
            except Exception as detail:
                print("query failed: " + test + ", ", detail)

if __name__ == '__main__':
    unittest.main()

"""
TODO: use
unittest.mock
import cgi
cgi.test()
to test script with HTTP headers and HTML. See docs.python.org/3/library/cgi.html
"""
import unittest 

""" create connection to API, test data service and query function """
import iteratequestionnaire as iq
iqcxn = iq.InfusionQuery()

class TestInfusionQuery(unittest.TestCase):
    ''' this class may be just to test InfusionQuery() class, with new test class for each component class '''

    def test_sampleQuery(self):
        """ makes 4 queries returning date created, leadsource and tags (groups) """
        table = 'Contact'
        returnFields = ['DateCreated', 'Leadsource']
        query = {'ContactType' : '%'}
        limit = 10
        page = 0
        datesource = iqcxn.infusionsoft.DataService('query', 'Contact',
                    10, 0, {'ContactType' : '%'}, ['DateCreated','Leadsource'])

        print(iqcxn.infusionsoft.DataService( 'query', table, limit,
                page, query, returnFields))
    
    def test_fullStringQuery_a(self):
    
        print(iqcxn.infusionsoft.DataService('query', 'Contact', 10, 0,
                {'ContactType' : '%'}, ['DateCreated','Leadsource'])) # returns an array of dicts
    
    def test_fullStringQuery_b(self):
    
        print(iqcxn.infusionsoft.DataService('query', 'Contact', 10, 0,
                {'ContactType' : '%'}, ['Groups'])) # get tags with 'Groups' field from 'Contact' table
    

if __name__ == '__main__':
    unittest.main()

refactor to use unittest, remove old main() function