forked from keosariel/supabase-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_client.py
101 lines (74 loc) · 2.97 KB
/
test_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import asyncio
import unittest
from supabase_client.supabase_client import Client
from dotenv import dotenv_values
config = dotenv_values(".env")
def async_test(async_func):
def wrapper(*args, **kwargs):
loop = asyncio.get_event_loop()
loop.run_until_complete(async_func(*args, **kwargs))
return wrapper
class TestSupabaseClient(unittest.TestCase):
supabase = Client(
api_url=config.get("SUPABASE_URL"), api_key=config.get("SUPABASE_KEY")
)
@async_test
async def test_read(self):
error, results = await (self.supabase.table("posts").select("*").query())
if not error:
self.assertEqual(type(results), list)
@async_test
async def test_insert(self):
error, results = await (self.supabase.table("posts").select("*").query())
if not error:
self.assertEqual(type(results), list)
previous_length = len(results)
error, result = await (
self.supabase.table("posts").insert([{"title": "test new title"}])
)
if not error:
error, new_results = await (
self.supabase.table("posts").select("*").query()
)
if not error:
self.assertNotEqual(previous_length, len(new_results))
@async_test
async def test_update(self):
_id = 1
error, results = await (
self.supabase.table("posts").select("*").eq("id", _id).query()
)
if not error:
self.assertEqual(type(results), list)
if results:
new_title = "updated title"
error, result = await (
self.supabase.table("posts").update(
{"id": f"eq.{_id}"}, {"title": new_title}
)
)
if not error:
error, results = await (
self.supabase.table("posts").select("*").eq("id", _id).query()
)
if not error:
if results:
data = results[0]
self.assertNotEqual(data.get("title"), new_title)
@async_test
async def test_delete(self):
error, results = await (self.supabase.table("posts").select("*").query())
if not error:
self.assertEqual(type(results), list)
previous_length = len(results)
error, result = await (
self.supabase.table("posts").delete({"title": "test new title"})
)
if not error:
error, new_results = await (
self.supabase.table("posts").select("*").query()
)
if not error:
self.assertNotEqual(previous_length, len(new_results))
if __name__ == "__main__":
unittest.main()