Skip to content

Commit

Permalink
add CI tests and update build.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Jul 25, 2024
1 parent b76f091 commit 2c69e4f
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
with:
version: latest
test:
environment: Unit Test
runs-on: 'ubuntu-latest'
steps:
- name: Checkout
Expand All @@ -35,5 +36,10 @@ jobs:
go-version: '1.17'
check-latest: true
- name: Test
env:
API_KEY: ${{ secrets.API_KEY }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
EU_API_KEY: ${{ secrets.EU_API_KEY }}
EU_SECRET_KEY: ${{ secrets.EU_SECRET_KEY }}
run: |
go test ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
xpmt
.DS_Store
cmd/xpmt/bin/
pkg/experiment/local/.env
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require github.com/spaolacci/murmur3 v1.1.0
require (
github.com/amplitude/analytics-go v1.0.1
github.com/jarcoal/httpmock v1.3.1
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
46 changes: 46 additions & 0 deletions pkg/experiment/local/client_eu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package local

import (
"github.com/amplitude/experiment-go-server/pkg/experiment"
"github.com/joho/godotenv"
"log"
"os"
"testing"
)

var clientEU *Client

func init() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
projectApiKey := os.Getenv("EU_API_KEY")
secretKey := os.Getenv("EU_SECRET_KEY")
cohortSyncConfig := CohortSyncConfig{
ApiKey: projectApiKey,
SecretKey: secretKey,
}
clientEU = Initialize("server-Qlp7XiSu6JtP2S3JzA95PnP27duZgQCF",
&Config{CohortSyncConfig: &cohortSyncConfig, ServerZone: "eu"})
err = clientEU.Start()
if err != nil {
panic(err)
}
}

func TestEvaluateV2CohortEU(t *testing.T) {
user := &experiment.User{UserId: "1", DeviceId: "0"}
flagKeys := []string{"sdk-local-evaluation-user-cohort"}
result, err := clientEU.EvaluateV2(user, flagKeys)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
variant := result["sdk-local-evaluation-user-cohort"]
if variant.Key != "on" {
t.Fatalf("Unexpected variant %v", variant)
}
if variant.Value != "on" {
t.Fatalf("Unexpected variant %v", variant)
}
}
56 changes: 53 additions & 3 deletions pkg/experiment/local/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ package local

import (
"github.com/amplitude/experiment-go-server/pkg/experiment"
"github.com/joho/godotenv"
"log"
"os"
"testing"
)

var client *Client

func init() {
client = Initialize("server-qz35UwzJ5akieoAdIgzM4m9MIiOLXLoz", nil)
err := client.Start()
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
projectApiKey := os.Getenv("API_KEY")
secretKey := os.Getenv("SECRET_KEY")
cohortSyncConfig := CohortSyncConfig{
ApiKey: projectApiKey,
SecretKey: secretKey,
}
client = Initialize("server-qz35UwzJ5akieoAdIgzM4m9MIiOLXLoz",
&Config{CohortSyncConfig: &cohortSyncConfig})
err = client.Start()
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -52,7 +66,6 @@ func TestEvaluate(t *testing.T) {
}
}


func TestEvaluateV2AllFlags(t *testing.T) {
user := &experiment.User{UserId: "test_user"}
result, err := client.EvaluateV2(user, nil)
Expand Down Expand Up @@ -157,3 +170,40 @@ func TestFlagMetadataLocalFlagKey(t *testing.T) {
t.Fatalf("Unexpected metadata %v", md)
}
}

func TestEvaluateV2Cohort(t *testing.T) {
user := &experiment.User{UserId: "12345"}
flagKeys := []string{"sdk-local-evaluation-user-cohort-ci-test"}
result, err := client.EvaluateV2(user, flagKeys)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
variant := result["sdk-local-evaluation-user-cohort-ci-test"]
if variant.Key != "on" {
t.Fatalf("Unexpected variant %v", variant)
}
if variant.Value != "on" {
t.Fatalf("Unexpected variant %v", variant)
}
}

func TestEvaluateV2GroupCohort(t *testing.T) {
user := &experiment.User{
UserId: "12345",
DeviceId: "device_id",
Groups: map[string][]string{
"org id": {"1"},
}}
flagKeys := []string{"sdk-local-evaluation-group-cohort-ci-test"}
result, err := client.EvaluateV2(user, flagKeys)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
variant := result["sdk-local-evaluation-group-cohort-ci-test"]
if variant.Key != "on" {
t.Fatalf("Unexpected variant %v", variant)
}
if variant.Value != "on" {
t.Fatalf("Unexpected variant %v", variant)
}
}
2 changes: 1 addition & 1 deletion pkg/experiment/local/flag_config_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func newFlagConfigApiV2(deploymentKey, serverURL string, flagConfigPollerRequest

func (a *flagConfigApiV2) getFlagConfigs() (map[string]*evaluation.Flag, error) {
client := &http.Client{}
endpoint, err := url.Parse("https://api.lab.amplitude.com/")
endpoint, err := url.Parse(a.ServerURL)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2c69e4f

Please sign in to comment.