piccolo 1.28.0__py3-none-any.whl → 1.29.0__py3-none-any.whl

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,3 +1,4 @@
1
+ import decimal
1
2
  import unittest
2
3
  from enum import Enum
3
4
 
@@ -35,6 +36,10 @@ class TestRandomBuilder(unittest.TestCase):
35
36
  random_float = RandomBuilder.next_float(maximum=1000)
36
37
  self.assertLessEqual(random_float, 1000)
37
38
 
39
+ def test_next_decimal(self):
40
+ random_decimal = RandomBuilder.next_decimal(precision=4, scale=2)
41
+ self.assertLessEqual(random_decimal, decimal.Decimal("99.99"))
42
+
38
43
  def test_next_int(self):
39
44
  random_int = RandomBuilder.next_int()
40
45
  self.assertLessEqual(random_int, 2147483647)
@@ -29,10 +29,10 @@ from piccolo.utils.pydantic import create_pydantic_model
29
29
 
30
30
  class TestVarcharColumn(TestCase):
31
31
  def test_varchar_length(self):
32
- class Director(Table):
32
+ class Manager(Table):
33
33
  name = Varchar(length=10)
34
34
 
35
- pydantic_model = create_pydantic_model(table=Director)
35
+ pydantic_model = create_pydantic_model(table=Manager)
36
36
 
37
37
  with self.assertRaises(ValidationError):
38
38
  pydantic_model(name="This is a really long name")
@@ -42,10 +42,10 @@ class TestVarcharColumn(TestCase):
42
42
 
43
43
  class TestEmailColumn(TestCase):
44
44
  def test_email(self):
45
- class Director(Table):
45
+ class Manager(Table):
46
46
  email = Email()
47
47
 
48
- pydantic_model = create_pydantic_model(table=Director)
48
+ pydantic_model = create_pydantic_model(table=Manager)
49
49
 
50
50
  self.assertEqual(
51
51
  pydantic_model.model_json_schema()["properties"]["email"]["anyOf"][
@@ -67,28 +67,28 @@ class TestNumericColumn(TestCase):
67
67
  """
68
68
 
69
69
  def test_numeric_digits(self):
70
- class Movie(Table):
71
- box_office = Numeric(digits=(5, 1))
70
+ class Band(Table):
71
+ royalties = Numeric(digits=(5, 1))
72
72
 
73
- pydantic_model = create_pydantic_model(table=Movie)
73
+ pydantic_model = create_pydantic_model(table=Band)
74
74
 
75
75
  with self.assertRaises(ValidationError):
76
76
  # This should fail as there are too much numbers after the decimal
77
77
  # point
78
- pydantic_model(box_office=decimal.Decimal("1.11"))
78
+ pydantic_model(royalties=decimal.Decimal("1.11"))
79
79
 
80
80
  with self.assertRaises(ValidationError):
81
81
  # This should fail as there are too much numbers in total
82
- pydantic_model(box_office=decimal.Decimal("11111.1"))
82
+ pydantic_model(royalties=decimal.Decimal("11111.1"))
83
83
 
84
- pydantic_model(box_office=decimal.Decimal("1.0"))
84
+ pydantic_model(royalties=decimal.Decimal("1.0"))
85
85
 
86
86
  def test_numeric_without_digits(self):
87
- class Movie(Table):
88
- box_office = Numeric()
87
+ class Band(Table):
88
+ royalties = Numeric()
89
89
 
90
90
  try:
91
- create_pydantic_model(table=Movie)
91
+ create_pydantic_model(table=Band)
92
92
  except TypeError:
93
93
  self.fail(
94
94
  "Creating numeric field without"
@@ -297,13 +297,13 @@ class TestColumnHelpText(TestCase):
297
297
  def test_column_help_text_present(self):
298
298
  help_text = "In millions of US dollars."
299
299
 
300
- class Movie(Table):
301
- box_office = Numeric(digits=(5, 1), help_text=help_text)
300
+ class Band(Table):
301
+ royalties = Numeric(digits=(5, 1), help_text=help_text)
302
302
 
303
- pydantic_model = create_pydantic_model(table=Movie)
303
+ pydantic_model = create_pydantic_model(table=Band)
304
304
 
305
305
  self.assertEqual(
306
- pydantic_model.model_json_schema()["properties"]["box_office"][
306
+ pydantic_model.model_json_schema()["properties"]["royalties"][
307
307
  "extra"
308
308
  ]["help_text"],
309
309
  help_text,
@@ -317,12 +317,12 @@ class TestTableHelpText(TestCase):
317
317
  """
318
318
 
319
319
  def test_table_help_text_present(self):
320
- help_text = "Movies which were released in cinemas."
320
+ help_text = "Bands playing concerts."
321
321
 
322
- class Movie(Table, help_text=help_text):
322
+ class Band(Table, help_text=help_text):
323
323
  name = Varchar()
324
324
 
325
- pydantic_model = create_pydantic_model(table=Movie)
325
+ pydantic_model = create_pydantic_model(table=Band)
326
326
 
327
327
  self.assertEqual(
328
328
  pydantic_model.model_json_schema()["extra"]["help_text"],
@@ -332,10 +332,10 @@ class TestTableHelpText(TestCase):
332
332
 
333
333
  class TestUniqueColumn(TestCase):
334
334
  def test_unique_column_true(self):
335
- class Director(Table):
335
+ class Manager(Table):
336
336
  name = Varchar(unique=True)
337
337
 
338
- pydantic_model = create_pydantic_model(table=Director)
338
+ pydantic_model = create_pydantic_model(table=Manager)
339
339
 
340
340
  self.assertEqual(
341
341
  pydantic_model.model_json_schema()["properties"]["name"]["extra"][
@@ -345,10 +345,10 @@ class TestUniqueColumn(TestCase):
345
345
  )
346
346
 
347
347
  def test_unique_column_false(self):
348
- class Director(Table):
348
+ class Manager(Table):
349
349
  name = Varchar()
350
350
 
351
- pydantic_model = create_pydantic_model(table=Director)
351
+ pydantic_model = create_pydantic_model(table=Manager)
352
352
 
353
353
  self.assertEqual(
354
354
  pydantic_model.model_json_schema()["properties"]["name"]["extra"][
@@ -360,48 +360,66 @@ class TestUniqueColumn(TestCase):
360
360
 
361
361
  class TestJSONColumn(TestCase):
362
362
  def test_default(self):
363
- class Movie(Table):
364
- meta = JSON()
365
- meta_b = JSONB()
363
+ class Studio(Table):
364
+ facilities = JSON()
365
+ facilities_b = JSONB()
366
366
 
367
- pydantic_model = create_pydantic_model(table=Movie)
367
+ pydantic_model = create_pydantic_model(table=Studio)
368
368
 
369
- json_string = '{"code": 12345}'
369
+ json_string = '{"guitar_amps": 6}'
370
370
 
371
- model_instance = pydantic_model(meta=json_string, meta_b=json_string)
372
- self.assertEqual(model_instance.meta, json_string) # type: ignore
373
- self.assertEqual(model_instance.meta_b, json_string) # type: ignore
371
+ model_instance = pydantic_model(
372
+ facilities=json_string, facilities_b=json_string
373
+ )
374
+ self.assertEqual(
375
+ model_instance.facilities,
376
+ json_string,
377
+ )
378
+ self.assertEqual(
379
+ model_instance.facilities_b,
380
+ json_string,
381
+ )
374
382
 
375
383
  def test_deserialize_json(self):
376
- class Movie(Table):
377
- meta = JSON()
378
- meta_b = JSONB()
384
+ class Studio(Table):
385
+ facilities = JSON()
386
+ facilities_b = JSONB()
379
387
 
380
388
  pydantic_model = create_pydantic_model(
381
- table=Movie, deserialize_json=True
389
+ table=Studio, deserialize_json=True
382
390
  )
383
391
 
384
- json_string = '{"code": 12345}'
385
- output = {"code": 12345}
392
+ json_string = '{"guitar_amps": 6}'
393
+ output = {"guitar_amps": 6}
386
394
 
387
- model_instance = pydantic_model(meta=json_string, meta_b=json_string)
388
- self.assertEqual(model_instance.meta, output) # type: ignore
389
- self.assertEqual(model_instance.meta_b, output) # type: ignore
395
+ model_instance = pydantic_model(
396
+ facilities=json_string, facilities_b=json_string
397
+ )
398
+ self.assertEqual(
399
+ model_instance.facilities,
400
+ output,
401
+ )
402
+ self.assertEqual(
403
+ model_instance.facilities_b,
404
+ output,
405
+ )
390
406
 
391
407
  def test_validation(self):
392
- class Movie(Table):
393
- meta = JSON()
394
- meta_b = JSONB()
408
+ class Studio(Table):
409
+ facilities = JSON()
410
+ facilities_b = JSONB()
395
411
 
396
412
  for deserialize_json in (True, False):
397
413
  pydantic_model = create_pydantic_model(
398
- table=Movie, deserialize_json=deserialize_json
414
+ table=Studio, deserialize_json=deserialize_json
399
415
  )
400
416
 
401
417
  json_string = "error"
402
418
 
403
419
  with self.assertRaises(pydantic.ValidationError):
404
- pydantic_model(meta=json_string, meta_b=json_string)
420
+ pydantic_model(
421
+ facilities=json_string, facilities_b=json_string
422
+ )
405
423
 
406
424
  def test_json_widget(self):
407
425
  """
@@ -409,112 +427,112 @@ class TestJSONColumn(TestCase):
409
427
  special widget in Piccolo Admin.
410
428
  """
411
429
 
412
- class Movie(Table):
413
- features = JSON()
430
+ class Studio(Table):
431
+ facilities = JSON()
414
432
 
415
- pydantic_model = create_pydantic_model(table=Movie)
433
+ pydantic_model = create_pydantic_model(table=Studio)
416
434
 
417
435
  self.assertEqual(
418
- pydantic_model.model_json_schema()["properties"]["features"][
436
+ pydantic_model.model_json_schema()["properties"]["facilities"][
419
437
  "extra"
420
438
  ]["widget"],
421
439
  "json",
422
440
  )
423
441
 
424
442
  def test_null_value(self):
425
- class Movie(Table):
426
- meta = JSON(null=True)
427
- meta_b = JSONB(null=True)
443
+ class Studio(Table):
444
+ facilities = JSON(null=True)
445
+ facilities_b = JSONB(null=True)
428
446
 
429
- pydantic_model = create_pydantic_model(table=Movie)
430
- movie = pydantic_model(meta=None, meta_b=None)
447
+ pydantic_model = create_pydantic_model(table=Studio)
448
+ movie = pydantic_model(facilities=None, facilities_b=None)
431
449
 
432
- self.assertIsNone(movie.meta) # type: ignore
433
- self.assertIsNone(movie.meta_b) # type: ignore
450
+ self.assertIsNone(movie.facilities)
451
+ self.assertIsNone(movie.facilities_b)
434
452
 
435
453
 
436
454
  class TestExcludeColumns(TestCase):
437
455
  def test_all(self):
438
- class Computer(Table):
439
- CPU = Varchar()
440
- GPU = Varchar()
456
+ class Band(Table):
457
+ name = Varchar()
458
+ bio = Text()
441
459
 
442
- pydantic_model = create_pydantic_model(Computer, exclude_columns=())
460
+ pydantic_model = create_pydantic_model(Band, exclude_columns=())
443
461
 
444
462
  properties = pydantic_model.model_json_schema()["properties"]
445
- self.assertIsInstance(properties["GPU"], dict)
446
- self.assertIsInstance(properties["CPU"], dict)
463
+ self.assertIsInstance(properties["name"], dict)
464
+ self.assertIsInstance(properties["bio"], dict)
447
465
 
448
466
  def test_exclude(self):
449
- class Computer(Table):
450
- CPU = Varchar()
451
- GPU = Varchar()
467
+ class Band(Table):
468
+ name = Varchar()
469
+ album = Varchar()
452
470
 
453
471
  pydantic_model = create_pydantic_model(
454
- Computer,
455
- exclude_columns=(Computer.CPU,),
472
+ Band,
473
+ exclude_columns=(Band.name,),
456
474
  )
457
475
 
458
476
  properties = pydantic_model.model_json_schema()["properties"]
459
- self.assertIsInstance(properties.get("GPU"), dict)
460
- self.assertIsNone(properties.get("CPU"))
477
+ self.assertIsInstance(properties.get("album"), dict)
478
+ self.assertIsNone(properties.get("dict"))
461
479
 
462
480
  def test_exclude_all_manually(self):
463
- class Computer(Table):
464
- GPU = Varchar()
465
- CPU = Varchar()
481
+ class Band(Table):
482
+ name = Varchar()
483
+ album = Varchar()
466
484
 
467
485
  pydantic_model = create_pydantic_model(
468
- Computer,
469
- exclude_columns=(Computer.GPU, Computer.CPU),
486
+ Band,
487
+ exclude_columns=(Band.name, Band.album),
470
488
  )
471
489
 
472
490
  self.assertEqual(pydantic_model.model_json_schema()["properties"], {})
473
491
 
474
492
  def test_exclude_all_meta(self):
475
- class Computer(Table):
476
- GPU = Varchar()
477
- CPU = Varchar()
493
+ class Band(Table):
494
+ name = Varchar()
495
+ album = Varchar()
478
496
 
479
497
  pydantic_model = create_pydantic_model(
480
- Computer,
481
- exclude_columns=tuple(Computer._meta.columns),
498
+ Band,
499
+ exclude_columns=tuple(Band._meta.columns),
482
500
  )
483
501
 
484
502
  self.assertEqual(pydantic_model.model_json_schema()["properties"], {})
485
503
 
486
504
  def test_invalid_column_str(self):
487
- class Computer(Table):
488
- CPU = Varchar()
489
- GPU = Varchar()
505
+ class Band(Table):
506
+ name = Varchar()
507
+ album = Varchar()
490
508
 
491
509
  with self.assertRaises(ValueError):
492
510
  create_pydantic_model(
493
- Computer,
494
- exclude_columns=("CPU",), # type: ignore
511
+ Band,
512
+ exclude_columns=("album",),
495
513
  )
496
514
 
497
515
  def test_invalid_column_different_table(self):
498
- class Computer(Table):
499
- CPU = Varchar()
500
- GPU = Varchar()
516
+ class Band(Table):
517
+ name = Varchar()
518
+ album = Varchar()
501
519
 
502
- class Computer2(Table):
503
- SSD = Varchar()
520
+ class Band2(Table):
521
+ photo = Varchar()
504
522
 
505
523
  with self.assertRaises(ValueError):
506
- create_pydantic_model(Computer, exclude_columns=(Computer2.SSD,))
524
+ create_pydantic_model(Band, exclude_columns=(Band2.photo,))
507
525
 
508
526
  def test_invalid_column_different_table_same_type(self):
509
- class Computer(Table):
510
- CPU = Varchar()
511
- GPU = Varchar()
527
+ class Band(Table):
528
+ name = Varchar()
529
+ album = Varchar()
512
530
 
513
- class Computer2(Table):
514
- CPU = Varchar()
531
+ class Band2(Table):
532
+ name = Varchar()
515
533
 
516
534
  with self.assertRaises(ValueError):
517
- create_pydantic_model(Computer, exclude_columns=(Computer2.CPU,))
535
+ create_pydantic_model(Band, exclude_columns=(Band2.name,))
518
536
 
519
537
  def test_exclude_nested(self):
520
538
  class Manager(Table):
@@ -892,7 +910,7 @@ class TestDBColumnName(TestCase):
892
910
 
893
911
  model = BandModel(regrettable_column_name="test")
894
912
 
895
- self.assertEqual(model.name, "test") # type: ignore
913
+ self.assertEqual(model.name, "test")
896
914
 
897
915
 
898
916
  class TestJSONSchemaExtra(TestCase):