1+ # Copyright Contributors to the Packit project.
2+ # SPDX-License-Identifier: MIT
3+
14import datetime
5+ from typing import Dict , List
6+
27import responses
3- import pytest
4- from typing import Any , Dict , List , Optional
5- import requests
68
7- from ogr .abstract import CommitStatus , CommitFlag
9+ from ogr .abstract import CommitFlag , CommitStatus
810from ogr .services .forgejo .commit_flag import ForgejoCommitFlag
911
12+
1013class MockProject :
1114 forge_api_url = "http://dummy-forgejo/api/v1"
1215 owner = "dummy_owner"
@@ -15,28 +18,31 @@ class MockProject:
1518 def get_auth_header (self ) -> Dict [str , str ]:
1619 return {"Authorization" : "Bearer dummy_token" }
1720
21+
1822@responses .activate
1923def test_get_commit_flag_integration ():
2024 project = MockProject ()
2125 commit = "abcdef123456"
2226 url = f"{ project .forge_api_url } /repos/{ project .owner } /{ project .repo } /commits/{ commit } /statuses"
23-
27+
2428 # Dummy response data simulating Forgejo API output.
25- dummy_response = [{
26- "commit" : commit ,
27- "state" : "success" ,
28- "context" : "CI" ,
29- "comment" : "All tests passed" ,
30- "id" : "123" ,
31- "url" : "http://dummy-forgejo/commit/abcdef123456/status" ,
32- "created" : "2023-01-01T12:00:00Z" ,
33- "updated" : "2023-01-01T12:30:00Z"
34- }]
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+ ]
3541 responses .add (responses .GET , url , json = dummy_response , status = 200 )
36-
42+
3743 # Call the method under test.
3844 flags : List [CommitFlag ] = ForgejoCommitFlag .get (project , commit )
39-
45+
4046 # Assertions using CommitStatus from packit.ogr.abstract.
4147 assert len (flags ) == 1
4248 flag = flags [0 ]
@@ -45,18 +51,23 @@ def test_get_commit_flag_integration():
4551 assert flag .context == "CI"
4652 assert flag .comment == "All tests passed"
4753 assert flag .uid == "123"
48-
49- expected_created = datetime .datetime .strptime ("2023-01-01T12:00:00Z" , "%Y-%m-%dT%H:%M:%SZ" )
50- 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+ )
5161 assert flag .created == expected_created
5262 assert flag .edited == expected_updated
5363
64+
5465@responses .activate
5566def test_set_commit_flag_integration ():
5667 project = MockProject ()
5768 commit = "abcdef123456"
5869 url = f"{ project .forge_api_url } /repos/{ project .owner } /{ project .repo } /commits/{ commit } /statuses"
59-
70+
6071 # Dummy response for setting a commit status.
6172 dummy_response = {
6273 "commit" : commit ,
@@ -66,28 +77,32 @@ def test_set_commit_flag_integration():
6677 "id" : "456" ,
6778 "url" : "http://dummy-forgejo/commit/abcdef123456/status" ,
6879 "created" : "2023-02-01T12:00:00Z" ,
69- "updated" : "2023-02-01T12:30:00Z"
80+ "updated" : "2023-02-01T12:30:00Z" ,
7081 }
7182 responses .add (responses .POST , url , json = dummy_response , status = 200 )
72-
83+
7384 # Call the set method to create a new commit flag.
7485 flag = ForgejoCommitFlag .set (
7586 project = project ,
7687 commit = commit ,
7788 state = CommitStatus .success ,
7889 target_url = "http://dummy-target" ,
7990 description = "Build succeeded" ,
80- context = "CI"
91+ context = "CI" ,
8192 )
82-
93+
8394 # Assertions to verify correct mapping using CommitStatus.
8495 assert flag .commit == commit
8596 assert flag .state == CommitStatus .success
8697 assert flag .context == "CI"
8798 assert flag .comment == "Build succeeded"
8899 assert flag .uid == "456"
89-
90- expected_created = datetime .datetime .strptime ("2023-02-01T12:00:00Z" , "%Y-%m-%dT%H:%M:%SZ" )
91- 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+ )
92107 assert flag .created == expected_created
93108 assert flag .edited == expected_updated
0 commit comments