*** Settings ***
Documentation       Tests of the /history route.
...                 Ensures that the history CRUD works corectly.

Resource            ../rest.resource
Resource            ../auth/auth.resource
Test Setup          ApiKey


*** Test Cases ***
Get history without behing connected
    &{history}=    GET    /history
    Output
    Integer    response status    401

Create and get an history record
    [Documentation]    Create an history item
    &{song}=    POST
    ...    /song
    ...    {"name": "Mama mia", "difficulties": {}, "midiPath": "/musics/Beethoven-125-4.midi", "musicXmlPath": "/musics/Beethoven-125-4.mxl"}
    Output
    ${userID}=    RegisterLogin    wowuser

    &{history}=    POST
    ...    /history
    ...    { "userID": ${userID}, "songID": ${song.body.id}, "score": 55, "difficulties": {}, "info": { "max_score": 0, "wrong": 0, "great": 0, "good": 0} }
    Output
    Integer    response status    201

    &{res}=    GET    /history
    Output
    Integer    response status    200
    Array    response body
    Integer    $[0].userID    ${userID}
    Integer    $[0].songID    ${song.body.id}
    Integer    $[0].score    55

    [Teardown]    Run Keywords    DELETE    /auth/me
    ...    AND    DELETE    /song/${song.body.id}

Create and get a duplicated history record
    [Documentation]    Create an history item
    &{song}=    POST
    ...    /song
    ...    {"name": "Mama mia", "difficulties": {}, "midiPath": "/musics/Beethoven-125-4.midi", "musicXmlPath": "/musics/Beethoven-125-4.mxl"}
    Output
    ${userID}=    RegisterLogin    wowuser

    &{history}=    POST
    ...    /history
    ...    { "userID": ${userID}, "songID": ${song.body.id}, "score": 55, "difficulties": {}, "info": { "max_score": 0, "wrong": 0, "great": 0, "good": 0} }
    Output
    Integer    response status    201

    &{history2}=    POST
    ...    /history
    ...    { "userID": ${userID}, "songID": ${song.body.id}, "score": 65, "difficulties": {}, "info": { "max_score": 0, "wrong": 0, "great": 0, "good": 0} }
    Output
    Integer    response status    201

    &{res}=    GET    /history
    Output
    Integer    response status    200
    Array    response body
    Integer    $[0].userID    ${userID}
    Integer    $[0].songID    ${song.body.id}
    Integer    $[0].score    65
    Integer    $[1].userID    ${userID}
    Integer    $[1].songID    ${song.body.id}
    Integer    $[1].score    55

    [Teardown]    Run Keywords    DELETE    /auth/me
    ...    AND    DELETE    /song/${song.body.id}

Create and get a search history record
    [Documentation]    Create a search history item
    ${userID}=    RegisterLogin    historyqueryuser

    GET    /search/song/toto
    Output
    Integer    response status    404

    GET    /search/song/tata
    Output
    Integer    response status    404

    POST   /history/search
    ...    { "query": "tata", "type": "song" }

    &{res}=    GET    /history/search
    Output
    Integer    response status    200
    Array    response body
    String    $[0].type    "song"
    String    $[0].query    "tata"
    ${len}=   Get Length    ${res.body}
    Should Be Equal As Integers    ${len}    1

    [Teardown]    DELETE    /auth/me

Get the history of a single song
    [Documentation]    Create an history item
    &{song}=    POST
    ...    /song
    ...    {"name": "Mama mia", "difficulties": {}, "midiPath": "/musics/Beethoven-125-4.midi", "musicXmlPath": "/musics/Beethoven-125-4.mxl"}
    Output
    &{song2}=    POST
    ...    /song
    ...    {"name": "Mama mia2", "difficulties": {}, "midiPath": "/musics/Beethoven-125-42.midi", "musicXmlPath": "/musics/Beethoven-125-24.mxl"}
    Output
    ${userID}=    RegisterLogin    wowuser

    &{history}=    POST
    ...    /history
    ...    { "userID": ${userID}, "songID": ${song.body.id}, "score": 55, "difficulties": {}, "info": { "max_score": 0, "wrong": 0, "great": 0, "good": 0} }
    Output
    Integer    response status    201
    &{history2}=    POST
    ...    /history
    ...    { "userID": ${userID}, "songID": ${song.body.id}, "score": 65, "difficulties": {}, "info": { "max_score": 0, "wrong": 0, "great": 0, "good": 0} }
    Output
    Integer    response status    201
    &{history3}=    POST
    ...    /history
    ...    { "userID": ${userID}, "songID": ${song2.body.id}, "score": 75, "difficulties": {}, "info": { "max_score": 0, "wrong": 0, "great": 0, "good": 0} }
    Output
    Integer    response status    201

    &{res}=    GET    /song/${song.body.id}/history
    Output
    Integer    response status    200
    Array    response body history
    Integer    $.history[0].userID    ${userID}
    Integer    $.history[0].songID    ${song.body.id}
    Integer    $.history[0].score    65
    Integer    $.history[1].userID    ${userID}
    Integer    $.history[1].songID    ${song.body.id}
    Integer    $.history[1].score    55
    Integer    $.best    65

    [Teardown]    Run Keywords    DELETE    /auth/me
    ...    AND    DELETE    /song/${song.body.id}
    ...    AND    DELETE    /song/${song2.body.id}
