-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from atselvan/feature/sync
Feature/sync
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ankiconnect | ||
|
||
import "github.com/privatesquare/bkst-go-utils/utils/errors" | ||
|
||
const ( | ||
ActionSync = "sync" | ||
) | ||
|
||
type ( | ||
// SyncManager describes the interface that can be used to perform sync operations on Anki. | ||
SyncManager interface { | ||
Trigger() *errors.RestErr | ||
} | ||
|
||
// syncManager implements SyncManager | ||
syncManager struct{ | ||
Client *Client | ||
} | ||
) | ||
|
||
// Trigger syncs local Anki data to Anki web. | ||
// The method returns an error if: | ||
// - the api request to ankiconnect fails. | ||
// - the api returns a http error. | ||
func (sm *syncManager) Trigger() *errors.RestErr { | ||
_, restErr := post[string, ParamsDefault](sm.Client, ActionSync, nil) | ||
if restErr != nil { | ||
return restErr | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ankiconnect | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSyncManager_Trigger(t *testing.T) { | ||
t.Run("success", func(t *testing.T) { | ||
httpmock.ActivateNonDefault(client.httpClient.GetClient()) | ||
defer httpmock.DeactivateAndReset() | ||
|
||
result := new(Result[string]) | ||
responder, err := httpmock.NewJsonResponder(http.StatusOK, result) | ||
assert.NoError(t, err) | ||
|
||
httpmock.RegisterResponder(http.MethodPost, ankiConnectUrl, responder) | ||
|
||
restErr := client.Sync.Trigger() | ||
assert.Nil(t, restErr) | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
httpmock.ActivateNonDefault(client.httpClient.GetClient()) | ||
defer httpmock.DeactivateAndReset() | ||
|
||
result := new(Result[string]) | ||
loadTestData(t, testDataPath+errorTestDataFileName, result) | ||
responder, err := httpmock.NewJsonResponder(http.StatusOK, result) | ||
assert.NoError(t, err) | ||
|
||
httpmock.RegisterResponder(http.MethodPost, ankiConnectUrl, responder) | ||
|
||
restErr := client.Sync.Trigger() | ||
assert.NotNil(t, restErr) | ||
assert.Equal(t, http.StatusBadRequest, restErr.StatusCode) | ||
assert.Equal(t, "some error message", restErr.Message) | ||
}) | ||
} |