Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / session / session_test.go
1 // Copyright (c) 2012-2018 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package session_test
6
7 import (
8         "fmt"
9         "github.com/revel/revel"
10         "github.com/revel/revel/session"
11         "github.com/stretchr/testify/assert"
12         "testing"
13 )
14
15 // test the commands
16 func TestSessionString(t *testing.T) {
17         session.InitSession(revel.RevelLog)
18         a := assert.New(t)
19         s := session.NewSession()
20         s.Set("happy", "day")
21         a.Equal("day", s.GetDefault("happy", nil, ""), fmt.Sprintf("Session Data %#v\n", s))
22
23 }
24
25 func TestSessionStruct(t *testing.T) {
26         session.InitSession(revel.RevelLog)
27         a := assert.New(t)
28         s := session.NewSession()
29         setSharedDataTest(s)
30         a.Equal("test", s.GetDefault("happy.a.aa", nil, ""), fmt.Sprintf("Session Data %#v\n", s))
31
32         stringMap := s.Serialize()
33         s1 := session.NewSession()
34         s1.Load(stringMap)
35         testSharedData(s, s1, t, a)
36
37 }
38
39 func setSharedDataTest(s session.Session) {
40         data := struct {
41                 A struct {
42                         Aa string
43                 }
44                 B int
45                 C string
46                 D float32
47         }{A: struct {
48                 Aa string
49         }{Aa: "test"},
50                 B: 5,
51                 C: "test",
52                 D: -325.25}
53         s.Set("happy", data)
54 }
55 func testSharedData(s, s1 session.Session, t *testing.T, a *assert.Assertions) {
56         // Compress the session to a string
57         t.Logf("Original session %#v\n", s)
58         t.Logf("New built session %#v\n", s1)
59         data,err := s1.Get("happy.a.aa")
60         a.Nil(err,"Expected nil")
61         a.Equal("test", data, fmt.Sprintf("Session Data %#v\n", s))
62         t.Logf("After test session %#v\n", s1)
63
64 }