|
| 1 | +package openrtb_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + . "github.com/bsm/openrtb/v3" |
| 8 | +) |
| 9 | + |
| 10 | +func TestQuantity(t *testing.T) { |
| 11 | + var subject *Quantity |
| 12 | + if err := fixture("quantity", &subject); err != nil { |
| 13 | + t.Fatalf("expected no error, got %+v", err) |
| 14 | + } |
| 15 | + |
| 16 | + exp := &Quantity{ |
| 17 | + Multiplier: 3.14, |
| 18 | + SourceType: MeasurementSourceTypePublisher, |
| 19 | + } |
| 20 | + |
| 21 | + if !reflect.DeepEqual(exp, subject) { |
| 22 | + t.Fatalf("expected %+v, got %+v", exp, subject) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestQuantity_Validate(t *testing.T) { |
| 27 | + subject := Quantity{} |
| 28 | + if got := subject.Validate(); got != ErrMissingMultiplier { |
| 29 | + t.Fatalf("expected %+v, got %+v", ErrMissingMultiplier, got) |
| 30 | + } |
| 31 | + |
| 32 | + // If MeasurementSourceType is MeasurementSourceTypeMeasurementVendor, the |
| 33 | + // Vendor field should not be empty. |
| 34 | + subject = Quantity{ |
| 35 | + Multiplier: 1.0, |
| 36 | + SourceType: MeasurementSourceTypeMeasurementVendor, |
| 37 | + } |
| 38 | + if got := subject.Validate(); got != ErrMissingMeasurementVendor { |
| 39 | + t.Fatalf("expected %+v, got %+v", ErrMissingMeasurementVendor, got) |
| 40 | + } |
| 41 | + |
| 42 | + subject.Vendor = "TestVendor" // Should fix the invalid Quantity |
| 43 | + if got := subject.Validate(); got != nil { |
| 44 | + t.Fatalf("expected no error, got %+v", got) |
| 45 | + } |
| 46 | + |
| 47 | + // All other value from MeasurementSourceType can be used without Vendor |
| 48 | + subject = Quantity{ |
| 49 | + Multiplier: 2.0, |
| 50 | + SourceType: MeasurementSourceTypeUnknown, |
| 51 | + } |
| 52 | + if got := subject.Validate(); got != nil { |
| 53 | + t.Fatalf("expected nil, got %+v", got) |
| 54 | + } |
| 55 | +} |
0 commit comments