Skip to content
This repository was archived by the owner on Mar 22, 2025. It is now read-only.

Commit b59a0fe

Browse files
committed
Added tests for the newly implemented features(Usertemp and REgion controll
1 parent 885c07a commit b59a0fe

File tree

2 files changed

+222
-29
lines changed

2 files changed

+222
-29
lines changed

Comfortstat/Comfortstat_test.go

Lines changed: 163 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ func TestHttpSetMinPrice(t *testing.T) {
207207

208208
// creates a fake request body with JSON data
209209
w = httptest.NewRecorder()
210-
fakebody = bytes.NewReader([]byte(`{"123, "unit": "SEK", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
211-
r = httptest.NewRequest("PUT", "localhost:8670/Comfortstat/Set%20Values/MinPrice", fakebody) // simulating a put request from a user to update the min temp
212-
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
210+
fakebody = bytes.NewReader([]byte(`{"123, "unit": "SEK", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
211+
r = httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/MinPrice", fakebody) // simulating a put request from a user to update the min temp
212+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
213213
ua.httpSetMinPrice(w, r)
214214
// save the rsponse
215215
resp = w.Result()
@@ -218,7 +218,7 @@ func TestHttpSetMinPrice(t *testing.T) {
218218
}
219219
//Good test case: GET
220220
w = httptest.NewRecorder()
221-
r = httptest.NewRequest("GET", "localhost:8670/Comfortstat/Set%20Values/MinPrice", nil)
221+
r = httptest.NewRequest("GET", "http://localhost:8670/Comfortstat/Set%20Values/MinPrice", nil)
222222
goodStatusCode = 200
223223
ua.httpSetMinPrice(w, r)
224224

@@ -245,7 +245,7 @@ func TestHttpSetMinPrice(t *testing.T) {
245245

246246
// force the case to hit default statement but alter the method
247247
w = httptest.NewRecorder()
248-
r = httptest.NewRequest("666", "localhost:8670/Comfortstat/Set%20Values/MinPrice", nil)
248+
r = httptest.NewRequest("666", "http://localhost:8670/Comfortstat/Set%20Values/MinPrice", nil)
249249
ua.httpSetMinPrice(w, r)
250250
//save the response
251251
resp = w.Result()
@@ -260,9 +260,9 @@ func TestHttpSetMaxPrice(t *testing.T) {
260260

261261
// creates a fake request body with JSON data
262262
w := httptest.NewRecorder()
263-
fakebody := bytes.NewReader([]byte(`{"value": 2, "unit": "SEK", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
264-
r := httptest.NewRequest("PUT", "localhost:8670/Comfortstat/Set%20Values/MaxPrice", fakebody) // simulating a put request from a user to update the min temp
265-
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
263+
fakebody := bytes.NewReader([]byte(`{"value": 2, "unit": "SEK", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
264+
r := httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/MaxPrice", fakebody) // simulating a put request from a user to update the min temp
265+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
266266
goodStatusCode := 200
267267
ua.httpSetMaxPrice(w, r)
268268

@@ -275,9 +275,9 @@ func TestHttpSetMaxPrice(t *testing.T) {
275275

276276
// creates a fake request body with JSON data
277277
w = httptest.NewRecorder()
278-
fakebody = bytes.NewReader([]byte(`{"123, "unit": "SEK", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
279-
r = httptest.NewRequest("PUT", "localhost:8670/Comfortstat/Set%20Values/MaxPrice", fakebody) // simulating a put request from a user to update the min temp
280-
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
278+
fakebody = bytes.NewReader([]byte(`{"123, "unit": "SEK", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
279+
r = httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/MaxPrice", fakebody) // simulating a put request from a user to update the min temp
280+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
281281
ua.httpSetMaxPrice(w, r)
282282

283283
// save the rsponse and read the body
@@ -331,9 +331,9 @@ func TestHttpSetDesiredTemp(t *testing.T) {
331331

332332
// creates a fake request body with JSON data
333333
w := httptest.NewRecorder()
334-
fakebody := bytes.NewReader([]byte(`{"value": 0, "unit": "Celsius", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
335-
r := httptest.NewRequest("PUT", "localhost:8670/Comfortstat/Set%20Values/DesiredTemp", fakebody) // simulating a put request from a user to update the min temp
336-
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
334+
fakebody := bytes.NewReader([]byte(`{"value": 0, "unit": "Celsius", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
335+
r := httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/DesiredTemp", fakebody) // simulating a put request from a user to update the min temp
336+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
337337
goodStatusCode := 200
338338

339339
ua.httpSetDesiredTemp(w, r)
@@ -348,9 +348,9 @@ func TestHttpSetDesiredTemp(t *testing.T) {
348348

349349
// creates a fake request body with JSON data
350350
w = httptest.NewRecorder()
351-
fakebody = bytes.NewReader([]byte(`{"123, "unit": "Celsius", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
352-
r = httptest.NewRequest("PUT", "localhost:8670/Comfortstat/Set%20Values/DesiredTemp", fakebody) // simulating a put request from a user to update the min temp
353-
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
351+
fakebody = bytes.NewReader([]byte(`{"123, "unit": "Celsius", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
352+
r = httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/DesiredTemp", fakebody) // simulating a put request from a user to update the min temp
353+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
354354

355355
ua.httpSetDesiredTemp(w, r)
356356
// save the rsponse and read the body
@@ -397,3 +397,149 @@ func TestHttpSetDesiredTemp(t *testing.T) {
397397
t.Errorf("expected the status to be bad but got: %v", resp.StatusCode)
398398
}
399399
}
400+
401+
func TestHttpSetUserTemp(t *testing.T) {
402+
ua := initTemplate().(*UnitAsset)
403+
//Godd test case: PUT
404+
405+
// creates a fake request body with JSON data
406+
w := httptest.NewRecorder()
407+
fakebody := bytes.NewReader([]byte(`{"value": 0, "unit": "Celsius", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
408+
r := httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/userTemp", fakebody) // simulating a put request from a user to update the min temp
409+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
410+
goodStatusCode := 200
411+
412+
ua.httpSetUserTemp(w, r)
413+
414+
// save the rsponse and read the body
415+
resp := w.Result()
416+
if resp.StatusCode != goodStatusCode {
417+
t.Errorf("expected good status code: %v, got %v", goodStatusCode, resp.StatusCode)
418+
}
419+
420+
//BAD case: PUT, if the fake body is formatted incorrectly
421+
422+
// creates a fake request body with JSON data
423+
w = httptest.NewRecorder()
424+
fakebody = bytes.NewReader([]byte(`{"123, "unit": "Celsius", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
425+
r = httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/userTemp", fakebody) // simulating a put request from a user to update the min temp
426+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
427+
428+
ua.httpSetUserTemp(w, r)
429+
// save the rsponse and read the body
430+
resp = w.Result()
431+
if resp.StatusCode == goodStatusCode {
432+
t.Errorf("expected bad status code: %v, got %v", goodStatusCode, resp.StatusCode)
433+
}
434+
//Good test case: GET
435+
w = httptest.NewRecorder()
436+
r = httptest.NewRequest("GET", "http://localhost:8670/Comfortstat/Set%20Values/userTemp", nil)
437+
goodStatusCode = 200
438+
ua.httpSetUserTemp(w, r)
439+
440+
// save the rsponse and read the body
441+
442+
resp = w.Result()
443+
if resp.StatusCode != goodStatusCode {
444+
t.Errorf("expected good status code: %v, got %v", goodStatusCode, resp.StatusCode)
445+
}
446+
body, _ := io.ReadAll(resp.Body)
447+
// this is a simple check if the JSON response contains the specific value/unit/version
448+
value := strings.Contains(string(body), `"value": 0`)
449+
unit := strings.Contains(string(body), `"unit": "Celsius"`)
450+
version := strings.Contains(string(body), `"version": "SignalA_v1.0"`)
451+
452+
if value != true {
453+
t.Errorf("expected the statment to be true!")
454+
}
455+
if unit != true {
456+
t.Errorf("expected the unit statement to be true!")
457+
}
458+
if version != true {
459+
t.Errorf("expected the version statment to be true!")
460+
}
461+
// bad test case: default part of code
462+
463+
// force the case to hit default statement but alter the method
464+
w = httptest.NewRecorder()
465+
r = httptest.NewRequest("666", "http://localhost:8670/Comfortstat/Set%20Values/userTemp", nil)
466+
// calls the method and extracts the response and save is in resp for the upcoming tests
467+
ua.httpSetUserTemp(w, r)
468+
resp = w.Result()
469+
if resp.StatusCode != http.StatusNotFound {
470+
t.Errorf("expected the status to be bad but got: %v", resp.StatusCode)
471+
}
472+
}
473+
474+
func TestHttpSetRegion(t *testing.T) {
475+
ua := initTemplate().(*UnitAsset)
476+
//Godd test case: PUT
477+
478+
// creates a fake request body with JSON data
479+
w := httptest.NewRecorder()
480+
fakebody := bytes.NewReader([]byte(`{"value": 1, "unit": "RegionPoint", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
481+
r := httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/Region", fakebody) // simulating a put request from a user to update the min temp
482+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
483+
goodStatusCode := 200
484+
485+
ua.httpSetRegion(w, r)
486+
487+
// save the rsponse and read the body
488+
resp := w.Result()
489+
if resp.StatusCode != goodStatusCode {
490+
t.Errorf("expected good status code: %v, got %v", goodStatusCode, resp.StatusCode)
491+
}
492+
493+
//BAD case: PUT, if the fake body is formatted incorrectly
494+
495+
// creates a fake request body with JSON data
496+
w = httptest.NewRecorder()
497+
fakebody = bytes.NewReader([]byte(`{"123, "unit": "RegionPoint", "version": "SignalA_v1.0"}`)) // converts the Jason data so it can be read
498+
r = httptest.NewRequest("PUT", "http://localhost:8670/Comfortstat/Set%20Values/Region", fakebody) // simulating a put request from a user to update the min temp
499+
r.Header.Set("Content-Type", "application/json") // basic setup to prevent the request to be rejected.
500+
501+
ua.httpSetRegion(w, r)
502+
// save the rsponse and read the body
503+
resp = w.Result()
504+
if resp.StatusCode == goodStatusCode {
505+
t.Errorf("expected bad status code: %v, got %v", goodStatusCode, resp.StatusCode)
506+
}
507+
//Good test case: GET
508+
w = httptest.NewRecorder()
509+
r = httptest.NewRequest("GET", "http://localhost:8670/Comfortstat/Set%20Values/Region", nil)
510+
goodStatusCode = 200
511+
ua.httpSetRegion(w, r)
512+
513+
// save the rsponse and read the body
514+
515+
resp = w.Result()
516+
if resp.StatusCode != goodStatusCode {
517+
t.Errorf("expected good status code: %v, got %v", goodStatusCode, resp.StatusCode)
518+
}
519+
body, _ := io.ReadAll(resp.Body)
520+
// this is a simple check if the JSON response contains the specific value/unit/version
521+
value := strings.Contains(string(body), `"value": 1`)
522+
unit := strings.Contains(string(body), `"unit": "RegionPoint"`)
523+
version := strings.Contains(string(body), `"version": "SignalA_v1.0"`)
524+
525+
if value != true {
526+
t.Errorf("expected the statment to be true!")
527+
}
528+
if unit != true {
529+
t.Errorf("expected the unit statement to be true!")
530+
}
531+
if version != true {
532+
t.Errorf("expected the version statment to be true!")
533+
}
534+
// bad test case: default part of code
535+
536+
// force the case to hit default statement but alter the method
537+
w = httptest.NewRecorder()
538+
r = httptest.NewRequest("666", "http://localhost:8670/Comfortstat/Set%20Values/Region", nil)
539+
// calls the method and extracts the response and save is in resp for the upcoming tests
540+
ua.httpSetRegion(w, r)
541+
resp = w.Result()
542+
if resp.StatusCode != http.StatusNotFound {
543+
t.Errorf("expected the status to be bad but got: %v", resp.StatusCode)
544+
}
545+
}

Comfortstat/things_test.go

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,43 +116,80 @@ func TestSetmethods(t *testing.T) {
116116
MinTempInputSignal := forms.SignalA_v1a{
117117
Value: 1.0,
118118
}
119-
MaxTempInputSignal := forms.SignalA_v1a{
120-
Value: 29.0,
121-
}
122-
MinPriceInputSignal := forms.SignalA_v1a{
123-
Value: 2.0,
124-
}
125-
MaxPriceInputSignal := forms.SignalA_v1a{
126-
Value: 12.0,
127-
}
128-
DesTempInputSignal := forms.SignalA_v1a{
129-
Value: 23.7,
130-
}
131119
//call and test MinTemp
132120
asset.setMinTemp(MinTempInputSignal)
133121
if asset.MinTemp != 1.0 {
134122
t.Errorf("expected MinTemp to be 1.0, got %f", asset.MinTemp)
135123
}
124+
// Simulate the input signals
125+
MaxTempInputSignal := forms.SignalA_v1a{
126+
Value: 29.0,
127+
}
136128
// call and test MaxTemp
137129
asset.setMaxTemp(MaxTempInputSignal)
138130
if asset.MaxTemp != 29.0 {
139131
t.Errorf("expected MaxTemp to be 25.0, got %f", asset.MaxTemp)
140132
}
133+
// Simulate the input signals
134+
MinPriceInputSignal := forms.SignalA_v1a{
135+
Value: 2.0,
136+
}
141137
//call and test MinPrice
142138
asset.setMinPrice(MinPriceInputSignal)
143139
if asset.MinPrice != 2.0 {
144140
t.Errorf("expected MinPrice to be 2.0, got %f", asset.MinPrice)
145141
}
142+
// Simulate the input signals
143+
MaxPriceInputSignal := forms.SignalA_v1a{
144+
Value: 12.0,
145+
}
146146
//call and test MaxPrice
147147
asset.setMaxPrice(MaxPriceInputSignal)
148148
if asset.MaxPrice != 12.0 {
149149
t.Errorf("expected MaxPrice to be 12.0, got %f", asset.MaxPrice)
150150
}
151+
// Simulate the input signals
152+
DesTempInputSignal := forms.SignalA_v1a{
153+
Value: 23.7,
154+
}
151155
// call and test DesiredTemp
152156
asset.setDesiredTemp(DesTempInputSignal)
153157
if asset.DesiredTemp != 23.7 {
154158
t.Errorf("expected Desierd temprature is to be 23.7, got %f", asset.DesiredTemp)
155159
}
160+
// Simulate the input signal and call the set method for the check
161+
RegionInputSignalSE2 := forms.SignalA_v1a{
162+
Value: 2,
163+
}
164+
asset.setRegion(RegionInputSignalSE2)
165+
if asset.Region != 2.0 {
166+
t.Errorf("expected Region to be SE2 (2), got %f", asset.Region)
167+
}
168+
// Simulate the input signal and call the set method for the check
169+
RegionInputSignalSE3 := forms.SignalA_v1a{
170+
Value: 3,
171+
}
172+
asset.setRegion(RegionInputSignalSE3)
173+
if asset.Region != 3.0 {
174+
t.Errorf("expected Region to be SE3 (3), got %f", asset.Region)
175+
}
176+
// Simulate the input signal and call the set method for the check
177+
RegionInputSignalSE1 := forms.SignalA_v1a{
178+
Value: 1,
179+
}
180+
asset.setRegion(RegionInputSignalSE1)
181+
if asset.Region != 1.0 {
182+
t.Errorf("expected Region to be SE1 (1), got %f", asset.Region)
183+
}
184+
// Simulate the input signal and call the set method for the check
185+
RegionInputSignalSE4 := forms.SignalA_v1a{
186+
Value: 4,
187+
}
188+
asset.setRegion(RegionInputSignalSE4)
189+
if asset.Region != 4.0 {
190+
t.Errorf("expected Region to be SE4 (4), got %f", asset.Region)
191+
}
192+
156193
}
157194

158195
func TestGetMethods(t *testing.T) {
@@ -212,6 +249,16 @@ func TestGetMethods(t *testing.T) {
212249
if result6.Value != uasset.SEKPrice {
213250
t.Errorf("expected electric price is to be %v, got %v", uasset.SEKPrice, result6.Value)
214251
}
252+
////USertemp////
253+
result7 := uasset.getUserTemp()
254+
if result7.Value != uasset.UserTemp {
255+
t.Errorf("expected the Usertemp to be %v, got %v", uasset.UserTemp, result7.Value)
256+
}
257+
////Region////
258+
result8 := uasset.getRegion()
259+
if result8.Value != uasset.Region {
260+
t.Errorf("expected the Region to be %v, got %v", uasset.Region, result8.Value)
261+
}
215262
}
216263

217264
func TestInitTemplate(t *testing.T) {

0 commit comments

Comments
 (0)