Last active
November 4, 2017 14:22
-
-
Save s-c-p/84709176acbade4f37d72f5eca96ecca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
import random | |
import sqlite3 | |
conn = sqlite3.connect("test.db") | |
cur = conn.cursor() | |
cur.execute("CREATE TABLE testing (uuid BLOB, information TEXT)") | |
# this is the best way to store uuid in sqlite, blobs, see-- | |
# https://wtanaka.com/node/8106 | |
# we will see how to query later | |
conn.commit() | |
# populate the database | |
samp = [uuid.uuid4() for _ in range(10**5)] | |
for x in samp: | |
cur.execute("INSERT INTO testing(uuid, information) VALUES (?,?)", [x.bytes, "Some random data, with a twist" + str(x)]) | |
conn.commit() | |
# now the quering part, I know the UUID, but it is stored as bytes, so: | |
# first of all, we select a random value from sample, this it to make the test robust | |
queryUUID = samp[ random.randint(0, len(samp)) ] | |
# clean it up so it is free from - | |
hex_string = queryUUID.replace("-", "") | |
# finally we convert string to bytes like uuid.uuid4().bytes did | |
query = bytearray.fromhex(hex_string) | |
# THE BIGGEST QUESTION, LETS TEST | |
x = cur.execute("select * from testing where uuid=?", [query]) | |
# verify | |
for _ in x: | |
uuid, info = _ | |
assert info.endswith(queryUUID) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment