|
| 1 | +import sys |
| 2 | +import unittest |
| 3 | + |
| 4 | +from crate import client |
| 5 | +from crate.client.exceptions import ProgrammingError |
| 6 | +from .layer import setUpCrateLayerBaseline, tearDownDropEntitiesBaseline |
| 7 | +from .settings import crate_host |
| 8 | + |
| 9 | + |
| 10 | +class BulkOperationTest(unittest.TestCase): |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + setUpCrateLayerBaseline(self) |
| 14 | + |
| 15 | + def tearDown(self): |
| 16 | + tearDownDropEntitiesBaseline(self) |
| 17 | + |
| 18 | + @unittest.skipIf(sys.version_info < (3, 8), "BulkResponse needs Python 3.8 or higher") |
| 19 | + def test_executemany_with_bulk_response_partial(self): |
| 20 | + |
| 21 | + # Import at runtime is on purpose, to permit skipping the test case. |
| 22 | + from crate.client.result import BulkResponse |
| 23 | + |
| 24 | + connection = client.connect(crate_host) |
| 25 | + cursor = connection.cursor() |
| 26 | + |
| 27 | + # Run SQL DDL. |
| 28 | + cursor.execute("CREATE TABLE foobar (id INTEGER PRIMARY KEY, name STRING);") |
| 29 | + |
| 30 | + # Run a batch insert that only partially succeeds. |
| 31 | + invalid_records = [(1, "Hotzenplotz 1"), (1, "Hotzenplotz 2")] |
| 32 | + result = cursor.executemany("INSERT INTO foobar (id, name) VALUES (?, ?)", invalid_records) |
| 33 | + |
| 34 | + # Verify CrateDB response. |
| 35 | + self.assertEqual(result, [{"rowcount": 1}, {"rowcount": -2}]) |
| 36 | + |
| 37 | + # Verify decoded response. |
| 38 | + bulk_response = BulkResponse(invalid_records, result) |
| 39 | + self.assertEqual(bulk_response.failed_records, [(1, "Hotzenplotz 2")]) |
| 40 | + self.assertEqual(bulk_response.record_count, 2) |
| 41 | + self.assertEqual(bulk_response.success_count, 1) |
| 42 | + self.assertEqual(bulk_response.failed_count, 1) |
| 43 | + |
| 44 | + cursor.execute("REFRESH TABLE foobar;") |
| 45 | + cursor.execute("SELECT * FROM foobar;") |
| 46 | + result = cursor.fetchall() |
| 47 | + self.assertEqual(result, [[1, "Hotzenplotz 1"]]) |
| 48 | + |
| 49 | + cursor.close() |
| 50 | + connection.close() |
| 51 | + |
| 52 | + @unittest.skipIf(sys.version_info < (3, 8), "BulkResponse needs Python 3.8 or higher") |
| 53 | + def test_executemany_empty(self): |
| 54 | + |
| 55 | + connection = client.connect(crate_host) |
| 56 | + cursor = connection.cursor() |
| 57 | + |
| 58 | + # Run SQL DDL. |
| 59 | + cursor.execute("CREATE TABLE foobar (id INTEGER PRIMARY KEY, name STRING);") |
| 60 | + |
| 61 | + # Run a batch insert that is empty. |
| 62 | + with self.assertRaises(ProgrammingError) as cm: |
| 63 | + cursor.executemany("INSERT INTO foobar (id, name) VALUES (?, ?)", []) |
| 64 | + self.assertEqual( |
| 65 | + str(cm.exception), |
| 66 | + "SQLParseException[The query contains a parameter placeholder $1, " |
| 67 | + "but there are only 0 parameter values]") |
| 68 | + |
| 69 | + cursor.close() |
| 70 | + connection.close() |
| 71 | + |
| 72 | + @unittest.skipIf(sys.version_info < (3, 8), "BulkResponse needs Python 3.8 or higher") |
| 73 | + def test_bulk_response_empty_records_or_results(self): |
| 74 | + |
| 75 | + # Import at runtime is on purpose, to permit skipping the test case. |
| 76 | + from crate.client.result import BulkResponse |
| 77 | + |
| 78 | + with self.assertRaises(ValueError) as cm: |
| 79 | + BulkResponse(records=None, results=None) |
| 80 | + self.assertEqual( |
| 81 | + str(cm.exception), |
| 82 | + "Processing a bulk response without records is an invalid operation") |
| 83 | + |
| 84 | + with self.assertRaises(ValueError) as cm: |
| 85 | + BulkResponse(records=[], results=None) |
| 86 | + self.assertEqual( |
| 87 | + str(cm.exception), |
| 88 | + "Processing a bulk response without results is an invalid operation") |
0 commit comments