diff --git a/Makefile b/Makefile index 38c0750..7e1f88f 100644 --- a/Makefile +++ b/Makefile @@ -16,23 +16,29 @@ BINARY_UNIX=$(BINARY_NAME)_unix # if we do "make run main", it will do "go run main.go" execute: - # will run the go executable, and hence the server. - ~/go/bin/goRubu + - @echo "** Please wait. Connecting with Mongo, Memcached and Spinning up the Go Server **" + - ~/go/bin/goRubu setup: - export GOBIN=~/go/bin/ + - export GOBIN=~/go/bin/ # my pwd is "/Users/home" - $(GOCMD) mod init goRubu + - $(GOCMD) mod init goRubu install: - # will build the package into a single binary. - echo "Executing go install" - $(GOINSTALL) + - @echo "** Will build the package into a single binary **" + - $(GOINSTALL) # make all -> will first install and then run execute all: - install execute + - install execute docker: - - chmod 777 scripts/build_docker.sh - - scripts/build_docker.sh + - @echo "** Spinning up the GoRubu, Mongo and Memcached Docker Containers **" + - @chmod 777 scripts/build_docker.sh + - @scripts/build_docker.sh + +#NOTE: @ before a command will stop showing that command. +test: + - @echo "** Running Tests **" + - $(GOTEST) ./tests -v + diff --git a/commands_benchmarks.md b/commands_benchmarks.md index 8ce105f..02a91c1 100644 --- a/commands_benchmarks.md +++ b/commands_benchmarks.md @@ -5,10 +5,10 @@ telnet localhost 11211 get https://goRubu/MTAyMTk= ``` -this will give the original url, to corresponding to the shorten url. -If you get nothing, it means there is not entry in memcached. +This will give the original url, corresponding to the shortened url. -2. To check Indexed in Mongo. + +2. To check Indexes in Mongo. ```bash db.collection.getIndexes() ``` @@ -19,8 +19,11 @@ go test ./tests -v go test ./tests -v - cover ``` -**NOTE** - Normally these two commands work, but with "go 1.13" they are not working. -Hence use these +**NOTE** - Normally these two commands work, but with "go 1.13", second command (the cover one) is not working. + +I have integrated [coveralls.io](https://coveralls.io/) with goRubu. +or you can use these. + ```bash go test ./... -v -coverpkg=./... -coverprofile=cover.txt go tool cover -html=cover.txt -o cover.html @@ -28,7 +31,7 @@ go tool cover -html=cover.txt -o cover.html ## Docker -1. This will give the logs of that container. +1. This will give the logs of a particular container. ```bash docker logs container_id ``` diff --git a/daos/mainDao.go b/daos/mainDao.go index 6a89874..1e82309 100644 --- a/daos/mainDao.go +++ b/daos/mainDao.go @@ -116,20 +116,20 @@ func CleanDb(uid int) { } // GetCounterValue - update counter field in second collection - incrementer -// { "_id" : ObjectId("5e9b7c0e7b3a8740a2f828c4"), "uniqueid" : "counter", "value" : 10000 } +// { "_id" : ObjectId("5e9b7c0e7b3a8740a2f828c4"), "value" : 10000 } func GetCounterValue() int { // as there will be one row only collection := client.Database(DB_NAME).Collection(COLLECTION2_NAME) - filter := bson.D{primitive.E{Key: "uniqueid", Value: "counter"}} - var result models.IncrementerModel - err := collection.FindOne(context.TODO(), filter).Decode(&result) + + err := collection.FindOne(context.TODO(), bson.D{}).Decode(&result) if err != nil { - log.Println("**ERROR while fetching counter value", err) - // its first hit. You need to create a counter value. - insertResult, _ := collection.InsertOne(context.Background(), models.IncrementerModel{UniqueId: "counter", Value: 10000}) - log.Println("Initialized counter value InsertedId: ", insertResult.InsertedID) - _ = collection.FindOne(context.TODO(), filter).Decode(&result) + log.Println("** ERROR while fetching counter value", err) + // it's a first hit i.e that is "incrementer" collection is right now empty. + // You need to create a counter value. + insertResult, _ := collection.InsertOne(context.Background(), models.IncrementerModel{Value: 10000}) + log.Println(" Initialized counter value InsertedId: ", insertResult.InsertedID) + _ = collection.FindOne(context.TODO(), bson.D{}).Decode(&result) } return result.Value @@ -138,8 +138,6 @@ func GetCounterValue() int { // UpdateCounter - update the counter value by 1 func UpdateCounter() { collection := client.Database(DB_NAME).Collection(COLLECTION2_NAME) - filter := bson.D{primitive.E{Key: "uniqueid", Value: "counter"}} - // $inc will increase value of counter by 1 update := bson.D{ {"$inc", bson.D{ @@ -147,7 +145,7 @@ func UpdateCounter() { }}, } - updateResult, err := collection.UpdateOne(context.TODO(), filter, update) + updateResult, err := collection.UpdateOne(context.TODO(), bson.D{}, update) if err != nil { log.Fatal("**ERROR while updating counter value", err) } diff --git a/models/mainModel.go b/models/mainModel.go index 8c5b704..7c7326a 100644 --- a/models/mainModel.go +++ b/models/mainModel.go @@ -13,9 +13,7 @@ type UrlModel struct { } // IncrementerModel - single row table -// uniqueid - counter // value 10000 type IncrementerModel struct { - UniqueId string `json: "uniqueid"` - Value int `json: "value"` + Value int `json: "value"` } diff --git a/tests/main_test.go b/tests/main_test.go index dd53499..1610584 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -11,6 +11,7 @@ import ( // main test function. Checks both url creation and url redirection // should start with "Test" and latter name should start with capital letter +// TODO: Increase the text coverage. Test for negative cases as well. func commonUtility(input string, wait string) string { shortenedUrl := service.CreateShortenedUrl(input)