|
| 1 | +// vtest retry: 3 |
| 2 | +// Tests for generic structs where the type parameter is used as a field type |
| 3 | +import db.sqlite |
| 4 | + |
| 5 | +// Test struct that will be used as a generic type parameter |
| 6 | +struct Payload { |
| 7 | + some_field_1 string |
| 8 | + some_field_2 int |
| 9 | +} |
| 10 | + |
| 11 | +// Generic struct with a field of type T - this requires proper ORM configuration |
| 12 | +// The payload field will be treated as a foreign key reference |
| 13 | +pub struct Message[T] { |
| 14 | + id int @[primary; sql: serial] |
| 15 | + data string |
| 16 | + payload T @[fkey: 'id'] |
| 17 | +} |
| 18 | + |
| 19 | +// Payload needs to be a proper ORM table with a primary key |
| 20 | +struct PayloadTable { |
| 21 | + id int @[primary; sql: serial] |
| 22 | + some_field_1 string |
| 23 | + some_field_2 int |
| 24 | +} |
| 25 | + |
| 26 | +// Generic struct with proper ORM configuration |
| 27 | +pub struct MessageWithPayload[T] { |
| 28 | + id int @[primary; sql: serial] |
| 29 | + data string |
| 30 | + payload T @[fkey: 'id'] |
| 31 | +} |
| 32 | + |
| 33 | +fn test_generic_struct_with_struct_field_and_primary_key() { |
| 34 | + mut db := sqlite.connect(':memory:')! |
| 35 | + |
| 36 | + // Create tables - PayloadTable first since it's referenced |
| 37 | + sql db { |
| 38 | + create table PayloadTable |
| 39 | + }! |
| 40 | + |
| 41 | + // Insert a payload |
| 42 | + payload := PayloadTable{ |
| 43 | + some_field_1: 'test' |
| 44 | + some_field_2: 42 |
| 45 | + } |
| 46 | + sql db { |
| 47 | + insert payload into PayloadTable |
| 48 | + }! |
| 49 | + |
| 50 | + // Verify the payload was inserted |
| 51 | + payloads := sql db { |
| 52 | + select from PayloadTable |
| 53 | + }! |
| 54 | + assert payloads.len == 1 |
| 55 | + assert payloads[0].some_field_1 == 'test' |
| 56 | + assert payloads[0].some_field_2 == 42 |
| 57 | + |
| 58 | + db.close()! |
| 59 | +} |
| 60 | + |
| 61 | +// Test that generic structs with simple fields work correctly |
| 62 | +pub struct SimpleMessage[T] { |
| 63 | + id int @[primary; sql: serial] |
| 64 | + data string |
| 65 | + status int |
| 66 | +} |
| 67 | + |
| 68 | +fn test_generic_struct_with_simple_fields() { |
| 69 | + mut db := sqlite.connect(':memory:')! |
| 70 | + |
| 71 | + sql db { |
| 72 | + create table SimpleMessage[Payload] |
| 73 | + }! |
| 74 | + |
| 75 | + // The table should be created successfully |
| 76 | + // Note: SimpleMessage[Payload] doesn't actually use Payload as a field type, |
| 77 | + // so this works fine |
| 78 | + |
| 79 | + db.close()! |
| 80 | +} |
| 81 | + |
| 82 | +// Test that skipping struct fields with @[sql: '-'] works |
| 83 | +pub struct MessageWithSkippedField[T] { |
| 84 | + id int @[primary; sql: serial] |
| 85 | + data string |
| 86 | + payload T @[sql: '-'] // Skip this field in ORM |
| 87 | +} |
| 88 | + |
| 89 | +fn test_generic_struct_with_skipped_field() { |
| 90 | + mut db := sqlite.connect(':memory:')! |
| 91 | + |
| 92 | + // This should work because the payload field is skipped |
| 93 | + sql db { |
| 94 | + create table MessageWithSkippedField[Payload] |
| 95 | + }! |
| 96 | + |
| 97 | + db.close()! |
| 98 | +} |
0 commit comments