This repository was archived by the owner on Nov 15, 2024. It is now read-only.
forked from Yandex-Practicum/go-db-sql-final
-
Notifications
You must be signed in to change notification settings - Fork 0
first itteration #1
Open
dimkene
wants to merge
1
commit into
master
Choose a base branch
from
test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,11 +6,12 @@ import ( | |
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| _ "modernc.org/sqlite" | ||
| ) | ||
|
|
||
| var ( | ||
| // randSource источник псевдо случайных чисел. | ||
| // Для повышения уникальности в качестве seed | ||
| // используется текущее время в unix формате (в виде числа) | ||
| randSource = rand.NewSource(time.Now().UnixNano()) | ||
|
|
@@ -31,58 +32,92 @@ func getTestParcel() Parcel { | |
| // TestAddGetDelete проверяет добавление, получение и удаление посылки | ||
| func TestAddGetDelete(t *testing.T) { | ||
| // prepare | ||
| db, err := // настройте подключение к БД | ||
| db, err := sql.Open("sqlite", "tracker.db") | ||
| require.NoError(t, err) | ||
| defer db.Close() | ||
|
|
||
| store := NewParcelStore(db) | ||
| parcel := getTestParcel() | ||
|
|
||
| // add | ||
| // добавьте новую посылку в БД, убедитесь в отсутствии ошибки и наличии идентификатора | ||
| id, err := store.Add(parcel) | ||
| require.NoError(t, err) | ||
| assert.GreaterOrEqual(t, id, 0) | ||
|
|
||
| parcel.Number = id | ||
|
|
||
| // get | ||
| // получите только что добавленную посылку, убедитесь в отсутствии ошибки | ||
| // проверьте, что значения всех полей в полученном объекте совпадают со значениями полей в переменной parcel | ||
| output, err := store.Get(id) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, output, parcel) | ||
|
|
||
| // delete | ||
| // удалите добавленную посылку, убедитесь в отсутствии ошибки | ||
| // проверьте, что посылку больше нельзя получить из БД | ||
| err = store.Delete(id) | ||
| require.NoError(t, err) | ||
| output, err = store.Get(parcel.Number) | ||
| require.ErrorIs(t, err, sql.ErrNoRows) | ||
| require.Empty(t, output) | ||
| } | ||
|
|
||
| // TestSetAddress проверяет обновление адреса | ||
| func TestSetAddress(t *testing.T) { | ||
| // prepare | ||
| db, err := // настройте подключение к БД | ||
| db, err := sql.Open("sqlite", "tracker.db") | ||
| require.NoError(t, err) | ||
| defer db.Close() | ||
|
|
||
| store := NewParcelStore(db) | ||
| parcel := getTestParcel() | ||
|
|
||
| // add | ||
| // добавьте новую посылку в БД, убедитесь в отсутствии ошибки и наличии идентификатора | ||
| id, err := store.Add(parcel) | ||
| require.NoError(t, err) | ||
| assert.GreaterOrEqual(t, id, 0) | ||
|
|
||
| // set address | ||
| // обновите адрес, убедитесь в отсутствии ошибки | ||
| newAddress := "new test address" | ||
| err = store.SetAddress(id, newAddress) | ||
| require.NoError(t, err) | ||
|
|
||
| // check | ||
| // получите добавленную посылку и убедитесь, что адрес обновился | ||
| output, err := store.Get(id) | ||
| require.NoError(t, err) | ||
| require.Equal(t, newAddress, output.Address) | ||
| } | ||
|
|
||
| // TestSetStatus проверяет обновление статуса | ||
| func TestSetStatus(t *testing.T) { | ||
| // prepare | ||
| db, err := // настройте подключение к БД | ||
| db, err := sql.Open("sqlite", "tracker.db") | ||
| require.NoError(t, err) | ||
| defer db.Close() | ||
|
|
||
| store := NewParcelStore(db) | ||
| parcel := getTestParcel() | ||
|
|
||
| // add | ||
| // добавьте новую посылку в БД, убедитесь в отсутствии ошибки и наличии идентификатора | ||
| id, err := store.Add(parcel) | ||
| require.NoError(t, err) | ||
| assert.GreaterOrEqual(t, id, 0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И здесь |
||
|
|
||
| // set status | ||
| // обновите статус, убедитесь в отсутствии ошибки | ||
| err = store.SetStatus(id, ParcelStatusDelivered) | ||
| require.NoError(t, err) | ||
|
|
||
| // check | ||
| // получите добавленную посылку и убедитесь, что статус обновился | ||
| output, err := store.Get(id) | ||
| require.NoError(t, err) | ||
| require.Equal(t, ParcelStatusDelivered, output.Status) | ||
| } | ||
|
|
||
| // TestGetByClient проверяет получение посылок по идентификатору клиента | ||
| func TestGetByClient(t *testing.T) { | ||
| // prepare | ||
| db, err := // настройте подключение к БД | ||
| db, err := sql.Open("sqlite", "tracker.db") | ||
| require.NoError(t, err) | ||
| defer db.Close() | ||
|
|
||
| store := NewParcelStore(db) | ||
| parcels := []Parcel{ | ||
| getTestParcel(), | ||
| getTestParcel(), | ||
|
|
@@ -98,7 +133,8 @@ func TestGetByClient(t *testing.T) { | |
|
|
||
| // add | ||
| for i := 0; i < len(parcels); i++ { | ||
| id, err := // добавьте новую посылку в БД, убедитесь в отсутствии ошибки и наличии идентификатора | ||
| id, err := store.Add(parcels[i]) | ||
| require.NoError(t, err) | ||
|
|
||
| // обновляем идентификатор добавленной у посылки | ||
| parcels[i].Number = id | ||
|
|
@@ -108,14 +144,14 @@ func TestGetByClient(t *testing.T) { | |
| } | ||
|
|
||
| // get by client | ||
| storedParcels, err := // получите список посылок по идентификатору клиента, сохранённого в переменной client | ||
| // убедитесь в отсутствии ошибки | ||
| // убедитесь, что количество полученных посылок совпадает с количеством добавленных | ||
| storedParcels, err := store.GetByClient(client) | ||
| require.NoError(t, err) | ||
| require.Equal(t, len(parcels), len(storedParcels)) | ||
|
|
||
| // check | ||
| for _, parcel := range storedParcels { | ||
| // в parcelMap лежат добавленные посылки, ключ - идентификатор посылки, значение - сама посылка | ||
| // убедитесь, что все посылки из storedParcels есть в parcelMap | ||
| // убедитесь, что значения полей полученных посылок заполнены верно | ||
| output, ok := parcelMap[parcel.Number] | ||
| require.True(t, ok) | ||
| require.Equal(t, output, parcel) | ||
| } | ||
| } | ||
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут нужно проверить что id заполнен, те не 0. Сейчас проверка допускает значение 0