From 298a6706bddb36c9c4ebedd5b12a15e8ce2459e1 Mon Sep 17 00:00:00 2001 From: devashishRaj <29112354+devashishRaj@users.noreply.github.com> Date: Sat, 13 Jan 2024 18:50:20 +0530 Subject: [PATCH] first test --- .github/workflows/build.yml | 33 ++++++++++ .../{lint_test_build.yml => lint.yml} | 13 +--- .github/workflows/test.yml | 24 ++++++++ client/Makefile | 8 +-- client/sendData/sendData_test.go | 60 ++++++++++++++++++- 5 files changed, 119 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/build.yml rename .github/workflows/{lint_test_build.yml => lint.yml} (75%) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..88ebc17 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,33 @@ +name: build +on: + push: + branches: + - '**' + pull_request: + +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read + +jobs: + goBuild: + name: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.21' + cache: false + - name: tidy + run: | + go clean -modcache + go mod tidy + - name: server build + run: go build server/main.go + - name: client build + run: | + cd client + make build-all + \ No newline at end of file diff --git a/.github/workflows/lint_test_build.yml b/.github/workflows/lint.yml similarity index 75% rename from .github/workflows/lint_test_build.yml rename to .github/workflows/lint.yml index b1d79ae..891a31d 100644 --- a/.github/workflows/lint_test_build.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: lint-test-build +name: lint on: push: branches: @@ -27,14 +27,5 @@ jobs: # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. version: latest - - name: tidy - run: | - go clean -modcache - go mod tidy - - name: server build - run: go build server/main.go - - name: client build - run: | - cd client - make build-all + \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..fd0a072 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,24 @@ +name: test +on: + push: + branches: + - '**' + pull_request: + +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read + +jobs: + goTest: + name: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: 'stable' + cache: false + - name: test + run: go test ./... -count=1 \ No newline at end of file diff --git a/client/Makefile b/client/Makefile index 06e6c20..70729ff 100644 --- a/client/Makefile +++ b/client/Makefile @@ -6,13 +6,7 @@ build-arm64: build-all: build-arm build-arm64 -tidy: - go mod tidy - .PHONY: build-arm build-arm64 build-all check -# Add a dependency for the build targets to run the tidy target -build-arm: tidy -build-arm64: tidy -build-all: tidy + diff --git a/client/sendData/sendData_test.go b/client/sendData/sendData_test.go index 8caba34..9c89641 100644 --- a/client/sendData/sendData_test.go +++ b/client/sendData/sendData_test.go @@ -1,7 +1,65 @@ package senddata -import "testing" +import ( + "bytes" + "devashishRaj/rpi_telemetry/server/handleError" + "encoding/json" + "io" + "log" + "net/http" + "net/http/httptest" + "reflect" + "testing" +) func TestHttpPost(t *testing.T) { + // Mock server + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Verify request headers + if r.Header.Get("Content-Type") != "application/json" { + t.Errorf("Expected Content-Type header to be application/json, but got %s", r.Header.Get("Content-Type")) + } + // Verify request body + body, err := io.ReadAll(r.Body) + if err != nil { + t.Fatal(err) + } + expectedJSON := []byte(`{"key":"value"}`) // Replace with your expected JSON + if !bytes.Equal(body, expectedJSON) { + t.Errorf("Expected request body to be %s, but got %s", expectedJSON, body) + } + + // Send a dummy response + w.WriteHeader(http.StatusOK) + _, err = w.Write([]byte("OK")) + handleError.CheckError("error in writing dummy responser", err) + })) + + // Close the server when the test is done + defer server.Close() + + // Redirect log output to a buffer for assertions + var buf bytes.Buffer + log.SetOutput(&buf) + + // Call the HttpPost function with the mock server URL and JSON data + URL := server.URL + jsonData := []byte(`{"key":"value"}`) // Replace with your actual JSON data + + // Call the function + HttpPost(URL, jsonData) + + // Verify received JSON data + var receivedData map[string]interface{} + err := json.Unmarshal(jsonData, &receivedData) + if err != nil { + t.Fatal(err) + } + + // Replace with your expected data + expectedData := map[string]interface{}{"key": "value"} + if !reflect.DeepEqual(receivedData, expectedData) { + t.Errorf("Expected received JSON data to be %v, but got %v", expectedData, receivedData) + } }