1
1
import copy
2
+ import pipes
3
+ import subprocess
4
+ import sys
5
+ import tempfile
2
6
import unittest
3
7
from dataclasses import dataclass
4
8
from test .pycardano .util import check_two_way_cbor
@@ -318,8 +322,8 @@ def test_clone_plutus_data():
318
322
319
323
assert cloned_vesting != my_vesting
320
324
321
- def test_unique_constr_ids ():
322
325
326
+ def test_unique_constr_ids ():
323
327
@dataclass
324
328
class A (PlutusData ):
325
329
pass
@@ -328,15 +332,19 @@ class A(PlutusData):
328
332
class B (PlutusData ):
329
333
pass
330
334
331
- assert A .CONSTR_ID != B .CONSTR_ID , "Different classes (different names) have same default constructor ID"
335
+ assert (
336
+ A .CONSTR_ID != B .CONSTR_ID
337
+ ), "Different classes (different names) have same default constructor ID"
332
338
B_tmp = B
333
339
334
340
@dataclass
335
341
class B (PlutusData ):
336
342
a : int
337
343
b : bytes
338
344
339
- assert B_tmp .CONSTR_ID != B .CONSTR_ID , "Different classes (different fields) have same default constructor ID"
345
+ assert (
346
+ B_tmp .CONSTR_ID != B .CONSTR_ID
347
+ ), "Different classes (different fields) have same default constructor ID"
340
348
341
349
B_tmp = B
342
350
@@ -345,4 +353,48 @@ class B(PlutusData):
345
353
a : bytes
346
354
b : bytes
347
355
348
- assert B_tmp .CONSTR_ID != B .CONSTR_ID , "Different classes (different field types) have same default constructor ID"
356
+ assert (
357
+ B_tmp .CONSTR_ID != B .CONSTR_ID
358
+ ), "Different classes (different field types) have same default constructor ID"
359
+
360
+
361
+ def test_deterministic_constr_ids_local ():
362
+ @dataclass
363
+ class A (PlutusData ):
364
+ a : int
365
+ b : bytes
366
+
367
+ A_tmp = A
368
+
369
+ @dataclass
370
+ class A (PlutusData ):
371
+ a : int
372
+ b : bytes
373
+
374
+ assert (
375
+ A_tmp .CONSTR_ID == A .CONSTR_ID
376
+ ), "Same class has different default constructor ID"
377
+
378
+
379
+ def test_deterministic_constr_ids_global ():
380
+ code = """
381
+ from dataclasses import dataclass
382
+ from pycardano import PlutusData
383
+
384
+ @dataclass
385
+ class A(PlutusData):
386
+ a: int
387
+ b: bytes
388
+
389
+ print(A.CONSTR_ID)
390
+ """
391
+ tmpfile = tempfile .TemporaryFile ()
392
+ tmpfile .write (code .encode ("utf8" ))
393
+ tmpfile .seek (0 )
394
+ res = subprocess .run ([sys .executable ], stdin = tmpfile , capture_output = True ).stdout
395
+ tmpfile .seek (0 )
396
+ res2 = subprocess .run ([sys .executable ], stdin = tmpfile , capture_output = True ).stdout
397
+
398
+ assert (
399
+ res == res2
400
+ ), "Same class has different default constructor id in two consecutive runs"
0 commit comments