@@ -316,18 +316,24 @@ func TestGetVariableByKeyWithMissingVariableError(t *testing.T) {
316316}
317317
318318func TestGetAttributeByKey (t * testing.T ) {
319- id := "id"
320319 key := "key"
321- attributeKeyToIDMap := make (map [string ]string )
322- attributeKeyToIDMap [key ] = id
323-
324320 attribute := entities.Attribute {
325321 Key : key ,
326322 }
323+
324+ // The old and new mappings to ensure backward compatibility
325+ attributeKeyMap := make (map [string ]entities.Attribute )
326+ attributeKeyMap [key ] = attribute
327+
328+ id := "id"
329+ attributeKeyToIDMap := make (map [string ]string )
330+ attributeKeyToIDMap [key ] = id
331+
327332 attributeMap := make (map [string ]entities.Attribute )
328333 attributeMap [id ] = attribute
329334
330335 config := & DatafileProjectConfig {
336+ attributeKeyMap : attributeKeyMap ,
331337 attributeKeyToIDMap : attributeKeyToIDMap ,
332338 attributeMap : attributeMap ,
333339 }
@@ -568,3 +574,129 @@ func TestGetFlagVariationsMap(t *testing.T) {
568574 assert .NotNil (t , flagVariationsMap ["feature_3" ])
569575 assert .Len (t , flagVariationsMap ["feature_3" ], 0 )
570576}
577+
578+ func TestCmabExperiments (t * testing.T ) {
579+ // Load the decide-test-datafile.json
580+ absPath , _ := filepath .Abs ("../../../test-data/decide-test-datafile.json" )
581+ datafile , err := os .ReadFile (absPath )
582+ assert .NoError (t , err )
583+
584+ // Parse the datafile to modify it
585+ var datafileJSON map [string ]interface {}
586+ err = json .Unmarshal (datafile , & datafileJSON )
587+ assert .NoError (t , err )
588+
589+ // Add CMAB to the first experiment with traffic allocation as an integer
590+ experiments := datafileJSON ["experiments" ].([]interface {})
591+ exp0 := experiments [0 ].(map [string ]interface {})
592+ exp0 ["cmab" ] = map [string ]interface {}{
593+ "attributes" : []string {"808797688" , "808797689" },
594+ "trafficAllocation" : 5000 , // Changed from array to integer
595+ }
596+
597+ // Convert back to JSON
598+ modifiedDatafile , err := json .Marshal (datafileJSON )
599+ assert .NoError (t , err )
600+
601+ // Create project config from modified datafile
602+ config , err := NewDatafileProjectConfig (modifiedDatafile , logging .GetLogger ("" , "DatafileProjectConfig" ))
603+ assert .NoError (t , err )
604+
605+ // Get the experiment key from the datafile
606+ exp0Key := exp0 ["key" ].(string )
607+
608+ // Test that Cmab fields are correctly mapped for experiment 0
609+ experiment0 , err := config .GetExperimentByKey (exp0Key )
610+ assert .NoError (t , err )
611+ assert .NotNil (t , experiment0 .Cmab )
612+ if experiment0 .Cmab != nil {
613+ // Test attribute IDs
614+ assert .Equal (t , 2 , len (experiment0 .Cmab .AttributeIds ))
615+ assert .Contains (t , experiment0 .Cmab .AttributeIds , "808797688" )
616+ assert .Contains (t , experiment0 .Cmab .AttributeIds , "808797689" )
617+
618+ // Test traffic allocation as integer
619+ assert .Equal (t , 5000 , experiment0 .Cmab .TrafficAllocation )
620+ }
621+ }
622+
623+ func TestCmabExperimentsNil (t * testing.T ) {
624+ // Load the decide-test-datafile.json (which doesn't have CMAB by default)
625+ absPath , _ := filepath .Abs ("../../../test-data/decide-test-datafile.json" )
626+ datafile , err := os .ReadFile (absPath )
627+ assert .NoError (t , err )
628+
629+ // Create project config from the original datafile
630+ config , err := NewDatafileProjectConfig (datafile , logging .GetLogger ("" , "DatafileProjectConfig" ))
631+ assert .NoError (t , err )
632+
633+ // Parse the datafile to get experiment keys
634+ var datafileJSON map [string ]interface {}
635+ err = json .Unmarshal (datafile , & datafileJSON )
636+ assert .NoError (t , err )
637+
638+ experiments := datafileJSON ["experiments" ].([]interface {})
639+ exp0 := experiments [0 ].(map [string ]interface {})
640+ exp0Key := exp0 ["key" ].(string )
641+
642+ // Test that Cmab field is nil for experiment 0
643+ experiment0 , err := config .GetExperimentByKey (exp0Key )
644+ assert .NoError (t , err )
645+ assert .Nil (t , experiment0 .Cmab , "CMAB field should be nil when not present in datafile" )
646+
647+ // Test another experiment if available
648+ if len (experiments ) > 1 {
649+ exp1 := experiments [1 ].(map [string ]interface {})
650+ exp1Key := exp1 ["key" ].(string )
651+
652+ experiment1 , err := config .GetExperimentByKey (exp1Key )
653+ assert .NoError (t , err )
654+ assert .Nil (t , experiment1 .Cmab , "CMAB field should be nil when not present in datafile" )
655+ }
656+ }
657+
658+ func TestGetAttributeKeyByID (t * testing.T ) {
659+ // Setup
660+ id := "id"
661+ key := "key"
662+ attributeIDToKeyMap := make (map [string ]string )
663+ attributeIDToKeyMap [id ] = key
664+
665+ config := & DatafileProjectConfig {
666+ attributeIDToKeyMap : attributeIDToKeyMap ,
667+ }
668+
669+ // Test successful case
670+ actual , err := config .GetAttributeKeyByID (id )
671+ assert .Nil (t , err )
672+ assert .Equal (t , key , actual )
673+ }
674+
675+ func TestGetAttributeKeyByIDWithMissingIDError (t * testing.T ) {
676+ // Setup
677+ config := & DatafileProjectConfig {}
678+
679+ // Test error case
680+ _ , err := config .GetAttributeKeyByID ("id" )
681+ if assert .Error (t , err ) {
682+ assert .Equal (t , fmt .Errorf (`attribute with ID "id" not found` ), err )
683+ }
684+ }
685+
686+ func TestGetAttributeByKeyWithDirectMapping (t * testing.T ) {
687+ key := "key"
688+ attribute := entities.Attribute {
689+ Key : key ,
690+ }
691+
692+ attributeKeyMap := make (map [string ]entities.Attribute )
693+ attributeKeyMap [key ] = attribute
694+
695+ config := & DatafileProjectConfig {
696+ attributeKeyMap : attributeKeyMap ,
697+ }
698+
699+ actual , err := config .GetAttributeByKey (key )
700+ assert .Nil (t , err )
701+ assert .Equal (t , attribute , actual )
702+ }
0 commit comments