22# SPDX-License-Identifier: MIT
33
44import datetime
5+ from typing import Dict , List
6+
57import responses
6- import pytest
7- from typing import Any , Dict , List , Optional
8- import requests
98
10- from ogr .abstract import CommitStatus , CommitFlag
9+ from ogr .abstract import CommitFlag , CommitStatus
1110from ogr .services .forgejo .commit_flag import ForgejoCommitFlag
1211
12+
1313class MockProject :
1414 forge_api_url = "http://dummy-forgejo/api/v1"
1515 owner = "dummy_owner"
@@ -18,28 +18,31 @@ class MockProject:
1818 def get_auth_header (self ) -> Dict [str , str ]:
1919 return {"Authorization" : "Bearer dummy_token" }
2020
21+
2122@responses .activate
2223def test_get_commit_flag_integration ():
2324 project = MockProject ()
2425 commit = "abcdef123456"
2526 url = f"{ project .forge_api_url } /repos/{ project .owner } /{ project .repo } /commits/{ commit } /statuses"
26-
27+
2728 # Dummy response data simulating Forgejo API output.
28- dummy_response = [{
29- "commit" : commit ,
30- "state" : "success" ,
31- "context" : "CI" ,
32- "comment" : "All tests passed" ,
33- "id" : "123" ,
34- "url" : "http://dummy-forgejo/commit/abcdef123456/status" ,
35- "created" : "2023-01-01T12:00:00Z" ,
36- "updated" : "2023-01-01T12:30:00Z"
37- }]
29+ dummy_response = [
30+ {
31+ "commit" : commit ,
32+ "state" : "success" ,
33+ "context" : "CI" ,
34+ "comment" : "All tests passed" ,
35+ "id" : "123" ,
36+ "url" : "http://dummy-forgejo/commit/abcdef123456/status" ,
37+ "created" : "2023-01-01T12:00:00Z" ,
38+ "updated" : "2023-01-01T12:30:00Z" ,
39+ },
40+ ]
3841 responses .add (responses .GET , url , json = dummy_response , status = 200 )
39-
42+
4043 # Call the method under test.
4144 flags : List [CommitFlag ] = ForgejoCommitFlag .get (project , commit )
42-
45+
4346 # Assertions using CommitStatus from packit.ogr.abstract.
4447 assert len (flags ) == 1
4548 flag = flags [0 ]
@@ -48,18 +51,23 @@ def test_get_commit_flag_integration():
4851 assert flag .context == "CI"
4952 assert flag .comment == "All tests passed"
5053 assert flag .uid == "123"
51-
52- expected_created = datetime .datetime .strptime ("2023-01-01T12:00:00Z" , "%Y-%m-%dT%H:%M:%SZ" )
53- expected_updated = datetime .datetime .strptime ("2023-01-01T12:30:00Z" , "%Y-%m-%dT%H:%M:%SZ" )
54+
55+ expected_created = datetime .datetime .strptime (
56+ "2023-01-01T12:00:00Z" , "%Y-%m-%dT%H:%M:%SZ" ,
57+ )
58+ expected_updated = datetime .datetime .strptime (
59+ "2023-01-01T12:30:00Z" , "%Y-%m-%dT%H:%M:%SZ" ,
60+ )
5461 assert flag .created == expected_created
5562 assert flag .edited == expected_updated
5663
64+
5765@responses .activate
5866def test_set_commit_flag_integration ():
5967 project = MockProject ()
6068 commit = "abcdef123456"
6169 url = f"{ project .forge_api_url } /repos/{ project .owner } /{ project .repo } /commits/{ commit } /statuses"
62-
70+
6371 # Dummy response for setting a commit status.
6472 dummy_response = {
6573 "commit" : commit ,
@@ -69,28 +77,32 @@ def test_set_commit_flag_integration():
6977 "id" : "456" ,
7078 "url" : "http://dummy-forgejo/commit/abcdef123456/status" ,
7179 "created" : "2023-02-01T12:00:00Z" ,
72- "updated" : "2023-02-01T12:30:00Z"
80+ "updated" : "2023-02-01T12:30:00Z" ,
7381 }
7482 responses .add (responses .POST , url , json = dummy_response , status = 200 )
75-
83+
7684 # Call the set method to create a new commit flag.
7785 flag = ForgejoCommitFlag .set (
7886 project = project ,
7987 commit = commit ,
8088 state = CommitStatus .success ,
8189 target_url = "http://dummy-target" ,
8290 description = "Build succeeded" ,
83- context = "CI"
91+ context = "CI" ,
8492 )
85-
93+
8694 # Assertions to verify correct mapping using CommitStatus.
8795 assert flag .commit == commit
8896 assert flag .state == CommitStatus .success
8997 assert flag .context == "CI"
9098 assert flag .comment == "Build succeeded"
9199 assert flag .uid == "456"
92-
93- expected_created = datetime .datetime .strptime ("2023-02-01T12:00:00Z" , "%Y-%m-%dT%H:%M:%SZ" )
94- expected_updated = datetime .datetime .strptime ("2023-02-01T12:30:00Z" , "%Y-%m-%dT%H:%M:%SZ" )
100+
101+ expected_created = datetime .datetime .strptime (
102+ "2023-02-01T12:00:00Z" , "%Y-%m-%dT%H:%M:%SZ" ,
103+ )
104+ expected_updated = datetime .datetime .strptime (
105+ "2023-02-01T12:30:00Z" , "%Y-%m-%dT%H:%M:%SZ" ,
106+ )
95107 assert flag .created == expected_created
96108 assert flag .edited == expected_updated
0 commit comments