Ejemplo n.º 1
0
def insert_into_table(user_id):
    try:
        # Prepared Statement
        sql = "INSERT INTO BSqnOU0gA6.users_dateTime (user_id, user_name, creation_date) VALUES (%s, %s, %s)"

        # Connect to database and get cursor
        conn, cursor = connect()

        # Check if id exists and if it does it will get the next available id (1 above the last line in table)
        select(table="BSqnOU0gA6.users_dateTime",
               where=["user_id", user_id],
               conn=conn,
               cursor=cursor)
        cursorLength = cursor.rowcount

        if cursorLength > 0:
            select(table="BSqnOU0gA6.users_dateTime", conn=conn, cursor=cursor)
            last_id = (list(cursor)[-1])[0]
            user_id = last_id + 1

        data = request.json  # Get data from json payload
        date = datetime.now(
        )  # Get current date and time for creation date field in users table

        post("BSqnOU0gA6.users_dateTime", [
            user_id,
            str(data.get("user_name")),
            date.strftime("%Y-%m-%d %H:%M:%S")
        ],
             conn=conn,
             cursor=cursor)

        # Disconnect from Database
        disconnect(conn, cursor)

        # If user generation succeeded
        return {"status": "ok", "user_added": data.get("user_name")}, 200

    except Exception as err:
        return {"status": "error", "reason": "id already exist"}, 500
Ejemplo n.º 2
0
def select_from_table(user_id):
    try:
        # Connect to database and get cursor
        conn, cursor = connect()

        select(table="BSqnOU0gA6.users_dateTime",
               where=["user_id", user_id],
               conn=conn,
               cursor=cursor)  # set the cursor

        name = None
        for row in cursor:
            name = row[1]
            disconnect(conn, cursor)  # Disconnect from Database

        if name is not None:
            return {"status": "ok", "user_name": name}, 200
        else:
            return {"status": "error", "reason": "no such id"}, 500

    except Exception as err:
        print(err)
Ejemplo n.º 3
0
def get_user_name(user_id):

    # user db_connection library to select data from db
    conn, cursor = connect()
    user_name = select(
        table='BSqnOU0gA6.users_dateTime',
        select_value="*",
        where=["user_id", user_id],
        conn=conn,
        cursor=cursor
    )  # SELECT user_name FROM BSqnOU0gA6.users_dateTime WHERE user_id = %s
    if user_name != 0:
        for row in cursor:
            disconnect(conn, cursor)
            return "<h1 id='user'>" + row[1] + "</h1>"
    else:
        disconnect(conn, cursor)
        return "<h1 id='error'>" + 'no such user: ' + user_id + "</h1>"
Ejemplo n.º 4
0
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import requests

from Module.db_connector import connect, disconnect, select

# Backend check part
try:
    # Connect to database and get cursor
    conn, cursor = connect()

    # Get all data from config table

    select('BSqnOU0gA6.config', conn=conn, cursor=cursor)
    last_line = list(cursor)[cursor.arraysize]

    user_id = input("Insert requested id for entry creation: ")
    http_link = str(last_line[1])
    browser = str(last_line[2])
    user_name = str(last_line[3])

    # request a post action to store data inside the database
    requests.post("%s/%s" % (http_link, user_id),
                  json={"user_name": user_name})

    # Check if user created successfully
    data = requests.get("%s/%s" % (http_link, user_id))
    if data.status_code == 200 and data.json()["user_name"] == user_name:
        print(
    requests.post("http://127.0.0.1:5000/users/{}".format(user_id),
                  json={"user_name": user_name})

    # Check if user created successfully
    data = requests.get("http://127.0.0.1:5000/users/{}".format(user_id))
    if data.status_code == 200 and data.json()["user_name"] in user_name:
        print(
            "\nStatus code is \'%i\' and user name is \'%s\' as requested by user.\n"
            % (data.status_code, user_name))

        # Connect to database and get cursor
        conn, cursor = connect()

        # Create table and select query
        select('BSqnOU0gA6.users_dateTime',
               where=["user_id", user_id],
               conn=conn,
               cursor=cursor)
        for row in cursor:
            print(
                "User\'s ID and Name are \'%s\' and \'%s\' and the values the user asked for are ID \'%s\' and Name "
                "\'%s\'." % (row[0], row[1], user_id, user_name))

        disconnect(conn, cursor)

    else:
        if data.status_code != 200:
            print(
                "Status code is \'%i\', the user was not created as requested."
                % data.status_code)
            raise Exception("Test Failed")