Skip to content

Commit

Permalink
first test
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishraj committed Jan 13, 2024
1 parent d1cd93e commit 298a670
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 19 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: lint-test-build
name: lint
on:
push:
branches:
Expand Down Expand Up @@ -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


24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 1 addition & 7 deletions client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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


60 changes: 59 additions & 1 deletion client/sendData/sendData_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 298a670

Please sign in to comment.