Python检查用户是否不在数据库中

时间:2016-02-16 12:00:48

标签: python database

所以下面我有3个def测试(test1,test2和test3),我专注于def test3,在那里,我希望能够检查数据库中的用户不是,有人知道我会怎么做吗?

代码:

   '''
security service tests
'''
import sys
sys.path.append('..')
import requests
import json
from common.constants import *
from config.settings import environment


security_environment = environment()['security_service']
service_url = "%s://%s:%d" % (
    security_environment['protocol'], 
    security_environment['host'], 
    security_environment['port'])


def test1():
  print "TEST 1 - get user details"
  headers     = {'Accept': 'application/json'}
  url         = "%s/user/1" % service_url
  response    = requests.get(url, headers=headers)
  status_code = response.status_code
  print "STATUS: %s" % status_code
  print "DATA:   %s" % response.json()
  return status_code == HTTP_OK


def test2():
  print "TEST 2 - Count amount of Users"
  headers     = {'Accept': 'application/json'}
  url         = "%s/users" % service_url
  response    = requests.get(url, headers=headers)
  status_code = response.status_code
  data = response.json()
  print (len(data))

  if status_code != HTTP_OK:
    return False
  return len(data) == 5


def test3():
  print "TEST 3 - Check if user is NOT in the Database"
  headers     = {'Accept': 'application/json'}
  url         = "%s/users" % service_url
  response    = requests.get(url, headers=headers)
  status_code = response.status_code
  users = response.json()

  if users is None:
    status_code != HTTP_OK

  else:
    status_code == HTTP_OK



if __name__ == "__main__":
  num_pass = 0
  num_fail = 0

  for test in [test1, test2, test3]:
    print "-----------------------------------------------------------"

    if test():
      num_pass += 1
      print "PASS"
    else:
      num_fail += 1
      print "FAIL"

  print "==========================================================="
  print "%d passed, %d failed" % (num_pass, num_fail)

1 个答案:

答案 0 :(得分:0)

向我们提供您的回复样本。

如果你的json数据看起来像这样:

driver.find_element_by_xpath("//a[contains(text(),'Add')]")

尝试这样的情绪:

objUser = [{'login': 'monkey1', 'name': 'Adam'},
           {'login': 'monkey2', 'name': 'Maria'}]