From 0bd254161b933e6a2cff73e76237553c5c31ce48 Mon Sep 17 00:00:00 2001 From: GD Radloff Date: Thu, 21 Sep 2023 16:44:52 +0200 Subject: [PATCH 1/9] Added update to main and fixed test --- api/src/loadshedding.rs | 10 +++++----- api/src/main.rs | 1 - api/src/tests.rs | 4 +--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/api/src/loadshedding.rs b/api/src/loadshedding.rs index 6ff64107..a53a7dfe 100644 --- a/api/src/loadshedding.rs +++ b/api/src/loadshedding.rs @@ -742,7 +742,7 @@ impl MunicipalityEntity { // filter schedules to relevant ones for schedule in unfiltered_schedules { let keep = schedule.is_within_timeslot(&time_to_search); - println!("{:?}", time_to_search.hour()); + //println!("{:?}", time_to_search.hour()); if keep { schedules.push(schedule); } @@ -850,10 +850,10 @@ impl SuburbEntity { .unwrap() .with_minute(0) .unwrap(); - println!("{:?}", time_now.timestamp()); + //println!("{:?}", time_now.timestamp()); let mut response: Vec = Vec::new(); let day_in_future = - get_date_time(Some((Local::now() + chrono::Duration::days(1)).timestamp())); + get_date_time(Some((get_date_time(time) + chrono::Duration::days(1)).timestamp())); let (group, mut all_stages, schedule) = match self .collect_information(&time_now.timestamp(), connection, db_functions) @@ -865,7 +865,7 @@ impl SuburbEntity { all_stages.reverse(); let mut time_to_search = time_now; - println!("{:?}", time_to_search.timestamp()); + //println!("{:?}", time_to_search.timestamp()); while time_to_search < day_in_future { let day = time_to_search.day() as i32; let time_slots: Vec = schedule @@ -1326,7 +1326,7 @@ impl<'de> Deserialize<'de> for SASTDateTime { // hack for now because library is not being co-operative let convert_to_sast = dt.timestamp() - 2*3600; let sast = get_date_time(Some(convert_to_sast)); - println!("{:?}", sast); + //println!("{:?}", sast); Ok(SASTDateTime(sast)) } } diff --git a/api/src/main.rs b/api/src/main.rs index 0c61baa1..3e00a53f 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -235,7 +235,6 @@ async fn build_rocket() -> Rocket { "/api-docs", FileServer::new("api-docs", rocket::fs::Options::IndexFile), ) - .attach(StageUpdater) .attach(cors.clone()) .manage::>(None) }; diff --git a/api/src/tests.rs b/api/src/tests.rs index fcef8e7f..7ba28574 100644 --- a/api/src/tests.rs +++ b/api/src/tests.rs @@ -181,7 +181,6 @@ async fn test_getstats() { assert_eq!(result,expected_output); } -/* #[rocket::async_test] async fn test_ai_endpoint() { let client = Client::tracked(build_rocket().await) @@ -201,7 +200,6 @@ async fn test_ai_endpoint() { assert!(body.success); } -*/ #[rocket::async_test] async fn test_loadshedding_helpers() { @@ -650,7 +648,7 @@ const TEST_SUBURB_DATA: &'static str = r#"{ }"#; const TEST_GETSTATS_EXPECTED_RESULT: &'static str = "{\"totalTime\":{\"on\":2520,\"off\":7560},\"perDayTimes\":{\"Sun\":{\"on\":360,\"off\":1080},\"Mon\":{\"on\":360,\"off\":1080},\"Sat\":{\"on\":360,\"off\":1080},\"Tue\":{\"on\":360,\"off\":1080},\"Thu\":{\"on\":360,\"off\":1080},\"Fri\":{\"on\":360,\"off\":1080},\"Wed\":{\"on\":360,\"off\":1080}},\"suburb\":{\"_id\":{\"$oid\":\"64b6b9b30d09aa7756061b30\"},\"municipality\":{\"$oid\":\"64b6b9b30d09aa7756061a47\"},\"name\":\"MUCKLENEUK\",\"geometry\":[1]}}"; -const TEST_GETSCHEDULE_EXPECTED_RESULT: &'static str = "{\"timesOff\":[{\"start\":1694743200,\"end\":1694665800},{\"start\":1694750400,\"end\":1694673000},{\"start\":1694757600,\"end\":1694680200},{\"start\":1694764800,\"end\":1694687400},{\"start\":1694772000,\"end\":1694694600},{\"start\":1694779200,\"end\":1694701800},{\"start\":1694786400,\"end\":1694709000},{\"start\":1694793600,\"end\":1694716200},{\"start\":1694800800,\"end\":1694723400},{\"start\":1694728800,\"end\":1694737800},{\"start\":1694822400,\"end\":1694745000},{\"start\":1694829600,\"end\":1694752200},{\"start\":1694836800,\"end\":1694759400},{\"start\":1694844000,\"end\":1694766600},{\"start\":1694851200,\"end\":1694773800},{\"start\":1694858400,\"end\":1694781000},{\"start\":1694865600,\"end\":1694788200},{\"start\":1694872800,\"end\":1694795400},{\"start\":1694880000,\"end\":1694802600},{\"start\":1694887200,\"end\":1694809800},{\"start\":1694815200,\"end\":1694824200},{\"start\":1694916000,\"end\":1694838600},{\"start\":1694923200,\"end\":1694845800},{\"start\":1695038400,\"end\":1694961000},{\"start\":1694973600,\"end\":1694982600},{\"start\":1695002400,\"end\":1695011400},{\"start\":1695096000,\"end\":1695018600},{\"start\":1695031200,\"end\":1695040200},{\"start\":1695124800,\"end\":1695047400},{\"start\":1695132000,\"end\":1695054600},{\"start\":1695060000,\"end\":1695069000},{\"start\":1695074400,\"end\":1695083400},{\"start\":1695088800,\"end\":1695097800},{\"start\":1695211200,\"end\":1695133800},{\"start\":1695218400,\"end\":1695141000},{\"start\":1695146400,\"end\":1695155400},{\"start\":1695261600,\"end\":1695270600},{\"start\":1695384000,\"end\":1695306600},{\"start\":1695391200,\"end\":1695313800},{\"start\":1695319200,\"end\":1695328200},{\"start\":1695333600,\"end\":1695342600},{\"start\":1695348000,\"end\":1695357000},{\"start\":1695441600,\"end\":1695364200},{\"start\":1695448800,\"end\":1695371400},{\"start\":1695376800,\"end\":1695385800},{\"start\":1695470400,\"end\":1695393000}]}"; +const TEST_GETSCHEDULE_EXPECTED_RESULT: &'static str = "{\"timesOff\":[{\"start\":1694743200,\"end\":1694665800},{\"start\":1694750400,\"end\":1694673000},{\"start\":1694757600,\"end\":1694680200},{\"start\":1694764800,\"end\":1694687400},{\"start\":1694772000,\"end\":1694694600},{\"start\":1694779200,\"end\":1694701800},{\"start\":1694786400,\"end\":1694709000},{\"start\":1694793600,\"end\":1694716200},{\"start\":1694800800,\"end\":1694723400},{\"start\":1694728800,\"end\":1694737800},{\"start\":1694822400,\"end\":1694745000},{\"start\":1694829600,\"end\":1694752200}]}"; const POLYGON_DATA: &'static str = r#"{ "_id": { "$oid": "64b6b9b30d09aa7756061a47" }, "name": "tshwane", From fda16ced063a07e3b1a186331cafe117743982da Mon Sep 17 00:00:00 2001 From: GD Radloff Date: Thu, 21 Sep 2023 18:01:45 +0200 Subject: [PATCH 2/9] Reverted Change to Main and added potential fix --- api/src/loadshedding.rs | 50 +++++++++++++++++++++-------------------- api/src/main.rs | 1 + 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/api/src/loadshedding.rs b/api/src/loadshedding.rs index a53a7dfe..7e804e82 100644 --- a/api/src/loadshedding.rs +++ b/api/src/loadshedding.rs @@ -1099,30 +1099,32 @@ impl SuburbEntity { impl LoadSheddingStage { pub async fn set_stage(&mut self) { // get the next thing from db - let con = &self.db.as_ref().unwrap().database("production"); - let now = get_date_time(None).timestamp(); - let query = doc! { - "startTime" : { - "$lte" : now - } - }; - let filter = doc! { - "startTime" : -1 - }; - let find_options = FindOneOptions::builder().sort(filter).build(); - let new_status: LoadSheddingStage = con - .collection("stage_log") - .find_one(query, find_options) - .await - .unwrap() - .unwrap(); - println!("self is: {:?}", self); - println!("new is: {:?}", new_status); - self.end_time = new_status.end_time; - self.start_time = new_status.start_time; - self.stage = new_status.stage; - println!("self is after operation: {:?}", self); - //println!("{:?}", self); + if let Some(client) = &self.db.as_ref() { + let con = client.database("production"); + let now = get_date_time(None).timestamp(); + let query = doc! { + "startTime" : { + "$lte" : now + } + }; + let filter = doc! { + "startTime" : -1 + }; + let find_options = FindOneOptions::builder().sort(filter).build(); + let new_status: LoadSheddingStage = con + .collection("stage_log") + .find_one(query, find_options) + .await + .unwrap() + .unwrap(); + println!("self is: {:?}", self); + println!("new is: {:?}", new_status); + self.end_time = new_status.end_time; + self.start_time = new_status.start_time; + self.stage = new_status.stage; + println!("self is after operation: {:?}", self); + //println!("{:?}", self); + } } pub async fn request_stage_data_update(&mut self) -> Result { diff --git a/api/src/main.rs b/api/src/main.rs index 3e00a53f..0c61baa1 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -235,6 +235,7 @@ async fn build_rocket() -> Rocket { "/api-docs", FileServer::new("api-docs", rocket::fs::Options::IndexFile), ) + .attach(StageUpdater) .attach(cors.clone()) .manage::>(None) }; From 0848cd622be536cc859cf6a5486a0cdb3f5a7b0f Mon Sep 17 00:00:00 2001 From: GD Radloff Date: Mon, 25 Sep 2023 17:43:48 +0200 Subject: [PATCH 3/9] Fixed Query bug --- api/src/loadshedding.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/loadshedding.rs b/api/src/loadshedding.rs index 7e804e82..5522a537 100644 --- a/api/src/loadshedding.rs +++ b/api/src/loadshedding.rs @@ -1150,7 +1150,7 @@ impl LoadSheddingStage { if let Some(client) = &self.db.as_ref() { let db_con = &client.database("production"); let query = doc! { - "start_time" : -1 + "startTime" : -1 }; let find_options = FindOneOptions::builder().sort(query).build(); From 11fbe0ea7fbf3b75eeda9341d2f2d93992245a39 Mon Sep 17 00:00:00 2001 From: AmberLz Date: Tue, 26 Sep 2023 22:14:18 +0200 Subject: [PATCH 4/9] Updated DEV --- .github/workflows/app-test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/app-test.yml b/.github/workflows/app-test.yml index cd55bb8e..ac764a59 100644 --- a/.github/workflows/app-test.yml +++ b/.github/workflows/app-test.yml @@ -4,6 +4,9 @@ on: branches: - main - dev + push: + branches: + - dev jobs: test: From 088da380a184d8d32c5064f792a8dc97048f6717 Mon Sep 17 00:00:00 2001 From: AmberLz Date: Wed, 27 Sep 2023 08:58:36 +0200 Subject: [PATCH 5/9] Create report.spec.ts --- .../src/app/report/report.spec.ts | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 app/WhereIsThePower/src/app/report/report.spec.ts diff --git a/app/WhereIsThePower/src/app/report/report.spec.ts b/app/WhereIsThePower/src/app/report/report.spec.ts new file mode 100644 index 00000000..199403f0 --- /dev/null +++ b/app/WhereIsThePower/src/app/report/report.spec.ts @@ -0,0 +1,99 @@ +// Import necessary modules and dependencies for testing +import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; +import { ReportPage } from './report.page'; +import { ReportService } from './report.service'; +import { AuthService } from '../authentication/auth.service'; +import { Router } from '@angular/router'; +import { of } from 'rxjs'; + +// Create mock services +const mockReportService = { + reportIssue: (reportType: string) => { + // Simulate a successful response with a status and a message + return of({ status: 'success', message: reportType, ' Report submitted successfully' }); + }, +}; + + +const mockAuthService = { + isUserLoggedIn: () => Promise.resolve(true), // Change to false for testing non-logged-in state +}; + +const mockRouter = { + navigate: jasmine.createSpy('navigate'), +}; + +describe('ReportPage', () => { + let component: ReportPage; + let fixture: ComponentFixture; + let authService: AuthService; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ReportPage], + providers: [ + { provide: ReportService, useValue: mockReportService }, + { provide: AuthService, useValue: mockAuthService }, + { provide: Router, useValue: mockRouter }, + ], + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(ReportPage); + component = fixture.componentInstance; + authService = TestBed.inject(AuthService); // Inject AuthService + + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should set "isLoggedIn" to true when the user is logged in', async () => { + await component.ionViewWillEnter(); + expect(component.isLoggedIn).toBe(true); + }); + + it('should navigate to "/tabs/tab-navigate" after reporting an issue', () => { + const reportType = 'SubstationBlew'; + component.report(reportType); + expect(mockRouter.navigate).toHaveBeenCalledWith(['/tabs/tab-navigate']); + }); + + it('should navigate to "/tabs/tab-profile" when "Go to Profile" button is clicked for non-logged-in users', () => { + authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state + fixture.detectChanges(); + const navigateSpy = spyOn(component['router'], 'navigate'); // Access private router property + const button = fixture.nativeElement.querySelector('ion-button'); + button.click(); + fixture.detectChanges(); + expect(navigateSpy).toHaveBeenCalledWith(['/tabs/tab-profile']); + }); + + it('should display the "Reporting is only available to registered users." message for non-logged-in users', () => { + authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state + fixture.detectChanges(); + const message = fixture.nativeElement.querySelector('h3'); + expect(message.textContent).toContain('Reporting is only available to registered users.'); + }); + + it('should display report options for logged-in users', () => { + authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state + fixture.detectChanges(); + const reportOptions = fixture.nativeElement.querySelectorAll('ion-col'); + expect(reportOptions.length).toBeGreaterThan(0); + }); + + it('should report "SubstationBlew" when the corresponding card is clicked', fakeAsync(() => { + authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state + fixture.detectChanges(); + const reportType = 'SubstationBlew'; + const reportSpy = spyOn(mockReportService, 'reportIssue').and.returnValue(of({})); + const card = fixture.nativeElement.querySelector('ion-card'); + card.click(); + tick(); + expect(reportSpy).toHaveBeenCalledWith(reportType); + })); +}); From 1316b7f938ed30b1e546e5584ca3443f5e27d352 Mon Sep 17 00:00:00 2001 From: AmberLz Date: Wed, 27 Sep 2023 09:01:49 +0200 Subject: [PATCH 6/9] Delete report.spec.ts --- .../src/app/report/report.spec.ts | 99 ------------------- 1 file changed, 99 deletions(-) delete mode 100644 app/WhereIsThePower/src/app/report/report.spec.ts diff --git a/app/WhereIsThePower/src/app/report/report.spec.ts b/app/WhereIsThePower/src/app/report/report.spec.ts deleted file mode 100644 index 199403f0..00000000 --- a/app/WhereIsThePower/src/app/report/report.spec.ts +++ /dev/null @@ -1,99 +0,0 @@ -// Import necessary modules and dependencies for testing -import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; -import { ReportPage } from './report.page'; -import { ReportService } from './report.service'; -import { AuthService } from '../authentication/auth.service'; -import { Router } from '@angular/router'; -import { of } from 'rxjs'; - -// Create mock services -const mockReportService = { - reportIssue: (reportType: string) => { - // Simulate a successful response with a status and a message - return of({ status: 'success', message: reportType, ' Report submitted successfully' }); - }, -}; - - -const mockAuthService = { - isUserLoggedIn: () => Promise.resolve(true), // Change to false for testing non-logged-in state -}; - -const mockRouter = { - navigate: jasmine.createSpy('navigate'), -}; - -describe('ReportPage', () => { - let component: ReportPage; - let fixture: ComponentFixture; - let authService: AuthService; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [ReportPage], - providers: [ - { provide: ReportService, useValue: mockReportService }, - { provide: AuthService, useValue: mockAuthService }, - { provide: Router, useValue: mockRouter }, - ], - }).compileComponents(); - }); - - beforeEach(() => { - fixture = TestBed.createComponent(ReportPage); - component = fixture.componentInstance; - authService = TestBed.inject(AuthService); // Inject AuthService - - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); - - it('should set "isLoggedIn" to true when the user is logged in', async () => { - await component.ionViewWillEnter(); - expect(component.isLoggedIn).toBe(true); - }); - - it('should navigate to "/tabs/tab-navigate" after reporting an issue', () => { - const reportType = 'SubstationBlew'; - component.report(reportType); - expect(mockRouter.navigate).toHaveBeenCalledWith(['/tabs/tab-navigate']); - }); - - it('should navigate to "/tabs/tab-profile" when "Go to Profile" button is clicked for non-logged-in users', () => { - authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state - fixture.detectChanges(); - const navigateSpy = spyOn(component['router'], 'navigate'); // Access private router property - const button = fixture.nativeElement.querySelector('ion-button'); - button.click(); - fixture.detectChanges(); - expect(navigateSpy).toHaveBeenCalledWith(['/tabs/tab-profile']); - }); - - it('should display the "Reporting is only available to registered users." message for non-logged-in users', () => { - authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state - fixture.detectChanges(); - const message = fixture.nativeElement.querySelector('h3'); - expect(message.textContent).toContain('Reporting is only available to registered users.'); - }); - - it('should display report options for logged-in users', () => { - authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state - fixture.detectChanges(); - const reportOptions = fixture.nativeElement.querySelectorAll('ion-col'); - expect(reportOptions.length).toBeGreaterThan(0); - }); - - it('should report "SubstationBlew" when the corresponding card is clicked', fakeAsync(() => { - authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state - fixture.detectChanges(); - const reportType = 'SubstationBlew'; - const reportSpy = spyOn(mockReportService, 'reportIssue').and.returnValue(of({})); - const card = fixture.nativeElement.querySelector('ion-card'); - card.click(); - tick(); - expect(reportSpy).toHaveBeenCalledWith(reportType); - })); -}); From 856a0444f4bdc9cb59b8ef3223025429437b93b7 Mon Sep 17 00:00:00 2001 From: AmberLz Date: Wed, 27 Sep 2023 09:02:46 +0200 Subject: [PATCH 7/9] Update report.spec.ts --- .../src/app/report/report.spec.ts | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 app/WhereIsThePower/src/app/report/report.spec.ts diff --git a/app/WhereIsThePower/src/app/report/report.spec.ts b/app/WhereIsThePower/src/app/report/report.spec.ts new file mode 100644 index 00000000..b169b38a --- /dev/null +++ b/app/WhereIsThePower/src/app/report/report.spec.ts @@ -0,0 +1,100 @@ +@@ -1, 99 + 0, 0 @@ +// Import necessary modules and dependencies for testing +import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; +import { ReportPage } from './report.page'; +import { ReportService } from './report.service'; +import { AuthService } from '../authentication/auth.service'; +import { Router } from '@angular/router'; +import { of } from 'rxjs'; + +// Create mock services +const mockReportService = { + reportIssue: (reportType: string) => { + // Simulate a successful response with a status and a message + return of({ status: 'success', message: ' Report submitted successfully' }); + }, +}; + + +const mockAuthService = { + isUserLoggedIn: () => Promise.resolve(true), // Change to false for testing non-logged-in state +}; + +const mockRouter = { + navigate: jasmine.createSpy('navigate'), +}; + +describe('ReportPage', () => { + let component: ReportPage; + let fixture: ComponentFixture; + let authService: AuthService; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ReportPage], + providers: [ + { provide: ReportService, useValue: mockReportService }, + { provide: AuthService, useValue: mockAuthService }, + { provide: Router, useValue: mockRouter }, + ], + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(ReportPage); + component = fixture.componentInstance; + authService = TestBed.inject(AuthService); // Inject AuthService + + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should set "isLoggedIn" to true when the user is logged in', async () => { + await component.ionViewWillEnter(); + expect(component.isLoggedIn).toBe(true); + }); + + it('should navigate to "/tabs/tab-navigate" after reporting an issue', () => { + const reportType = 'SubstationBlew'; + component.report(reportType); + expect(mockRouter.navigate).toHaveBeenCalledWith(['/tabs/tab-navigate']); + }); + + it('should navigate to "/tabs/tab-profile" when "Go to Profile" button is clicked for non-logged-in users', () => { + authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state + fixture.detectChanges(); + const navigateSpy = spyOn(component['router'], 'navigate'); // Access private router property + const button = fixture.nativeElement.querySelector('ion-button'); + button.click(); + fixture.detectChanges(); + expect(navigateSpy).toHaveBeenCalledWith(['/tabs/tab-profile']); + }); + + it('should display the "Reporting is only available to registered users." message for non-logged-in users', () => { + authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state + fixture.detectChanges(); + const message = fixture.nativeElement.querySelector('h3'); + expect(message.textContent).toContain('Reporting is only available to registered users.'); + }); + + it('should display report options for logged-in users', () => { + authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state + fixture.detectChanges(); + const reportOptions = fixture.nativeElement.querySelectorAll('ion-col'); + expect(reportOptions.length).toBeGreaterThan(0); + }); + + it('should report "SubstationBlew" when the corresponding card is clicked', fakeAsync(() => { + authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state + fixture.detectChanges(); + const reportType = 'SubstationBlew'; + const reportSpy = spyOn(mockReportService, 'reportIssue').and.returnValue(of({})); + const card = fixture.nativeElement.querySelector('ion-card'); + card.click(); + tick(); + expect(reportSpy).toHaveBeenCalledWith(reportType); + })); +}); From 39fa18b48e31f1073513221456d6d4e56c90a8a9 Mon Sep 17 00:00:00 2001 From: AmberLz Date: Wed, 27 Sep 2023 11:54:23 +0200 Subject: [PATCH 8/9] Revert "Update report.spec.ts" This reverts commit 856a0444f4bdc9cb59b8ef3223025429437b93b7. --- .../src/app/report/report.spec.ts | 100 ------------------ 1 file changed, 100 deletions(-) delete mode 100644 app/WhereIsThePower/src/app/report/report.spec.ts diff --git a/app/WhereIsThePower/src/app/report/report.spec.ts b/app/WhereIsThePower/src/app/report/report.spec.ts deleted file mode 100644 index b169b38a..00000000 --- a/app/WhereIsThePower/src/app/report/report.spec.ts +++ /dev/null @@ -1,100 +0,0 @@ -@@ -1, 99 + 0, 0 @@ -// Import necessary modules and dependencies for testing -import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; -import { ReportPage } from './report.page'; -import { ReportService } from './report.service'; -import { AuthService } from '../authentication/auth.service'; -import { Router } from '@angular/router'; -import { of } from 'rxjs'; - -// Create mock services -const mockReportService = { - reportIssue: (reportType: string) => { - // Simulate a successful response with a status and a message - return of({ status: 'success', message: ' Report submitted successfully' }); - }, -}; - - -const mockAuthService = { - isUserLoggedIn: () => Promise.resolve(true), // Change to false for testing non-logged-in state -}; - -const mockRouter = { - navigate: jasmine.createSpy('navigate'), -}; - -describe('ReportPage', () => { - let component: ReportPage; - let fixture: ComponentFixture; - let authService: AuthService; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [ReportPage], - providers: [ - { provide: ReportService, useValue: mockReportService }, - { provide: AuthService, useValue: mockAuthService }, - { provide: Router, useValue: mockRouter }, - ], - }).compileComponents(); - }); - - beforeEach(() => { - fixture = TestBed.createComponent(ReportPage); - component = fixture.componentInstance; - authService = TestBed.inject(AuthService); // Inject AuthService - - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); - - it('should set "isLoggedIn" to true when the user is logged in', async () => { - await component.ionViewWillEnter(); - expect(component.isLoggedIn).toBe(true); - }); - - it('should navigate to "/tabs/tab-navigate" after reporting an issue', () => { - const reportType = 'SubstationBlew'; - component.report(reportType); - expect(mockRouter.navigate).toHaveBeenCalledWith(['/tabs/tab-navigate']); - }); - - it('should navigate to "/tabs/tab-profile" when "Go to Profile" button is clicked for non-logged-in users', () => { - authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state - fixture.detectChanges(); - const navigateSpy = spyOn(component['router'], 'navigate'); // Access private router property - const button = fixture.nativeElement.querySelector('ion-button'); - button.click(); - fixture.detectChanges(); - expect(navigateSpy).toHaveBeenCalledWith(['/tabs/tab-profile']); - }); - - it('should display the "Reporting is only available to registered users." message for non-logged-in users', () => { - authService.isUserLoggedIn = () => Promise.resolve(false); // Mock non-logged-in state - fixture.detectChanges(); - const message = fixture.nativeElement.querySelector('h3'); - expect(message.textContent).toContain('Reporting is only available to registered users.'); - }); - - it('should display report options for logged-in users', () => { - authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state - fixture.detectChanges(); - const reportOptions = fixture.nativeElement.querySelectorAll('ion-col'); - expect(reportOptions.length).toBeGreaterThan(0); - }); - - it('should report "SubstationBlew" when the corresponding card is clicked', fakeAsync(() => { - authService.isUserLoggedIn = () => Promise.resolve(true); // Mock logged-in state - fixture.detectChanges(); - const reportType = 'SubstationBlew'; - const reportSpy = spyOn(mockReportService, 'reportIssue').and.returnValue(of({})); - const card = fixture.nativeElement.querySelector('ion-card'); - card.click(); - tick(); - expect(reportSpy).toHaveBeenCalledWith(reportType); - })); -}); From 7b0e9a531a60fc6138e243e71bcf6ae5944100ee Mon Sep 17 00:00:00 2001 From: GD Radloff Date: Wed, 27 Sep 2023 12:24:13 +0200 Subject: [PATCH 9/9] Added manual correction --- api/src/loadshedding.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/api/src/loadshedding.rs b/api/src/loadshedding.rs index 5522a537..e472583e 100644 --- a/api/src/loadshedding.rs +++ b/api/src/loadshedding.rs @@ -205,6 +205,7 @@ pub struct LoadSheddingStage { #[serde(skip_serializing, skip_deserializing)] db: Option, stage: i32, + pub update: Option } #[derive(Debug, Deserialize, Clone)] @@ -653,6 +654,7 @@ impl LoadsheddingData { end_time: self.end.0.timestamp(), db: None, stage: self.stage, + update: Some(true) } } } @@ -1168,6 +1170,7 @@ impl LoadSheddingStage { start_time: 0, end_time: 0, db: None, + update: Some(true) }, }; let latest_in_db = result.start_time; @@ -1210,7 +1213,11 @@ impl LoadSheddingStage { mongodb::options::UpdateModifications::Document(doc! { "$set" : {"stage" : new_data.stage} }); - let _ = db_data.update(update, db_con).await; + if let Some(update_value) = db_data.update { + if update_value { + let _ = db_data.update(update, db_con).await; + } + } } } // else if no match @@ -1265,6 +1272,7 @@ impl Fairing for StageUpdater { start_time: 0, end_time: 0, db: None, + update: Some(true) })); let rocket = rocket.manage(Some(stage_info)); Ok(rocket)