TestSendBooks tests that books are put to correct 'buckets' by running a test server and recording the uuid from the incoming data. It also tests that the uuid of the created books and book_uuids of their notes are updated accordingly based on the server response.
(t *testing.T)
| 1925 | // uuid from the incoming data. It also tests that the uuid of the created books and book_uuids of their notes |
| 1926 | // are updated accordingly based on the server response. |
| 1927 | func TestSendBooks(t *testing.T) { |
| 1928 | // set up |
| 1929 | ctx := context.InitTestCtx(t) |
| 1930 | testutils.Login(t, &ctx) |
| 1931 | |
| 1932 | db := ctx.DB |
| 1933 | |
| 1934 | database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 0) |
| 1935 | |
| 1936 | // should be ignored |
| 1937 | database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) |
| 1938 | database.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b2-uuid", "b2-label", 2, false, false) |
| 1939 | // should be created |
| 1940 | database.MustExec(t, "inserting b3", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 0, false, true) |
| 1941 | database.MustExec(t, "inserting b4", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b4-uuid", "b4-label", 0, false, true) |
| 1942 | // should be only expunged locally without syncing to server |
| 1943 | database.MustExec(t, "inserting b5", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b5-uuid", "b5-label", 0, true, true) |
| 1944 | // should be deleted |
| 1945 | database.MustExec(t, "inserting b6", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b6-uuid", "b6-label", 10, true, true) |
| 1946 | // should be updated |
| 1947 | database.MustExec(t, "inserting b7", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b7-uuid", "b7-label", 11, false, true) |
| 1948 | database.MustExec(t, "inserting b8", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b8-uuid", "b8-label", 18, false, true) |
| 1949 | |
| 1950 | // some random notes |
| 1951 | database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 10, "n1 body", 1541108743, false, false) |
| 1952 | database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", "b5-uuid", 10, "n2 body", 1541108743, false, false) |
| 1953 | database.MustExec(t, "inserting n3", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", "b6-uuid", 10, "n3 body", 1541108743, false, false) |
| 1954 | database.MustExec(t, "inserting n4", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n4-uuid", "b7-uuid", 10, "n4 body", 1541108743, false, false) |
| 1955 | // notes that belong to the created book. Their book_uuid should be updated. |
| 1956 | database.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n5-uuid", "b3-uuid", 10, "n5 body", 1541108743, false, false) |
| 1957 | database.MustExec(t, "inserting n6", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n6-uuid", "b3-uuid", 10, "n6 body", 1541108743, false, false) |
| 1958 | database.MustExec(t, "inserting n7", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n7-uuid", "b4-uuid", 10, "n7 body", 1541108743, false, false) |
| 1959 | |
| 1960 | var createdLabels []string |
| 1961 | var updatesUUIDs []string |
| 1962 | var deletedUUIDs []string |
| 1963 | |
| 1964 | // fire up a test server |
| 1965 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1966 | if r.URL.String() == "/v3/books" && r.Method == "POST" { |
| 1967 | var payload client.CreateBookPayload |
| 1968 | |
| 1969 | err := json.NewDecoder(r.Body).Decode(&payload) |
| 1970 | if err != nil { |
| 1971 | t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) |
| 1972 | return |
| 1973 | } |
| 1974 | |
| 1975 | createdLabels = append(createdLabels, payload.Name) |
| 1976 | |
| 1977 | resp := client.CreateBookResp{ |
| 1978 | Book: client.RespBook{ |
| 1979 | UUID: fmt.Sprintf("server-%s-uuid", payload.Name), |
| 1980 | }, |
| 1981 | } |
| 1982 | |
| 1983 | w.Header().Set("Content-Type", "application/json") |
| 1984 | if err := json.NewEncoder(w).Encode(resp); err != nil { |
nothing calls this directly
no test coverage detected