github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/users/users_test.go (about) 1 // Tests for the users package 2 package users 3 4 import ( 5 "testing" 6 7 "github.com/fragmenta/query" 8 9 "github.com/bitcubate/cryptojournal/src/lib/resource" 10 ) 11 12 var testName = "foo" 13 14 func TestSetup(t *testing.T) { 15 err := resource.SetupTestDatabase(2) 16 if err != nil { 17 t.Fatalf("users: Setup db failed %s", err) 18 } 19 20 // Delete all users first 21 _, err = query.ExecSQL("delete from users;") 22 if err != nil { 23 t.Fatalf("error setting up:%s", err) 24 } 25 26 query.ExecSQL("ALTER SEQUENCE users_id_seq RESTART WITH 1;") 27 28 } 29 30 // Test Create method 31 func TestCreateUsers(t *testing.T) { 32 userParams := map[string]string{ 33 "name": testName, 34 "status": "100", 35 } 36 37 id, err := New().Create(userParams) 38 if err != nil { 39 t.Fatalf("users: Create user failed :%s", err) 40 } 41 42 user, err := Find(id) 43 if err != nil { 44 t.Fatalf("users: Create user find failed") 45 } 46 47 t.Logf("USER FOUND:\n%d-%s-%s", user.ID, user.Name, testName) 48 49 if user.Name != testName { 50 t.Fatalf("users: Create user name failed expected:%s got:%s", testName, user.Name) 51 } 52 53 } 54 55 // Test Index (List) method 56 func TestListUsers(t *testing.T) { 57 58 // Get all users (we should have at least one) 59 results, err := FindAll(Query()) 60 if err != nil { 61 t.Fatalf("users: List no user found :%s", err) 62 } 63 64 if len(results) < 1 { 65 t.Fatalf("users: List no users found :%s", err) 66 } 67 68 } 69 70 // Test Update method 71 func TestUpdateUsers(t *testing.T) { 72 73 // Get the last user (created in TestCreateUsers above) 74 user, err := FindFirst("name=?", testName) 75 if err != nil { 76 t.Fatalf("users: Update no user found :%s", err) 77 } 78 79 name := "bar" 80 userParams := map[string]string{"name": name} 81 err = user.Update(userParams) 82 if err != nil { 83 t.Fatalf("users: Update user failed :%s", err) 84 } 85 86 // Fetch the user again from db 87 user, err = Find(user.ID) 88 if err != nil { 89 t.Fatalf("users: Update user fetch failed :%s", user.Name) 90 } 91 92 if user.Name != name { 93 t.Fatalf("users: Update user failed :%s", user.Name) 94 } 95 96 } 97 98 // TestQuery tests trying to find published resources 99 func TestQuery(t *testing.T) { 100 101 results, err := FindAll(Published()) 102 if err != nil { 103 t.Fatalf("users: error getting users :%s", err) 104 } 105 if len(results) == 0 { 106 t.Fatalf("users: published users not found :%s", err) 107 } 108 109 results, err = FindAll(Query().Where("id>=? AND id <=?", 0, 100)) 110 if err != nil || len(results) == 0 { 111 t.Fatalf("users: no user found :%s", err) 112 } 113 114 } 115 116 // Test Destroy method 117 func TestDestroyUsers(t *testing.T) { 118 119 results, err := FindAll(Query()) 120 if err != nil || len(results) == 0 { 121 t.Fatalf("users: Destroy no user found :%s", err) 122 } 123 user := results[0] 124 count := len(results) 125 126 err = user.Destroy() 127 if err != nil { 128 t.Fatalf("users: Destroy user failed :%s", err) 129 } 130 131 // Check new length of users returned 132 results, err = FindAll(Query()) 133 if err != nil { 134 t.Fatalf("users: Destroy error getting results :%s", err) 135 } 136 137 // length should be one less than previous 138 if len(results) != count-1 { 139 t.Fatalf("users: Destroy user count wrong :%d", len(results)) 140 } 141 142 } 143 144 // TestAllowedParams should always return some params 145 func TestAllowedParams(t *testing.T) { 146 if len(AllowedParams()) == 0 { 147 t.Fatalf("users: no allowed params") 148 } 149 }