2022-04-10 07:47:15 +03:00
|
|
|
module Test.QuerySpec.BasicSpec (spec) where
|
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
import Control.Arrow ((>>>))
|
2022-09-20 06:59:47 +03:00
|
|
|
import Control.Lens ((%~), (&), (?~))
|
2022-09-21 08:11:53 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API
|
2022-04-10 07:47:15 +03:00
|
|
|
import Servant.API (NamedRoutes)
|
|
|
|
import Servant.Client (Client, (//))
|
2022-09-28 02:41:21 +03:00
|
|
|
import Test.Data (TestData (..))
|
2022-04-10 07:47:15 +03:00
|
|
|
import Test.Data qualified as Data
|
2022-08-04 04:00:48 +03:00
|
|
|
import Test.Expectations (jsonShouldBe, rowsShouldBe)
|
2022-04-10 07:47:15 +03:00
|
|
|
import Test.Hspec (Spec, describe, it)
|
|
|
|
import Prelude
|
|
|
|
|
2022-09-28 02:41:21 +03:00
|
|
|
spec :: TestData -> Client IO (NamedRoutes Routes) -> SourceName -> Config -> Spec
|
|
|
|
spec TestData {..} api sourceName config = describe "Basic Queries" $ do
|
2022-04-10 07:47:15 +03:00
|
|
|
describe "Column Fields" $ do
|
|
|
|
it "can query for a list of artists" $ do
|
2022-06-24 09:58:25 +03:00
|
|
|
let query = artistsQueryRequest
|
2022-07-20 08:20:49 +03:00
|
|
|
receivedArtists <- Data.sortResponseRowsBy "ArtistId" <$> (api // _query) sourceName config query
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-09-28 02:41:21 +03:00
|
|
|
let expectedArtists = _tdArtistsRows
|
2022-08-04 04:00:48 +03:00
|
|
|
Data.responseRows receivedArtists `rowsShouldBe` expectedArtists
|
|
|
|
_qrAggregates receivedArtists `jsonShouldBe` Nothing
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
it "can query for a list of albums with a subset of columns" $ do
|
2022-10-06 02:23:49 +03:00
|
|
|
let fields = Data.mkFieldsMap [("ArtistId", _tdColumnField "ArtistId"), ("Title", _tdColumnField "Title")]
|
2022-07-29 11:05:57 +03:00
|
|
|
let query = albumsQueryRequest & qrQuery . qFields ?~ fields
|
2022-07-20 08:20:49 +03:00
|
|
|
receivedAlbums <- Data.sortResponseRowsBy "Title" <$> (api // _query) sourceName config query
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
let filterToRequiredProperties =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.filterWithKey (\(FieldName propName) _value -> propName == "ArtistId" || propName == "Title")
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-09-28 02:41:21 +03:00
|
|
|
let expectedAlbums = Data.sortBy (FieldName "Title") $ filterToRequiredProperties <$> _tdAlbumsRows
|
2022-08-04 04:00:48 +03:00
|
|
|
Data.responseRows receivedAlbums `rowsShouldBe` expectedAlbums
|
|
|
|
_qrAggregates receivedAlbums `jsonShouldBe` Nothing
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
it "can project columns into fields with different names" $ do
|
2022-10-06 02:23:49 +03:00
|
|
|
let fields = Data.mkFieldsMap [("Artist_Id", _tdColumnField "ArtistId"), ("Artist_Name", _tdColumnField "Name")]
|
2022-07-29 11:05:57 +03:00
|
|
|
let query = artistsQueryRequest & qrQuery . qFields ?~ fields
|
2022-07-20 08:20:49 +03:00
|
|
|
receivedArtists <- Data.sortResponseRowsBy "ArtistId" <$> (api // _query) sourceName config query
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
let renameProperties =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.mapKeys
|
2022-04-10 07:47:15 +03:00
|
|
|
( \case
|
2022-09-21 08:11:53 +03:00
|
|
|
(FieldName "ArtistId") -> (FieldName "Artist_Id")
|
|
|
|
(FieldName "Name") -> (FieldName "Artist_Name")
|
2022-04-10 07:47:15 +03:00
|
|
|
other -> other
|
|
|
|
)
|
|
|
|
|
2022-09-28 02:41:21 +03:00
|
|
|
let expectedArtists = Data.sortBy (FieldName "ArtistId") $ renameProperties <$> _tdArtistsRows
|
2022-08-04 04:00:48 +03:00
|
|
|
Data.responseRows receivedArtists `rowsShouldBe` expectedArtists
|
|
|
|
_qrAggregates receivedArtists `jsonShouldBe` Nothing
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
describe "Limit & Offset" $ do
|
|
|
|
it "can use limit and offset to paginate results" $ do
|
2022-06-24 09:58:25 +03:00
|
|
|
let allQuery = artistsQueryRequest
|
2022-07-29 11:05:57 +03:00
|
|
|
let page1Query = artistsQueryRequest & qrQuery %~ (qLimit ?~ 10 >>> qOffset ?~ 0)
|
|
|
|
let page2Query = artistsQueryRequest & qrQuery %~ (qLimit ?~ 10 >>> qOffset ?~ 10)
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-07-20 08:20:49 +03:00
|
|
|
allArtists <- Data.responseRows <$> (api // _query) sourceName config allQuery
|
|
|
|
page1Artists <- Data.responseRows <$> (api // _query) sourceName config page1Query
|
|
|
|
page2Artists <- Data.responseRows <$> (api // _query) sourceName config page2Query
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-08-04 04:00:48 +03:00
|
|
|
page1Artists `rowsShouldBe` take 10 allArtists
|
|
|
|
page2Artists `rowsShouldBe` take 10 (drop 10 allArtists)
|
2022-09-28 02:41:21 +03:00
|
|
|
where
|
|
|
|
artistsQueryRequest :: QueryRequest
|
|
|
|
artistsQueryRequest =
|
2022-10-06 02:23:49 +03:00
|
|
|
let fields = Data.mkFieldsMap [("ArtistId", _tdColumnField "ArtistId"), ("Name", _tdColumnField "Name")]
|
2022-09-28 02:41:21 +03:00
|
|
|
query = Data.emptyQuery & qFields ?~ fields
|
|
|
|
in QueryRequest _tdArtistsTableName [] query
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-09-28 02:41:21 +03:00
|
|
|
albumsQueryRequest :: QueryRequest
|
|
|
|
albumsQueryRequest =
|
2022-10-06 02:23:49 +03:00
|
|
|
let fields = Data.mkFieldsMap [("AlbumId", _tdColumnField "AlbumId"), ("ArtistId", _tdColumnField "ArtistId"), ("Title", _tdColumnField "Title")]
|
2022-09-28 02:41:21 +03:00
|
|
|
query = Data.emptyQuery & qFields ?~ fields
|
|
|
|
in QueryRequest _tdAlbumsTableName [] query
|