Skip to content

Commit e7fff80

Browse files
authored
test: Added tests for library (#45)
* test: Added tests for library * fix: CR
1 parent 03a36a9 commit e7fff80

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Albert Einstein was a renowned physicist who made significant contributions to the field of theoretical physics. Born on March 14, 1879, in Ulm, in the Kingdom of Württemberg in the German Empire, Einstein's early life showed signs of his later intellectual prowess.
2+
3+
Einstein attended the Swiss Federal Institute of Technology in Zurich, where he studied physics and mathematics. Despite facing challenges and financial difficulties, he persevered in his studies and graduated in 1900. After graduation, he struggled to secure a teaching position but eventually found work as a patent examiner at the Swiss Patent Office.
4+
5+
In 1905, often referred to as his "miracle year," Einstein published four groundbreaking papers that transformed the scientific landscape. These papers covered the photoelectric effect, Brownian motion, special relativity, and the famous equation E=mc², demonstrating the equivalence of mass and energy.
6+
7+
His theory of special relativity, published in the paper "On the Electrodynamics of Moving Bodies," challenged traditional notions of space and time. It introduced the concept of spacetime and showed that time is relative, depending on the observer's motion.
8+
9+
In 1915, Einstein presented the general theory of relativity, providing a new understanding of gravitation. According to general relativity, massive objects like planets and stars cause a curvature in spacetime, influencing the motion of other objects. This theory successfully explained phenomena like the bending of light around massive objects.
10+
11+
Einstein's work laid the foundation for modern cosmology and astrophysics. His predictions, such as the bending of light by gravity, were later confirmed through experiments and observations.
12+
13+
Apart from his scientific endeavors, Einstein was an advocate for civil rights, pacifism, and Zionism. He spoke out against discrimination and injustice, using his platform to promote social and political causes. In 1933, Einstein fled Nazi Germany and settled in the United States, where he continued his scientific research.
14+
15+
Einstein received the Nobel Prize in Physics in 1921 for his explanation of the photoelectric effect. Despite his immense contributions to science, he remained humble and often expressed a deep curiosity about the mysteries of the universe.
16+
17+
In the latter part of his life, Einstein worked towards a unified field theory, attempting to combine electromagnetism and gravity into a single framework. However, this goal remained elusive, and Einstein's efforts in this direction were not as successful as his earlier work.
18+
19+
Albert Einstein passed away on April 18, 1955, leaving behind a legacy that continues to shape our understanding of the physical world. His intellectual brilliance, coupled with his commitment to social justice, has made him an enduring symbol of scientific achievement and moral responsibility. The impact of Einstein's ideas extends far beyond the realm of physics, influencing fields as diverse as philosophy, literature, and popular culture.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import time
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
from ai21 import AI21Client
7+
8+
LIBRARY_FILE_TO_UPLOAD = str(Path(__file__).parent.parent / "resources" / "library_file.txt")
9+
DEFAULT_LABELS = ["einstein", "science"]
10+
11+
12+
def _wait_for_file_to_process(client: AI21Client, file_id: str, timeout: float = 20):
13+
start_time = time.time()
14+
15+
elapsed_time = time.time() - start_time
16+
while elapsed_time < timeout:
17+
file_response = client.library.files.get(file_id)
18+
19+
if file_response.status == "PROCESSED":
20+
return
21+
22+
elapsed_time = time.time() - start_time
23+
time.sleep(0.5)
24+
25+
raise TimeoutError(f"Timeout: {timeout} seconds passed. File processing not completed")
26+
27+
28+
def _delete_file(client: AI21Client, file_id: str):
29+
_wait_for_file_to_process(client, file_id)
30+
client.library.files.delete(file_id)
31+
32+
33+
@pytest.fixture(scope="module", autouse=True)
34+
def file_in_library():
35+
"""
36+
Uploads a file to the library and deletes it after the test is done
37+
This happens in a scope of a module so the file is uploaded only once
38+
:return: file_id: str
39+
"""
40+
client = AI21Client()
41+
42+
file_id = client.library.files.create(file_path=LIBRARY_FILE_TO_UPLOAD, labels=DEFAULT_LABELS)
43+
_wait_for_file_to_process(client, file_id)
44+
yield file_id
45+
_delete_file(client, file_id=file_id)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from pathlib import Path
2+
from time import sleep
3+
4+
from ai21 import AI21Client
5+
from tests.integration_tests.clients.studio.conftest import LIBRARY_FILE_TO_UPLOAD, DEFAULT_LABELS
6+
7+
8+
def test_library__when_upload__should_get_file_id(file_in_library: str):
9+
assert file_in_library is not None
10+
11+
12+
def test_library__when_list__should_get_file_id_in_list_of_files(file_in_library: str):
13+
client = AI21Client()
14+
15+
files = client.library.files.list()
16+
assert files[0].file_id == file_in_library
17+
assert files[0].name == Path(LIBRARY_FILE_TO_UPLOAD).name
18+
19+
20+
def test_library__when_get__should_match_file_id(file_in_library: str):
21+
client = AI21Client()
22+
23+
file_response = client.library.files.get(file_in_library)
24+
assert file_response.file_id == file_in_library
25+
26+
27+
def test_library__when_update__should_update_labels_successfully(file_in_library: str):
28+
client = AI21Client()
29+
30+
file_response = client.library.files.get(file_in_library)
31+
assert set(file_response.labels) == set(DEFAULT_LABELS)
32+
sleep(2)
33+
34+
new_labels = DEFAULT_LABELS + ["new_label"]
35+
client.library.files.update(file_in_library, labels=new_labels)
36+
file_response = client.library.files.get(file_in_library)
37+
assert set(file_response.labels) == set(new_labels)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from ai21 import AI21Client
2+
3+
4+
def test_library_answer__when_answer_not_in_context__should_return_false(file_in_library: str):
5+
client = AI21Client()
6+
response = client.library.answer.create(question="Who is Tony Stark?")
7+
assert response.answer is None
8+
assert not response.answer_in_context
9+
10+
11+
def test_library_answer__when_answer_in_context__should_return_true(file_in_library: str):
12+
client = AI21Client()
13+
response = client.library.answer.create(question="Who was Albert Einstein?")
14+
assert response.answer is not None
15+
assert response.answer_in_context
16+
assert response.sources[0].file_id == file_in_library
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ai21 import AI21Client
2+
3+
4+
def test_library_search__when_search__should_return_relevant_results(file_in_library: str):
5+
client = AI21Client()
6+
response = client.library.search.create(
7+
query="What did Albert Einstein get a Nobel Prize for?", labels=["einstein"]
8+
)
9+
assert len(response.results) > 0
10+
for result in response.results:
11+
assert result.file_id == file_in_library

0 commit comments

Comments
 (0)