|
1 | | -{-# LANGUAGE BlockArguments #-} |
2 | | - |
3 | 1 | module Tests.InsertSession where |
4 | | - |
5 | | -import Foreword |
6 | | - |
7 | | -import Primer.App ( |
8 | | - newApp, |
9 | | - newEmptyApp, |
10 | | - ) |
11 | | -import Primer.Database ( |
12 | | - SessionData (..), |
13 | | - SessionId, |
14 | | - insertSession, |
15 | | - newSessionId, |
16 | | - querySessionId, |
17 | | - safeMkSessionName, |
18 | | - ) |
19 | | -import Primer.Database.Selda ( |
20 | | - SeldaDbException (InsertError), |
21 | | - ) |
22 | | -import Primer.Database.Selda.Test.Util ( |
23 | | - lowPrecisionCurrentTime, |
24 | | - runTmpDb, |
25 | | - ) |
26 | | -import Primer.Test.App ( |
27 | | - comprehensive, |
28 | | - ) |
29 | | -import Primer.Test.Util ( |
30 | | - assertException, |
31 | | - (@?=), |
32 | | - ) |
33 | | -import Test.Tasty (TestTree) |
34 | | -import Test.Tasty.HUnit (testCaseSteps) |
35 | | - |
36 | | -expectedError :: SessionId -> SeldaDbException -> Bool |
37 | | -expectedError id_ (InsertError s _) = s == id_ |
38 | | -expectedError _ _ = False |
39 | | - |
40 | | -test_insertSession_roundtrip :: TestTree |
41 | | -test_insertSession_roundtrip = testCaseSteps "insertSession database round-tripping" $ \step' -> |
42 | | - runTmpDb $ do |
43 | | - let step = liftIO . step' |
44 | | - step "Insert comprehensive" |
45 | | - now <- lowPrecisionCurrentTime |
46 | | - let version = "git123" |
47 | | - let name = safeMkSessionName "comprehensive" |
48 | | - sessionId <- liftIO newSessionId |
49 | | - insertSession version sessionId comprehensive name now |
50 | | - |
51 | | - step "Retrieve it" |
52 | | - result <- querySessionId sessionId |
53 | | - result @?= Right (SessionData comprehensive name now) |
54 | | - |
55 | | - let jpName = safeMkSessionName "サンプルプログラム" |
56 | | - step "Insert app with Japanese name" |
57 | | - sid1 <- liftIO newSessionId |
58 | | - insertSession version sid1 comprehensive jpName now |
59 | | - r1 <- querySessionId sid1 |
60 | | - r1 @?= Right (SessionData comprehensive jpName now) |
61 | | - |
62 | | - let cnName = safeMkSessionName "示例程序" |
63 | | - step "Insert app with simplified Chinese name" |
64 | | - sid2 <- liftIO newSessionId |
65 | | - insertSession version sid2 comprehensive cnName now |
66 | | - r2 <- querySessionId sid2 |
67 | | - r2 @?= Right (SessionData comprehensive cnName now) |
68 | | - |
69 | | - let arName = safeMkSessionName "برنامج مثال" |
70 | | - step "Insert app with Arabic name" |
71 | | - sid3 <- liftIO newSessionId |
72 | | - insertSession version sid3 comprehensive arName now |
73 | | - r3 <- querySessionId sid3 |
74 | | - r3 @?= Right (SessionData comprehensive arName now) |
75 | | - |
76 | | - let emName = safeMkSessionName "😄😂🤣🤗 🦊 🦈" |
77 | | - step "Insert app with emoji name" |
78 | | - sid4 <- liftIO newSessionId |
79 | | - insertSession version sid4 comprehensive emName now |
80 | | - r4 <- querySessionId sid4 |
81 | | - r4 @?= Right (SessionData comprehensive emName now) |
82 | | - |
83 | | -test_insertSession_failure :: TestTree |
84 | | -test_insertSession_failure = testCaseSteps "insertSession failure modes" $ \step' -> |
85 | | - runTmpDb $ do |
86 | | - let step = liftIO . step' |
87 | | - |
88 | | - step "Insert program" |
89 | | - now <- lowPrecisionCurrentTime |
90 | | - let version = "git123" |
91 | | - let name = safeMkSessionName "testNewApp" |
92 | | - sessionId <- liftIO newSessionId |
93 | | - insertSession version sessionId newApp name now |
94 | | - |
95 | | - step "Attempt to insert the same program and metadata again" |
96 | | - assertException "insertSession" (expectedError sessionId) $ insertSession version sessionId newApp name now |
97 | | - |
98 | | - step "Attempt to insert a different program with the same metadata" |
99 | | - assertException "insertSession" (expectedError sessionId) $ insertSession version sessionId newEmptyApp name now |
100 | | - |
101 | | - step "Attempt to insert the same program with a different version" |
102 | | - let newVersion = "new-" <> version |
103 | | - assertException "insertSession" (expectedError sessionId) $ insertSession newVersion sessionId newApp name now |
104 | | - |
105 | | - step "Attempt to insert the same program with a different name" |
106 | | - let newName = safeMkSessionName "new name" |
107 | | - assertException "insertSession" (expectedError sessionId) $ insertSession version sessionId newApp newName now |
108 | | - |
109 | | - step "Attempt to insert the same program with a different timestamp" |
110 | | - now' <- lowPrecisionCurrentTime |
111 | | - assertException "insertSession" (expectedError sessionId) $ insertSession version sessionId newApp newName now' |
0 commit comments