relations-sqlite3 0.6.11__tar.gz → 0.6.12__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: relations-sqlite3
3
- Version: 0.6.11
3
+ Version: 0.6.12
4
4
  Summary: DB Modeling for SQLite using the sqlite3 library
5
5
  Home-page: https://github.com/relations-dil/python-relations-sqlite3
6
6
  Author: Gaffer Fitch
@@ -9,7 +9,7 @@ Classifier: Programming Language :: Python :: 3
9
9
  Classifier: License :: OSI Approved :: MIT License
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE.txt
12
- Requires-Dist: relations-dil>=0.6.18
12
+ Requires-Dist: relations-dil>=0.6.19
13
13
  Requires-Dist: relations-sqlite>=0.6.4
14
14
  Dynamic: author
15
15
  Dynamic: author-email
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: relations-sqlite3
3
- Version: 0.6.11
3
+ Version: 0.6.12
4
4
  Summary: DB Modeling for SQLite using the sqlite3 library
5
5
  Home-page: https://github.com/relations-dil/python-relations-sqlite3
6
6
  Author: Gaffer Fitch
@@ -9,7 +9,7 @@ Classifier: Programming Language :: Python :: 3
9
9
  Classifier: License :: OSI Approved :: MIT License
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE.txt
12
- Requires-Dist: relations-dil>=0.6.18
12
+ Requires-Dist: relations-dil>=0.6.19
13
13
  Requires-Dist: relations-sqlite>=0.6.4
14
14
  Dynamic: author
15
15
  Dynamic: author-email
@@ -1,2 +1,2 @@
1
- relations-dil>=0.6.18
1
+ relations-dil>=0.6.19
2
2
  relations-sqlite>=0.6.4
@@ -143,6 +143,19 @@ class Source(relations_sql.SOURCE, relations.Source): # pylint: disable=too-many
143
143
 
144
144
  return query
145
145
 
146
+ @staticmethod
147
+ def write(cursor, model, sql, args):
148
+ """
149
+ Executes a write, a duplicate is relations' unique error, with the driver's error as its cause
150
+ """
151
+
152
+ try:
153
+ cursor.execute(sql, args)
154
+ except sqlite3.IntegrityError as exception:
155
+ if str(exception).startswith("UNIQUE constraint failed"):
156
+ raise relations.UniqueError(model, str(exception)) from exception
157
+ raise
158
+
146
159
  @staticmethod
147
160
  def create_id(cursor, model, query):
148
161
  """
@@ -150,7 +163,7 @@ class Source(relations_sql.SOURCE, relations.Source): # pylint: disable=too-many
150
163
  """
151
164
 
152
165
  query.generate()
153
- cursor.execute(query.sql, tuple(query.args))
166
+ Source.write(cursor, model, query.sql, tuple(query.args))
154
167
 
155
168
  model[model._id] = cursor.lastrowid
156
169
 
@@ -170,7 +183,7 @@ class Source(relations_sql.SOURCE, relations.Source): # pylint: disable=too-many
170
183
  else:
171
184
  create_query = query or self.create_query(model)
172
185
  create_query.generate()
173
- cursor.execute(create_query.sql, tuple(create_query.args))
186
+ self.write(cursor, model, create_query.sql, tuple(create_query.args))
174
187
 
175
188
  cursor.close()
176
189
 
@@ -391,14 +404,14 @@ class Source(relations_sql.SOURCE, relations.Source): # pylint: disable=too-many
391
404
  rows = cursor.fetchall()
392
405
 
393
406
  if model._mode == "one" and len(rows) > 1:
394
- raise relations.ModelError(model, "more than one retrieved")
407
+ raise relations.MultipleError(model, "more than one retrieved")
395
408
 
396
409
  if model._mode == "one" and model._role != "child":
397
410
 
398
411
  if len(rows) < 1:
399
412
 
400
413
  if verify:
401
- raise relations.ModelError(model, "none retrieved")
414
+ raise relations.NotFoundError(model, "none retrieved")
402
415
  return None
403
416
 
