Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test #753

Merged
merged 9 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/reduce/reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

import (
"github.com/shopspring/decimal"

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

Expand Down
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")
dongzl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading