How to get / post method in rspec rails6 - rspec-rails

m new to rails
I am using RSpec gem for testing
This is my controller
class QuestionsController < ApplicationController
before_action :require_user, except: %i[show index]
before_action :authorize_user, only: %i[edit update destroy]
def index
#questions = Question.all
end
def show
#answers = Answer.where('question_id = :question_id ', { question_id: params[:id] })
#count = Answer.where('question_id = :question_id ', { question_id: params[:id] }).count
$count_global = #count
session[:question_id] = params[:id]
#questions = Question.all
end
def new
#question = Question.new
end
def edit; end
def create
#question = Question.new(question_params)
#question.user_id *strong text*= current_user.id
#question.save
redirect_to root_path
end
end
This is spec/requests/question_spec.rb
describe "GET /new" do
it "renders a successful response" do
get new_question_url
expect(response).to be_successful
end
end
The test is not passing
The error is
Failure/Error: expect(response).to be_successful
expected `#<ActionDispatch::TestResponse:0x0000558594f082e0 #mon_data=#<Monitor:0x0000558594f08290>, #mon_data_..., #method=nil, #request_method=nil, #remote_ip=nil, #original_fullpath=nil, #fullpath=nil, #ip=nil>>.successful?` to be truthy, got false
Could anyone help regarding this issue

Related

Why is the variable modified when nginx Lua processes the request?

I just started studying Lua.
Every time I request, I want to check the name parameter in the request parameter. However, it is actually found that the self.name has changed occasionally.
For example,
request A with params: request_id = 123 & name = ABC,
request B with params: request_id = 321 & name = EFG,
in the log, I found that there are requests_id = 123, but name = EFG.
Why is that? Is my class incorrectly written?
Here is the sample codeļ¼š
main.lua:
local checker = require "checker"
local ch = checker:new()
if ch:check_name() then
ngx.header["Content-Type"] = "application/json"
ngx.status = ngx.HTTP_FORBIDDEN
ngx.exit(ngx.HTTP_FORBIDDEN)
end
checker.lua:
local utils = require "utils"
local _M = {}
function _M:new()
local o = {}
setmetatable(o, self)
self.__index = self
self.args = utils.get_req_args() or {}
local name = self.args["name"] or ""
local request_id = self.args["request_id"] or ""
self.name = name
return o
end
function _M:check_name()
ngx.log(ngx.ERR, "request_id: ", self.request_id, " name: ", self.name)
-- do some check ...
end
utils.lua
local json = require "cjson"
local _M = {}
function _M.new(self)
return self
end
function _M.get_req_args()
-- GET
local args = nil
if ngx.var.request_method == "GET" then
args = ngx.req.get_uri_args()
-- POST
elseif ngx.var.request_method == "POST" then
ngx.req.read_body()
local data = ngx.req.get_body_data()
args = json.decode(data)
end
return args
end

Is it possible to randomly sample YouTube comments with YouTube API V3?