404
417
  model._record = model._build("update", _read=self.values_retrieve(model, dict(rows[0])))
@@ -494,7 +507,7 @@ class Source(relations_sql.SOURCE, relations.Source): # pylint: disable=too-many
494
507
  if update_query.SET.expressions:
495
508
 
496
509
  update_query.generate()
497
- cursor.execute(update_query.sql, update_query.args)
510
+ self.write(cursor, model, update_query.sql, update_query.args)
498
511
  updated += cursor.rowcount
499
512
 
500
513
  ties = model._record.tie({})
@@ -524,7 +537,7 @@ class Source(relations_sql.SOURCE, relations.Source): # pylint: disable=too-many
524
537
  if update_query.SET.expressions:
525
538
 
526
539
  update_query.generate()
527
- cursor.execute(update_query.sql, update_query.args)
540
+ self.write(cursor, updating, update_query.sql, update_query.args)
528
541
  updated += cursor.rowcount
529
542
 
530
543
 
@@ -20,7 +20,7 @@ setup(
20
20
  'relations_sqlite3'
21
21
  ],
22
22
  install_requires=[
23
- 'relations-dil>=0.6.18',
23
+ 'relations-dil>=0.6.19',
24
24
  'relations-sqlite>=0.6.4'
25
25
  ],
26
26
  url="https://github.com/relations-dil/python-relations-sqlite3",
@@ -262,6 +262,32 @@ CREATE UNIQUE INDEX `test_source`.`simple_name` ON `simple` (`name`);
262
262
  self.assertEqual(query.sql, """INSERT INTO `test_source`.`sis` (`name`) VALUES (?)""")
263
263
  self.assertEqual(query.args, ["sure"])
264
264
 
265
+ def test_write(self):
266
+
267
+ self.source.execute(Simple.define())
268
+
269
+ simple = Simple("sure")
270
+
271
+ cursor = self.source.connection.cursor()
272
+
273
+ # a write that works
274
+
275
+ self.source.write(cursor, simple, "INSERT INTO test_source.simple (name) VALUES (?)", ("one",))
276
+
277
+ # a duplicate is core's unique error, with the driver's error as its cause
278
+
279
+ with self.assertRaises(relations.UniqueError) as caught:
280
+ self.source.write(cursor, simple, "INSERT INTO test_source.simple (name) VALUES (?)", ("one",))
281
+
282
+ self.assertEqual(caught.exception.model, simple)
283
+ self.assertIsInstance(caught.exception.__cause__, sqlite3.IntegrityError)
284
+
285
+ # any other integrity error is left as it is
286
+
287
+ self.assertRaises(sqlite3.IntegrityError, self.source.write, cursor, simple, "INSERT INTO test_source.simple (name) VALUES (?)", (None,))
288
+
289
+ cursor.close()
290
+
265
291
  def test_create_id(self):
266
292
 
267
293
  self.source.execute(Simple.define())
@@ -279,6 +305,12 @@ CREATE UNIQUE INDEX `test_source`.`simple_name` ON `simple` (`name`);
279
305
  cursor.execute("SELECT * FROM test_source.simple")
280
306
  self.assertEqual(cursor.fetchone()["id"], simple.id)
281
307
 
308
+ # a duplicate is core's unique error
309
+
310
+ again = Simple("sure")
311
+
312
+ self.assertRaises(relations.UniqueError, self.source.create_id, cursor, again, self.source.create_query(again))
313
+
282
314
  cursor.close()
283
315
 
284
316
  def test_create(self):
@@ -365,6 +397,10 @@ CREATE UNIQUE INDEX `test_source`.`simple_name` ON `simple` (`name`);
365
397
  {"id": 2, "name": "stray", "what": {}}
366
398
  ])
367
399
 
400
+ # a duplicate is core's unique error
401
+
402
+ self.assertRaises(relations.UniqueError, Simple("sure").create)
403
+
368
404
  def test_retrieve_field(self):
369
405
 
370
406
  field = relations.Field(int, name="id")
