Best Python code snippet using pytest-django_python
datasetTest.py
Source:datasetTest.py  
1# =============================================================================2# AUSTRALIAN NATIONAL UNIVERSITY OPEN SOURCE LICENSE (ANUOS LICENSE)3# VERSION 1.34# 5# The contents of this file are subject to the ANUOS License Version 1.36# (the "License"); you may not use this file except in compliance with7# the License. You may obtain a copy of the License at:8# 9#   https://sourceforge.net/projects/febrl/10# 11# Software distributed under the License is distributed on an "AS IS"12# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See13# the License for the specific language governing rights and limitations14# under the License.15# 16# The Original Software is: "datasetTest.py"17# 18# The Initial Developer of the Original Software is:19#   Dr Peter Christen (Research School of Computer Science, The Australian20#                      National University)21# 22# Copyright (C) 2002 - 2011 the Australian National University and23# others. All Rights Reserved.24# 25# Contributors:26# 27# Alternatively, the contents of this file may be used under the terms28# of the GNU General Public License Version 2 or later (the "GPL"), in29# which case the provisions of the GPL are applicable instead of those30# above. The GPL is available at the following URL: http://www.gnu.org/31# If you wish to allow use of your version of this file only under the32# terms of the GPL, and not to allow others to use your version of this33# file under the terms of the ANUOS License, indicate your decision by34# deleting the provisions above and replace them with the notice and35# other provisions required by the GPL. If you do not delete the36# provisions above, a recipient may use your version of this file under37# the terms of any one of the ANUOS License or the GPL.38# =============================================================================39#40# Freely extensible biomedical record linkage (Febrl) - Version 0.4.241#42# See: http://datamining.anu.edu.au/linkage.html43#44# =============================================================================45"""Test module for dataset.py.46"""47# =============================================================================48# Import necessary modules (Python standard modules first, then Febrl modules)49import logging50import os51import string52import sys53import unittest54sys.path.append('..')55import dataset56log_level = logging.WARNING  # logging.INFO57# =============================================================================58class TestCase(unittest.TestCase):59  # Initialise test case  - - - - - - - - - - - - - - - - - - - - - - - - - - -60  #61  def setUp(self):62    pass  # Nothing to initialise63  # Clean up test case  - - - - - - - - - - - - - - - - - - - - - - - - - - - -64  #65  def tearDown(self):66    pass  # Nothing to clean up67  # ---------------------------------------------------------------------------68  # Start test cases69  def testCSV(self):   # - - - - - - - - - - - - - - - - - - - - - - - - - - -70    """Test CSV data set"""71    for test_file in ['./test-data.csv','./test-data.csv.gz']:72      # Initialise data set for reading73      #74      test_ds = dataset.DataSetCSV(description='A test CSV data set',75                                   access_mode='read',76                                   field_list=[('rec-id',0),('gname',1),77                                               ('surname',2),('streetnumb',3),78                                               ('streetname_type',4),79                                               ('suburb',5),('postcode',6)],80                                   rec_ident='rec-id',81                                   header_line=False,82                                   write_header=True,83                                   file_name=test_file)84      assert test_ds.dataset_type == 'CSV', \85             'Test data set has wrong type (should be "CSV"): "%s"' % \86             (str(test_ds.dataset_type))87      assert isinstance(test_ds.field_list,list), \88             'CSV data set field list is not of a list: %s' % \89             (str(test_ds.field_list))90      assert test_ds.access_mode == 'read', \91             'CSV data set has wrong access mode (should be "read"): %s' % \92             (str(test_ds.access_mode))93      assert isinstance(test_ds.file_name, str), \94             'CSV data set file name is not a string: %s, %s' % \95             (type(test_ds.file_name), str(test_ds.file_name))96      assert test_ds.num_records == 21, \97             'CSV data set has wrong number of records (should be 21): %d' % \98             (test_ds.num_records)99      assert test_ds.next_rec_num == 0, \100             'CSV data set has wrong next record number (should be 0): %d' % \101             (test_ds.next_rec_num)102      # Read a single record103      #104      test_rec_dict = test_ds.read()105      assert test_ds.next_rec_num == 1, \106             'CSV data set has wrong next record number (should be 1): %d' % \107             (test_ds.next_rec_num)108      assert isinstance(test_rec_dict, dict), \109             'Record returned is not of type dictionary: %s, %s' % \110             (type(test_rec_dict), str(test_rec_dict))111      assert len(test_rec_dict) == 1, \112             'More or less than one record returned: %d, %s' % \113             (len(test_rec_dict), str(test_rec_dict))114      # Read another single record115      #116      test_rec_dict = test_ds.read()117      assert test_ds.next_rec_num == 2, \118             'CSV data set has wrong next record number (should be 2): %d' % \119             (test_ds.next_rec_num)120      assert isinstance(test_rec_dict, dict), \121             'Record returned is not of type dictionary: %s, %s' % \122             (type(test_rec_dict), str(test_rec_dict))123      assert len(test_rec_dict) == 1, \124             'More or less than one record returned: %d, %s' % \125             (len(test_rec_dict), str(test_rec_dict))126      # Read three records127      #128      test_rec_dict = test_ds.read(3)129      assert test_ds.next_rec_num == 5, \130             'CSV data set has wrong next record number (should be 5): %d' % \131             (test_ds.next_rec_num)132      assert isinstance(test_rec_dict, dict), \133             'Record returned is not of type dictionary: %s, %s' % \134             (type(test_rec_dict), str(test_rec_dict))135      assert len(test_rec_dict) == 3, \136             'More or less than three record returned: %d, %s' % \137             (len(test_rec_dict), str(test_rec_dict))138      # Read twenty records (as there are only 21 in the data set only 16139      # should be returned, as 5 have been read before)140      #141      test_rec_dict = test_ds.read(20)142      assert test_ds.next_rec_num == 21, \143             'CSV data set has wrong next record number (should be 21): %d' % \144             (test_ds.next_rec_num)145      assert isinstance(test_rec_dict, dict), \146             'Record returned is not of type dictionary: %s, %s' % \147             (type(test_rec_dict), str(test_rec_dict))148      assert len(test_rec_dict) == 16, \149             'More or less than sixteen record returned: %d, %s' % \150             (len(test_rec_dict), str(test_rec_dict))151      # Read records 3 records from record number 15 onwards152      #153      test_rec_dict = test_ds.read(15,3)154      assert test_ds.next_rec_num == 18, \155             'CSV data set has wrong next record number (should be 18): %d' % \156             (test_ds.next_rec_num)157      assert isinstance(test_rec_dict, dict), \158             'Record returned is not of type dictionary: %s, %s' % \159             (type(test_rec_dict), str(test_rec_dict))160      assert len(test_rec_dict) == 3, \161             'More or less than three record returned: %d, %s' % \162             (len(test_rec_dict), str(test_rec_dict))163      # Read records 5 records from record number 2 onwards164      #165      test_rec_dict = test_ds.read(2,5)166      assert test_ds.next_rec_num == 7, \167             'CSV data set has wrong next record number (should be 7): %d' % \168             (test_ds.next_rec_num)169      assert isinstance(test_rec_dict, dict), \170             'Record returned is not of type dictionary: %s, %s' % \171             (type(test_rec_dict), str(test_rec_dict))172      assert len(test_rec_dict) == 5, \173             'More or less than five record returned: %d, %s' % \174             (len(test_rec_dict), str(test_rec_dict))175      # Read all records176      #177      all_test_rec_dict = test_ds.read(0,30)178      assert test_ds.next_rec_num == 21, \179             'CSV data set has wrong next record number (should be 21): %d' % \180             (test_ds.next_rec_num)181      assert isinstance(all_test_rec_dict, dict), \182             'Record returned is not of type dictionary: %s, %s' % \183             (type(all_test_rec_dict), str(all_test_rec_dict))184      assert len(all_test_rec_dict) == 21, \185             'More or less than tenty-one record returned: %d, %s' % \186             (len(all_test_rec_dict), str(all_test_rec_dict))187      test_ds.finalise()188    # Initialise data set for writing (with quotes set) - - - - - - - - - - - -189    #190    try:191      os.remove('./test-data2.csv')  # Remove previously written test data set192    except:193      pass194    test_ds = dataset.DataSetCSV(description='A test CSV data set',195                                 access_mode='write',196                                 field_list=[('rec-id',0),('gname',1),197                                             ('surname',2),('streetnumb',3),198                                             ('streetname_type',4),199                                             ('suburb',5),('postcode',6)],200                                 rec_ident='rec-id',201                                 header_line=True,202                                 write_header=True,203                                 write_quote="'",204                                 file_name='./test-data2.csv')205    assert test_ds.dataset_type == 'CSV', \206           'Test data set has wrong type (should be "CSV"): "%s"' % \207           (str(test_ds.dataset_type))208    assert isinstance(test_ds.field_list,list), \209           'CSV data set field list is not of a list: %s' % \210           (str(test_ds.field_list))211    assert test_ds.access_mode == 'write', \212           'CSV data set has wrong access mode (should be "write"): %s' % \213           (str(test_ds.access_mode))214    assert isinstance(test_ds.file_name, str), \215           'CSV data set file name is not a string: %s, %s' % \216           (type(test_ds.file_name), str(test_ds.file_name))217    assert test_ds.num_records == 0, \218           'CSV data set has wrong number of records (should be 0): %d' % \219           (test_ds.num_records)220    assert test_ds.next_rec_num == 0, \221           'CSV data set has wrong next record number (should be 0): %d' % \222           (test_ds.next_rec_num)223    # Write a single record224    #225    test_rec_0 = {'00':all_test_rec_dict['00']}226    test_ds.write(test_rec_0)227    assert test_ds.num_records == 1, \228           'CSV data set has wrong number of records: %d (should be 1)' % \229           (test_ds.num_records)230    assert test_ds.next_rec_num == 1, \231           'CSV data set has wrong next record number: %d (should be 1)' % \232           (test_ds.next_rec_num)233    # Write another single record234    #235    test_rec_1 = {'10':all_test_rec_dict['10']}236    test_ds.write(test_rec_1)237    assert test_ds.num_records == 2, \238           'CSV data set has wrong number of records: %d (should be 2)' % \239           (test_ds.num_records)240    assert test_ds.next_rec_num == 2, \241           'CSV data set has wrong next record number: %d (should be 2)' % \242           (test_ds.next_rec_num)243    # Write all records244    #245    test_ds.write(all_test_rec_dict)246    assert test_ds.num_records == 23, \247           'CSV data set has wrong number of records: %d (should be 23)' % \248           (test_ds.num_records)249    assert test_ds.next_rec_num == 23, \250           'CSV data set has wrong next record number: %d (should be 23)' % \251           (test_ds.next_rec_num)252    test_ds.finalise()253    test_ds = None254    # Initialise data set for appending (with quotes set) - - - - - - - - - - -255    #256    test_ds = dataset.DataSetCSV(description='A test CSV data set',257                                 access_mode='append',258                                 field_list=[('rec-id',0),('gname',1),259                                             ('surname',2),('streetnumb',3),260                                             ('streetname_type',4),261                                             ('suburb',5),('postcode',6)],262                                 rec_ident='rec-id',263                                 header_line=True,264                                 write_header=True,265                                 write_quote='"',266                                 file_name='./test-data2.csv')267    assert test_ds.dataset_type == 'CSV', \268           'Test data set has wrong type (should be "CSV"): "%s"' % \269           (str(test_ds.dataset_type))270    assert isinstance(test_ds.field_list,list), \271           'CSV data set field list is not of a list: %s' % \272           (str(test_ds.field_list))273    assert test_ds.access_mode == 'append', \274           'CSV data set has wrong access mode (should be "append"): %s' % \275           (str(test_ds.access_mode))276    assert isinstance(test_ds.file_name, str), \277           'CSV data set file name is not a string: %s, %s' % \278           (type(test_ds.file_name), str(test_ds.file_name))279    assert test_ds.num_records == 23, \280           'CSV data set has wrong number of records (should be 23): %d' % \281           (test_ds.num_records)282    assert test_ds.next_rec_num == 23, \283           'CSV data set has wrong next record number (should be 23): %d' % \284           (test_ds.next_rec_num)285    # Write a single record286    #287    test_rec_0 = {'00':all_test_rec_dict['00']}288    test_ds.write(test_rec_0)289    assert test_ds.num_records == 24, \290           'CSV data set has wrong number of records: %d (should be 24)' % \291           (test_ds.num_records)292    assert test_ds.next_rec_num == 24, \293           'CSV data set has wrong next record number: %d (should be 24)' % \294           (test_ds.next_rec_num)295    # Write another single record296    #297    test_rec_1 = {'10':all_test_rec_dict['10']}298    test_ds.write(test_rec_1)299    assert test_ds.num_records == 25, \300           'CSV data set has wrong number of records: %d (should be 25)' % \301           (test_ds.num_records)302    assert test_ds.next_rec_num == 25, \303           'CSV data set has wrong next record number: %d (should be 25)' % \304           (test_ds.next_rec_num)305    # Write all records306    #307    test_ds.write(all_test_rec_dict)308    assert test_ds.num_records == 46, \309           'CSV data set has wrong number of records: %d (should be 46)' % \310           (test_ds.num_records)311    assert test_ds.next_rec_num == 46, \312           'CSV data set has wrong next record number: %d (should be 46)' % \313           (test_ds.next_rec_num)314    test_ds.finalise()315    test_ds = None316    # Initialise data set for reading - - - - - - - - - - - - - - - - - - - - -317    #318    test_ds = dataset.DataSetCSV(description='A test CSV data set',319                                 access_mode='read',320                                 rec_ident='rec-id',321                                 header_line=True,322                                 write_header=True,323                                 file_name='./test-data2.csv')324    assert test_ds.dataset_type == 'CSV', \325           'Test data set has wrong type (should be "CSV"): "%s"' % \326           (str(test_ds.dataset_type))327    assert isinstance(test_ds.field_list,list), \328           'CSV data set field list is not of a list: %s' % \329           (str(test_ds.field_list))330    assert test_ds.access_mode == 'read', \331           'CSV data set has wrong access mode (should be "read"): %s' % \332           (str(test_ds.access_mode))333    assert isinstance(test_ds.file_name, str), \334           'CSV data set file name is not a string: %s, %s' % \335           (type(test_ds.file_name), str(test_ds.file_name))336    assert test_ds.num_records == 46, \337           'CSV data set has wrong number of records (should be 46): %d' % \338           (test_ds.num_records)339    assert test_ds.next_rec_num == 0, \340           'CSV data set has wrong next record number (should be 0): %d' % \341           (test_ds.next_rec_num)342    # Read a single record343    #344    test_rec_dict = test_ds.read()345    assert test_ds.next_rec_num == 1, \346           'CSV data set has wrong next record number (should be 1): %d' % \347           (test_ds.next_rec_num)348    assert isinstance(test_rec_dict, dict), \349           'Record returned is not of type dictionary: %s, %s' % \350           (type(test_rec_dict), str(test_rec_dict))351    assert len(test_rec_dict) == 1, \352           'More or less than one record returned: %d, %s' % \353           (len(test_rec_dict), str(test_rec_dict))354    # Read another single record355    #356    test_rec_dict = test_ds.read()357    assert test_ds.next_rec_num == 2, \358           'CSV data set has wrong next record number (should be 2): %d' % \359           (test_ds.next_rec_num)360    assert isinstance(test_rec_dict, dict), \361           'Record returned is not of type dictionary: %s, %s' % \362           (type(test_rec_dict), str(test_rec_dict))363    assert len(test_rec_dict) == 1, \364           'More or less than one record returned: %d, %s' % \365           (len(test_rec_dict), str(test_rec_dict))366    # Read three records367    #368    test_rec_dict = test_ds.read(3)369    assert test_ds.next_rec_num == 5, \370           'CSV data set has wrong next record number (should be 5): %d' % \371           (test_ds.next_rec_num)372    assert isinstance(test_rec_dict, dict), \373           'Record returned is not of type dictionary: %s, %s' % \374           (type(test_rec_dict), str(test_rec_dict))375    assert len(test_rec_dict) == 3, \376           'More or less than three record returned: %d, %s' % \377           (len(test_rec_dict), str(test_rec_dict))378    # Readall() iterator379    #380    rec_cnt = 0381    for test_rec_tuple in test_ds.readall():382      assert isinstance(test_rec_tuple, tuple), \383             'Record returned is not of type "tuple": %s, %s' % \384             (type(test_rec_tuple), str(test_rec_tuple))385      assert len(test_rec_tuple) == 2, \386             'More or less than one record returned: %d, %s' % \387             (len(test_rec_tuple), str(test_rec_tuple))388      rec_cnt += 1389      assert test_ds.next_rec_num == rec_cnt, \390             'CSV data set has wrong next record number (should be %d): %d' % \391             (rec_cnt, test_ds.next_rec_num)392    assert test_ds.num_records == rec_cnt, \393           'CSV data set has wrong number of records (should be %d): %d' % \394           (rec_cnt, test_ds.num_records)395    test_ds.finalise()396    test_ds = None397  def testCOL(self):   # - - - - - - - - - - - - - - - - - - - - - - - - - - -398    """Test COL data set"""399    test_data_col_width = [6,10,10,13,19,21,11,8]400    for test_file in ['./test-data.col','./test-data.col.gz']:401      # Initialise data set for reading, not using header line402      #403      test_ds = dataset.DataSetCOL(description='A test COL data set',404                                   access_mode='read',405                                   field_list=[('rec-id',6),('gname',10),406                                               ('surname',10),407                                               ('streetnumber',13),408                                               ('address_1',19),409                                               ('address_2',21),410                                               ('suburb',11),('postcode',8)],411                                   rec_ident='rec-id',412                                   header_line=False,413                                   write_header=True,414                                   file_name=test_file)415      assert test_ds.dataset_type == 'COL', \416             'Test data set has wrong type (should be "COL"): "%s"' % \417             (str(test_ds.dataset_type))418      assert isinstance(test_ds.field_list,list), \419             'COL data set field list is not of a list: %s' % \420             (str(test_ds.field_list))421      assert test_ds.access_mode == 'read', \422             'COL data set has wrong access mode (should be "read"): %s' % \423             (str(test_ds.access_mode))424      assert isinstance(test_ds.file_name, str), \425             'COL data set file name is not a string: %s, %s' % \426             (type(test_ds.file_name), str(test_ds.file_name))427      assert test_ds.num_records == 21, \428             'COL data set has wrong number of records (should be 21): %d' % \429             (test_ds.num_records)430      assert test_ds.next_rec_num == 0, \431             'COL data set has wrong next record number (should be 0): %d' % \432             (test_ds.next_rec_num)433      # Read a single record434      #435      test_rec_dict = test_ds.read()436      assert test_ds.next_rec_num == 1, \437             'COL data set has wrong next record number (should be 1): %d' % \438             (test_ds.next_rec_num)439      assert isinstance(test_rec_dict, dict), \440             'Record returned is not of type dictionary: %s, %s' % \441             (type(test_rec_dict), str(test_rec_dict))442      assert len(test_rec_dict) == 1, \443             'More or less than one record returned: %d, %s' % \444             (len(test_rec_dict), str(test_rec_dict))445      # Read another single record446      #447      test_rec_dict = test_ds.read()448      assert test_ds.next_rec_num == 2, \449             'COL data set has wrong next record number (should be 2): %d' % \450             (test_ds.next_rec_num)451      assert isinstance(test_rec_dict, dict), \452             'Record returned is not of type dictionary: %s, %s' % \453             (type(test_rec_dict), str(test_rec_dict))454      assert len(test_rec_dict) == 1, \455             'More or less than one record returned: %d, %s' % \456             (len(test_rec_dict), str(test_rec_dict))457      # Read three records458      #459      test_rec_dict = test_ds.read(3)460      assert test_ds.next_rec_num == 5, \461             'COL data set has wrong next record number (should be 5): %d' % \462             (test_ds.next_rec_num)463      assert isinstance(test_rec_dict, dict), \464             'Record returned is not of type dictionary: %s, %s' % \465             (type(test_rec_dict), str(test_rec_dict))466      assert len(test_rec_dict) == 3, \467             'More or less than three record returned: %d, %s' % \468             (len(test_rec_dict), str(test_rec_dict))469      # Read twenty records (as there are only 21 in the data set only 16470      # should be returned, as 5 have been read before)471      #472      test_rec_dict = test_ds.read(20)473      assert test_ds.next_rec_num == 21, \474             'COL data set has wrong next record number (should be 21): %d' % \475             (test_ds.next_rec_num)476      assert isinstance(test_rec_dict, dict), \477             'Record returned is not of type dictionary: %s, %s' % \478             (type(test_rec_dict), str(test_rec_dict))479      assert len(test_rec_dict) == 16, \480             'More or less than sixteen record returned: %d, %s' % \481             (len(test_rec_dict), str(test_rec_dict))482      # Read records 3 records from record number 15 onwards483      #484      test_rec_dict = test_ds.read(15,3)485      assert test_ds.next_rec_num == 18, \486             'COL data set has wrong next record number (should be 18): %d' % \487             (test_ds.next_rec_num)488      assert isinstance(test_rec_dict, dict), \489             'Record returned is not of type dictionary: %s, %s' % \490             (type(test_rec_dict), str(test_rec_dict))491      assert len(test_rec_dict) == 3, \492             'More or less than three record returned: %d, %s' % \493             (len(test_rec_dict), str(test_rec_dict))494      # Read records 5 records from record number 2 onwards495      #496      test_rec_dict = test_ds.read(2,5)497      assert test_ds.next_rec_num == 7, \498             'COL data set has wrong next record number (should be 7): %d' % \499             (test_ds.next_rec_num)500      assert isinstance(test_rec_dict, dict), \501             'Record returned is not of type dictionary: %s, %s' % \502             (type(test_rec_dict), str(test_rec_dict))503      assert len(test_rec_dict) == 5, \504             'More or less than five record returned: %d, %s' % \505             (len(test_rec_dict), str(test_rec_dict))506      # Read all records507      #508      all_test_rec_dict = test_ds.read(0,30)509      assert test_ds.next_rec_num == 21, \510             'COL data set has wrong next record number (should be 21): %d' % \511             (test_ds.next_rec_num)512      assert isinstance(all_test_rec_dict, dict), \513             'Record returned is not of type dictionary: %s, %s' % \514             (type(all_test_rec_dict), str(all_test_rec_dict))515      assert len(all_test_rec_dict) == 21, \516             'More or less than tenty-one record returned: %d, %s' % \517             (len(all_test_rec_dict), str(all_test_rec_dict))518      test_ds.finalise()519    for test_file in ['./test-data.col','./test-data.col.gz']:520      # Initialise data set for reading, now using header line521      #522      test_ds = dataset.DataSetCOL(description='A test COL data set',523                                   access_mode='read',524                                   field_list=test_data_col_width,525                                   rec_ident='rec_id',526                                   header_line=True,527                                   write_header=True,528                                   file_name=test_file)529      assert test_ds.dataset_type == 'COL', \530             'Test data set has wrong type (should be "COL"): "%s"' % \531             (str(test_ds.dataset_type))532      assert isinstance(test_ds.field_list,list), \533             'COL data set field list is not of a list: %s' % \534             (str(test_ds.field_list))535      assert test_ds.access_mode == 'read', \536             'COL data set has wrong access mode (should be "read"): %s' % \537             (str(test_ds.access_mode))538      assert isinstance(test_ds.file_name, str), \539             'COL data set file name is not a string: %s, %s' % \540             (type(test_ds.file_name), str(test_ds.file_name))541      assert test_ds.num_records == 20, \542             'COL data set has wrong number of records (should be 20): %d' % \543             (test_ds.num_records)544      assert test_ds.next_rec_num == 0, \545             'COL data set has wrong next record number (should be 0): %d' % \546             (test_ds.next_rec_num)547      # Read three records548      #549      test_rec_dict = test_ds.read(3)550      assert test_ds.next_rec_num == 3, \551             'COL data set has wrong next record number (should be 3): %d' % \552             (test_ds.next_rec_num)553      assert isinstance(test_rec_dict, dict), \554             'Record returned is not of type dictionary: %s, %s' % \555             (type(test_rec_dict), str(test_rec_dict))556      assert len(test_rec_dict) == 3, \557             'More or less than three record returned: %d, %s' % \558             (len(test_rec_dict), str(test_rec_dict))559      # Read records 7 records from record number 10 onwards560      #561      test_rec_dict = test_ds.read(10,7)562      assert test_ds.next_rec_num == 17, \563             'COL data set has wrong next record number (should be 17): %d' % \564             (test_ds.next_rec_num)565      assert isinstance(test_rec_dict, dict), \566             'Record returned is not of type dictionary: %s, %s' % \567             (type(test_rec_dict), str(test_rec_dict))568      assert len(test_rec_dict) == 7, \569             'More or less than seven record returned: %d, %s' % \570             (len(test_rec_dict), str(test_rec_dict))571      # Read all records572      #573      all_test_rec_dict = test_ds.read(0,30)574      assert test_ds.next_rec_num == 20, \575             'COL data set has wrong next record number (should be 20): %d' % \576             (test_ds.next_rec_num)577      assert isinstance(all_test_rec_dict, dict), \578             'Record returned is not of type dictionary: %s, %s' % \579             (type(all_test_rec_dict), str(all_test_rec_dict))580      assert len(all_test_rec_dict) == 20, \581             'More or less than tenty-one record returned: %d, %s' % \582             (len(all_test_rec_dict), str(all_test_rec_dict))583      test_ds.finalise()584    # Initialise data set for writing (with header lines) - - - - - - - - - - -585    #586    try:587      os.remove('./test-data2.col')  # Remove previously written test data set588    except:589      pass590    test_ds = dataset.DataSetCOL(description='A test COL data set',591                                 access_mode='write',592                                 field_list=[('rec-id',6),('gname',10),593                                               ('surname',10),594                                               ('streetnumber',13),595                                               ('address_1',19),596                                               ('address_2',21),597                                               ('suburb',11),('postcode',8)],598                                 rec_ident='rec-id',599                                 header_line=True,600                                 write_header=True,601                                 file_name='./test-data2.col')602    assert test_ds.dataset_type == 'COL', \603           'Test data set has wrong type (should be "COL"): "%s"' % \604           (str(test_ds.dataset_type))605    assert isinstance(test_ds.field_list,list), \606           'COL data set field list is not of a list: %s' % \607           (str(test_ds.field_list))608    assert test_ds.access_mode == 'write', \609           'COL data set has wrong access mode (should be "write"): %s' % \610           (str(test_ds.access_mode))611    assert isinstance(test_ds.file_name, str), \612           'COL data set file name is not a string: %s, %s' % \613           (type(test_ds.file_name), str(test_ds.file_name))614    assert test_ds.num_records == 0, \615           'COL data set has wrong number of records (should be 0): %d' % \616           (test_ds.num_records)617    assert test_ds.next_rec_num == 0, \618           'COL data set has wrong next record number (should be 0): %d' % \619           (test_ds.next_rec_num)620    # Write a single record621    #622    test_rec_0 = {'00':all_test_rec_dict['00']}623    test_ds.write(test_rec_0)624    assert test_ds.num_records == 1, \625           'COL data set has wrong number of records: %d (should be 1)' % \626           (test_ds.num_records)627    assert test_ds.next_rec_num == 1, \628           'COL data set has wrong next record number: %d (should be 1)' % \629           (test_ds.next_rec_num)630    # Write another single record631    #632    test_rec_1 = {'10':all_test_rec_dict['10']}633    test_ds.write(test_rec_1)634    assert test_ds.num_records == 2, \635           'COL data set has wrong number of records: %d (should be 2)' % \636           (test_ds.num_records)637    assert test_ds.next_rec_num == 2, \638           'COL data set has wrong next record number: %d (should be 2)' % \639           (test_ds.next_rec_num)640    # Write all records641    #642    test_ds.write(all_test_rec_dict)643    assert test_ds.num_records == 22, \644           'COL data set has wrong number of records: %d (should be 22)' % \645           (test_ds.num_records)646    assert test_ds.next_rec_num == 22, \647           'COL data set has wrong next record number: %d (should be 22)' % \648           (test_ds.next_rec_num)649    test_ds.finalise()650    test_ds = None651    # Initialise data set for appending (no header line) - - - - - - - - - - -652    #653    test_ds = dataset.DataSetCOL(description='A test COL data set',654                                 access_mode='append',655                                 field_list=[('rec-id',6),('gname',10),656                                               ('surname',10),657                                               ('streetnumber',13),658                                               ('address_1',19),659                                               ('address_2',21),660                                               ('suburb',11),('postcode',8)],661                                 rec_ident='rec-id',662                                 header_line=True,663                                 write_header=False,664                                 file_name='./test-data2.col')665    assert test_ds.dataset_type == 'COL', \666           'Test data set has wrong type (should be "COL"): "%s"' % \667           (str(test_ds.dataset_type))668    assert isinstance(test_ds.field_list,list), \669           'COL data set field list is not of a list: %s' % \670           (str(test_ds.field_list))671    assert test_ds.access_mode == 'append', \672           'COL data set has wrong access mode (should be "append"): %s' % \673           (str(test_ds.access_mode))674    assert isinstance(test_ds.file_name, str), \675           'COL data set file name is not a string: %s, %s' % \676           (type(test_ds.file_name), str(test_ds.file_name))677    assert test_ds.num_records == 22, \678           'COL data set has wrong number of records (should be 22): %d' % \679           (test_ds.num_records)680    assert test_ds.next_rec_num == 22, \681           'COL data set has wrong next record number (should be 22): %d' % \682           (test_ds.next_rec_num)683    # Write a single record684    #685    test_rec_0 = {'00':all_test_rec_dict['00']}686    test_ds.write(test_rec_0)687    assert test_ds.num_records == 23, \688           'COL data set has wrong number of records: %d (should be 23)' % \689           (test_ds.num_records)690    assert test_ds.next_rec_num == 23, \691           'COL data set has wrong next record number: %d (should be 23)' % \692           (test_ds.next_rec_num)693    # Write another single record694    #695    test_rec_1 = {'10':all_test_rec_dict['10']}696    test_ds.write(test_rec_1)697    assert test_ds.num_records == 24, \698           'COL data set has wrong number of records: %d (should be 24)' % \699           (test_ds.num_records)700    assert test_ds.next_rec_num == 24, \701           'COL data set has wrong next record number: %d (should be 24)' % \702           (test_ds.next_rec_num)703    # Write all records704    #705    test_ds.write(all_test_rec_dict)706    assert test_ds.num_records == 44, \707           'COL data set has wrong number of records: %d (should be 44)' % \708           (test_ds.num_records)709    assert test_ds.next_rec_num == 44, \710           'COL data set has wrong next record number: %d (should be 44)' % \711           (test_ds.next_rec_num)712    test_ds.finalise()713    test_ds = None714    # Initialise data set for reading - - - - - - - - - - - - - - - - - - - - -715    #716    test_ds = dataset.DataSetCOL(description='A test COL data set',717                                 access_mode='read',718                                 rec_ident='rec_id',719                                 field_list = test_data_col_width,720                                 header_line=True,721                                 write_header=True,722                                 file_name='./test-data2.col')723    assert test_ds.dataset_type == 'COL', \724           'Test data set has wrong type (should be "COL"): "%s"' % \725           (str(test_ds.dataset_type))726    assert isinstance(test_ds.field_list,list), \727           'COL data set field list is not of a list: %s' % \728           (str(test_ds.field_list))729    assert test_ds.access_mode == 'read', \730           'COL data set has wrong access mode (should be "read"): %s' % \731           (str(test_ds.access_mode))732    assert isinstance(test_ds.file_name, str), \733           'COL data set file name is not a string: %s, %s' % \734           (type(test_ds.file_name), str(test_ds.file_name))735    assert test_ds.num_records == 44, \736           'COL data set has wrong number of records (should be 44): %d' % \737           (test_ds.num_records)738    assert test_ds.next_rec_num == 0, \739           'COL data set has wrong next record number (should be 0): %d' % \740           (test_ds.next_rec_num)741    # Read a single record742    #743    test_rec_dict = test_ds.read()744    assert test_ds.next_rec_num == 1, \745           'COL data set has wrong next record number (should be 1): %d' % \746           (test_ds.next_rec_num)747    assert isinstance(test_rec_dict, dict), \748           'Record returned is not of type dictionary: %s, %s' % \749           (type(test_rec_dict), str(test_rec_dict))750    assert len(test_rec_dict) == 1, \751           'More or less than one record returned: %d, %s' % \752           (len(test_rec_dict), str(test_rec_dict))753    # Read another single record754    #755    test_rec_dict = test_ds.read()756    assert test_ds.next_rec_num == 2, \757           'COL data set has wrong next record number (should be 2): %d' % \758           (test_ds.next_rec_num)759    assert isinstance(test_rec_dict, dict), \760           'Record returned is not of type dictionary: %s, %s' % \761           (type(test_rec_dict), str(test_rec_dict))762    assert len(test_rec_dict) == 1, \763           'More or less than one record returned: %d, %s' % \764           (len(test_rec_dict), str(test_rec_dict))765    # Read three records766    #767    test_rec_dict = test_ds.read(3)768    assert test_ds.next_rec_num == 5, \769           'COL data set has wrong next record number (should be 5): %d' % \770           (test_ds.next_rec_num)771    assert isinstance(test_rec_dict, dict), \772           'Record returned is not of type dictionary: %s, %s' % \773           (type(test_rec_dict), str(test_rec_dict))774    assert len(test_rec_dict) == 3, \775           'More or less than three record returned: %d, %s' % \776           (len(test_rec_dict), str(test_rec_dict))777    # Readall() iterator778    #779    rec_cnt = 0780    for test_rec_tuple in test_ds.readall():781      assert isinstance(test_rec_tuple, tuple), \782             'Record returned is not of type "tuple": %s, %s' % \783             (type(test_rec_tuple), str(test_rec_tuple))784      assert len(test_rec_tuple) == 2, \785             'More or less than one record returned: %d, %s' % \786             (len(test_rec_tuple), str(test_rec_tuple))787      rec_cnt += 1788      assert test_ds.next_rec_num == rec_cnt, \789             'COL data set has wrong next record number (should be %d): %d' % \790             (rec_cnt, test_ds.next_rec_num)791    assert test_ds.num_records == rec_cnt, \792           'COL data set has wrong number of records (should be %d): %d' % \793           (rec_cnt, test_ds.num_records)794    test_ds.finalise()795    test_ds = None796  def testMemory(self):   # ---------------------------------------------------797    """Test Memory data set"""798    # First load records from CSV data set799    #800    csv_ds = dataset.DataSetCSV(description='A test CSV data set',801                                access_mode='read',802                                rec_ident='rec-id',803                                header_line=False,804                                field_list=[('rec-id',0),('gname',1),805                                            ('surname',2),('streetnumb',3),806                                            ('streetname_type',4),807                                            ('suburb',5),('postcode',6)],808                                write_header=True,809                                file_name='./test-data.csv')810    all_csv_rec_dict = csv_ds.read(21)  # Read all records811    assert isinstance(all_csv_rec_dict, dict), \812           'Record returned is not of type dictionary: %s, %s' % \813           (type(all_csv_rec_dict), str(all_csv_rec_dict))814    assert len(all_csv_rec_dict) == 21, \815           'CSV data set has wrong next record number (should be 21): %d' % \816           (len(all_csv_rec_dict))817    csv_ds.finalise()818    # Initialise memory data set for read-writing819    #820    test_ds = dataset.DataSetMemory(description='A test Memory data set',821                                    access_mode='readwrite',822                                    field_list=[('rec-id',''),('gname',''),823                                                ('surname',''),824                                                ('streetnumb',''),825                                                ('streetname_type',''),826                                                ('suburb',''),('postcode','')],827                                    rec_ident='rec-id')828    assert test_ds.dataset_type == 'MEMORY', \829           'Test data set has wrong type (should be "MEMORY"): "%s"' % \830           (str(test_ds.dataset_type))831    assert isinstance(test_ds.field_list,list), \832           'Memory data set field list is not of a list: %s' % \833           (str(test_ds.field_list))834    assert test_ds.access_mode == 'readwrite', \835           'Memory data set has wrong access mode (should be "readwrite"):' + \836           '%s' % (str(test_ds.access_mode))837    assert test_ds.num_records == 0, \838           'Memory data set has wrong number of records (should be 0): %d' % \839           (test_ds.num_records)840    # Write a single record841    #842    test_rec_0 = {'00':all_csv_rec_dict['00']}843    test_ds.write(test_rec_0)844    assert test_ds.num_records == 1, \845           'Memory data set has wrong number of records: %d (should be 1)' % \846           (test_ds.num_records)847    # Write another single record848    #849    test_rec_1 = {'10':all_csv_rec_dict['10']}850    test_ds.write(test_rec_1)851    assert test_ds.num_records == 2, \852           'Memory data set has wrong number of records: %d (should be 2)' % \853           (test_ds.num_records)854    # Write all records - should result in 2 warnings of duplicate identifiers855    #856    test_ds.write(all_csv_rec_dict)857    assert test_ds.num_records == 21, \858           'Memory data set has wrong number of records: %d (should be 21)' % \859           (test_ds.num_records)860    # Read a single record861    #862    test_rec_dict = test_ds.read('00')863    assert isinstance(test_rec_dict, dict), \864           'Record returned is not of type dictionary: %s, %s' % \865           (type(test_rec_dict), str(test_rec_dict))866    assert len(test_rec_dict) == 1, \867           'More or less than one record returned: %d, %s' % \868           (len(test_rec_dict), str(test_rec_dict))869    assert '00' in test_rec_dict, \870           'Record identifier "00" is not key in record dictionary:' + \871           '%s' % (str(test_rec_dict))872    # Read a single record873    #874    test_rec_dict = test_ds.read('73')875    assert isinstance(test_rec_dict, dict), \876           'Record returned is not of type dictionary: %s, %s' % \877           (type(test_rec_dict), str(test_rec_dict))878    assert len(test_rec_dict) == 1, \879           'More or less than one record returned: %d, %s' % \880           (len(test_rec_dict), str(test_rec_dict))881    assert '73' in test_rec_dict, \882           'Record identifier "73" is not key in record dictionary:' \883           + '%s' % (str(test_rec_dict))884    # Read three records885    #886    test_rec_dict = test_ds.read(['60','44','41'])887    assert isinstance(test_rec_dict, dict), \888           'Record returned is not of type dictionary: %s, %s' % \889           (type(test_rec_dict), str(test_rec_dict))890    assert len(test_rec_dict) == 3, \891           'More or less than three record returned: %d, %s' % \892           (len(test_rec_dict), str(test_rec_dict))893    # Read another three records (with one duplicate identifier given)894    #895    test_rec_dict = test_ds.read(['20','72','20'])896    assert isinstance(test_rec_dict, dict), \897           'Record returned is not of type dictionary: %s, %s' % \898           (type(test_rec_dict), str(test_rec_dict))899    assert len(test_rec_dict) == 2, \900           'More or less than three record returned: %d, %s' % \901           (len(test_rec_dict), str(test_rec_dict))902    # Readall() iterator903    #904    rec_cnt = 0905    for test_rec_tuple in test_ds.readall():906      assert isinstance(test_rec_tuple, tuple), \907             'Record returned is not of type "tuple": %s, %s' % \908             (type(test_rec_tuple), str(test_rec_tuple))909      assert len(test_rec_tuple) == 2, \910             'More or less than one record returned: %d, %s' % \911             (len(test_rec_tuple), str(test_rec_euple))912      rec_cnt += 1913    assert test_ds.num_records == rec_cnt, \914           'CSV data set has wrong number of records (should be %d): %d' % \915           (rec_cnt, test_ds.num_records)916    test_ds.finalise()917    test_ds = None918  def testShelve(self):   # ---------------------------------------------------919    """Test Shelve data set"""920    # First load records from CSV data set921    #922    csv_ds = dataset.DataSetCSV(description='A test CSV data set',923                                access_mode='read',924                                rec_ident='rec-id',925                                header_line=False,926                                field_list=[('rec-id',0),('gname',1),927                                            ('surname',2),('streetnumb',3),928                                            ('streetname_type',4),929                                            ('suburb',5),('postcode',6)],930                                write_header=True,931                                file_name='./test-data.csv')932    all_csv_rec_dict = csv_ds.read(21)  # Read all records933    assert isinstance(all_csv_rec_dict, dict), \934           'Record returned is not of type dictionary: %s, %s' % \935           (type(all_csv_rec_dict), str(all_csv_rec_dict))936    assert len(all_csv_rec_dict) == 21, \937           'CSV data set has wrong next record number (should be 21): %d' % \938           (len(all_csv_rec_dict))939    csv_ds.finalise()940    # Initialise shelve data set for read-writing941    #942    test_ds = dataset.DataSetShelve(description='A test Shelve data set',943                                    file_name = 'test-data.slv',944                                    clear = True,  # False,945                                    access_mode='readwrite',946                                    field_list=[('rec-id',''),('gname',''),947                                                ('surname',''),948                                                ('streetnumb',''),949                                                ('streetname_type',''),950                                                ('suburb',''),('postcode','')],951                                    rec_ident='rec-id')952    assert test_ds.dataset_type == 'SHELVE', \953           'Test data set has wrong type (should be "SHELVE"): "%s"' % \954           (str(test_ds.dataset_type))955    assert isinstance(test_ds.field_list,list), \956           'Shelve data set field list is not of a list: %s' % \957           (str(test_ds.field_list))958    assert test_ds.access_mode == 'readwrite', \959           'Shelve data set has wrong access mode (should be "readwrite"):' + \960           '%s' % (str(test_ds.access_mode))961    assert test_ds.num_records == 0, \962           'Shelve data set has wrong number of records (should be 0): %d' % \963           (test_ds.num_records)964    # Write a single record965    #966    test_rec_0 = {'00':all_csv_rec_dict['00']}967    test_ds.write(test_rec_0)968    assert test_ds.num_records == 1, \969           'Shelve data set has wrong number of records: %d (should be 1)' % \970           (test_ds.num_records)971    # Write another single record972    #973    test_rec_1 = {'10':all_csv_rec_dict['10']}974    test_ds.write(test_rec_1)975    assert test_ds.num_records == 2, \976           'Shelve data set has wrong number of records: %d (should be 2)' % \977           (test_ds.num_records)978    # Write all records - should result in 2 warnings of duplicate identifiers979    #980    test_ds.write(all_csv_rec_dict)981    assert test_ds.num_records == 21, \982           'Shelve data set has wrong number of records: %d (should be 21)' % \983           (test_ds.num_records)984    # Read a single record985    #986    test_rec_dict = test_ds.read('00')987    assert isinstance(test_rec_dict, dict), \988           'Record returned is not of type dictionary: %s, %s' % \989           (type(test_rec_dict), str(test_rec_dict))990    assert len(test_rec_dict) == 1, \991           'More or less than one record returned: %d, %s' % \992           (len(test_rec_dict), str(test_rec_dict))993    assert '00' in test_rec_dict, \994           'Record identifier "00" is not key in record dictionary:' + \995           '%s' % (str(test_rec_dict))996    # Read a single record997    #998    test_rec_dict = test_ds.read('73')999    assert isinstance(test_rec_dict, dict), \1000           'Record returned is not of type dictionary: %s, %s' % \1001           (type(test_rec_dict), str(test_rec_dict))1002    assert len(test_rec_dict) == 1, \1003           'More or less than one record returned: %d, %s' % \1004           (len(test_rec_dict), str(test_rec_dict))1005    assert '73' in test_rec_dict, \1006           'Record identifier "73" is not key in record dictionary:' \1007           + '%s' % (str(test_rec_dict))1008    # Read three records1009    #1010    test_rec_dict = test_ds.read(['60','44','41'])1011    assert isinstance(test_rec_dict, dict), \1012           'Record returned is not of type dictionary: %s, %s' % \1013           (type(test_rec_dict), str(test_rec_dict))1014    assert len(test_rec_dict) == 3, \1015           'More or less than three record returned: %d, %s' % \1016           (len(test_rec_dict), str(test_rec_dict))1017    # Read another three records (with one duplicate identifier given)1018    #1019    test_rec_dict = test_ds.read(['20','72','20'])1020    assert isinstance(test_rec_dict, dict), \1021           'Record returned is not of type dictionary: %s, %s' % \1022           (type(test_rec_dict), str(test_rec_dict))1023    assert len(test_rec_dict) == 2, \1024           'More or less than three record returned: %d, %s' % \1025           (len(test_rec_dict), str(test_rec_dict))1026    # Readall() iterator1027    #1028    rec_cnt = 01029    for test_rec_tuple in test_ds.readall():1030      assert isinstance(test_rec_tuple, tuple), \1031             'Record returned is not of type "tuple": %s, %s' % \1032             (type(test_rec_tuple), str(test_rec_tuple))1033      assert len(test_rec_tuple) == 2, \1034             'More or less than one record returned: %d, %s' % \1035             (len(test_rec_tuple), str(test_rec_tuple))1036      rec_cnt += 11037    assert test_ds.num_records == rec_cnt, \1038           'CSV data set has wrong number of records (should be %d): %d' % \1039           (rec_cnt, test_ds.num_records)1040    test_ds.finalise()1041    test_ds = None1042  def testCSVdelimiter(self):   # - - - - - - - - - - - - - - - - - - - - - - -1043    """Test CSV data set with different delimiters"""1044    for (test_file, delim, num_col) in [('./test-comma.txt', ',', 6),1045                                        ('./test-comma.txt', 'x', 1),1046                                        ('./test-period.txt', ':', 6),1047                                        ('./test-period.txt', ',', 1),1048                                        ('./test-tabulator.txt', '\t', 6),1049                                        ('./test-tabulator.txt', ',', 1),1050                                        ('./test-tabulator-comma.txt', ',', 6),1051                                        ('./test-tabulator-comma.txt', ',', 6),1052                                        ('./test-tabulator-comma.txt',';', 1),1053                                        ('./test-tabulator.txt', chr(9), 6)]:1054      # Create a field list1055      #1056      field_name_list = []1057      for i in range(num_col):1058        field_name_list.append(('field-%d' % (i), i))1059      # Initialise data set for reading1060      #1061      test_ds = dataset.DataSetCSV(description='A test CSV data set',1062                                   access_mode='read',1063                                   field_list = field_name_list,1064                                   delimiter = delim,1065                                   rec_ident='rec-id',1066                                   header_line=False,1067                                   strip_fields=False,1068                                   write_header=True,1069                                   file_name=test_file)1070      assert test_ds.dataset_type == 'CSV', \1071             'Test data set has wrong type (should be "CSV"): "%s"' % \1072             (str(test_ds.dataset_type))1073      assert isinstance(test_ds.field_list,list), \1074             'CSV data set field list is not of a list: %s' % \1075             (str(test_ds.field_list))1076      assert test_ds.access_mode == 'read', \1077             'CSV data set has wrong access mode (should be "read"): %s' % \1078             (str(test_ds.access_mode))1079      assert test_ds.delimiter == delim, \1080             'CSV data set has wrong delimiter (should be "%s"): %s' % \1081             (delim, str(test_ds.delimiter))1082      assert len(test_ds.field_list) == num_col, \1083             'CSV data set has wrong number of fields (should be %d): %d' % \1084             (num_col, len(test_ds.field_list))1085      # Now check for delimiters in the data set1086      #1087      rec_cnt = 01088      for (rec_id, rec_data) in test_ds.readall():1089        for col_val in rec_data:1090          assert delim not in col_val, \1091                 'Delimiter character "%s" in data set values: "%s"' % \1092                 (delim, col_val)1093        if (('tabulator-comma' in test_file) and (delim not in [',', '\t'])):1094          assert len(rec_data) == 11095        elif (('tabulator-comma' in test_file) and (delim in [',', '\t'])):1096          assert len(rec_data) == 61097        elif (('comma' in test_file) and (delim == 'x')):1098          assert len(rec_data) == 11099        elif (('comma' in test_file) and (delim == ',')):1100          assert len(rec_data) == 61101        elif (('tabulator' in test_file) and (delim != '\t')):1102          assert len(rec_data) == 11103        elif (('tabulator' in test_file) and (delim == '\t')):1104          assert len(rec_data) == 61105        elif (('period' in test_file) and (delim != ':')):1106          assert len(rec_data) == 11107        elif (('period' in test_file) and (delim == ':')):1108          assert len(rec_data) == 61109        # Check the values in the current record1110        #1111        if (num_col == 6):1112          col_cnt = 01113          for col_val in rec_data:1114            this_num = (rec_cnt+col_cnt) % 101115            check_col_val = str(this_num)*(col_cnt+1)1116            if (('tabulator-comma' in test_file) and (delim == '\t') and \1117                (col_cnt > 0)):1118              check_col_val = ','+check_col_val1119            elif (('tabulator-comma' in test_file) and (delim == ',') and \1120                  (col_cnt <5)):1121              check_col_val = check_col_val+'\t'1122            assert col_val == check_col_val, 'Different column value than ' + \1123                   'expected in row %d: "%s" (should be "%s"' % \1124                   (rec_cnt, col_val, check_col_val)1125            col_cnt += 11126        rec_cnt += 11127      test_ds.finalise()1128      test_ds = None1129  def testMissValStripFields(self):   # - - - - - - - - - - - - - - - - - - - -1130    """Test data sets regarding missing values and strip fields arguments"""1131    for (miss_val, strip_fields_flag) in [('15', False),('15', True),1132                                          (['3'], False),(['3'], True),1133                                          (['3', '2906'], False),1134                                          (['3', '2906'], True),1135                                          (['xyz', '1'], False),1136                                          (['xyz', '1'], True),1137                                          ('rivett', False),1138                                          ('rivett', True)]:1139      test_ds = dataset.DataSetCSV(description='A test CSV data set',1140                                   access_mode='read',1141                                   field_list=[('rec-id',0),('gname',1),1142                                               ('surname',2),('streetnumb',3),1143                                               ('streetname_type',4),1144                                               ('suburb',5),('postcode',6)],1145                                   rec_ident='rec-id',1146                                   header_line=False,1147                                   write_header=True,1148                                   miss_v=miss_val,1149                                   strip_f=strip_fields_flag,1150                                   file_name='./test-data.csv')1151      assert test_ds.strip_fields in [True, False], \1152             'CSV data set strip fields is not a boolean: %s' % \1153             (str(test_ds.strip_fields))1154      assert test_ds.strip_fields == strip_fields_flag, \1155             'CSV data set has wrong strip fields values: %s (should be: %s)' \1156             % (str(test_ds.strip_fields), str(strip_fields_flag))1157      assert isinstance(test_ds.miss_val, list), \1158             'CSV data set missing values variable is not a list: %s' % \1159             (str(test_ds.miss_val))1160      if (isinstance(miss_val, str)):1161        tmp_miss_val = [miss_val]1162      else:1163        tmp_miss_val = miss_val1164      assert test_ds.miss_val == tmp_miss_val, \1165             'CSV data set has wrong missing values: %s (should be: %s)' % \1166             (str(test_ds.miss_val), str(tmp_miss_val))1167      # Now check all values in the records from the data set1168      #1169      rec_cnt = 01170      for (rec_id, rec_data) in test_ds.readall():1171        for col_val in rec_data:1172          if ((strip_fields_flag == True) and (len(col_val) > 0)):1173            assert col_val[0] not in string.whitespace, \1174                   'Field value not stripped of whitespaces: "%s"' % (col_val)1175            assert col_val[-1] not in string.whitespace, \1176                   'Field value not stripped of whitespaces: "%s"' % (col_val)1177          assert col_val not in tmp_miss_val, \1178                 'Missing value not removed: "%s" (from list: %s' % \1179                 (col_val, str(tmp_miss_val))1180      test_ds.finalise()1181      test_ds = None1182      test_ds = dataset.DataSetCOL(description='A test COL data set',1183                                   access_mode='read',1184                                   field_list=[('rec-id',6),('gname',10),1185                                               ('surname',10),1186                                               ('streetnumber',13),1187                                               ('address_1',19),1188                                               ('address_2',21),1189                                               ('suburb',11),('postcode',8)],1190                                   rec_ident='rec-id',1191                                   header_line=False,1192                                   write_header=True,1193                                   miss_v=miss_val,1194                                   strip_f=strip_fields_flag,1195                                   file_name='./test-data.col')1196      assert test_ds.strip_fields in [True, False], \1197             'COL data set strip fields is not a boolean: %s' % \1198             (str(test_ds.strip_fields))1199      assert test_ds.strip_fields == strip_fields_flag, \1200             'COL data set has wrong strip fields values: %s (should be: %s)' \1201             % (str(test_ds.strip_fields), str(strip_fields_flag))1202      assert isinstance(test_ds.miss_val, list), \1203             'COL data set missing values variable is not a list: %s' % \1204             (str(test_ds.miss_val))1205      if (isinstance(miss_val, str)):1206        tmp_miss_val = [miss_val]1207      else:1208        tmp_miss_val = miss_val1209      assert test_ds.miss_val == tmp_miss_val, \1210             'COL data set has wrong missing values: %s (should be: %s)' % \1211             (str(test_ds.miss_val), str(tmp_miss_val))1212      # Now check all values in the records from the data set1213      #1214      rec_cnt = 01215      for (rec_id, rec_data) in test_ds.readall():1216        for col_val in rec_data:1217          if ((strip_fields_flag == True) and (len(col_val) > 0)):1218            assert col_val[0] not in string.whitespace, \1219                   'Field value not stripped of whitespaces: "%s"' % (col_val)1220            assert col_val[-1] not in string.whitespace, \1221                   'Field value not stripped of whitespaces: "%s"' % (col_val)1222          assert col_val not in tmp_miss_val, \1223                 'Missing value not removed: "%s" (from list: %s' % \1224                 (col_val, str(tmp_miss_val))1225      test_ds.finalise()1226      test_ds = None1227      test_ds = dataset.DataSetMemory(description='A test Memory data set',1228                                      access_mode='readwrite',1229                                      field_list=[('rec-id',''),('gname',''),1230                                                  ('surname',''),1231                                                  ('streetnumb',''),1232                                                  ('streetname_type',''),1233                                                  ('suburb',''),1234                                                  ('postcode','')],1235                                      miss_v=miss_val,1236                                      strip_f=strip_fields_flag,1237                                      rec_ident='rec-id')1238      assert test_ds.strip_fields in [True, False], \1239             'MEM data set strip fields is not a boolean: %s' % \1240             (str(test_ds.strip_fields))1241      assert test_ds.strip_fields == strip_fields_flag, \1242             'MEM data set has wrong strip fields values: %s (should be: %s)' \1243             % (str(test_ds.strip_fields), str(strip_fields_flag))1244      assert isinstance(test_ds.miss_val, list), \1245             'MEM data set missing values variable is not a list: %s' % \1246             (str(test_ds.miss_val))1247      if (isinstance(miss_val, str)):1248        tmp_miss_val = [miss_val]1249      else:1250        tmp_miss_val = miss_val1251      assert test_ds.miss_val == tmp_miss_val, \1252             'MEM data set has wrong missing values: %s (should be: %s)' % \1253             (str(test_ds.miss_val), str(tmp_miss_val))1254      # Write some records1255      #1256      test_data = {'00':['50','mitchell','polmear','341','fitchett street',1257                         '',"    o'connor  ",'  2906   '],1258                   '01':['61','isaad','whte  ','  15','  rivett circuit','',1259                         'rivett','   2906'],1260                   '02':['  62  ','isaac','wiglht','  15  ','tyrrell circuit',1261                         'rivett  ','2906   ']}1262      test_ds.write(test_data)1263      # Now check all values in the records from the data set1264      #1265      rec_cnt = 01266      for (rec_id, rec_data) in test_ds.readall():1267        for col_val in rec_data:1268          if ((strip_fields_flag == True) and (len(col_val) > 0)):1269            assert col_val[0] not in string.whitespace, \1270                   'Field value not stripped of whitespaces: "%s"' % (col_val)1271            assert col_val[-1] not in string.whitespace, \1272                   'Field value not stripped of whitespaces: "%s"' % (col_val)1273          assert col_val not in tmp_miss_val, \1274                 'Missing value not removed: "%s" (from list: %s' % \1275                 (col_val, str(tmp_miss_val))1276      test_ds.finalise()1277      test_ds = None1278      test_ds = dataset.DataSetShelve(description='A test Shelve data set',1279                                      file_name = 'test-data.slv',1280                                      clear = True,  # False,1281                                      access_mode='readwrite',1282                                      field_list=[('rec-id',''),('gname',''),1283                                                  ('surname',''),1284                                                  ('streetnumb',''),1285                                                  ('streetname_type',''),1286                                                  ('suburb',''),1287                                                  ('postcode','')],1288                                      miss_v=miss_val,1289                                      strip_f=strip_fields_flag,1290                                      rec_ident='rec-id')1291      assert test_ds.strip_fields in [True, False], \1292             'SHL data set strip fields is not a boolean: %s' % \1293             (str(test_ds.strip_fields))1294      assert test_ds.strip_fields == strip_fields_flag, \1295             'SHL data set has wrong strip fields values: %s (should be: %s)' \1296             % (str(test_ds.strip_fields), str(strip_fields_flag))1297      assert isinstance(test_ds.miss_val, list), \1298             'SHL data set missing values variable is not a list: %s' % \1299             (str(test_ds.miss_val))1300      if (isinstance(miss_val, str)):1301        tmp_miss_val = [miss_val]1302      else:1303        tmp_miss_val = miss_val1304      assert test_ds.miss_val == tmp_miss_val, \1305             'SHL data set has wrong missing values: %s (should be: %s)' % \1306             (str(test_ds.miss_val), str(tmp_miss_val))1307      # Write some records1308      #1309      test_data = {'00':['50','mitchell','polmear','341','fitchett street',1310                         '',"    o'connor  ",'  2906   '],1311                   '01':['61','isaad','whte  ','  15','  rivett circuit','',1312                         'rivett','   2906'],1313                   '02':['  62  ','isaac','wiglht','  15  ','tyrrell circuit',1314                         'rivett  ','2906   ']}1315      test_ds.write(test_data)1316      # Now check all values in the records from the data set1317      #1318      rec_cnt = 01319      for (rec_id, rec_data) in test_ds.readall():1320        for col_val in rec_data:1321          if ((strip_fields_flag == True) and (len(col_val) > 0)):1322            assert col_val[0] not in string.whitespace, \1323                   'Field value not stripped of whitespaces: "%s"' % (col_val)1324            assert col_val[-1] not in string.whitespace, \1325                   'Field value not stripped of whitespaces: "%s"' % (col_val)1326          assert col_val not in tmp_miss_val, \1327                 'Missing value not removed: "%s" (from list: %s' % \1328                 (col_val, str(tmp_miss_val))1329      test_ds.finalise()1330      test_ds = None1331# =============================================================================1332# Start tests when called from command line1333if (__name__ == "__main__"):1334  # Intialise a logger, set level to info1335  #1336  my_logger = logging.getLogger()  # New logger at root level1337  my_logger.setLevel(log_level)1338  unittest.main()  # Run all test...tester2.py
Source:tester2.py  
1from mailroom5 import *2import os, fnmatch3def create_test_dict():4    ds = DonorCollection({})5    ds.add_donor(Donor("Ben Wyatt", [1663.23, 4300.87, 10432.0]))6    ds.add_donor(Donor("Ron Swanson", [100000]))7    ds.add_donor(Donor("April Ludgate", [10, 1.52, 0.25]))8    ds.add_donor(Donor("Ann Perkins", [100, 100]))9    ds.add_donor(Donor("Leslie Knope", [1663.23, 4300.87, 1432.0]))10    return ds11#---------TESTING FUNCTION 1: SEND THANK YOU---------#12def tester_add_donor():13    Tom = Donor('Thomas montgomery haverford', [10])14    print(Tom.name)15    test_ds = create_test_dict()16    test_ds.add_donor(Tom)17    print(test_ds.donors.keys())18    assert test_ds.donors["Thomas Montgomery Haverford"].donations == [10]19def test_add_donation():20    test_ds = create_test_dict()21    test_ds.donors['Ron Swanson'].add_donation(10)22    print(test_ds.donors.keys())23    print(test_ds.donors["Ron Swanson"].donations)24    assert test_ds.donors["Ron Swanson"].donations == [100000, 10]25def test_thank_you():26    test_ds = create_test_dict()27    thank_you_print = test_ds.donors['Ron Swanson'].generate_thank_you()28    print(thank_you_print)29    assert thank_you_print == f"Dear Ron,\n\tThank you for your generous donation of $100000.00! " \30                          "Each dollar you donate ends up providing endless value for our town. \n\t" \31                          "We appreciate your gift and hope our partnership extends well into the future. \n" \32                          "Regards, \n\tThe Pawnee Restoration Fund"33def test_check_donor():34    test_ds = create_test_dict()35    assert test_ds.check_donor("Ron Swanson") is True36    assert test_ds.check_donor("Tammy Two") is False37# ---------TESTING FUNCTION 2: GENERATE REPORT---------#38def test_gen_report():39    test_ds = create_test_dict()40    assert test_ds.donors["Ron Swanson"].donor_report == ["Ron Swanson", 100000, 1, 100000]41def test_sum_donors():42    test_ds = create_test_dict()43    print(test_ds.printed_report[1])44    print(test_ds.printed_report[2])45    print(test_ds.printed_report[3])46    print(test_ds.printed_report[4])47    print(test_ds.printed_report[5])48    assert test_ds.printed_report[1] == f"{'Ron Swanson': <20}${float(100000):^20,.2f}{'1' : ^20}${'100000.00' : >20}"49    assert test_ds.printed_report[2] == f"{'Ben Wyatt': <20}${float(16396.1):^20,.2f}{'3' : ^20}${'5465.37' : >20}"50    assert test_ds.printed_report[3] == f"{'Leslie Knope': <20}${float(7396.1):^20,.2f}{'3' : ^20}${'2465.37' : >20}"51    assert test_ds.printed_report[4] == f"{'Ann Perkins': <20}${float(200):^20,.2f}{'2' : ^20}${'100.00' : >20}"52    assert test_ds.printed_report[5] == f"{'April Ludgate': <20}${float(11.77):^20,.2f}{'3' : ^20}${'3.92' : >20}"53# ---------TESTING FUNCTION 3: THANK YOU DUMP---------#54def test_file_save():55    test_ds = create_test_dict()56    td = os.getcwdb()57    for name, donor in test_ds.donors.items():58        donor.save_file(td)59    listOfFiles = os.listdir('.')60    print(listOfFiles)61    filenames = {"Leslie_Knope.txt", "April_Ludgate.txt", "Ben_Wyatt.txt", "Ron_Swanson.txt", "Ann_Perkins.txt"}62    print(filenames)63    for name in filenames:64        os.remove(name)65    assert filenames.issubset(listOfFiles) is True...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
