2022-04-10 07:47:15 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
|
|
|
module Test.Data
|
2022-09-28 02:41:21 +03:00
|
|
|
( -- = Test Data
|
|
|
|
TestData (..),
|
|
|
|
mkTestData,
|
2022-07-29 11:05:57 +03:00
|
|
|
-- = Utilities
|
|
|
|
emptyQuery,
|
2022-04-10 07:47:15 +03:00
|
|
|
sortBy,
|
2022-06-24 09:58:25 +03:00
|
|
|
filterColumnsByQueryFields,
|
2022-08-01 03:40:45 +03:00
|
|
|
filterColumns,
|
|
|
|
renameColumns,
|
2022-07-29 11:05:57 +03:00
|
|
|
onlyKeepRelationships,
|
2022-07-20 08:20:49 +03:00
|
|
|
queryFields,
|
|
|
|
responseRows,
|
|
|
|
sortResponseRowsBy,
|
2022-07-28 08:39:48 +03:00
|
|
|
responseAggregates,
|
2022-09-21 08:11:53 +03:00
|
|
|
mkFieldsMap,
|
|
|
|
insertField,
|
|
|
|
deleteField,
|
|
|
|
field,
|
2022-07-20 08:20:49 +03:00
|
|
|
_ColumnFieldNumber,
|
|
|
|
_ColumnFieldString,
|
|
|
|
_ColumnFieldBoolean,
|
2022-08-19 10:00:46 +03:00
|
|
|
orderByColumn,
|
2022-04-10 07:47:15 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-06-13 23:58:44 +03:00
|
|
|
import Codec.Compression.GZip qualified as GZip
|
2022-10-06 02:23:49 +03:00
|
|
|
import Command (NameCasing (..), TestConfig (..))
|
2022-09-21 08:11:53 +03:00
|
|
|
import Control.Arrow (first, (>>>))
|
2022-10-10 05:23:43 +03:00
|
|
|
import Control.Lens (Index, IxValue, Ixed, Traversal', ix, lens, (%~), (&), (^.), (^..), (^?))
|
2022-09-21 08:11:53 +03:00
|
|
|
import Data.Aeson (eitherDecodeStrict)
|
2022-07-26 05:28:57 +03:00
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Aeson.Lens (_Bool, _Number, _String)
|
2022-08-01 03:40:45 +03:00
|
|
|
import Data.Bifunctor (bimap)
|
2022-04-10 07:47:15 +03:00
|
|
|
import Data.ByteString (ByteString)
|
2022-06-13 23:58:44 +03:00
|
|
|
import Data.ByteString.Lazy qualified as BSL
|
2022-08-04 11:34:45 +03:00
|
|
|
import Data.CaseInsensitive (CI)
|
2022-06-13 23:58:44 +03:00
|
|
|
import Data.CaseInsensitive qualified as CI
|
2022-04-10 07:47:15 +03:00
|
|
|
import Data.FileEmbed (embedFile, makeRelativeToProject)
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-08-01 03:40:45 +03:00
|
|
|
import Data.List (find, sortOn)
|
2022-08-04 11:34:45 +03:00
|
|
|
import Data.List.NonEmpty (NonEmpty (..))
|
|
|
|
import Data.List.NonEmpty qualified as NonEmpty
|
2022-07-20 08:20:49 +03:00
|
|
|
import Data.Maybe (fromMaybe, mapMaybe)
|
2022-04-10 07:47:15 +03:00
|
|
|
import Data.Scientific (Scientific)
|
|
|
|
import Data.Text (Text)
|
2022-06-13 23:58:44 +03:00
|
|
|
import Data.Text qualified as Text
|
|
|
|
import Data.Text.Encoding qualified as Text
|
2022-07-20 08:20:49 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
2022-06-13 23:58:44 +03:00
|
|
|
import Text.XML qualified as XML
|
|
|
|
import Text.XML.Lens qualified as XML
|
2022-04-10 07:47:15 +03:00
|
|
|
import Prelude
|
|
|
|
|
|
|
|
schemaBS :: ByteString
|
2022-09-27 10:20:46 +03:00
|
|
|
schemaBS = $(makeRelativeToProject "test/Test/Data/schema-tables.json" >>= embedFile)
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-07-20 08:20:49 +03:00
|
|
|
schemaTables :: [API.TableInfo]
|
2022-09-21 08:11:53 +03:00
|
|
|
schemaTables = sortOn API._tiName . either error id . eitherDecodeStrict $ schemaBS
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-07-28 08:39:48 +03:00
|
|
|
numericColumns :: [API.ColumnName]
|
2022-09-21 08:11:53 +03:00
|
|
|
numericColumns = schemaTables >>= (API._tiColumns >>> mapMaybe (\API.ColumnInfo {..} -> if _ciType == API.NumberTy then Just _ciName else Nothing))
|
2022-07-28 08:39:48 +03:00
|
|
|
|
2022-06-13 23:58:44 +03:00
|
|
|
chinookXmlBS :: ByteString
|
2022-09-27 10:20:46 +03:00
|
|
|
chinookXmlBS = $(makeRelativeToProject "test/Test/Data/ChinookData.xml.gz" >>= embedFile)
|
2022-06-13 23:58:44 +03:00
|
|
|
|
|
|
|
chinookXml :: XML.Document
|
|
|
|
chinookXml = XML.parseLBS_ XML.def . GZip.decompress $ BSL.fromStrict chinookXmlBS
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
readTableFromXmlIntoRows :: API.TableName -> [HashMap API.FieldName API.FieldValue]
|
2022-08-04 11:34:45 +03:00
|
|
|
readTableFromXmlIntoRows tableName =
|
2022-06-13 23:58:44 +03:00
|
|
|
rowToJsonObject <$> tableRows
|
|
|
|
where
|
2022-08-04 11:34:45 +03:00
|
|
|
tableNameToXmlTag :: API.TableName -> CI Text
|
|
|
|
tableNameToXmlTag (API.TableName names) = CI.mk . Text.intercalate "_" $ NonEmpty.toList names
|
|
|
|
|
2022-06-13 23:58:44 +03:00
|
|
|
tableRows :: [XML.Element]
|
2022-08-04 11:34:45 +03:00
|
|
|
tableRows = chinookXml ^.. XML.root . XML.nodes . traverse . XML._Element . XML.named (tableNameToXmlTag tableName)
|
2022-06-13 23:58:44 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
rowToJsonObject :: XML.Element -> HashMap API.FieldName API.FieldValue
|
2022-06-13 23:58:44 +03:00
|
|
|
rowToJsonObject element =
|
|
|
|
let columnElements = element ^.. XML.nodes . traverse . XML._Element
|
|
|
|
keyValuePairs = columnElementToProperty <$> columnElements
|
2022-09-21 08:11:53 +03:00
|
|
|
in HashMap.fromList keyValuePairs
|
2022-06-13 23:58:44 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
columnElementToProperty :: XML.Element -> (API.FieldName, API.FieldValue)
|
2022-06-13 23:58:44 +03:00
|
|
|
columnElementToProperty columnElement =
|
2022-09-21 08:11:53 +03:00
|
|
|
let name = columnElement ^. XML.localName
|
2022-07-28 08:39:48 +03:00
|
|
|
value = case columnElement ^. XML.nodes of
|
|
|
|
[] -> API.mkColumnFieldValue $ J.Null
|
|
|
|
_ ->
|
|
|
|
let textValue = Text.concat $ columnElement ^.. XML.text
|
2022-09-21 08:11:53 +03:00
|
|
|
in if API.ColumnName name `elem` numericColumns
|
2022-07-28 08:39:48 +03:00
|
|
|
then case eitherDecodeStrict $ Text.encodeUtf8 textValue of
|
|
|
|
Left _ -> API.mkColumnFieldValue $ J.String textValue
|
|
|
|
Right scientific -> API.mkColumnFieldValue $ J.Number scientific
|
|
|
|
else API.mkColumnFieldValue $ J.String textValue
|
2022-09-21 08:11:53 +03:00
|
|
|
in (API.FieldName name, value)
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-08-04 11:34:45 +03:00
|
|
|
mkTableName :: Text -> API.TableName
|
|
|
|
mkTableName = API.TableName . (:| [])
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
artistsTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
artistsTableName = mkTableName "Artist"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
artistsRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
artistsRows = sortBy (API.FieldName "ArtistId") $ readTableFromXmlIntoRows artistsTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
artistsRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue)
|
2022-07-29 11:05:57 +03:00
|
|
|
artistsRowsById =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.fromList $ mapMaybe (\artist -> (,artist) <$> artist ^? field "ArtistId" . _ColumnFieldNumber) artistsRows
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
albumsRelationshipName :: API.RelationshipName
|
|
|
|
albumsRelationshipName = API.RelationshipName "Albums"
|
|
|
|
|
|
|
|
artistsTableRelationships :: API.TableRelationships
|
|
|
|
artistsTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "ArtistId", API.ColumnName "ArtistId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
artistsTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (albumsRelationshipName, API.Relationship albumsTableName API.ArrayRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
albumsTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
albumsTableName = mkTableName "Album"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
albumsRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
albumsRows = sortBy (API.FieldName "AlbumId") $ readTableFromXmlIntoRows albumsTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
albumsRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue)
|
2022-08-19 10:00:46 +03:00
|
|
|
albumsRowsById =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.fromList $ mapMaybe (\album -> (,album) <$> album ^? field "AlbumId" . _ColumnFieldNumber) albumsRows
|
2022-08-19 10:00:46 +03:00
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
albumsTableRelationships :: API.TableRelationships
|
|
|
|
albumsTableRelationships =
|
2022-08-01 03:40:45 +03:00
|
|
|
let artistsJoinFieldMapping = HashMap.fromList [(API.ColumnName "ArtistId", API.ColumnName "ArtistId")]
|
|
|
|
tracksJoinFieldMapping = HashMap.fromList [(API.ColumnName "AlbumId", API.ColumnName "AlbumId")]
|
2022-07-29 11:05:57 +03:00
|
|
|
in API.TableRelationships
|
|
|
|
albumsTableName
|
|
|
|
( HashMap.fromList
|
2022-08-01 03:40:45 +03:00
|
|
|
[ (artistRelationshipName, API.Relationship artistsTableName API.ObjectRelationship artistsJoinFieldMapping),
|
|
|
|
(tracksRelationshipName, API.Relationship tracksTableName API.ArrayRelationship tracksJoinFieldMapping)
|
2022-07-29 11:05:57 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
artistRelationshipName :: API.RelationshipName
|
|
|
|
artistRelationshipName = API.RelationshipName "Artist"
|
|
|
|
|
2022-08-01 03:40:45 +03:00
|
|
|
tracksRelationshipName :: API.RelationshipName
|
|
|
|
tracksRelationshipName = API.RelationshipName "Tracks"
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
customersTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
customersTableName = mkTableName "Customer"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
customersRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
customersRows = sortBy (API.FieldName "CustomerId") $ readTableFromXmlIntoRows customersTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
customersTableRelationships :: API.TableRelationships
|
|
|
|
customersTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "SupportRepId", API.ColumnName "EmployeeId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
customersTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (supportRepRelationshipName, API.Relationship employeesTableName API.ObjectRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
supportRepRelationshipName :: API.RelationshipName
|
|
|
|
supportRepRelationshipName = API.RelationshipName "SupportRep"
|
|
|
|
|
|
|
|
employeesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
employeesTableName = mkTableName "Employee"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
employeesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
employeesRows = sortBy (API.FieldName "EmployeeId") $ readTableFromXmlIntoRows employeesTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
employeesRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue)
|
2022-07-29 11:05:57 +03:00
|
|
|
employeesRowsById =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.fromList $ mapMaybe (\employee -> (,employee) <$> employee ^? field "EmployeeId" . _ColumnFieldNumber) employeesRows
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
employeesTableRelationships :: API.TableRelationships
|
|
|
|
employeesTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "EmployeeId", API.ColumnName "SupportRepId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
employeesTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (supportRepForCustomersRelationshipName, API.Relationship customersTableName API.ArrayRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
supportRepForCustomersRelationshipName :: API.RelationshipName
|
|
|
|
supportRepForCustomersRelationshipName = API.RelationshipName "SupportRepForCustomers"
|
|
|
|
|
|
|
|
invoicesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
invoicesTableName = mkTableName "Invoice"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
invoicesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
invoicesRows = sortBy (API.FieldName "InvoiceId") $ readTableFromXmlIntoRows invoicesTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-08-01 03:40:45 +03:00
|
|
|
invoiceLinesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
invoiceLinesTableName = mkTableName "InvoiceLine"
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
invoiceLinesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
invoiceLinesRows = sortBy (API.FieldName "InvoiceLineId") $ readTableFromXmlIntoRows invoiceLinesTableName
|
2022-08-01 03:40:45 +03:00
|
|
|
|
|
|
|
mediaTypesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
mediaTypesTableName = mkTableName "MediaType"
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
mediaTypesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
mediaTypesRows = sortBy (API.FieldName "MediaTypeId") $ readTableFromXmlIntoRows mediaTypesTableName
|
2022-08-01 03:40:45 +03:00
|
|
|
|
|
|
|
tracksTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
tracksTableName = mkTableName "Track"
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
tracksRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
tracksRows = sortBy (API.FieldName "TrackId") $ readTableFromXmlIntoRows tracksTableName
|
2022-08-01 03:40:45 +03:00
|
|
|
|
|
|
|
tracksTableRelationships :: API.TableRelationships
|
|
|
|
tracksTableRelationships =
|
|
|
|
let invoiceLinesJoinFieldMapping = HashMap.fromList [(API.ColumnName "TrackId", API.ColumnName "TrackId")]
|
|
|
|
mediaTypeJoinFieldMapping = HashMap.fromList [(API.ColumnName "MediaTypeId", API.ColumnName "MediaTypeId")]
|
2022-08-19 10:00:46 +03:00
|
|
|
albumJoinFieldMapping = HashMap.fromList [(API.ColumnName "AlbumId", API.ColumnName "AlbumId")]
|
2022-09-20 06:59:47 +03:00
|
|
|
genreJoinFieldMapping = HashMap.fromList [(API.ColumnName "GenreId", API.ColumnName "GenreId")]
|
2022-08-01 03:40:45 +03:00
|
|
|
in API.TableRelationships
|
|
|
|
tracksTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (invoiceLinesRelationshipName, API.Relationship invoiceLinesTableName API.ArrayRelationship invoiceLinesJoinFieldMapping),
|
2022-08-19 10:00:46 +03:00
|
|
|
(mediaTypeRelationshipName, API.Relationship mediaTypesTableName API.ObjectRelationship mediaTypeJoinFieldMapping),
|
2022-09-20 06:59:47 +03:00
|
|
|
(albumRelationshipName, API.Relationship albumsTableName API.ObjectRelationship albumJoinFieldMapping),
|
|
|
|
(genreRelationshipName, API.Relationship genresTableName API.ObjectRelationship genreJoinFieldMapping)
|
2022-08-01 03:40:45 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
invoiceLinesRelationshipName :: API.RelationshipName
|
|
|
|
invoiceLinesRelationshipName = API.RelationshipName "InvoiceLines"
|
|
|
|
|
|
|
|
mediaTypeRelationshipName :: API.RelationshipName
|
|
|
|
mediaTypeRelationshipName = API.RelationshipName "MediaType"
|
|
|
|
|
2022-08-19 10:00:46 +03:00
|
|
|
albumRelationshipName :: API.RelationshipName
|
|
|
|
albumRelationshipName = API.RelationshipName "Album"
|
|
|
|
|
2022-09-20 06:59:47 +03:00
|
|
|
genreRelationshipName :: API.RelationshipName
|
|
|
|
genreRelationshipName = API.RelationshipName "Genre"
|
|
|
|
|
|
|
|
genresTableName :: API.TableName
|
|
|
|
genresTableName = mkTableName "Genre"
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
genresRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
genresRows = sortBy (API.FieldName "GenreId") $ readTableFromXmlIntoRows genresTableName
|
2022-09-20 06:59:47 +03:00
|
|
|
|
|
|
|
genresTableRelationships :: API.TableRelationships
|
|
|
|
genresTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "GenreId", API.ColumnName "GenreId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
genresTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (tracksRelationshipName, API.Relationship tracksTableName API.ArrayRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2022-09-28 02:41:21 +03:00
|
|
|
data TestData = TestData
|
|
|
|
{ -- = Schema
|
|
|
|
_tdSchemaTables :: [API.TableInfo],
|
|
|
|
-- = Artists table
|
|
|
|
_tdArtistsTableName :: API.TableName,
|
|
|
|
_tdArtistsRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
_tdArtistsRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue),
|
|
|
|
_tdArtistsTableRelationships :: API.TableRelationships,
|
|
|
|
_tdAlbumsRelationshipName :: API.RelationshipName,
|
|
|
|
-- = Albums table
|
|
|
|
_tdAlbumsTableName :: API.TableName,
|
|
|
|
_tdAlbumsRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
_tdAlbumsRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue),
|
|
|
|
_tdAlbumsTableRelationships :: API.TableRelationships,
|
|
|
|
_tdArtistRelationshipName :: API.RelationshipName,
|
|
|
|
_tdTracksRelationshipName :: API.RelationshipName,
|
|
|
|
-- = Customers table
|
|
|
|
_tdCustomersTableName :: API.TableName,
|
|
|
|
_tdCustomersRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
_tdCustomersTableRelationships :: API.TableRelationships,
|
|
|
|
_tdSupportRepRelationshipName :: API.RelationshipName,
|
|
|
|
-- = Employees table
|
|
|
|
_tdEmployeesTableName :: API.TableName,
|
|
|
|
_tdEmployeesRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
_tdEmployeesRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue),
|
|
|
|
_tdEmployeesTableRelationships :: API.TableRelationships,
|
|
|
|
_tdSupportRepForCustomersRelationshipName :: API.RelationshipName,
|
|
|
|
-- = Invoices table
|
|
|
|
_tdInvoicesTableName :: API.TableName,
|
|
|
|
_tdInvoicesRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
-- = InvoiceLines table
|
|
|
|
_tdInvoiceLinesTableName :: API.TableName,
|
|
|
|
_tdInvoiceLinesRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
-- = MediaTypes table
|
|
|
|
_tdMediaTypesTableName :: API.TableName,
|
|
|
|
_tdMediaTypesRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
-- = Tracks table
|
|
|
|
_tdTracksTableName :: API.TableName,
|
|
|
|
_tdTracksRows :: [HashMap API.FieldName API.FieldValue],
|
|
|
|
_tdTracksTableRelationships :: API.TableRelationships,
|
|
|
|
_tdInvoiceLinesRelationshipName :: API.RelationshipName,
|
|
|
|
_tdMediaTypeRelationshipName :: API.RelationshipName,
|
|
|
|
_tdAlbumRelationshipName :: API.RelationshipName,
|
|
|
|
_tdGenreRelationshipName :: API.RelationshipName,
|
|
|
|
-- = Genres table
|
|
|
|
_tdGenresTableName :: API.TableName,
|
|
|
|
_tdGenresRows :: [HashMap API.FieldName API.FieldValue],
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdGenresTableRelationships :: API.TableRelationships,
|
|
|
|
-- = Utility functions
|
|
|
|
_tdColumnName :: Text -> API.ColumnName,
|
|
|
|
_tdColumnField :: Text -> API.Field,
|
|
|
|
_tdQueryComparisonColumn :: Text -> API.ComparisonColumn,
|
|
|
|
_tdCurrentComparisonColumn :: Text -> API.ComparisonColumn,
|
|
|
|
_tdOrderByColumn :: [API.RelationshipName] -> Text -> API.OrderDirection -> API.OrderByElement
|
2022-09-28 02:41:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mkTestData :: TestConfig -> TestData
|
|
|
|
mkTestData TestConfig {..} =
|
|
|
|
TestData
|
2022-10-06 02:23:49 +03:00
|
|
|
{ _tdSchemaTables = formatTableInfo <$> schemaTables,
|
|
|
|
_tdArtistsTableName = formatTableName artistsTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdArtistsRows = artistsRows,
|
|
|
|
_tdArtistsRowsById = artistsRowsById,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdArtistsTableRelationships = formatTableRelationships artistsTableRelationships,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdAlbumsRelationshipName = albumsRelationshipName,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdAlbumsTableName = formatTableName albumsTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdAlbumsRows = albumsRows,
|
|
|
|
_tdAlbumsRowsById = albumsRowsById,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdAlbumsTableRelationships = formatTableRelationships albumsTableRelationships,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdArtistRelationshipName = artistRelationshipName,
|
|
|
|
_tdTracksRelationshipName = tracksRelationshipName,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdCustomersTableName = formatTableName customersTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdCustomersRows = customersRows,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdCustomersTableRelationships = formatTableRelationships customersTableRelationships,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdSupportRepRelationshipName = supportRepRelationshipName,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdEmployeesTableName = formatTableName employeesTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdEmployeesRows = employeesRows,
|
|
|
|
_tdEmployeesRowsById = employeesRowsById,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdEmployeesTableRelationships = formatTableRelationships employeesTableRelationships,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdSupportRepForCustomersRelationshipName = supportRepForCustomersRelationshipName,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdInvoicesTableName = formatTableName invoicesTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdInvoicesRows = invoicesRows,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdInvoiceLinesTableName = formatTableName invoiceLinesTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdInvoiceLinesRows = invoiceLinesRows,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdMediaTypesTableName = formatTableName mediaTypesTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdMediaTypesRows = mediaTypesRows,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdTracksTableName = formatTableName tracksTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdTracksRows = tracksRows,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdTracksTableRelationships = formatTableRelationships tracksTableRelationships,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdInvoiceLinesRelationshipName = invoiceLinesRelationshipName,
|
|
|
|
_tdMediaTypeRelationshipName = mediaTypeRelationshipName,
|
|
|
|
_tdAlbumRelationshipName = albumRelationshipName,
|
|
|
|
_tdGenreRelationshipName = genreRelationshipName,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdGenresTableName = formatTableName genresTableName,
|
2022-09-28 02:41:21 +03:00
|
|
|
_tdGenresRows = genresRows,
|
2022-10-06 02:23:49 +03:00
|
|
|
_tdGenresTableRelationships = formatTableRelationships genresTableRelationships,
|
|
|
|
_tdColumnName = API.ColumnName . applyNameCasing _tcColumnNameCasing,
|
|
|
|
_tdColumnField = columnField . applyNameCasing _tcColumnNameCasing,
|
|
|
|
_tdQueryComparisonColumn = queryComparisonColumn . applyNameCasing _tcColumnNameCasing,
|
|
|
|
_tdCurrentComparisonColumn = currentComparisonColumn . applyNameCasing _tcColumnNameCasing,
|
|
|
|
_tdOrderByColumn = \targetPath name -> orderByColumn targetPath (applyNameCasing _tcColumnNameCasing name)
|
2022-09-28 02:41:21 +03:00
|
|
|
}
|
|
|
|
where
|
2022-10-06 02:23:49 +03:00
|
|
|
formatTableName :: API.TableName -> API.TableName
|
|
|
|
formatTableName = applyTableNamePrefix _tcTableNamePrefix . API.TableName . fmap (applyNameCasing _tcTableNameCasing) . API.unTableName
|
|
|
|
|
|
|
|
formatTableRelationships :: API.TableRelationships -> API.TableRelationships
|
|
|
|
formatTableRelationships =
|
|
|
|
prefixTableRelationships
|
|
|
|
>>> API.trRelationships . traverse . API.rColumnMapping %~ (HashMap.toList >>> fmap (bimap formatColumnName formatColumnName) >>> HashMap.fromList)
|
|
|
|
|
|
|
|
formatColumnName :: API.ColumnName -> API.ColumnName
|
|
|
|
formatColumnName = API.ColumnName . applyNameCasing _tcColumnNameCasing . API.unColumnName
|
|
|
|
|
2022-09-28 02:41:21 +03:00
|
|
|
prefixTableRelationships :: API.TableRelationships -> API.TableRelationships
|
|
|
|
prefixTableRelationships =
|
2022-10-06 02:23:49 +03:00
|
|
|
API.trSourceTable %~ formatTableName
|
|
|
|
>>> API.trRelationships . traverse . API.rTargetTable %~ formatTableName
|
|
|
|
|
|
|
|
formatTableInfo :: API.TableInfo -> API.TableInfo
|
|
|
|
formatTableInfo =
|
|
|
|
API.tiName %~ formatTableName
|
|
|
|
>>> API.tiColumns . traverse . API.ciName %~ formatColumnName
|
2022-10-10 05:23:43 +03:00
|
|
|
>>> API.tiPrimaryKey . traverse %~ formatColumnName
|
|
|
|
>>> API.tiForeignKeys . lens API.unForeignKeys (const API.ForeignKeys) . traverse
|
2022-10-06 02:23:49 +03:00
|
|
|
%~ ( API.cForeignTable %~ formatTableName
|
|
|
|
>>> API.cColumnMapping %~ (HashMap.toList >>> fmap (bimap formatColumnName formatColumnName) >>> HashMap.fromList)
|
|
|
|
)
|
2022-09-28 02:41:21 +03:00
|
|
|
|
|
|
|
applyTableNamePrefix :: [Text] -> API.TableName -> API.TableName
|
|
|
|
applyTableNamePrefix prefix tableName@(API.TableName rawTableName) =
|
|
|
|
case NonEmpty.nonEmpty prefix of
|
|
|
|
Just prefix' -> API.TableName (prefix' <> rawTableName)
|
|
|
|
Nothing -> tableName
|
|
|
|
|
2022-10-06 02:23:49 +03:00
|
|
|
applyNameCasing :: NameCasing -> Text -> Text
|
|
|
|
applyNameCasing casing text = case casing of
|
|
|
|
PascalCase -> text
|
|
|
|
Lowercase -> Text.toLower text
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
emptyQuery :: API.Query
|
|
|
|
emptyQuery = API.Query Nothing Nothing Nothing Nothing Nothing Nothing
|
2022-07-28 08:39:48 +03:00
|
|
|
|
2022-07-20 08:20:49 +03:00
|
|
|
sortBy :: (Ixed m, Ord (IxValue m)) => Index m -> [m] -> [m]
|
2022-04-10 07:47:15 +03:00
|
|
|
sortBy propName = sortOn (^? ix propName)
|
2022-06-24 09:58:25 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
filterColumnsByQueryFields :: API.Query -> HashMap API.FieldName API.FieldValue -> HashMap API.FieldName API.FieldValue
|
2022-06-24 09:58:25 +03:00
|
|
|
filterColumnsByQueryFields query =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.filterWithKey (\key _value -> key `elem` columns)
|
2022-06-24 09:58:25 +03:00
|
|
|
where
|
2022-09-21 08:11:53 +03:00
|
|
|
columns = HashMap.keys $ queryFields query
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
filterColumns :: [Text] -> [HashMap API.FieldName API.FieldValue] -> [HashMap API.FieldName API.FieldValue]
|
2022-08-01 03:40:45 +03:00
|
|
|
filterColumns columns =
|
2022-09-21 08:11:53 +03:00
|
|
|
fmap (HashMap.filterWithKey (\key _value -> key `elem` columns'))
|
2022-08-01 03:40:45 +03:00
|
|
|
where
|
2022-09-21 08:11:53 +03:00
|
|
|
columns' = API.FieldName <$> columns
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
renameColumns :: [(Text, Text)] -> [HashMap API.FieldName API.FieldValue] -> [HashMap API.FieldName API.FieldValue]
|
2022-08-01 03:40:45 +03:00
|
|
|
renameColumns columns =
|
2022-09-21 08:11:53 +03:00
|
|
|
fmap (HashMap.fromList . fmap rename . HashMap.toList)
|
2022-08-01 03:40:45 +03:00
|
|
|
where
|
2022-09-21 08:11:53 +03:00
|
|
|
columns' = bimap API.FieldName API.FieldName <$> columns
|
2022-08-01 03:40:45 +03:00
|
|
|
rename original@(key, value) =
|
|
|
|
case find (\(k, _) -> k == key) columns' of
|
|
|
|
Just (_, renamedKey) -> (renamedKey, value)
|
|
|
|
Nothing -> original
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
onlyKeepRelationships :: [API.RelationshipName] -> API.TableRelationships -> API.TableRelationships
|
|
|
|
onlyKeepRelationships names tableRels =
|
|
|
|
tableRels & API.trRelationships %~ HashMap.filterWithKey (\relName _ -> relName `elem` names)
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
queryFields :: API.Query -> HashMap API.FieldName API.Field
|
2022-07-20 08:20:49 +03:00
|
|
|
queryFields = fromMaybe mempty . API._qFields
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
responseRows :: API.QueryResponse -> [HashMap API.FieldName API.FieldValue]
|
2022-07-20 08:20:49 +03:00
|
|
|
responseRows = fromMaybe [] . API._qrRows
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
sortResponseRowsBy :: Text -> API.QueryResponse -> API.QueryResponse
|
|
|
|
sortResponseRowsBy columnName response = response & API.qrRows %~ (fmap (sortBy (API.FieldName columnName)))
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
responseAggregates :: API.QueryResponse -> HashMap API.FieldName J.Value
|
2022-07-28 08:39:48 +03:00
|
|
|
responseAggregates = fromMaybe mempty . API._qrAggregates
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
mkFieldsMap :: [(Text, v)] -> HashMap API.FieldName v
|
|
|
|
mkFieldsMap = HashMap.fromList . fmap (first API.FieldName)
|
|
|
|
|
|
|
|
insertField :: Text -> v -> HashMap API.FieldName v -> HashMap API.FieldName v
|
|
|
|
insertField fieldName = HashMap.insert (API.FieldName fieldName)
|
|
|
|
|
|
|
|
deleteField :: Text -> HashMap API.FieldName v -> HashMap API.FieldName v
|
|
|
|
deleteField fieldName = HashMap.delete (API.FieldName fieldName)
|
|
|
|
|
|
|
|
field :: (Ixed m, Index m ~ API.FieldName) => Text -> Traversal' m (IxValue m)
|
|
|
|
field fieldName = ix (API.FieldName fieldName)
|
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
_ColumnFieldNumber :: Traversal' API.FieldValue Scientific
|
|
|
|
_ColumnFieldNumber = API._ColumnFieldValue . _Number
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
_ColumnFieldString :: Traversal' API.FieldValue Text
|
|
|
|
_ColumnFieldString = API._ColumnFieldValue . _String
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
_ColumnFieldBoolean :: Traversal' API.FieldValue Bool
|
|
|
|
_ColumnFieldBoolean = API._ColumnFieldValue . _Bool
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
columnField :: Text -> API.Field
|
|
|
|
columnField = API.ColumnField . API.ColumnName
|
|
|
|
|
2022-09-20 06:59:47 +03:00
|
|
|
queryComparisonColumn :: Text -> API.ComparisonColumn
|
|
|
|
queryComparisonColumn columnName = API.ComparisonColumn API.QueryTable $ API.ColumnName columnName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-20 06:59:47 +03:00
|
|
|
currentComparisonColumn :: Text -> API.ComparisonColumn
|
|
|
|
currentComparisonColumn columnName = API.ComparisonColumn API.CurrentTable $ API.ColumnName columnName
|
2022-08-19 10:00:46 +03:00
|
|
|
|
|
|
|
orderByColumn :: [API.RelationshipName] -> Text -> API.OrderDirection -> API.OrderByElement
|
|
|
|
orderByColumn targetPath columnName orderDirection =
|
|
|
|
API.OrderByElement targetPath (API.OrderByColumn $ API.ColumnName columnName) orderDirection
|