Skip to content

Commit

Permalink
chore(test): add unit test (#753)
Browse files Browse the repository at this point in the history
* feat(test): add unit test

* feat(test): add unit test

* feat(test): add unit test

* chore(test): fix code review

* chore(test): fix code review

* chore(test): fix code review

* chore(test): fix code review

* feat(test): add unit test

* chore(test): add header
  • Loading branch information
dongzl authored Sep 4, 2023
1 parent 01c7adf commit ca6a25c
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/util/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package env

import (
"os"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/arana-db/arana/pkg/constants"
)

func TestIsDevelopEnvironment(t *testing.T) {
// Test case 1: Environment variable is set to "1"
os.Setenv(constants.EnvDevelopEnvironment, "1")
isDev := IsDevelopEnvironment()
assert.True(t, isDev)
os.Unsetenv(constants.EnvDevelopEnvironment)

// Test case 2: Environment variable is set to "yes"
os.Setenv(constants.EnvDevelopEnvironment, "yes")
isDev = IsDevelopEnvironment()
assert.True(t, isDev)
os.Unsetenv(constants.EnvDevelopEnvironment)

// Test case 3: Environment variable is set to "on"
os.Setenv(constants.EnvDevelopEnvironment, "on")
isDev = IsDevelopEnvironment()
assert.True(t, isDev)
os.Unsetenv(constants.EnvDevelopEnvironment)

// Test case 4: Environment variable is set to "true"
os.Setenv(constants.EnvDevelopEnvironment, "true")
isDev = IsDevelopEnvironment()
assert.True(t, isDev)
os.Unsetenv(constants.EnvDevelopEnvironment)

// Test case 5: Environment variable is set to any other value
//os.Setenv(constants.EnvDevelopEnvironment, "false")
//isDev = IsDevelopEnvironment()
//if isDev {
// t.Errorf("Expected IsDevelopEnvironment() to return false, but got true")
//}
}
43 changes: 43 additions & 0 deletions pkg/util/file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package file

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestIsYaml(t *testing.T) {
// Test with a valid YAML file
path := "example.yaml"
result := IsYaml(path)
assert.True(t, result)

// Test with a valid YML file
path = "example.yml"
result = IsYaml(path)
assert.True(t, result)

// Test with an invalid file extension
path = "example.txt"
result = IsYaml(path)
assert.False(t, result)
}
57 changes: 57 additions & 0 deletions pkg/util/identity/identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package identity

import (
"os"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestGetNodeIdentity(t *testing.T) {
// Test case 1: Testing when AranaNodeId is set
expectedNodeId := "some-node-id"
os.Setenv("ARANA_NODE_ID", expectedNodeId)
assert.Equal(t, expectedNodeId, GetNodeIdentity())
os.Unsetenv("ARANA_NODE_ID")

// Test case 2: Testing when PodName is set
expectedPodName := "some-pod-name"
os.Setenv("POD_NAME", expectedPodName)
assert.Equal(t, expectedPodName, GetNodeIdentity())
os.Unsetenv("POD_NAME")

// Test case 3: Testing when neither AranaNodeId nor PodName is set
//expectedIP := "some-ip"
//net.FindSelfIP = func() (string, error) {
// return expectedIP, nil
//}
//assert.Equal(t, expectedIP, GetNodeIdentity())
//net.FindSelfIP = nil

// Test case 4: Testing when all values are empty
//expectedUUID := "some-uuid"
//uuid.NewString = func() string {
// return expectedUUID
//}
//assert.Equal(t, expectedUUID, GetNodeIdentity())
//uuid.NewString = nil
}
Loading

0 comments on commit ca6a25c

Please sign in to comment.