11import os
22
33import database .events # noqa: F401
4- from database .tests .factories import RepositoryFactory
4+ from database .tests .factories import OwnerFactory , RepositoryFactory
55
66
77def test_shelter_repo_sync (dbsession , mock_configuration , mocker ):
88 # this prevents the pubsub SDK from trying to load credentials
99 os .environ ["PUBSUB_EMULATOR_HOST" ] = "localhost"
10+ publish = mocker .patch ("google.cloud.pubsub_v1.PublisherClient.publish" )
1011
1112 mock_configuration .set_params (
1213 {
@@ -20,40 +21,45 @@ def test_shelter_repo_sync(dbsession, mock_configuration, mocker):
2021 }
2122 )
2223
23- publish = mocker .patch ("google.cloud.pubsub_v1.PublisherClient.publish" )
24-
2524 # this triggers the publish via SQLAlchemy events (after_insert)
26- repo = RepositoryFactory (repoid = 91728376 , name = "test-123" )
25+ repo = RepositoryFactory (
26+ repoid = 91728376 , name = "test-123" , owner = OwnerFactory (ownerid = 123 ), private = False
27+ )
2728 dbsession .add (repo )
2829 dbsession .commit ()
2930
3031 publish .assert_called_once_with (
3132 "projects/test-project-id/topics/test-topic-id" ,
3233 b'{"type": "repo", "sync": "one", "id": 91728376}' ,
3334 )
34-
35- publish = mocker .patch ("google.cloud.pubsub_v1.PublisherClient.publish" )
35+ publish_calls = publish .call_args_list
3636
3737 # Synchronize object flush for history.deleted to be perceived by sqlalchemy
3838 dbsession .refresh (repo )
3939
4040 # this triggers the publish via SQLAlchemy events (after_update)
4141 repo .name = "test-456"
4242 dbsession .commit ()
43+ dbsession .refresh (repo )
44+ assert len (publish_calls ) == 2
4345
44- # same name shouldn't trigger (after_update)
45- repo .name = "test-456 "
46+ # Does not trigger another publish with untracked field
47+ repo .message = "foo "
4648 dbsession .commit ()
49+ dbsession .refresh (repo )
50+ assert len (publish_calls ) == 2
4751
48- # this wouldn't trigger the publish via SQLAlchemy events (after_update) since it's an unimportant attribute
49- repo .activated = True
52+ # Triggers call when owner is changed
53+ repo .owner = OwnerFactory ( ownerid = 456 )
5054 dbsession .commit ()
55+ dbsession .refresh (repo )
56+ assert len (publish_calls ) == 3
5157
52- # this is from the first trigger
53- publish . assert_called_once_with (
54- "projects/test-project-id/topics/test-topic-id" ,
55- b'{"type": " repo", "sync": "one", "id": 91728376}' ,
56- )
58+ # Triggers call when private is changed
59+ repo . private = True
60+ dbsession . commit ()
61+ dbsession . refresh ( repo )
62+ assert len ( publish_calls ) == 4
5763
5864
5965def test_repo_sync_when_shelter_disabled (dbsession , mock_configuration , mocker ):
0 commit comments