|
| 1 | +//go:build examples |
| 2 | + |
| 3 | +package skipparser |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/jackc/pgx/v5" |
| 10 | + "github.com/jackc/pgx/v5/pgtype" |
| 11 | + |
| 12 | + "github.com/sqlc-dev/sqlc/internal/sqltest/local" |
| 13 | +) |
| 14 | + |
| 15 | +func TestSkipParser(t *testing.T) { |
| 16 | + ctx := context.Background() |
| 17 | + uri := local.PostgreSQL(t, []string{"schema.sql"}) |
| 18 | + db, err := pgx.Connect(ctx, uri) |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + defer db.Close(ctx) |
| 23 | + |
| 24 | + q := New(db) |
| 25 | + |
| 26 | + // Test CountProducts on empty database |
| 27 | + count, err := q.CountProducts(ctx) |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + if count != 0 { |
| 32 | + t.Errorf("expected 0 products, got %d", count) |
| 33 | + } |
| 34 | + |
| 35 | + // Test CreateProduct |
| 36 | + product, err := q.CreateProduct(ctx, CreateProductParams{ |
| 37 | + Name: "Test Product", |
| 38 | + Price: "99.99", |
| 39 | + Tags: []string{"electronics", "test"}, |
| 40 | + Metadata: []byte(`{"color": "blue", "weight": 1.5}`), |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + if product.ID == 0 { |
| 46 | + t.Error("expected product ID to be non-zero") |
| 47 | + } |
| 48 | + if product.Name != "Test Product" { |
| 49 | + t.Errorf("expected name 'Test Product', got %s", product.Name) |
| 50 | + } |
| 51 | + t.Logf("Created product: %+v", product) |
| 52 | + |
| 53 | + // Test GetProduct |
| 54 | + fetchedProduct, err := q.GetProduct(ctx, product.ID) |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + if fetchedProduct.ID != product.ID { |
| 59 | + t.Errorf("expected ID %d, got %d", product.ID, fetchedProduct.ID) |
| 60 | + } |
| 61 | + t.Logf("Fetched product: %+v", fetchedProduct) |
| 62 | + |
| 63 | + // Test ListProducts |
| 64 | + products, err := q.ListProducts(ctx, ListProductsParams{ |
| 65 | + Limit: 10, |
| 66 | + Offset: 0, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + if len(products) != 1 { |
| 72 | + t.Errorf("expected 1 product, got %d", len(products)) |
| 73 | + } |
| 74 | + t.Logf("Listed products: %+v", products) |
| 75 | + |
| 76 | + // Test UpdateProduct |
| 77 | + updatedProduct, err := q.UpdateProduct(ctx, UpdateProductParams{ |
| 78 | + ID: product.ID, |
| 79 | + Name: "Updated Product", |
| 80 | + Price: "149.99", |
| 81 | + Tags: []string{"electronics", "updated"}, |
| 82 | + Metadata: []byte(`{"color": "red", "weight": 2.0}`), |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + if updatedProduct.Name != "Updated Product" { |
| 88 | + t.Errorf("expected name 'Updated Product', got %s", updatedProduct.Name) |
| 89 | + } |
| 90 | + t.Logf("Updated product: %+v", updatedProduct) |
| 91 | + |
| 92 | + // Test SearchProductsByTag |
| 93 | + tagProducts, err := q.SearchProductsByTag(ctx, "electronics") |
| 94 | + if err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + if len(tagProducts) != 1 { |
| 98 | + t.Errorf("expected 1 product with tag 'electronics', got %d", len(tagProducts)) |
| 99 | + } |
| 100 | + t.Logf("Products with tag 'electronics': %+v", tagProducts) |
| 101 | + |
| 102 | + // Test CountProducts after insert |
| 103 | + count, err = q.CountProducts(ctx) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + if count != 1 { |
| 108 | + t.Errorf("expected 1 product, got %d", count) |
| 109 | + } |
| 110 | + |
| 111 | + // Test DeleteProduct |
| 112 | + err = q.DeleteProduct(ctx, product.ID) |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + // Verify deletion |
| 118 | + count, err = q.CountProducts(ctx) |
| 119 | + if err != nil { |
| 120 | + t.Fatal(err) |
| 121 | + } |
| 122 | + if count != 0 { |
| 123 | + t.Errorf("expected 0 products after deletion, got %d", count) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestSkipParserComplexTypes(t *testing.T) { |
| 128 | + ctx := context.Background() |
| 129 | + uri := local.PostgreSQL(t, []string{"schema.sql"}) |
| 130 | + db, err := pgx.Connect(ctx, uri) |
| 131 | + if err != nil { |
| 132 | + t.Fatal(err) |
| 133 | + } |
| 134 | + defer db.Close(ctx) |
| 135 | + |
| 136 | + q := New(db) |
| 137 | + |
| 138 | + // Test with empty arrays and JSON |
| 139 | + product, err := q.CreateProduct(ctx, CreateProductParams{ |
| 140 | + Name: "Minimal Product", |
| 141 | + Price: "19.99", |
| 142 | + Tags: []string{}, |
| 143 | + Metadata: []byte(`{}`), |
| 144 | + }) |
| 145 | + if err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + t.Logf("Created minimal product: %+v", product) |
| 149 | + |
| 150 | + // Test with nil values where allowed (using pgtype for nullable fields) |
| 151 | + product2, err := q.CreateProduct(ctx, CreateProductParams{ |
| 152 | + Name: "Another Product", |
| 153 | + Price: "29.99", |
| 154 | + Tags: nil, |
| 155 | + Metadata: nil, |
| 156 | + }) |
| 157 | + if err != nil { |
| 158 | + t.Fatal(err) |
| 159 | + } |
| 160 | + t.Logf("Created product with nil arrays: %+v", product2) |
| 161 | + |
| 162 | + // Cleanup |
| 163 | + _ = q.DeleteProduct(ctx, product.ID) |
| 164 | + _ = q.DeleteProduct(ctx, product2.ID) |
| 165 | +} |
0 commit comments