Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
556 lines
22 KiB
Python
556 lines
22 KiB
Python
"""Spec conformance cases for the where language (all determined by SPEC.md alone)."""
|
|
|
|
import contextlib
|
|
import io
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from minidb import Column, Database, JsonFileStorage
|
|
from minidb.cli import main
|
|
from minidb.errors import MiniDBError
|
|
from minidb.query import QueryError, QueryNameError, QuerySyntaxError, QueryTypeError, compile_where
|
|
|
|
COLUMNS = ["id", "name", "age", "score", "active", "city", "note"]
|
|
|
|
# name, age, score, active, city, note (ids 1..12 in this order)
|
|
PEOPLE = [
|
|
("Alice", 30, 88.5, True, "Berlin", "likes tea"),
|
|
("Bob", 25, 72.0, False, "Paris", None),
|
|
("Carol", None, 91.25, True, "Berlin", "50% done"),
|
|
("Dave", 41, None, None, "London", "a_b"),
|
|
("Eve", 35, 65.5, True, None, ""),
|
|
("Frank", 19, 55.0, False, "Paris", "O'Brien"),
|
|
("Grace", 30, 70.0, True, "Rome", "x"),
|
|
("heidi", 52, 99.9, False, "Berlin", "Tea time"),
|
|
("Ivan", 25, 72.0, None, "Rome", None),
|
|
("Judy", 60, 40.0, True, "London", "100%"),
|
|
(None, 22, 50.0, False, "Paris", "anon"),
|
|
("Mallory", 0, 0.0, True, "", "zero"),
|
|
]
|
|
ALL = list(range(1, 13))
|
|
|
|
|
|
def make_people():
|
|
table = Database().create_table("people", [
|
|
Column("name"), Column("age", "int"), Column("score", "float"),
|
|
Column("active", "bool"), Column("city"), Column("note"),
|
|
])
|
|
for name, age, score, active, city, note in PEOPLE:
|
|
table.insert(name=name, age=age, score=score, active=active, city=city, note=note)
|
|
return table
|
|
|
|
|
|
SELECT_CASES = [
|
|
# comparisons
|
|
("age = 30", [1, 7]),
|
|
("age != 30", [2, 4, 5, 6, 8, 9, 10, 11, 12]),
|
|
("age <> 25", [1, 4, 5, 6, 7, 8, 10, 11, 12]),
|
|
("age < 25", [6, 11, 12]),
|
|
("age <= 25", [2, 6, 9, 11, 12]),
|
|
("age > 40", [4, 8, 10]),
|
|
("age >= 52", [8, 10]),
|
|
("score > 70", [1, 2, 3, 8, 9]),
|
|
("score >= 70", [1, 2, 3, 7, 8, 9]),
|
|
("age = 30.0", [1, 7]),
|
|
("score = 72", [2, 9]),
|
|
("score = 88.50", [1]),
|
|
("score = 0", [12]),
|
|
("age = 0", [12]),
|
|
("age = -0", [12]),
|
|
("age = 007", []),
|
|
("age = 99999999999999999999", []),
|
|
("name = 'Alice'", [1]),
|
|
("name = 'alice'", []),
|
|
("name < 'C'", [1, 2]),
|
|
("name >= 'h'", [8]),
|
|
("note = 'O''Brien'", [6]),
|
|
("note = ''", [5]),
|
|
("city = ''", [12]),
|
|
("'Paris' = city", [2, 6, 11]),
|
|
("active = TRUE", [1, 3, 5, 7, 10, 12]),
|
|
("active = FALSE", [2, 6, 8, 11]),
|
|
("active", [1, 3, 5, 7, 10, 12]),
|
|
("NOT active", [2, 6, 8, 11]),
|
|
("active != TRUE", [2, 6, 8, 11]),
|
|
("active > FALSE", [1, 3, 5, 7, 10, 12]),
|
|
# NULL handling
|
|
("age = NULL", []),
|
|
("age != NULL", []),
|
|
("NOT (age = NULL)", []),
|
|
("age IS NULL", [3]),
|
|
("age IS NOT NULL", [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
("note IS NULL", [2, 9]),
|
|
("city IS NULL", [5]),
|
|
("active IS NULL", [4, 9]),
|
|
("name IS NULL OR age IS NULL", [3, 11]),
|
|
("score IS NULL AND active IS NULL", [4]),
|
|
("NULL IS NULL", ALL),
|
|
("5 IS NOT NULL", ALL),
|
|
("NULL", []),
|
|
("NOT NULL", []),
|
|
("TRUE", ALL),
|
|
("FALSE", []),
|
|
("TRUE OR NULL", ALL),
|
|
("FALSE OR NULL", []),
|
|
# logic and precedence
|
|
("age > 30 OR active = TRUE", [1, 3, 4, 5, 7, 8, 10, 12]),
|
|
("age > 30 AND active = TRUE", [5, 10]),
|
|
("NOT (age > 30)", [1, 2, 6, 7, 9, 11, 12]),
|
|
("NOT age > 30", [1, 2, 6, 7, 9, 11, 12]),
|
|
("NOT age > 30 AND active", [1, 7, 12]),
|
|
("age = 25 OR age = 30 AND active", [1, 2, 7, 9]),
|
|
("(age = 25 OR age = 30) AND active", [1, 7]),
|
|
("NOT (age = 25 OR age = 30)", [4, 5, 6, 8, 10, 11, 12]),
|
|
("NOT NOT active", [1, 3, 5, 7, 10, 12]),
|
|
("NOT active OR age > 50", [2, 6, 8, 10, 11]),
|
|
("active AND NOT (age < 30)", [1, 5, 7, 10]),
|
|
("NOT (age > 100 OR city = 'Paris')", [1, 4, 7, 8, 9, 10, 12]),
|
|
("NOT (age > 0 AND city = 'Berlin')", [2, 4, 6, 7, 9, 10, 11, 12]),
|
|
("city = 'Berlin' OR city = 'Rome' OR city = 'Paris'", [1, 2, 3, 6, 7, 8, 9, 11]),
|
|
("age > 20 AND age < 40 AND active", [1, 5, 7]),
|
|
("((((age = 30))))", [1, 7]),
|
|
("(active) AND (city = 'Berlin')", [1, 3]),
|
|
# IN
|
|
("age IN (25, 30)", [1, 2, 7, 9]),
|
|
("age IN (25, 30, NULL)", [1, 2, 7, 9]),
|
|
("age NOT IN (25, 30)", [4, 5, 6, 8, 10, 11, 12]),
|
|
("age NOT IN (25, 30, NULL)", []),
|
|
("city IN ('Rome', 'London')", [4, 7, 9, 10]),
|
|
("city NOT IN ('Rome', 'London')", [1, 2, 3, 6, 8, 11, 12]),
|
|
("score IN (72, 55.0)", [2, 6, 9]),
|
|
("name IN ('Alice')", [1]),
|
|
("active IN (TRUE)", [1, 3, 5, 7, 10, 12]),
|
|
("age in (1, 2, 3, 30)", [1, 7]),
|
|
("age IN (30, 30)", [1, 7]),
|
|
("age IN(30,25)", [1, 2, 7, 9]),
|
|
("NULL IN (1)", []),
|
|
("NULL NOT IN (1)", []),
|
|
("3 IN (1, 2, 3)", ALL),
|
|
("id IN (2, 4, 6) AND NOT active", [2, 6]),
|
|
# BETWEEN
|
|
("age BETWEEN 25 AND 35", [1, 2, 5, 7, 9]),
|
|
("age NOT BETWEEN 25 AND 35", [4, 6, 8, 10, 11, 12]),
|
|
("age BETWEEN 35 AND 25", []),
|
|
("age BETWEEN 30 AND 30", [1, 7]),
|
|
("age BETWEEN -5 AND 20", [6, 12]),
|
|
("age BETWEEN 0 AND 100", [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
("score BETWEEN 50 AND 72", [2, 5, 6, 7, 9, 11]),
|
|
("name BETWEEN 'B' AND 'D'", [2, 3]),
|
|
("age BETWEEN 20 AND 40 AND active", [1, 5, 7]),
|
|
("active AND age BETWEEN 20 AND 40", [1, 5, 7]),
|
|
("age BETWEEN 25 AND 35 OR city = 'London'", [1, 2, 4, 5, 7, 9, 10]),
|
|
("age BETWEEN NULL AND 30", []),
|
|
("age BETWEEN 1 AND NULL", []),
|
|
("age NOT BETWEEN 1 AND NULL", [12]),
|
|
# LIKE and ILIKE
|
|
("name LIKE 'A%'", [1]),
|
|
("name LIKE '%e'", [1, 4, 5, 7]),
|
|
("name LIKE '_ob'", [2]),
|
|
("name LIKE '____'", [4, 9, 10]),
|
|
("name LIKE 'alice'", []),
|
|
("name ILIKE 'alice'", [1]),
|
|
("name ILIKE 'H%'", [8]),
|
|
("name LIKE 'h%'", [8]),
|
|
("name LIKE 'H%'", []),
|
|
("name LIKE '%'", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]),
|
|
("name LIKE '%a%'", [3, 4, 6, 7, 9, 12]),
|
|
("name ILIKE '%A%'", [1, 3, 4, 6, 7, 9, 12]),
|
|
("note LIKE '50%'", [3]),
|
|
("note LIKE '%0%'", [3, 10]),
|
|
("note LIKE 'a_b'", [4]),
|
|
("note LIKE 'a__'", [4]),
|
|
("note LIKE ''", [5]),
|
|
("note LIKE '%'", [1, 3, 4, 5, 6, 7, 8, 10, 11, 12]),
|
|
("note NOT LIKE '%tea%'", [3, 4, 5, 6, 7, 8, 10, 11, 12]),
|
|
("note ILIKE '%tea%'", [1, 8]),
|
|
("note NOT ILIKE '%tea%'", [3, 4, 5, 6, 7, 10, 11, 12]),
|
|
("note LIKE '%''%'", [6]),
|
|
("note LIKE note", [1, 3, 4, 5, 6, 7, 8, 10, 11, 12]),
|
|
("name LIKE NULL", []),
|
|
("name LIKE '.%'", []),
|
|
("name LIKE '[A-C]%'", []),
|
|
("name LIKE 'A.ice'", []),
|
|
("note LIKE '%.%'", []),
|
|
# layout and tokens
|
|
(" age =\t30\n", [1, 7]),
|
|
("age>=30", [1, 4, 5, 7, 8, 10]),
|
|
("age<=-1", []),
|
|
("age<>30", [2, 4, 5, 6, 8, 9, 10, 11, 12]),
|
|
("(age=30)AND(active)", [1, 7]),
|
|
("NOT(age=30)", [2, 4, 5, 6, 8, 9, 10, 11, 12]),
|
|
("name='Alice'", [1]),
|
|
("age Is Not NuLL", [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
("note IS NULL", [2, 9]),
|
|
# operands not reached are not type checked
|
|
("id < 0 AND name = 5", []),
|
|
("id > 0 OR name = 5", ALL),
|
|
("FALSE AND name = 5", []),
|
|
("TRUE OR age = 'x'", ALL),
|
|
("note IS NULL AND note = 5", []),
|
|
("age = NULL OR note = NULL", []),
|
|
]
|
|
|
|
SYNTAX_ERROR_CASES = [
|
|
("", 0),
|
|
(" ", 3),
|
|
("age >", 5),
|
|
("age > 5 AND", 11),
|
|
("age = 1 OR", 10),
|
|
("(", 1),
|
|
("()", 1),
|
|
("NOT", 3),
|
|
("(age = 1", 8),
|
|
("age = = 1", 6),
|
|
("age == 1", 5),
|
|
("age = 1)", 7),
|
|
("age = 1 name = 2", 8),
|
|
("age = 1 2", 8),
|
|
("age IN ()", 8),
|
|
("age IN (1,)", 10),
|
|
("age IN (1 2)", 10),
|
|
("age IN (1, 'a', )", 16),
|
|
("age IN (foo)", 8),
|
|
("age IN 1", 7),
|
|
("age BETWEEN 1 2", 14),
|
|
("age BETWEEN 1", 13),
|
|
("age BETWEEN AND 5", 12),
|
|
("age NOT 5", 8),
|
|
("age NOT NOT IN (1)", 8),
|
|
("age NOT", 7),
|
|
("age IS 5", 7),
|
|
("age IS NOT 5", 11),
|
|
("age IS NULL NULL", 12),
|
|
("age LIKE", 8),
|
|
("= 5", 0),
|
|
("AND age = 1", 0),
|
|
("age = 1 AND AND age = 2", 12),
|
|
("age (= 1)", 4),
|
|
("age < > 1", 6),
|
|
("age =< 1", 5),
|
|
("age = 5 -3", 8),
|
|
("(age = 1) 5", 10),
|
|
("(age) = 1", 6),
|
|
("age = (1)", 6),
|
|
("age\t=\n=", 6),
|
|
# lexical errors
|
|
("age = 'abc", 6),
|
|
("age = 'it''", 6),
|
|
("age = 1 # comment", 8),
|
|
("age ! 1", 4),
|
|
("age = - 5", 6),
|
|
("age = 5 - 3", 8),
|
|
("age = 1.", 7),
|
|
("age = .5", 6),
|
|
("age = 1.5.2", 9),
|
|
("\u00e9 = 1", 0),
|
|
("name = 'caf\u00e9' $", 14),
|
|
("age = = 1 'oops", 10),
|
|
]
|
|
|
|
NAME_ERROR_CASES = [
|
|
("agee > 5", "agee", 0),
|
|
("age > 5 AND foo = 1", "foo", 12),
|
|
("Age = 1", "Age", 0),
|
|
("age = 1 AND (bar IS NULL OR baz = 1)", "bar", 13),
|
|
("5 = zzz", "zzz", 4),
|
|
("NOT missing", "missing", 4),
|
|
("name LIKE other", "other", 10),
|
|
("age BETWEEN lo AND hi", "lo", 12),
|
|
("age = 1 OR id = 2 OR nope IS NULL", "nope", 21),
|
|
]
|
|
|
|
TYPE_ERROR_QUERIES = [
|
|
"name = 5", "age = 'x'", "age = '30'", "active = 1", "age > TRUE", "score = 'x'", "NOT name = 5",
|
|
"age", "name", "5", "'abc'", "name LIKE 5", "age LIKE '3%'", "note ILIKE 3",
|
|
"age IN (1, 'a')", "active IN (1)", "age BETWEEN 'a' AND 5", "name BETWEEN 1 AND 2",
|
|
"age > 100 AND name = 5", "city = 5 AND FALSE",
|
|
]
|
|
|
|
|
|
class SelectSpec(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.people = make_people()
|
|
|
|
def test_select_cases(self):
|
|
for text, expected in SELECT_CASES:
|
|
with self.subTest(query=text):
|
|
self.assertEqual([r["id"] for r in self.people.select(where=text)], expected)
|
|
|
|
def test_compile_where_agrees_with_select(self):
|
|
rows = self.people.select()
|
|
for text, expected in SELECT_CASES[:60]:
|
|
with self.subTest(query=text):
|
|
predicate = compile_where(text, COLUMNS)
|
|
self.assertEqual([r["id"] for r in rows if predicate(r)], expected)
|
|
|
|
def test_predicate_returns_real_booleans(self):
|
|
predicate = compile_where("age = NULL", COLUMNS)
|
|
self.assertIs(predicate({"age": 1}), False)
|
|
self.assertIs(predicate({"age": None}), False)
|
|
self.assertIs(compile_where("age > 0", COLUMNS)({"age": 1}), True)
|
|
|
|
|
|
class ErrorSpec(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.people = make_people()
|
|
|
|
def test_syntax_error_positions(self):
|
|
for text, position in SYNTAX_ERROR_CASES:
|
|
with self.subTest(query=text):
|
|
with self.assertRaises(QuerySyntaxError) as caught:
|
|
compile_where(text, COLUMNS)
|
|
self.assertEqual(caught.exception.position, position)
|
|
self.assertTrue(str(caught.exception).endswith("at position %d" % position), str(caught.exception))
|
|
|
|
def test_syntax_errors_come_through_the_table(self):
|
|
for text, position in SYNTAX_ERROR_CASES[:12]:
|
|
with self.subTest(query=text):
|
|
with self.assertRaises(QuerySyntaxError) as caught:
|
|
self.people.select(where=text)
|
|
self.assertEqual(caught.exception.position, position)
|
|
|
|
def test_name_errors(self):
|
|
for text, name, position in NAME_ERROR_CASES:
|
|
with self.subTest(query=text):
|
|
with self.assertRaises(QueryNameError) as caught:
|
|
compile_where(text, COLUMNS)
|
|
self.assertEqual((caught.exception.name, caught.exception.position), (name, position))
|
|
|
|
def test_name_error_does_not_need_rows(self):
|
|
empty = Database().create_table("t", [Column("age", "int")])
|
|
with self.assertRaises(QueryNameError):
|
|
empty.select(where="height > 1")
|
|
with self.assertRaises(QueryNameError):
|
|
empty.count(where="height > 1")
|
|
|
|
def test_syntax_error_beats_name_error(self):
|
|
for text, position in (("foo = ", 6), ("foo bar", 4), ("(foo = 1", 8), ("foo = 1 'x", 8)):
|
|
with self.subTest(query=text):
|
|
with self.assertRaises(QuerySyntaxError) as caught:
|
|
compile_where(text, COLUMNS)
|
|
self.assertEqual(caught.exception.position, position)
|
|
|
|
def test_type_errors(self):
|
|
for text in TYPE_ERROR_QUERIES:
|
|
with self.subTest(query=text):
|
|
with self.assertRaises(QueryTypeError):
|
|
self.people.select(where=text)
|
|
|
|
def test_type_errors_are_raised_when_evaluating_not_compiling(self):
|
|
for text in TYPE_ERROR_QUERIES:
|
|
with self.subTest(query=text):
|
|
predicate = compile_where(text, COLUMNS)
|
|
self.assertTrue(callable(predicate))
|
|
|
|
def test_type_error_needs_a_row_that_reaches_it(self):
|
|
predicate = compile_where("x = 5", ["x"])
|
|
self.assertIs(predicate({"x": None}), False)
|
|
self.assertIs(predicate({"x": 5}), True)
|
|
with self.assertRaises(QueryTypeError):
|
|
predicate({"x": "a"})
|
|
only_nulls = Database().create_table("t", [Column("age", "int")])
|
|
only_nulls.insert_many([{"age": None}, {"age": None}])
|
|
self.assertEqual(only_nulls.select(where="age = 'x'"), [])
|
|
empty = Database().create_table("t", [Column("age", "int")])
|
|
self.assertEqual(empty.select(where="age = 'x'"), [])
|
|
|
|
def test_short_circuit_in_in_and_between(self):
|
|
table = Database().create_table("t", [Column("age", "int")])
|
|
table.insert(age=30)
|
|
self.assertEqual([r["id"] for r in table.select(where="age IN (30, 'a')")], [1])
|
|
self.assertEqual([r["id"] for r in table.select(where="age IN (30, NULL, 'a')")], [1])
|
|
self.assertEqual(table.select(where="age NOT IN (30, 'a')"), [])
|
|
self.assertEqual(table.select(where="age BETWEEN 40 AND 'z'"), [])
|
|
with self.assertRaises(QueryTypeError):
|
|
table.select(where="age IN (1, 'a')")
|
|
with self.assertRaises(QueryTypeError):
|
|
table.select(where="age BETWEEN 10 AND 'z'")
|
|
with self.assertRaises(QueryTypeError):
|
|
table.select(where="age BETWEEN 'a' AND 5")
|
|
|
|
def test_unknown_does_not_short_circuit(self):
|
|
table = Database().create_table("t", [Column("age", "int"), Column("name")])
|
|
table.insert(age=None, name="x")
|
|
with self.assertRaises(QueryTypeError):
|
|
table.select(where="age > 100 AND name = 5")
|
|
with self.assertRaises(QueryTypeError):
|
|
table.select(where="age > 100 OR name = 5")
|
|
|
|
def test_class_hierarchy(self):
|
|
for cls in (QuerySyntaxError, QueryNameError, QueryTypeError):
|
|
self.assertTrue(issubclass(cls, QueryError))
|
|
self.assertTrue(issubclass(QueryError, MiniDBError))
|
|
|
|
|
|
class TableApiSpec(unittest.TestCase):
|
|
def setUp(self):
|
|
self.people = make_people()
|
|
|
|
def ids(self, **kwargs):
|
|
return [r["id"] for r in self.people.select(**kwargs)]
|
|
|
|
def test_callable_and_none_are_unchanged(self):
|
|
self.assertEqual(self.ids(), ALL)
|
|
self.assertEqual(self.ids(where=None), ALL)
|
|
self.assertEqual(self.ids(where=lambda row: row["age"] == 25), [2, 9])
|
|
|
|
def test_options_still_work_with_strings(self):
|
|
self.assertEqual(self.ids(where="age > 30", order_by="age", descending=True, limit=2), [10, 8])
|
|
self.assertEqual(self.ids(where="age > 30", order_by="age"), [5, 4, 8, 10])
|
|
rows = self.people.select(where="age > 30", columns=["name"], order_by="id")
|
|
self.assertEqual([r["name"] for r in rows], ["Dave", "Eve", "heidi", "Judy"])
|
|
self.assertEqual(self.ids(where="city = 'Paris'", order_by="score"), [11, 6, 2])
|
|
|
|
def test_count(self):
|
|
self.assertEqual(self.people.count(where="active"), 6)
|
|
self.assertEqual(self.people.count(where="age > 30 AND active"), 2)
|
|
self.assertEqual(self.people.count(where="age = NULL"), 0)
|
|
self.assertEqual(self.people.count(), 12)
|
|
|
|
def test_update(self):
|
|
self.assertEqual(self.people.update({"note": "seen"}, where="city = 'Paris'"), 3)
|
|
self.assertEqual(self.ids(where="note = 'seen'"), [2, 6, 11])
|
|
self.assertEqual(self.people.update({"age": 99}, where="id <= 2"), 2)
|
|
self.assertEqual(self.ids(where="age = 99"), [1, 2])
|
|
self.assertEqual(self.people.update({"age": 1}, where="FALSE"), 0)
|
|
|
|
def test_delete(self):
|
|
self.assertEqual(self.people.delete(where="age IS NULL"), 1)
|
|
self.assertEqual(len(self.people), 11)
|
|
self.assertEqual(self.people.delete(where="active IS NULL OR age < 20"), 4)
|
|
self.assertEqual(self.ids(), [1, 2, 5, 7, 8, 10, 11])
|
|
|
|
def test_failed_queries_change_nothing(self):
|
|
before = self.people.select()
|
|
with self.assertRaises(QueryTypeError):
|
|
self.people.update({"age": 0}, where="age > 100 AND name = 5")
|
|
with self.assertRaises(QueryTypeError):
|
|
self.people.delete(where="name = 5")
|
|
with self.assertRaises(QuerySyntaxError):
|
|
self.people.update({"age": 0}, where="age >")
|
|
with self.assertRaises(QueryNameError):
|
|
self.people.delete(where="height > 1")
|
|
self.assertEqual(self.people.select(), before)
|
|
|
|
def test_index_stays_consistent(self):
|
|
self.people.create_index("city")
|
|
self.assertEqual(self.people.update({"city": "Rome"}, where="city = 'Paris'"), 3)
|
|
self.assertEqual([r["id"] for r in self.people.find("city", "Rome")], [2, 6, 7, 9, 11])
|
|
self.assertEqual(self.people.find("city", "Paris"), [])
|
|
self.people.delete(where="city = 'Rome' AND age > 20")
|
|
self.assertEqual([r["id"] for r in self.people.find("city", "Rome")], [6])
|
|
|
|
def test_blank_expression_is_a_syntax_error(self):
|
|
for call in (
|
|
lambda: self.people.select(where=""),
|
|
lambda: self.people.count(where=" "),
|
|
lambda: self.people.update({"age": 1}, where=""),
|
|
lambda: self.people.delete(where="\n"),
|
|
):
|
|
with self.assertRaises(QuerySyntaxError):
|
|
call()
|
|
|
|
def test_other_types_still_raise_type_error(self):
|
|
for bad in (5, ["age > 1"], 1.5, {"age": 1}):
|
|
with self.assertRaises(TypeError):
|
|
self.people.select(where=bad)
|
|
with self.assertRaises(TypeError):
|
|
self.people.count(where=bad)
|
|
|
|
|
|
class CliSpec(unittest.TestCase):
|
|
def setUp(self):
|
|
self._folder = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self._folder.cleanup)
|
|
self.path = os.path.join(self._folder.name, "people.json")
|
|
db = Database(JsonFileStorage(self.path))
|
|
table = db.create_table("people", [
|
|
Column("name"), Column("age", "int"), Column("score", "float"),
|
|
Column("active", "bool"), Column("city"), Column("note"),
|
|
])
|
|
for name, age, score, active, city, note in PEOPLE:
|
|
table.insert(name=name, age=age, score=score, active=active, city=city, note=note)
|
|
db.save()
|
|
|
|
def run_cli(self, *args):
|
|
out, err = io.StringIO(), io.StringIO()
|
|
with contextlib.redirect_stderr(err):
|
|
try:
|
|
code = main(["--db", self.path, *args], out=out)
|
|
except SystemExit as exc:
|
|
code = exc.code if isinstance(exc.code, int) else 1
|
|
return code, out.getvalue(), err.getvalue()
|
|
|
|
def stored_ids(self):
|
|
table = Database(JsonFileStorage(self.path)).table("people")
|
|
return [r["id"] for r in table.select()]
|
|
|
|
def test_select_where(self):
|
|
code, out, _ = self.run_cli("select", "people", "--where", "age > 40", "--columns", "id,name")
|
|
self.assertEqual((code, out), (0, "id,name\n4,Dave\n8,heidi\n10,Judy\n"))
|
|
|
|
def test_select_where_with_order_and_limit(self):
|
|
code, out, _ = self.run_cli(
|
|
"select", "people", "--where", "city IN ('Paris', 'Rome') AND NOT active",
|
|
"--columns", "id", "--order-by", "score", "--desc", "--limit", "2",
|
|
)
|
|
self.assertEqual((code, out), (0, "id\n2\n6\n"))
|
|
|
|
def test_select_without_where_is_unchanged(self):
|
|
_, out, _ = self.run_cli("select", "people", "--columns", "id")
|
|
self.assertEqual(out.split(), ["id"] + [str(i) for i in ALL])
|
|
|
|
def test_update_where(self):
|
|
code, out, _ = self.run_cli("update", "people", "--where", "city = 'Paris'", "note=seen")
|
|
self.assertEqual((code, out), (0, "updated 3\n"))
|
|
table = Database(JsonFileStorage(self.path)).table("people")
|
|
self.assertEqual([r["id"] for r in table.select(where=lambda r: r["note"] == "seen")], [2, 6, 11])
|
|
|
|
def test_delete_where(self):
|
|
code, out, _ = self.run_cli("delete", "people", "--where", "active IS NULL")
|
|
self.assertEqual((code, out), (0, "deleted 2\n"))
|
|
self.assertEqual(self.stored_ids(), [1, 2, 3, 5, 6, 7, 8, 10, 11, 12])
|
|
|
|
def test_id_options_still_work(self):
|
|
self.assertEqual(self.run_cli("delete", "people", "--id", "12")[:2], (0, "deleted 1\n"))
|
|
self.assertEqual(self.run_cli("update", "people", "--id", "1", "age=31")[:2], (0, "updated 1\n"))
|
|
|
|
def test_query_errors_exit_with_two(self):
|
|
cases = [
|
|
("age =", "position 5"),
|
|
("", "position 0"),
|
|
("height > 1", None),
|
|
("name = 5", None),
|
|
]
|
|
for text, fragment in cases:
|
|
with self.subTest(where=text):
|
|
code, out, err = self.run_cli("select", "people", "--where", text)
|
|
self.assertEqual((code, out), (2, ""))
|
|
self.assertTrue(err.startswith("error: "), err)
|
|
if fragment:
|
|
self.assertIn(fragment, err)
|
|
|
|
def test_failed_update_and_delete_change_nothing(self):
|
|
self.assertEqual(self.run_cli("update", "people", "--where", "age >", "age=1")[0], 2)
|
|
self.assertEqual(self.run_cli("delete", "people", "--where", "name = 5")[0], 2)
|
|
self.assertEqual(self.stored_ids(), ALL)
|
|
|
|
def test_id_and_where_are_mutually_exclusive_and_one_is_required(self):
|
|
for args in (
|
|
("delete", "people", "--id", "1", "--where", "age > 1"),
|
|
("update", "people", "--id", "1", "--where", "age > 1", "age=2"),
|
|
("delete", "people"),
|
|
("update", "people", "age=2"),
|
|
):
|
|
with self.subTest(args=args):
|
|
code, _, _ = self.run_cli(*args)
|
|
self.assertNotEqual(code, 0)
|
|
self.assertEqual(self.stored_ids(), ALL)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|