def startSession_fails_missing_audience_id_are_attributes_set(): try: ok = True g.log.info(verification_header.format("startSession profile attributes test; steps:")) g.log.info("1 call startSession with non-existing audience ID, setting several attributes (not all), it will fail") g.log.info("2 call getProfile") g.log.info("3 check if attributes from getProfile have values set in previous failed startSession") pars = g.uaci_params.deepcopy() pars.set_audience_ID(IA.tstampAudienceID()) # g.log.info("call start Session with non-existing cookie") ss = IA.StartSession(pars, g.prof01, "false") sent_attributes = ss.get_send_attributes("Profile sent by startsession") # sent_attributes.dump() ss.call(True) # ss.dump() g.log.info("call getProfile for non-existing cookie") profile = IA.GetProfile(pars) ret = profile.call(True) returned_attrs = profile.create_Attributes() diffs = sent_attributes.left_compare(returned_attrs) sent_attributes.compare_values(returned_attrs, diffs) sent_attributes.left_print_side_2_side(returned_attrs) g.log.info("\n\nProfile Dump -------------------") profile.dump() finally: IA.EndSession(g.uaci_params)
def startSession_trigger_stored_procedure(): try: ok = True pars = g.uaci_params.deepcopy() pars.set_audience_ID(IA.tstampAudienceID()) # pars.set_channel("EV_Testing") attrs = g.prof01.deepcopy() g.log.error("(for this test we only need to set an attribute)") attrs.set_attribute("ds_segment","enrico_segment",strict = True) ss = IA.StartSession(pars, attrs, "false") sent_attributes = ss.get_send_attributes("Profile sent by startsession") ss.call(True) # ss.dump() g.log.info("\ncall getProfile") profile = IA.GetProfile(pars) ret = profile.call(True) returned_attrs = profile.create_Attributes() diffs = sent_attributes.left_compare(returned_attrs) g.log.info("\n") sent_attributes.compare_values(returned_attrs, diffs) g.log.info("\n") sent_attributes.left_print_side_2_side(returned_attrs) finally: IA.EndSession(g.uaci_params)
def API_unit_test(uaci_params, inter_point): ''' One method of calling the Interact API is by using JSON over HTTP The REST API returns SessionIDs and messages in the HTML-escaped format and not in the Unicode format. :return: ''' global head, log ok = True # GET VERSION # ok = ok and callAPI(GetVersion(sess_id)) ok = ok and IA.GetVersion(uaci_params).call(True) # START SESSION ok = ok and IA.StartSession(uaci_params, None,"false").call(True) # GET OFFERS ok = ok and IA.GetOffers(uaci_params, inter_point).call(True) # POST EVENT eventNameString = g.events[0][0] po = PostEvent(uaci_params,eventNameString) ok = ok and po.call(True) # add a couple of attrributes attrs = Attributes(('ds_segment',"string","enrico_ds_segment")) attrs.add_tuple(('ds_cmp_1', 'numeric',0.0)) po.sett_attrs(attrs) ok = ok and po.call(True) # GET PROFILE ok = ok and IA.GetProfile(uaci_params).call(True) # SET DEBUG ok = ok and SetDebug(uaci_params,False).call(True) ok = ok and SetDebug(uaci_params,True).call(True) # BATCH cmdsList = list() cmdsList.append(SetDebug(uaci_params,False)) cmdsList.append(GetVersion(uaci_params)) # cmdsList.append(GetProfile(uaci_params)) bc = BatchCmds(uaci_params, cmdsList) ok = ok and bc.call(True) # END SESSION ok = ok and EndSession(uaci_params).call(True)
def startSession_fails_do_offers_work(): try: ok = True pars = g.uaci_params.deepcopy() pars.set_channel("EV_Testing") attrs = g.prof01.deepcopy() pars.set_audience_ID(IA.tstampAudienceID()) g.log.info(g.verification_header.format("startSession profile attributes test; steps:")) # g.log.info("1 call startSession with non-existing audience ID, setting several attributes (not all), it will fail") ss = IA.StartSession(pars, attrs, "false") ok = ss.call(True) if ok: g.log.error("###### ERROR: startSession, expected to fall, succeeded") # return False # --- 2nd call, after 1st failed, startSession setting things up for new cookie g.log.info("OK, startSession, expected to fail, failed, it means we have new cookie") g.log.info("\n2nd startSession call, after 1st failed as expected, setting things up for new cookie") g.log.error("(for this test we only need to set an attribute)") attrs.set_attribute("ds_segment","enrico_segment",strict = True) ss = IA.StartSession(pars, attrs, "false") sent_attributes = ss.get_send_attributes("Profile sent by startsession") ss.call(True) # ss.dump() g.log.info("\ncall getProfile, not strictly necessary, just to verify attributes of startSession have been set correctly") profile = IA.GetProfile(pars) ret = profile.call(True) returned_attrs = profile.create_Attributes() diffs = sent_attributes.left_compare(returned_attrs) g.log.info("\n") sent_attributes.compare_values(returned_attrs, diffs) # sent_attributes.left_print_side_2_side(returned_attrs) g.log.info("call getOffers") getOffers = IA.GetOffers(pars, "ip01") ok = getOffers.call(False) g.log.info("\nDump getOffers response") getOffers.dump(True) pass finally: IA.EndSession(g.uaci_params)
def stress_test_developing(): ''' Trying to mirror the specs :return: ''' nr_stress_cookies = 1000 pars = g.uaci_params.deepcopy() new_vistor_attrs = g.new_vistor_profile_attrs.deepcopy() cookies = genAudienceIDs(nr_stress_cookies) start_time = time.time() verbose = False try: ok = True for cookie in cookies: pars.set_audience_ID(cookie) #ut.logBanner("TEST for AudienceID: <{}>".format(cookie)) for rep in range(1,2): # g.log.info("cookie <{}> repetition {}".format(cookie,rep)) for event in g.events: # pageActionLog("PageLoad") ### START SESSION #apiActionLog("START SESSION") ss = IA.StartSession(pars, None, "false") ok = ss.call(verbose) if not ok: #g.log.info("<{}> is new visitor, setting profile with second startSession".format(cookie)) ss.set_attributes(new_vistor_attrs,True) #apiActionLog("START SESSION (SECOND, to set new visitor profile, this also will give error but it's ok)") ok = ss.call(verbose=False, dumpAttributes=False) else: #g.log.info("### "+cookie + " is NOT new ###") pass ### GET PROFILE # apiActionLog("GET PROFILE (NOT in specs, for internal use)") profile = IA.GetProfile(pars) ret = profile.call(verbose) attrs_before_evt = profile.create_Attributes("before event") ### POST EVENTs # keep event and corresp attribute name in a tuple event_name, attribute_name = event attribute_name = attribute_name.lower() attr_value_before = attrs_before_evt.get_attribute_value(attribute_name) #g.log.info("before post event: {}={}".format(attribute_name, attr_value_before)) #apiActionLog("POST EVENT event: [{}] (on db should correspond to [{}])".format(event_name, attribute_name)) po = IA.PostEvent(pars,event_name) # add a couple of attrributes # attrs = IA.Attributes(('ds_segment',"string",event)) # attrs.add_tuple(('ds_cmp_1', 'numeric',0.0)) # po.sett_attrs(attrs) ok = po.call(verbose = False, dumpAttributes = False) #time.sleep(1) ### GET PROFILE #apiActionLog("GET PROFILE, ### INTERNAL ###, just to demonstrate Interact does not realize DB update") ret = profile.call(verbose) attrs_after_evt = profile.create_Attributes("after event") attr_value_after = attrs_after_evt.get_attribute_value(attribute_name) # g.log.info("after post event {}: {} = {}".format(event_name,attribute_name, attr_value_after)) # check on DB row = jdb.dbGetProfileCol(cookie, attribute_name) if row is not None: #g.log.info("query on DB: {} = {}".format(attribute_name,row[attribute_name])) pass else: #g.log.error(cookie+"not found in db") pass #diffs = attrs_after_evt.full_compare(attrs_before_evt) #attrs_after_evt.compare_values(attrs_before_evt, diffs) # attrs_before_evt.left_print_side_2_side(attrs_after_evt) #g.log.info("after post event {} = ".format(attrs_after_evt.get_attribute_value(event(2)))) #apiActionLog("to make interact aware of attribute update on DB we have to perform now"+ # "\nendSession + startSession + getProfile" # + "\n(here sent individually, batch command should improve performance, not tested)"+ # "\nprobably endSession and getProfile unneeded, just startSession may suffice, but be safe") #apiActionLog("END SESSION --- to restart and read values on DB") es = IA.EndSession(pars) es.call() #apiActionLog("START SESSION --- restart to read attributes from DB") ss.set_attributes(None, overwrite=True) ok = ss.call(verbose) #apiActionLog("GET PROFILE --- check if interact now got from DB attributes from post event") ret = profile.call(verbose) attrs_after_evt = profile.create_Attributes("after event") attr_value_after = attrs_after_evt.get_attribute_value(attribute_name) #g.log.info("after post event: {} = {}".format(attribute_name, attr_value_after)) # check on DB row = jdb.dbGetProfileCol(cookie, attribute_name) if row is not None: #g.log.info("query on DB: {} = {}".format(attribute_name,row[attribute_name])) pass else: pass #g.log.error(cookie+"not found in db") # pageActionLog("PageExit") # g.log.info("") #g.log.info("") #pageActionLog("HERE, BEFORE ENDSESSION WE WOULD NEED TO COMMANDED, BY WEB CODE, VIA AN EVENT, TO SAVE PROFILE TO DB") ### END SESSION #apiActionLog("END SESSION") ok = IA.EndSession(pars) if not ok: pass g.log.error("failed endSession, cookie: {} sessionId: {}".format(cookie,pars.get_session_id())) #g.log.info("SESSION ENDED for cookie <{}>\n\n".format(cookie)) #g.log.info("cookie '{}' Test ended\n".format(cookie)) #g.log.info( jdb.dbGetAudIDRow(cookie,0, verbose = False)) #g.log.info("SQL for query: \n"+generateSQLForAudID(cookie)) end_time = time.time() duration_msecs = (end_time - start_time)*1000 g.log.info("processed {} cookies in {}".format(nr_stress_cookies,duration_msecs*1000)) # except Exception as exc: # g.log.error("Generic Error") finally: IA.EndSession(pars) return True
def testEvents(cookiePar = None): ''' Trying to mirror the specs :return: ''' global nr_test_cookies cookie = genAudienceIDs(1)[0] if (cookiePar is None) else cookiePar verbose = False pars = g.uaci_params.deepcopy() new_vistor_attrs = g.new_vistor_profile_attrs.deepcopy() pars.set_audience_ID(cookie) try: ok = True ut.logBanner("TEST attributes for AudienceID: <{}>".format(cookie)) for rep in range(1,3): g.log.info("cookie <{}> repetition {}".format(cookie,rep)) for event in g.events: # pageActionLog("PageLoad") ### START SESSION # apiActionLog("START SESSION") ss = IA.StartSession(pars, None, "false") ok = ss.call(verbose) if not ok: g.log.info("<{}> is new visitor, setting profile with second startSession".format(cookie)) ss.set_attributes(new_vistor_attrs,True) # apiActionLog("START SESSION (SECOND, to set new visitor profile)") ok = ss.call(verbose=True, dumpAttributes=True) else: g.log.info("### "+cookie + " is NOT new ###") pass ### GET PROFILE apiActionLog("GET PROFILE (NOT in specs, for internal use)") profile = IA.GetProfile(pars) ret = profile.call(verbose = False) attrs_before_evt = profile.create_Attributes("before event") ### POST EVENTs # keep event and corresp attribute name in a tuple event_name, attribute_name = event attribute_name = attribute_name.lower() attr_value_before = attrs_before_evt.get_attribute_value(attribute_name) #g.log.info("before post event: {}={}".format(attribute_name, attr_value_before)) apiActionLog("POST EVENT event: [{}] (on db should correspond to [{}])".format(event_name, attribute_name)) po = IA.PostEvent(pars,event_name) # add a couple of attrributes # attrs = IA.Attributes(('ds_segment',"string",event)) # attrs.add_tuple(('ds_cmp_1', 'numeric',0.0)) # po.sett_attrs(attrs) ok = po.call(verbose = False, dumpAttributes = False) #time.sleep(1) ### GET PROFILE # apiActionLog("GET PROFILE") ret = profile.call(verbose) attrs_after_evt = profile.create_Attributes("after event") attr_value_after = attrs_after_evt.get_attribute_value(attribute_name) # g.log.info("after post event {}: {} = {}".format(event_name,attribute_name, attr_value_after)) # check on DB row = jdb.dbGetProfileCol(cookie, attribute_name) if row is not None: #g.log.info("query on DB: {} = {}".format(attribute_name,row[attribute_name])) pass else: g.log.error(cookie+"not found in db") #diffs = attrs_after_evt.full_compare(attrs_before_evt) #attrs_after_evt.compare_values(attrs_before_evt, diffs) # attrs_before_evt.left_print_side_2_side(attrs_after_evt) #g.log.info("after post event {} = ".format(attrs_after_evt.get_attribute_value(event(2)))) # apiActionLog("END SESSION --- to restart and read values on DB") ok = IA.EndSession(pars) ss.set_attributes(None, overwrite=True) # apiActionLog("START SESSION --- restart to read attributes from DB") ok = ss.call(verbose) # apiActionLog("GET PROFILE --- check if we got from DB attributes from post event") ret = profile.call(verbose) attrs_after_evt = profile.create_Attributes("after event") attr_value_after = attrs_after_evt.get_attribute_value(attribute_name) g.log.info("after post event (endSession, startSession, getProfile): {} = {}".format(attribute_name, attr_value_after)) # check on DB row = jdb.dbGetProfileCol(cookie, attribute_name) if row is not None: g.log.info("query on DB: {} = {}".format(attribute_name,row[attribute_name])) else: g.log.error(cookie+"not found in db") # pageActionLog("PageExit") ### END SESSION # apiActionLog("END SESSION") ok = IA.EndSession(pars) if not ok: g.log.error("failed endSession, cookie: {} sessionId: {}".format(cookie,pars.get_session_id())) g.log.info("SESSION ENDED for cookie <{}>\n\n".format(cookie)) g.log.info("cookie '{}' Test ended\n".format(cookie)) except Exception as exc: g.log.error("Generic Error") g.log.error("Generic Error") finally: IA.EndSession(pars).call() return True
def session_web01_test(nr_test_cookies, nr_repetitions = 1, verbose = False, dumpCalls = False, checkOnDB = True): ''' Trying to mirror the specs :return: ''' try: ok = True pars = g.uaci_params.deepcopy() new_vistor_attrs = g.new_vistor_profile_attrs.deepcopy() cookies = genAudienceIDs(nr_test_cookies, "testP") for iterCount, cookie in enumerate(cookies): # cookie_known = None pars.set_audience_ID(cookie) ut.logBanner("TEST for AudienceID: <{}>".format(cookie)) for rep in range(1,1+nr_repetitions): g.log.info("cookie <{}> repetition {}".format(cookie,rep)) for event_nr, event in enumerate(g.events): pageActionLog("PageLoad") ### START SESSION apiActionLog("START SESSION") ss = IA.StartSession(pars, None, "false") ok2 = ss.call(verbose) if not ok2: cookie_known = False g.log.info("\n<{}> is new visitor, setting profile with second startSession".format(cookie)) ss.set_attributes(new_vistor_attrs,True) apiActionLog("START SESSION (SECOND, to set new visitor profile, this also will give error but it's ok)") ok2 = ss.call(verbose, dumpAttributes=False) else: g.log.info("### <"+cookie + "> is NOT new ###") cookie_known = True ### GET PROFILE apiActionLog("GET PROFILE ### Only useful/needed if cookie is known/exists ###") profile = IA.GetProfile(pars) ok = ok and profile.call(verbose) attrs_before_evt = profile.create_Attributes("before event") ### POST EVENTs # keep event and corresp attribute name in a tuple event_name, attribute_name = event attribute_name = attribute_name.lower() attr_value_before = attrs_before_evt.get_attribute_value(attribute_name) po = IA.PostEvent(pars,event_name) if (event_name == "ds_registeryourinterest"): email = "test_{}@example.com".format(cookie) attrs = IA.Attributes("Post Event Attributes") attrs.add_tuple(('Command_Payload',"string",email)) po.sett_attrs(attrs) #g.log.info("before post event: {}={}".format(attribute_name, attr_value_before)) apiActionLog("POST EVENT event: [{}] (on db should correspond to [{}])".format(event_name, attribute_name)) ok = ok and po.call(verbose, dumpAttributes = dumpCalls) ### GET PROFILE - INTERNAL, DISABLE # apiActionLog("GET PROFILE, ### INTERNAL ###, just to demonstrate Interact does not realize DB update") # ret = profile.call(verbose) # attrs_after_evt = profile.create_Attributes("after event") # attr_value_after = attrs_after_evt.get_attribute_value(attribute_name) # g.log.info("after post event [{}]: <{}> = {}".format(event_name,attribute_name, attr_value_after)) # # check on DB # row = jdb.dbGetProfileCol(cookie,attribute_name) # if row is not None: # g.log.info("<{}> value on DB = {}".format(attribute_name,row[attribute_name])) # else: # g.log.error(cookie+" not found in db") apiActionLog("to make interact aware of attribute update on DB we have to perform now:"+ "\nendSession + startSession + getProfile" "\nwith current interact version endSession and getProfile unneeded, just startSession is enough+" "\nbut let's do things properly with all the 3, may send them singularly or together in one batch") batchThem = ((iterCount+rep+event_nr) % 2) == 1 if batchThem: g.log.info("This time will send them in BATCH") endSession = IA.EndSession(pars) ss.set_attributes(None, overwrite=True) batchCommands = IA.BatchCmds(pars) batchCommands.append(endSession).append(ss).append(profile) ok = ok and batchCommands.call(verbose,True,dumpCalls) g.log.info(batchCommands.get_rsp_json()) else: g.log.info("This time will send them one by one") apiActionLog("END SESSION --- to restart and read values on DB") es = IA.EndSession(pars) ok = ok and es.call(verbose) apiActionLog("START SESSION --- restart to read attributes from DB") ss.set_attributes(None, overwrite=True) ok = ok and ss.call(verbose) apiActionLog("GET PROFILE --- check if interact session now got from DB attributes set from previous post event") ok = ok and profile.call(verbose) attrs_after_evt = profile.create_Attributes("after event") attr_value_after_batch = attrs_after_evt.get_attribute_value(attribute_name) g.log.info("\nafter post event and Batch: {} = {}".format(attribute_name, attr_value_after_batch)) if checkOnDB: row = jdb.dbGetProfileCol(cookie, attribute_name) if row is not None: # compare attribute values if (attribute_name in row): g.log.info("query on DB: {} = {}".format(attribute_name,str(row[attribute_name]))) dbAttrNum = row[attribute_name] #int(row[attribute_name]) profAttrNum = attr_value_after_batch if (dbAttrNum != profAttrNum): g.log.info("test failed, attr values not equal: profile ={} DB ={}" .format(profAttrNum,dbAttrNum)) return False else: pass else: g.log.error("test failed, did not find in table attr: {}" .format(attribute_name)) else: g.log.error("failed query: cannot find on db attribute: {}".format(attribute_name)) pageActionLog("PageExit") g.log.info(""); g.log.info("") apiActionLog("HERE, BEFORE ENDSESSION WE WOULD NEED TO COMMANDED, BY WEB CODE, VIA AN EVENT, TO SAVE PROFILE TO DB") ### END SESSION apiActionLog("END SESSION") es = IA.EndSession(pars) if es.call(verbose): # workaround failure of endSession if we sent batch previously if batchThem: g.log.error("endSession did NOT fail after a batch, usually it fails") else: if batchThem: g.log.warn("endSession failed, always does if we previous sent batch") pass # for unknown reasons fails if we sent batch previously # so consider this ok and continue to test else: # no Batch, so real failure ok = False if not ok: g.log.error("failed endSession, cookie: {} sessionId: {}".format(cookie,pars.get_session_id())) g.log.info("SESSION ENDED for cookie <{}>\n\n".format(cookie)) g.log.info(" ----- cookie '{}' TEST ENDED ------\n".format(cookie)) if checkOnDB: g.log.info(jdb.dbGetAudIDRow(cookie, 0, verbose = False)) g.log.info("SQL for query: \n" + jdb.generateSQLForAudID(cookie)) # except Exception as exc: # g.log.error("Generic Error") finally: # IA.EndSession(pars).call() pass return ok