Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.11] Make Google Drive ftest use shared fixture (#1719) #1794

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def _texts(self):
self.fake_provider.extra_large_text(),
]

@cached_property
def fake(self):
return self.fake_provider.fake

@cached_property
def _htmls(self):
return [
Expand Down
60 changes: 28 additions & 32 deletions tests/sources/fixtures/google_drive/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,38 @@
#
"""Module to handle api calls received from connector."""

import io
import os
import random
import string
import time

from flask import Flask, request

DOCS_COUNT = {"small": 750, "medium": 1500, "large": 3000}
from tests.commons import WeightedFakeProvider

DATA_SIZE = os.environ.get("DATA_SIZE")
fake_provider = WeightedFakeProvider()

DATA_SIZE = os.environ.get("DATA_SIZE", "medium").lower()

def generate_random_string(length):
"""Function that generates random string with fixed lenght.
match DATA_SIZE:
case "small":
DOCS_COUNT = 250
case "medium":
DOCS_COUNT = 1000
case "large":
DOCS_COUNT = 5000

Args:
length (int): Length of generated string

Returns:
str: Random string
"""
return "".join([random.choice(string.ascii_letters) for _ in range(length)])
app = Flask(__name__)

PRE_REQUEST_SLEEP = float(os.environ.get("PRE_REQUEST_SLEEP", "0.1"))

def generate_document_data():
"""Function to generate random data content.

Returns:
io.BytesIO: Dummy attachment content
"""
# 1KB text file
file_content = generate_random_string(1000)
return io.BytesIO(bytes(file_content, encoding="utf-8"))
def get_num_docs():
print(DOCS_COUNT)


app = Flask(__name__)
@app.before_request
def before_request():
time.sleep(PRE_REQUEST_SLEEP)


@app.route("/drive/v3/about", methods=["GET"])
Expand All @@ -66,14 +62,14 @@ def files_list():
{
"kind": "drive#file",
"mimeType": "text/plain",
"id": generate_random_string(length=16),
"id": fake_provider.fake.sha1(),
"name": f"file_name_{id_}",
"fileExtension": "txt",
"size": 12345,
"modifiedTime": 1687860674,
"parents": [],
}
for id_ in range(DOCS_COUNT.get(DATA_SIZE, "small"))
for id_ in range(DOCS_COUNT)
]
return {"nextPageToken": "dummyToken", "files": files_list}

Expand All @@ -84,15 +80,15 @@ def files_get(file_id):

# response includes the file contents in the response body
if req_params.get("alt", None) == "media":
return generate_document_data()
return fake_provider.get_html()

# response includes file metadata
else:
return {
"kind": "drive#file",
"id": "file_0",
"name": "file_name_0",
"mimeType": "text/plain",
}
return {
"kind": "drive#file",
"id": "file_0",
"name": "file_name_0",
"mimeType": "text/html",
}


@app.route("/token", methods=["POST"])
Expand Down