@@ -564,10 +600,10 @@ CREATE UNIQUE INDEX `test_source`.`simple_name` ON `simple` (`name`);
564
600
  Unit([["stuff"], ["people"]]).create()
565
601
 
566
602
  models = Unit.one(name__in=["people", "stuff"])
567
- self.assertRaisesRegex(relations.ModelError, "unit: more than one retrieved", models.retrieve)
603
+ self.assertRaisesRegex(relations.MultipleError, "unit: more than one retrieved", models.retrieve)
568
604
 
569
605
  model = Unit.one(name="things")
570
- self.assertRaisesRegex(relations.ModelError, "unit: none retrieved", model.retrieve)
606
+ self.assertRaisesRegex(relations.NotFoundError, "unit: none retrieved", model.retrieve)
571
607
 
572
608
  self.assertIsNone(model.retrieve(False))
573
609
 
@@ -609,10 +645,10 @@ WHERE
609
645
  Unit([["stuff"], ["people"]]).create()
610
646
 
611
647
  models = Unit.one(name__in=["people", "stuff"])
612
- self.assertRaisesRegex(relations.ModelError, "unit: more than one retrieved", models.retrieve)
648
+ self.assertRaisesRegex(relations.MultipleError, "unit: more than one retrieved", models.retrieve)
613
649
 
614
650
  model = Unit.one(name="things")
615
- self.assertRaisesRegex(relations.ModelError, "unit: none retrieved", model.retrieve)
651
+ self.assertRaisesRegex(relations.NotFoundError, "unit: none retrieved", model.retrieve)
616
652
 
617
653
  self.assertIsNone(model.retrieve(False))
618
654
 
@@ -657,10 +693,10 @@ LIMIT ?""")
657
693
  Unit([["stuff"], ["people"]]).create()
658
694
 
659
695
  models = Unit.one(name__in=["people", "stuff"])
660
- self.assertRaisesRegex(relations.ModelError, "unit: more than one retrieved", models.retrieve)
696
+ self.assertRaisesRegex(relations.MultipleError, "unit: more than one retrieved", models.retrieve)
661
697
 
662
698
  model = Unit.one(name="things")
663
- self.assertRaisesRegex(relations.ModelError, "unit: none retrieved", model.retrieve)
699
+ self.assertRaisesRegex(relations.NotFoundError, "unit: none retrieved", model.retrieve)
664
700
 
665
701
  self.assertIsNone(model.retrieve(False))
666
702
 
@@ -789,10 +825,10 @@ LIMIT ?""")
789
825
  Unit([["stuff"], ["people"]]).create()
790
826
 
791
827
  models = Unit.one(name__in=["people", "stuff"])
792
- self.assertRaisesRegex(relations.ModelError, "unit: more than one retrieved", models.retrieve)
828
+ self.assertRaisesRegex(relations.MultipleError, "unit: more than one retrieved", models.retrieve)
793
829
 
794
830
  model = Unit.one(name="things")
795
- self.assertRaisesRegex(relations.ModelError, "unit: none retrieved", model.retrieve)
831
+ self.assertRaisesRegex(relations.NotFoundError, "unit: none retrieved", model.retrieve)
796
832
 
797
833
  self.assertIsNone(model.retrieve(False))
798
834
 
@@ -1303,6 +1339,18 @@ LIMIT ?""")
1303
1339
 
1304
1340
  self.assertRaisesRegex(relations.FieldError, "no mass update with inject", Pet.many(name="rex").set(owner_id=sam.id).update)
1305
1341
 
1342
+ # a duplicate is core's unique error, for one record and for many
1343
+
1344
+ self.source.execute(Simple.define())
1345
+ self.source.execute(Plain.define())
1346
+
1347
+ Simple("dup one").create()
1348
+ two = Simple("dup two").create()
1349
+
1350
+ two.name = "dup one"
1351
+ self.assertRaises(relations.UniqueError, two.update)
1352
+ self.assertRaises(relations.UniqueError, Simple.many().set(name="same").update)
1353
+
1306
1354
  def test_delete_query(self):
1307
1355
 
1308
1356
  self.source.execute(Unit.define())