~evanj/cms

20ecf61fa10999f549bd442293105843fb5930ed — Evan M Jones 4 months ago cb201a1
Fix(c/space): Test coverage was whacked out over t.Run usage.
2 files changed, 95 insertions(+), 102 deletions(-)

M internal/c/space/space_test.go
M makefile
M internal/c/space/space_test.go => internal/c/space/space_test.go +94 -101
@@ 116,25 116,21 @@ func TestAll(t *testing.T) {
	)

	type SpaceTest struct {
		name string

		create func(*mock_space.Mockdber)
		get    func(*mock_space.Mockdber)
		update func(*mock_space.Mockdber)
		copy   func(*mock_space.Mockdber)
		delete func(*mock_space.Mockdber)

		createSC int
		create   func(*mock_space.Mockdber)
		getSC    int
		get      func(*mock_space.Mockdber)
		updateSC int
		update   func(*mock_space.Mockdber)
		copySC   int
		copy     func(*mock_space.Mockdber)
		deleteSC int
		delete   func(*mock_space.Mockdber)
	}

	tests := []SpaceTest{

		SpaceTest{
			name:     "TestAllHappyPath",
			createSC: http.StatusTemporaryRedirect,
			create: func(db *mock_space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()


@@ 168,7 164,6 @@ func TestAll(t *testing.T) {
		},

		SpaceTest{
			name:     "TestAllBadUser",
			createSC: http.StatusBadRequest,
			create: func(db *mock_space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(nil, space.ErrNoLogin).AnyTimes()


@@ 192,7 187,6 @@ func TestAll(t *testing.T) {
		},

		SpaceTest{
			name:     "TestAllBadSpace",
			createSC: http.StatusBadRequest,
			create: func(db *mock_space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()


@@ 222,95 216,94 @@ func TestAll(t *testing.T) {
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

			var (
				ctrl = gomock.NewController(t)
				db   = mock_space.NewMockdber(ctrl)
				l    = log.New(bytes.NewBufferString(os.DevNull), "", 0)
				s    = space.New(l, db)
				ts   = httptest.NewServer(s)
			)

			// Create
			{
				test.create(db)

				req, _ := newrequest("POST", ts.URL, url.Values{"name": []string{sname}, "desc": []string{sdesc}})
				req.SetBasicAuth(uname, upass)
				req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
				req.Header.Set("Accept", "text/html")

				res, _ := http.DefaultClient.Do(req)
				assert.Equal(t, res.StatusCode, test.createSC)
			}

			// Get
			{
				test.get(db)

				req, _ := newrequest("GET", ts.URL, url.Values{"space": []string{sItem.ID()}})
				req.SetBasicAuth(uname, upass)
				req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
				req.Header.Set("Accept", "text/html")

				res, _ := http.DefaultClient.Do(req)
				assert.Equal(t, res.StatusCode, test.getSC)
			}

			// Update
			{
				test.update(db)

				req, _ := newrequest("PATCH", ts.URL,
					url.Values{
						"space": []string{sItem.ID()},
						"name":  []string{snameUpdate},
						"desc":  []string{sdescUpdate}})

				req.SetBasicAuth(uname, upass)
				req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
				req.Header.Set("Accept", "text/html")

				res, _ := http.DefaultClient.Do(req)
				assert.Equal(t, res.StatusCode, test.updateSC)
			}

			// Copy
			{
				test.copy(db)

				req, _ := newrequest("PUT", ts.URL,
					url.Values{
						"space": []string{sItemUpdate.ID()},
						"name":  []string{snameCopy},
						"desc":  []string{sdescCopy}})
				req.SetBasicAuth(uname, upass)
				req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
				req.Header.Set("Accept", "text/html")

				res, _ := http.DefaultClient.Do(req)
				assert.Equal(t, res.StatusCode, test.copySC)
			}

			// Delete
			{
				test.delete(db)

				req, _ := newrequest("DELETE", ts.URL,
					url.Values{
						"space":  []string{sItem.ID()},
						"method": []string{"DELETE"},
					})

				req.SetBasicAuth(uname, upass)
				req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
				req.Header.Set("Accept", "text/html")

				res, _ := http.DefaultClient.Do(req)
				assert.Equal(t, res.StatusCode, test.deleteSC)
			}
		})
		// NOTE: If we run each iteration in their own t.Run it kills test
		// coverage...

		var (
			ctrl = gomock.NewController(t)
			db   = mock_space.NewMockdber(ctrl)
			l    = log.New(bytes.NewBufferString(os.DevNull), "", 0)
			s    = space.New(l, db)
			ts   = httptest.NewServer(s)
		)

		// Create
		{
			test.create(db)

			req, _ := newrequest("POST", ts.URL, url.Values{"name": []string{sname}, "desc": []string{sdesc}})
			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := http.DefaultClient.Do(req)
			assert.Equal(t, res.StatusCode, test.createSC)
		}

		// Get
		{
			test.get(db)

			req, _ := newrequest("GET", ts.URL, url.Values{"space": []string{sItem.ID()}})
			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := http.DefaultClient.Do(req)
			assert.Equal(t, res.StatusCode, test.getSC)
		}

		// Update
		{
			test.update(db)

			req, _ := newrequest("PATCH", ts.URL,
				url.Values{
					"space": []string{sItem.ID()},
					"name":  []string{snameUpdate},
					"desc":  []string{sdescUpdate}})

			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := http.DefaultClient.Do(req)
			assert.Equal(t, res.StatusCode, test.updateSC)
		}

		// Copy
		{
			test.copy(db)

			req, _ := newrequest("PUT", ts.URL,
				url.Values{
					"space": []string{sItemUpdate.ID()},
					"name":  []string{snameCopy},
					"desc":  []string{sdescCopy}})
			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := http.DefaultClient.Do(req)
			assert.Equal(t, res.StatusCode, test.copySC)
		}

		// Delete
		{
			test.delete(db)

			req, _ := newrequest("DELETE", ts.URL,
				url.Values{
					"space":  []string{sItem.ID()},
					"method": []string{"DELETE"},
				})

			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := http.DefaultClient.Do(req)
			assert.Equal(t, res.StatusCode, test.deleteSC)
		}
	}
}

M makefile => makefile +1 -1
@@ 17,7 17,7 @@ gen:
	@go generate ./...

test: 
	@env $(ENV) go test ./...
	@env $(ENV) go test ./... -count 1

coverage: 
	@env $(ENV) go test ./... -cover -coverprofile=coverage.out ; go tool cover -html=coverage.out