def test_if(): """Test IFs statements and boolean functions""" record = MasterRecord() record.update(test_data) expr_list = ( ('if p(v26) then v26^a fi', 'Paris'), ('if p(v25) then v26^a fi', ''), ('if p(v26) then v26^a else v26^b fi', 'Paris'), ('if p(v25) then v26^a else v26^b fi', 'Unesco'), ('if a(v26) then v26^a fi', ''), ('if a(v25) then v26^a fi', 'Paris'), ('if a(v26) then v26^a else v26^b fi', 'Unesco'), ('if a(v25) then v26^a else v26^b fi', 'Paris'), ('if p(v26) then v26^a fi', 'Paris'), ('if p(v26) then v26^a,v26^b fi', 'ParisUnesco'), ('if p(v26) then v26^a v26^b fi', 'ParisUnesco'), # field expression ("if v26>'' then v26^a fi", 'Paris'), ("if v50:'bib' then v26^a fi", 'Paris'), # test for heading and trailing spaces ("if 1=1 then 'ok' fi ", "ok"), (" if 1=1 then 'ok' fi", "ok"), (" if 1=1 then 'ok' fi ", "ok"), ("if nocc(v70)=2 then 'OK' else 'NOK' fi", "OK"), ) run_list(expr_list, record)
def test_resave_record(): for ext in ('.mst', '.xrf'): fname = join("..", "sample", 'one%s' % ext) if exists(fname): remove(fname) mf = MasterFile(join("..", "sample", "one.mst")) rec = MasterRecord() rec["300"] = "XXXX" rec.save(mf) print rec del mf mf = MasterFile(join("..", "sample", "one.mst")) rec = MasterRecord(mfn=2) rec["300"] = "XXXX" rec.save(mf) print rec del mf mf = MasterFile(join("..", "sample", "one.mst")) rec = mf[1] rec["300"] = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" rec.save(mf) print rec del mf
def test_boolean_expr(): """Test Boolean expressions""" record = MasterRecord() record.update(test_data) expr_list = ( ("1<>2", "True"), ("1=2", "False"), ("1>2", "False"), ("1 > 2", "False"), ("1> 2", "False"), ("1 >2", "False"), ("1<2", "True"), ("1>=2", "False"), ("2>=1", "True"), ("2<=1", "False"), ("2>=1", "True"), ("1 > 2", "False"), ("1>2 or 2>1", "True"), ("1>2 and 2>1", "False"), ("1<2 or 2>1", "True"), ("1<2 and 2>1", "True"), ("1<2 xor 2>1", "False"), ("1>2 xor 2>1", "True"), ("not 1<2", "False"), ("not (1<2)", "False"), ) run_list(expr_list, record)
def test_max_width(): """Test max width and field alignment""" record = MasterRecord() record.update(test_data) # Fake config and change default param MAX_LINE_WIDTH record.mst = Lyer() record.mst.config.INPUT_ENCODING = 'utf-8' record.mst.config.OUTPUT_ENCODING = 'utf-8' record.mst.config.MAX_LINE_WIDTH = 40 record.mst.config.YACC_DEBUG = False expr_list = ( ("v44", 'Methodology of plant eco-physiology: \nproceedings of the Montpellier \nSymposium' ), ("v44(0,0)", 'Methodology of plant eco-physiology: \nproceedings of the Montpellier \nSymposium' ), ("v44(10)", ' Methodology of plant \neco-physiology: proceedings of the \nMontpellier Symposium' ), ("v44(0,5)", 'Methodology of plant eco-physiology: \n proceedings of the Montpellier \n Symposium' ), ("v44(10,5)", ' Methodology of plant \n eco-physiology: proceedings of the \n Montpellier Symposium' ), # special case of unbreakable line ("'%s'" % ('a' * 120), u'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ), ) run_list(expr_list, record)
def test_repeatable_literals(): """Test repeatable literals""" record = MasterRecord() record.update(test_data) expr_list = ( ('|Do not show|v1', ''), ('|Show|v26', 'Show^aParis^bUnesco^c-1965'), ('mpu,|Show|v26^a', 'SHOWPARIS'), ('v1|Do not show|', ''), ('v26|Show|', '^aParis^bUnesco^c-1965Show'), ('|Do not show|v26^z', ''), ('|Show|v26^a', 'ShowParis'), ('v26^z|Do not show|', ''), ('v26^b|Show|', 'UnescoShow'), ('| Author:|v70', ' Author:Magalhaes, A.C. Author:Franco, C.M.'), ('| Author:|+v70', 'Magalhaes, A.C. Author:Franco, C.M.'), ('|<Left> |v70| <Right>|', u'<Left> Magalhaes, A.C. <Right><Left> Franco, C.M. <Right>'), ('|<Left> |+v70| <Right>|', u'Magalhaes, A.C. <Right><Left> Franco, C.M. <Right>'), ('|<Left> |v70+| <Right>|', u'<Left> Magalhaes, A.C. <Right><Left> Franco, C.M.'), ('|<Left> |+v70+| <Right>|', u'Magalhaes, A.C. <Right><Left> Franco, C.M.'), ('|Show |v26^a|; |/', 'Show Paris; \n'), ('|Show |+v26^a|; |/', 'Paris; \n'), ('|Show |v26^a+|; |/', 'Show Paris\n'), ('|Show |+v26^a+|; |/', 'Paris\n'), ('|Show |v26^a(5,5)|; |/', ' Show Paris; \n'), ('|Show |+v26^a(5,5)|; |/', ' Paris; \n'), ('|<antes>|+v70,|meio|+v26+|depois|/', 'Magalhaes, A.C.<antes>Franco, C.M.^aParis^bUnesco^c-1965\n'), ) run_list(expr_list, record)
def test_spacing(): """Test for vertical spacing commands: # / %""" record = MasterRecord() record.update(test_data) expr_list = ( ('v26^b v26^a', 'UnescoParis'), ('v26^b v26^a', 'UnescoParis'), ('v26^b/v26^a', 'Unesco\nParis'), ('v26^b///v26^a', 'Unesco\nParis'), ('v26^b#v26^a', 'Unesco\nParis'), ('v26^b/#v26^a', 'Unesco\n\nParis'), ('v26^b##v26^a', 'Unesco\n\nParis'), ('v26^b###%v26^a', 'Unesco\nParis'), ('v26^bx10v26^a', 'Unesco Paris'), ('v26^bX10v26^a', 'Unesco Paris'), ('v26^bX100v26^a', "Unesco\nParis"), ('v26^bC0v26^a', "Unesco\nParis"), ("v26^bC7v26^a", "UnescoParis"), ("v26^bC8v26^a", "Unesco Paris"), # ignore tabulation that exceeds MAX_WIDTH ("v26^bC120v26^a", "UnescoParis"), ("v26^bC5v26^a", "Unesco\n Paris"), ("### X3 v26^b", "\n\n\n Unesco"), ("###,X3,v26^b", "\n\n\n Unesco"), ("v26^a#", "Paris\n"), ("v26^a///", "Paris\n"), ("v26^a#", "Paris\n"), ("v26^a%##v26^b%##v26^c", "Paris\nUnesco\n\n-1965"), ) run_list(expr_list, record)
def test_delete_record(): for ext in ('.mst', '.xrf'): fname = join("..", "sample", 'one%s' % ext) if exists(fname): remove(fname) mf = MasterFile(join("..", "sample", "one.mst")) rec = MasterRecord() rec["300"] = "XXXX" rec.save(mf) print rec del mf mf = MasterFile(join("..", "sample", "one.mst")) l1 = len(mf) mf.delete(1) mf.commit() del mf mf = MasterFile(join("..", "sample", "one.mst")) l2 = len(mf) del mf print "l1=" + str(l1) print "l2=" + str(l2) assert l2 == l1 - 1, "wrong number of active records after deleting record"
def test_proc_gsplit(): """Test proc gsplit""" record = MasterRecord() record.v1 = 'Perez, J.; Garcia, Maria; Machado, A.' expr_list = ( ("proc('gsplit=1=;'),v1", u'Perez, J. Garcia, Maria Machado, A.'), ("proc('Gsplit=1=;'),v1", u'Perez, J. Garcia, Maria Machado, A.'), ) run_list(expr_list, record)
def test_first_subfield_default(): """Test sub-field acessor ^* in field without subfields""" record = MasterRecord() record.update(test_data) first_subfield = format("v50^*", record) assert first_subfield == 'Incl. bibl.', 'Failed to extract first subfield' record.v13 = "First ^ok" assert format( "v13^*", record) == 'First ', 'Failed during first subfield extraction'
def createRecord(params): """Create new record <?xml version='1.0'?> <methodCall> <methodName>createRecord</methodName> <params> <param> <value> <struct> <member> <name>collection</name> <value><string>sample</string></value> </member> <member> <name>database</name> <value><string>dbtest</string></value> </member> <member> <name>26</name> <value><string>^aParis^bUnesco^c-1965</string></value> </member> <member> <name>30</name> <value><string>^ap. 211-224^billus.</string></value> </member> <member> <name>50</name> <value><string>Incl. bibl.</string></value> </member> </struct> </value> </param> </params> </methodCall> """ try: colname, dbname, collection, database = get_database_info(params) recno = database.nxtmfn mstrecord = MasterRecord(recno) for tag in params.keys(): if tag in ('database', 'collection'): continue value = params[tag] if type(value) is list: mstfield = [MasterField(tag, valtag) for valtag in value] else: mstfield = MasterField(tag, value) mstrecord[tag] = mstfield mstrecord.save(database) return _("Record mfn=%s created") % recno except Exception, e: return _("** Create Record (%s)") % str(e)
def test_boolean_functions(): """Test boolean evaluation functions P() and A()""" record = MasterRecord() record.update(test_data) expr_list = ( ('p(v26)', 'True'), ('a(v26)', 'False'), ('P(v26)', 'True'), ('A(v26)', 'False'), ) run_list(expr_list, record)
def test_field_expressions(): """Test field access expressions""" record = MasterRecord() record.update(test_data) expr_list = ( ("v30^a,v30^b", 'p. 211-224illus.'), ("v26^a", 'Paris'), ("v26^a*2", 'ris'), ("v26^a.2", 'Pa'), ("v26^a*1.3", 'ari'), ) run_list(expr_list, record)
def test_compile_code(): """Compile a simple ast into Python executable code""" rec = MasterRecord() data = "Paper on: <plant physiology><plant transpiration><measurement and instruments>" rec.v69 = data ast = parser.parse("V69") chain = flatten(ast) pft = compiler.compile_code(chain) result = pft(rec=rec, mst=None) #print "result=", repr(result), "\ndata=", repr(data) # remove line braks prior to comparison with original data assert result.replace("\n", "") == data
def test_add_repeatable(): """Test adding repeatable fields""" rec = MasterRecord() rec.v90 = ["Hello", "Good-bye", "^a123^bBelex"] assert rec.v90 == rec[90] == rec["90"] # Indexing begins from 1 assert rec.v90[1].data == "Hello" assert rec.v90.tag == 90 assert rec.v90[2].tag == 90 assert type(rec.v90) == MasterContainerField assert rec.v90[3].a == '123' assert rec.v90[3].b == 'Belex'
def test_numerical_expr(): """Test Numerical expressions""" record = MasterRecord() record.update(test_data) expr_list = ( ('0.155e+3', '155.0'), ('1e-3', '0.001'), ('2*3+9', '15'), ("2*(3+9)", '24'), ("10-(4*(2-1))", "6"), ("15*0.001", "0.015"), ) run_list(expr_list, record)
def test_update_flags(): """Test update flags (xrf and master files)""" xref_fd = join('fixtures', 'testflag.xrf') db_fd = join('fixtures', 'testflag.mst') if exists(xref_fd): remove(xref_fd) if exists(db_fd): remove(db_fd) dbtest = MasterFile(db_fd) rec = MasterRecord() rec.update(test_data) rec.save(dbtest) xrf_rec = dbtest.xrf[1] assert xrf_rec.status == 'active' assert xrf_rec.status == 'active' assert xrf_rec.new_flag == True assert xrf_rec.modified_flag == False rec = dbtest[1] rec[27] = 'Test new field' rec.save(dbtest) assert xrf_rec.new_flag == False assert xrf_rec.modified_flag == True dbtest.invertdb(expr='70 0 MHU,(V70/)') assert xrf_rec.new_flag == False assert xrf_rec.modified_flag == False
def test_unconditional_literals(): """Test unconditional literals""" record = MasterRecord() record.update(test_data) expr_list = ( ("v30^a,' Belex ', v30^b", 'p. 211-224 Belex illus.'), ("v26^a,' Belex'", 'Paris Belex'), ("'Belex'", 'Belex'), ("mpu,'Belex'", 'BELEX'), ("'Belex ',v26^a", "Belex Paris"), ("'Belex 'v26^a", "Belex Paris"), ("v26^a' Belex'", "Paris Belex"), ("'Be''lex'", "Belex"), ) run_list(expr_list, record)
def create(self, request): """Method to create database or record """ try: try: colname,dbname,collection,database,index = get_database_info(request) except DatabaseNotFound: colname,dbname,collection,database,index = get_database_info(request,1) dbname = get_xml_parameters(request).database if not dbname: raise Exception(DB_NAME_ERROR) try: newdb = collection[dbname] except: pass else: raise Exception(DB_ALREADY_EXISTS) newdb = MasterFile(os.path.join(collection.path_list[0], '%s.mst'%dbname)) collection.databases[dbname] = newdb return HttpResponse(XML_OK) else: recno = database.nxtmfn mstrecord = MasterRecord(recno) return self.create_update_record(request, database, mstrecord, recno) except Exception, e: return HttpResponse(XML_ERROR % 'CREATE (%s)' % str(e))
def test_strfuncs(): record = MasterRecord(mfn=1) record.update(test_data) if platform == 'linux2': value = '1.131400E+00' else: value = '1.131400E+000' expr_list = ( ("f(1.1314,2)", value), ("f(3.1415,10,5)", ' 3.14150'), ("f(1.1314,3,0)", ' 1'), ("s('Pa','ris')", 'Paris'), ("s(mfn,v26)", '000001^aParis^bUnesco^c-1965'), ) run_list(expr_list, record)
def test_delete_record(): """Create 3 records and delete the one in the middle""" clean_start() size = 3 mf = MasterFile(testmst) for i in range(size): rec = MasterRecord() rec.update(test_data) rec.update({131: "flag %d" % i}) rec.save(mf) mf.delete(2)
def test_update(): """Create a record, modify it and save it and check previous""" clean_start() mf = MasterFile(testmst) r = MasterRecord() r.update(test_data) r.save(mf) r[90] = "This is a new field" r.save(mf) prev = mf.previous(r) assert prev is not None assert len(prev) + 1 == len(r)
def test_conditional_literals(): """Test conditional literals""" record = MasterRecord() record.update(test_data) expr_list = ( ('"Do not show"v1', ''), ('"Show"v26', 'Show^aParis^bUnesco^c-1965'), ('mpu,"Show"v26', 'SHOW^APARIS^BUNESCO^C-1965'), ('v1"Do not show"', ''), ('v26"Show"', '^aParis^bUnesco^c-1965Show'), ('"Do not show"v26^z', ''), ('"Show"v26^a', 'ShowParis'), ('v26^z"Do not show"', ''), ('v26^b"Show"', 'UnescoShow'), ('"Authors:"v70', 'Authors:Magalhaes, A.C.Franco, C.M.'), ('v70" (authors)"', 'Magalhaes, A.C.Franco, C.M. (authors)'), ('"->"x3v26', '-> ^aParis^bUnesco^c-1965'), ) run_list(expr_list, record)
def test_max_width_off(): """Test max width==0 and field alignment""" record = MasterRecord() record.update(test_data) # Fake config and change default param MAX_LINE_WIDTH record.mst = Lyer() record.mst.config.INPUT_ENCODING = 'utf-8' record.mst.config.OUTPUT_ENCODING = 'utf-8' record.mst.config.MAX_LINE_WIDTH = 0 record.mst.config.YACC_DEBUG = False expr_list = ( ("v44", '\nMethodology of plant eco-physiology: proceedings of the Montpellier Symposium' ), ("v44(0,0)", ''), ("v44(10)", ''), ("v44(0,5)", ''), ("v44(10,5)", ''), ) run_list(expr_list, record)
def test_numfunc(): """Test Numerical functions""" record = MasterRecord() record.update(test_data) record.v13 = ["a 10", "b 20", "c 30"] expr_list = ( ('val(26)', '26'), ('val(v26)', '-1965'), ('val(V26)', '-1965'), ("v26^c*1", '1965'), ("val('xxxx7yyyy8zzzz')", "7"), ("val('water')", "0"), ("val(10)*val(2)", "20"), ("val('10')*val('2')", "20"), ("val('dfsdf sdfsdf sdfsdfds')=0", "True"), ("val('dfsdf sdfsdf sdfsdfds')", "0"), ("val(v26)/10*-1", "196.5"), ("val(val(1))", "1"), ("val(-1)", "-1"), ("rsum(10,20,30)", "60"), ("rmax(10,20,30)", "30"), ("rmin(10,20,30)", "10"), ("ravr(10,20,30)", "20"), ("RSUM(10,20,30)", "60"), ("RMAX(10,20,30)", "30"), ("RMIN(10,20,30)", "10"), ("RAVR(10,20,30)", "20"), ("rsum('10')", "10"), ("rsum(10)", "10"), ("rsum('10,20,30')", "60"), ("rsum('a,10,b,20,c,30')", "60"), ("rsum(v26)", "-1965"), ("rsum(v26,1965)", "0"), ("rsum(v70)", "0"), ("rsum(v13)", "60"), ("rmax(v13)", "30"), ("rmin(v13)", "10"), ("ravr(v13)", "20"), ("rsum(v30)", "-13"), ) run_list(expr_list, record)
def test_dummy_field(): """Test Dummy field constructs Dt, Dt^x, Nt, Nt^x""" record = MasterRecord() record.update(test_data) expr_list = ( ('"Ola:"V26^a', 'Ola:Paris'), ('"Ola:"D26^a', 'Ola:'), ('"Ola:"N26^a', ''), ('"Ola:"D23^a', ''), ('"Ola:"N23^a', 'Ola:'), ('"Ola:"v26^a', 'Ola:Paris'), ('"Ola:"d26^a', 'Ola:'), ('"Ola:"n26^a', ''), ('"Ola:"d23^a', ''), ('"Ola:"n23^a', 'Ola:'), ('"Ola:"v26', 'Ola:^aParis^bUnesco^c-1965'), ('"Ola:"d26', 'Ola:'), ('"Ola:"n26', ''), ('"Ola:"d23', ''), ('"Ola:"n23', 'Ola:'), ) run_list(expr_list, record)
def test_singlerecord_master(): """Create a MasterFile with a single record inside""" clean_start() mf = MasterFile(testmst) rec = MasterRecord() rec.update(test_data) rec.save(mf) del mf # force flush
def test_restore_previous(): """Create a record, modify it, save it and restore previous version""" clean_start() mf = MasterFile(testmst) r = MasterRecord() r.update(test_data) r.save(mf) r[90] = "This is a new field" r.save(mf) prev = mf.previous(r) prev.save(mf) old_r = mf[1] try: old_r[90] assert False, "Field with tag 90 should not exist anymore." except KeyError: pass assert r.status == ACTIVE assert len(old_r) == len(prev)
def test_write_multiple_records(): """Write multiple records in a newly created database.""" clean_start() size = 381 mf = MasterFile(testmst) for i in range(size): rec = MasterRecord() rec.update(test_data) rec.save(mf) mf.commit() assert len(mf) == size assert mf[90].v50.data == 'Incl. bibl.' del mf
def test_undelete(): """Create a record and undelete it""" clean_start() mf = MasterFile(testmst) r = MasterRecord() r.update(test_data) r.save(mf) mf.delete(1) mf.commit() r = mf[1] assert r.status == LOGICALLY_DELETED assert mf.xrf[1].status == 'logically deleted' assert len(mf) == 0 mf.undelete(1) r = mf[1] assert r.status == ACTIVE assert mf.xrf[1].status == 'active' assert len(mf) == 1
def test_ilit(): """Test Inconditional literal""" rec = MasterRecord() rec.v26 = "^aParis^bUnesco^c-1965" rec.v30 = "^ap. 211-224^billus." ast = parser.parse("'Pais:'v26^a,v30^a")