more robust testing

This commit is contained in:
Lucy Cifferello
2020-06-09 12:48:28 -06:00
parent 87a6b9bb9b
commit 57627163ff
9 changed files with 118 additions and 58 deletions

View File

@@ -10,24 +10,24 @@ import Database.Persist.Sql
import Model
import Settings
fetchApp :: MonadIO m => AppIdentifier -> AppVersion -> ReaderT SqlBackend m (Maybe (Entity App))
fetchApp appId appVersion = selectFirst [AppAppId ==. appId, AppSemver ==. appVersion] []
fetchApp :: MonadIO m => AppIdentifier -> AppVersion -> ReaderT SqlBackend m (Maybe (Entity SApp))
fetchApp appId appVersion = selectFirst [SAppAppId ==. appId, SAppVersion ==. appVersion] []
createApp :: MonadIO m => AppIdentifier -> AppSeed -> ReaderT SqlBackend m (Maybe (Key App))
createApp :: MonadIO m => AppIdentifier -> AppSeed -> ReaderT SqlBackend m (Maybe (Key SApp))
createApp appId AppSeed{..} = do
time <- liftIO $ getCurrentTime
insertUnique $ App
time <- liftIO $ getCurrentTime
insertUnique $ SApp
time
Nothing
title
appSeedTitle
appId
descShort
descLong
appVersion
releaseNotes'
iconType
appSeedDescShort
appSeedDescLong
appSeedVersion
appSeedReleaseNotes
appSeedIconType
createMetric :: MonadIO m => Maybe (Key App) -> AppIdentifier -> ReaderT SqlBackend m ()
createMetric :: MonadIO m => Maybe (Key SApp) -> AppIdentifier -> ReaderT SqlBackend m ()
createMetric appId event = do
time <- liftIO $ getCurrentTime
insert_ $ Metric
@@ -41,15 +41,15 @@ createAllAppVersions app appId = do
-- inseryt new records and replace existing records (matching any unique constraint)
putMany $ toList $ storeAppToSeed time appId app
storeAppToSeed :: UTCTime -> AppIdentifier -> StoreApp -> NonEmpty App
storeAppToSeed time appId StoreApp{..} = map (\b -> App
storeAppToSeed :: UTCTime -> AppIdentifier -> StoreApp -> NonEmpty SApp
storeAppToSeed time appId StoreApp{..} = map (\b -> SApp
time
Nothing
storeAppTitle
appId
storeAppDescShort
storeAppDescLong
(semver b)
(releaseNotes b)
(version' b)
(releaseNotes' b)
storeAppIconType
) storeAppSemver
) storeAppVersionInfo

View File

@@ -83,22 +83,22 @@ getApp rootDir ext@(Extension appId) = do
Nothing -> sendResponseStatus status400 ("App not present in manifest" :: Text)
Just StoreApp{..} -> do
-- look up at specfic version
VersionInfo{..} <- case NE.filter (\v -> appVersion == semver v) storeAppSemver of
VersionInfo{..} <- case NE.filter (\v -> appVersion == version' v) storeAppVersionInfo of
[] -> sendResponseStatus status400 ("App version not present in manifest" :: Text)
x : _ -> pure x
pure $ AppSeed
{ title = storeAppTitle
, descShort = storeAppDescShort
, descLong = storeAppDescLong
, appVersion = semver
, releaseNotes' = releaseNotes
, iconType = storeAppIconType
{ appSeedTitle = storeAppTitle
, appSeedDescShort = storeAppDescShort
, appSeedDescLong = storeAppDescLong
, appSeedVersion = version'
, appSeedReleaseNotes = releaseNotes'
, appSeedIconType = storeAppIconType
}
-- create metric based off these app details
appKey <- runDB $ createApp appId' deets
case appKey of
Nothing -> $logWarn $ "app at this version already exists in db, no need to insert"
Just k -> runDB $ createMetric (Just k) appId' -- log app download
Nothing -> $logWarn $ "app at this version already exists in db, no need to insert" -- unreachable
-- log app download
Just k -> runDB $ createMetric (Just k) appId'
Just a -> runDB $ createMetric (Just $ entityKey a) appId'
sz <- liftIO $ fileSize <$> getFileStatus filePath
addHeader "Content-Length" (show sz)

View File

@@ -12,23 +12,23 @@ import Lib.Types.Semver
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
App
SApp
createdAt UTCTime
updatedAt UTCTime Maybe
title Text
appId Text
descShort Text
descLong Text
semver AppVersion
version AppVersion
releaseNotes Text
iconType Text
UniqueSemver semver
UniqueVersion version
deriving Eq
deriving Show
Metric
createdAt UTCTime
appId AppId Maybe default=null
appId SAppId Maybe default=null
event Text
deriving Eq
deriving Show

View File

@@ -96,19 +96,19 @@ getAppManifest resourcesDir = do
type AppIdentifier = Text
data AppSeed = AppSeed
{ title :: Text
, descShort :: Text
, descLong :: Text
, appVersion :: AppVersion
, releaseNotes' :: Text
, iconType :: Text
{ appSeedTitle :: Text
, appSeedDescShort :: Text
, appSeedDescLong :: Text
, appSeedVersion :: AppVersion
, appSeedReleaseNotes :: Text
, appSeedIconType :: Text
} deriving (Show)
data StoreApp = StoreApp
{ storeAppTitle :: Text
, storeAppDescShort :: Text
, storeAppDescLong :: Text
, storeAppSemver :: NonEmpty VersionInfo -- TODO rename
, storeAppVersionInfo :: NonEmpty VersionInfo
, storeAppIconType :: Text
} deriving (Show)
@@ -124,19 +124,19 @@ instance FromJSON AppManifest where
storeAppIconType <- config .: "icon-type"
storeAppDescShort <- config .: "description" >>= (.: "short")
storeAppDescLong <- config .: "description" >>= (.: "long")
storeAppSemver <- config .: "version-info" >>= \case
storeAppVersionInfo <- config .: "version-info" >>= \case
[] -> fail "No Valid Version Info"
(x:xs) -> pure $ x :| xs
return $ (appId, StoreApp {..})
return $ AppManifest (HM.fromList apps)
data VersionInfo = VersionInfo
{ semver :: AppVersion
, releaseNotes :: Text
{ version' :: AppVersion
, releaseNotes' :: Text
} deriving (Eq, Ord, Show)
instance FromJSON VersionInfo where
parseJSON = withObject "version info" $ \o -> do
semver <- o .: "version"
releaseNotes <- o .: "release-notes"
version' <- o .: "version"
releaseNotes' <- o .: "release-notes"
pure VersionInfo {..}