I have been trying to download all the YouTube comments on popular videos using python requests, but it has been throwing up the following error after about a quarter of the total comments:
{'error': {'code': 400, 'message': "The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the request's input is invalid. Check the structure of the commentThread resource in the request body to ensure that it is valid.", 'errors': [{'message': "The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the request's input is invalid. Check the structure of the commentThread resource in the request body to ensure that it is valid.", 'domain': 'youtube.commentThread', 'reason': 'processingFailure', 'location': 'body', 'locationType': 'other'}]}}
I found this thread detailing the same issue, and it seems that it is not possible to download all the comments on popular videos.
This is my code:
import argparse
import urllib
import requests
import json
import time
start_time = time.time()
class YouTubeApi():
YOUTUBE_COMMENTS_URL = 'https://www.googleapis.com/youtube/v3/commentThreads'
comment_counter = 0
with open("API_keys.txt", "r") as f:
key_list = f.readlines()
key_list = [key.strip('/n') for key in key_list]
def format_comments(self, results, likes_required):
comments_list = []
try:
for item in results["items"]:
comment = item["snippet"]["topLevelComment"]
likes = comment["snippet"]["likeCount"]
if likes < likes_required:
continue
author = comment["snippet"]["authorDisplayName"]
text = comment["snippet"]["textDisplay"]
str = "Comment by {}:\n \"{}\"\n\n".format(author, text)
str = str.encode('ascii', 'replace').decode()
comments_list.append(str)
self.comment_counter += 1
print("Comments downloaded:", self.comment_counter, end="\r")
except(KeyError):
print(results)
return comments_list
def get_video_comments(self, video_id, likes_required):
with open("API_keys.txt", "r") as f:
key_list = f.readlines()
key_list = [key.strip('/n') for key in key_list]
if self.comment_counter <= 900000:
key = self.key_list[0]
elif self.comment_counter <= 1800000:
key = self.key_list[1]
elif self.comment_counter <= 2700000:
key = self.key_list[2]
elif self.comment_counter <= 3600000:
key = self.key_list[3]
elif self.comment_counter <= 4500000:
key = self.key_list[4]
params = {
'part': 'snippet,replies',
'maxResults': 100,
'videoId': video_id,
'textFormat': 'plainText',
'key': key
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
try:
#data = self.openURL(self.YOUTUBE_COMMENTS_URL, params)
comments_data = requests.get(self.YOUTUBE_COMMENTS_URL, params=params, headers=headers)
except ChunkedEncodingError:
tries = 5
print("Chunked Error. Retrying...")
for n in range(tries):
try:
x = 0
x += 1
print("Trying", x, "times")
response = session.post("https://www.youtube.com/comment_service_ajax", params=params, data=data, headers=headers)
comments_data = json.loads(response.text)
except ChunkedEncodingError as c:
print(c)
results = comments_data.json()
nextPageToken = results.get("nextPageToken")
commments_list = []
commments_list += self.format_comments(results, likes_required)
while nextPageToken:
params.update({'pageToken': nextPageToken})
try:
comments_data = requests.get(self.YOUTUBE_COMMENTS_URL, params=params, headers=headers)
except ChunkedEncodingError as c:
tries = 5
print("Chunked Error. Retrying...")
for n in range(tries):
try:
x = 0
x += 1
print("Trying", x, "times")
response = session.post("https://www.youtube.com/comment_service_ajax", params=params, data=data, headers=headers)
comments_data = json.loads(response.text)
except ChunkedEncodingError as c:
print(c)
results = comments_data.json()
nextPageToken = results.get("nextPageToken")
commments_list += self.format_comments(results, likes_required)
return commments_list
def get_video_id_list(self, filename):
try:
with open(filename, 'r') as file:
URL_list = file.readlines()
except FileNotFoundError:
exit("File \"" + filename + "\" not found")
list = []
for url in URL_list:
if url == "\n": # ignore empty lines
continue
if url[-1] == '\n': # delete '\n' at the end of line
url = url[:-1]
if url.find('='): # get id
id = url[url.find('=') + 1:]
list.append(id)
else:
print("Wrong URL")
return list
def main():
yt = YouTubeApi()
parser = argparse.ArgumentParser(add_help=False, description=("Download youtube comments from many videos into txt file"))
required = parser.add_argument_group("required arguments")
optional = parser.add_argument_group("optional arguments")
here: https://console.developers.google.com/apis/credentials")
optional.add_argument("--likes", '-l', help="The amount of likes a comment needs to be saved", type=int)
optional.add_argument("--input", '-i', help="URL list file name")
optional.add_argument("--output", '-o', help="Output file name")
optional.add_argument("--help", '-h', help="Help", action='help')
args = parser.parse_args()
# --------------------------------------------------------------------- #
likes = 0
if args.likes:
likes = args.likes
input_file = "URL_list.txt"
if args.input:
input_file = args.input
output_file = "Comments.txt"
if args.output:
output_file = args.output
list = yt.get_video_id_list(input_file)
if not list:
exit("No URLs in input file")
try:
vid_counter = 0
with open(output_file, "a") as f:
for video_id in list:
vid_counter += 1
print("Downloading comments for video ", vid_counter, ", id: ", video_id, sep='')
comments = yt.get_video_comments(video_id, likes)
if comments:
for comment in comments:
f.write(comment)
print('\nDone!')
except KeyboardInterrupt:
exit("User Aborted the Operation")
# --------------------------------------------------------------------- #
if __name__ == '__main__':
main()
The next best method would be to randomly sample them. Does anyone know if this is possible with the API V3?
Even if the API returns a processingFailure error, you could still catch that (or any other API error for that matter) for to terminate gracefully your pagination loop. This way your script will provide the top-level comments that it fetched from of the API prior to the occurrence of the first API error.
The error response provided by the YouTube Data API is (usually) of the following form:
{
"error": {
"errors": [
{
"domain": <string>,
"reason": <string>,
"message": <string>,
"locationType": <string>,
"location": <string>
}
],
"code": <integer>,
"message": <string>
}
}
Hence, you could have defined the following function:
def is_error_response(response):
error = response.get('error')
if error is None:
return False
print("API Error: "
f"code={error['code']} "
f"domain={error['errors'][0]['domain']} "
f"reason={error['errors'][0]['reason']} "
f"message={error['errors'][0]['message']!r}")
return True
that you'll invoke after each statement of form results = comments_data.json(). In case of the first occurrence of that statement, you'll have:
results = comments_data.json()
if is_error_response(results):
return []
nextPageToken = results.get("nextPageToken")
For the second instance of that statement:
results = comments_data.json()
if is_error_response(results):
return comments_list
nextPageToken = results.get("nextPageToken")
Notice that the function is_error_response above prints out an error message on stdout in case its argument in an API error response; this is for the purpose of having the user of your script informed about the API call failure.

FLASK-ADDING DATETIME CALCULATION INTO DATABASE

I m trying to perform a little calculation and Logic on date.time with Flask application.
1.) the application will calculate the difference between issue date and expiry date called "remaining days" , The application will check if remaining days is less than 365 days and trigger a function
I attempted the first logic to manipulate the data and submit to database
`#bp.route('/cerpacs/add', methods=['GET', 'POST'])
#login_required
def add_cerpac():
"""
Add a an Cerpac/ expartriates to the database
"""
check_admin()
add_cerpac = True
form =CerpacForm()
if form.validate_on_submit():
cerpac = Cerpac(cerpac_serial_no=form.cerpac_serial_no.data,
cerpac_issue_date= form.cerpac_issue_date.data,
cerpac_exp_date=form.cerpac_exp_date.data,
employee =form.employee.data, )
form.cerpac_issue_date.data = cerpac.cerpac_issue_date
form.cerpac_exp_date.data = cerpac.cerpac_exp_date
if request.method == 'POST':
todays_date = datetime.now()
t = cerpac.cerpac_issue_date
t1 = cerpac.cerpac_exp_date
remaining_days = t1 - t
print(remaining_days) - good prints my result!
remaining_days = cerpac.remaining_days ----not adding to database
try:
add cerpac to the database
db.session.add(cerpac)
db.session.commit()
flash('You have successfully added a Cerpac.' )`
`
my model:
class Cerpac(db.Model):
__tablename__ = 'cerpacs'
id = db.Column(db.Integer, primary_key=True)
cerpac_issue_date = db.Column(db.DateTime)
cerpac_exp_date=db.Column(db.DateTime)
remaining_days = db.Column(db.DateTime)
cerpac_serial_no = db.Column(db.String(60))
cerpac_upload = db.Column(db.String(20), default='cerpac.jpg')
renew_status = db.Column(db.Boolean, default=False)
process_status = db.Column(db.Boolean, default=False)
renewcerpac_id = db.Column(db.Integer, db.ForeignKey('renewcerpacs.id'))
employee_id = db.Column(db.Integer, db.ForeignKey('employees.id'))
def __repr__(self):
return '<Cerpac {}>'.format(self.name) model:
I want to add this to database and eventually write a function like this:
I had a mistake also in the code because I had error issue_date not defined. How do I define issue_date as a date.time variable?
def remaining_days(issue_date, expired_date):
issue_date = datetime(issue_date)
days_to_go = expired - issue
if days_to_go == 365:
renew_status== True
print("time to renew")
print("We are Ok")
I would simplify this to something like:
from datetime import datetime
class Cerpac(db.Model):
...
cerpac_exp_date=db.Column(db.DateTime)
...
#property
def remaining_days(self):
return (self.cerpac_exp_date - self.cerpac_issue_date).days
#property
def days_to_expiry(self):
return (self.cerpac_exp_date - datetime.now()).days
Then, days_to_expiry and remaining_days become properties calculated when you query, and update automatically when they renew their cards.

Serialization fails with custom Torch class

Serialization can fails with a class object created containing __pairs:
test = torch.class('test')
function test:__init()
self.data = {}
end
function test:__pairs(...)
return pairs(self.data, ...)
end
function test:get_data()
print(self.data)
end
a = test.new()
a.data = {"asdasd"}
b = torch.serialize(a)
c = torch.deserialize(b)
print(torch.typename(c))
print(c:get_data())
The following returns:
test
nil
The engine behind the torch.serialization is located in the File-class. The File:writeObject us the key function. For the above example the action for a Torch class starts at line 201 with the:
elseif typeidx == TYPE_TORCH then
The type is identified in the File:isWritableObject.
One could probably implement the metatable function write but in the above example the problem was non-torch metatable function __pairs that should be __pairs__ (see torch.getmetatable):
test = torch.class('test')
function test:__init()
self.data = {}
end
function test:__pairs__(...)
return pairs(self.data, ...)
end
function test:get_data()
print(self.data)
end
a = test.new()
a.data = {"asdasd"}
b = torch.serialize(a)
c = torch.deserialize(b)
print(torch.typename(c))
print(c:get_data())
This gives the expected:
test
{
1 : "asdasd"
}

Lua recursive scrolling menu not working?

Hey guys, just wondering why ain't my menu working, I've been coding it for like 8 hours now and just can't figure out what's wrong.
Menu = {
label = "Mahin Menu",
current = current or true,
open = open or true,
subMenus = {}
}
function Menu.newSubMenu()
return {
setup = Menu.setup,
print = Menu.print,
toggleOpen = Menu.toggleOpen,
getCurrentMenu = Menu.getCurrentMenu,
getLastMenu = Menu.getLastMenu,
getNextMenu = Menu.getNextMenu,
getPrevMenu = Menu.getPrevMenu
}
end
function Menu:setup(m_parent, m_label, m_action)
self.parent = m_parent
self.label = m_label
self.action = m_action
self.subMenus = {}
self.current = false
self.open = false
table.insert(m_parent.subMenus, self)
end
function Menu:print(indent)
io.write(string.rep(" ", indent))
if #self.subMenus>0 then
if self.open == true then
io.write("[-]")
else
io.write("[+]")
end
else
io.write(" ")
end
if self.current == true then
io.write("<" .. self.label .. ">")
else
io.write(" " .. self.label)
end
io.write("\n")
if #self.subMenus>0 and self.open == true then
for i=1,#self.subMenus do
self.subMenus[i]:print(indent+1)
end
end
end
function Menu:toggleOpen()
if self.open == true then
self.open = false
else
self.open = true
end
end
function Menu:getCurrentMenu()
if self.current == true then
return self
else
for k=1,#self.subMenus do
local v = self.subMenus[k]:getCurrentMenu()
if v ~= nil then
return v
end
end
end
end
function Menu:getLastMenu()
if self.open == true and #self.subMenus > 0 then
return self.subMenus[#self.subMenus]:getLastMenu()
else
return self
end
end
function Menu:getNextMenu(bool)
bool = bool or false
if bool == false then
if #self.subMenus > 0 and self.open == true then
return self.subMenus[1]
end
end
if self.parent then
if self.parent.subMenus[#self.parent.subMenus] == self then
self.parent:getNextMenu(true)
else
for i=1,#self.parent.subMenus do
if self.parent.subMenus[i] == self then
print(self.parent.subMenus[i+1].label)
return self.parent.subMenus[i+1]
end
end
end
else
return self
end
end
function Menu:getPrevMenu()
if self.parent then
for k=1,#self.parent.subMenus do
if self.parent.subMenus[k] == self then
if k == 1 then
return self.parent
elseif #self.parent.subMenus[k-1].subMenus > 0 and self.parent.subMenus[k-1].open == true then
local x = self.parent.subMenus[k-1]
while #x.subMenus > 0 and x.open == true do
x = x.subMenus[#x.subMenus]
end
return x
else
return self.parent.subMenus[k-1]
end
end
end
else
return self
end
end
Test = Menu.newSubMenu()
Test:setup(Menu, "Test item")
Mahi = Menu.newSubMenu()
Mahi:setup(Menu, "Mahi item")
Mahi.open = true
Testx = Menu.newSubMenu()
Testx:setup(Mahi, "Lalall")
Testx.open= true
Sadmad = Menu.newSubMenu()
Sadmad:setup(Testx, "Woot")
Asd = Menu.newSubMenu()
Asd:setup(Menu, "Asd menu")
Asd.current = true
Menu.current = false
repeat
print(string.rep("\n",2))
Menu:print(0)
x=io.read()
if x == "z" then
x = Menu:getCurrentMenu()
print(Menu:getCurrentMenu().label)
print(Menu:getCurrentMenu():getNextMenu().label)
y = Menu:getCurrentMenu():getNextMenu()
x.current = false
y.current = true
elseif x == "a" then
x = Menu:getCurrentMenu()
y = Menu:getCurrentMenu():getPrevMenu()
x.current = false
y.current = true
end
until x == "sad"
"
there's the code, and when ever i try to move my current from "Asd menu" downwards, it'll error:
menu.lua:150: attempt to index a nil value
which doesn't make any sense, it's clearly declared, and I've tried adding prints and they always give me Asd menu O.o
Same goes for if I'll try to move from Woot to Asd menu, same exact error, and I have no idea why, since I added those prints
print(Menu:getCurrentMenu().label)
print(Menu:getCurrentMenu():getNextMenu().label)
and they do give me Asd menu, but then it says that trying to index a nil value at the second print line, but it sill does print? I'm out of ideas, any help out here?
You are missing a return statement in line 92.
Note that this line does not actually return anything, so the function is returning nil.
After changing it to return self.parent:getNextMenu(true) it seems to be working.

Resources