Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Broadcast, Subscribe, Bus, Weather and unit tests(with Spaces) #51

Open
wants to merge 66 commits into
base: cinnabot-go
Choose a base branch
from

Conversation

pengnam
Copy link

@pengnam pengnam commented Dec 22, 2017

New Changes:

Functions added

  1. Bus (refactored)
  2. Weather (refactored)
  3. Broadcast (new)
  • Broadcast should be used as followed: call /broadcast with tags, valid tags will be returned in a mark-up message, and the subsequent message to that will be forwarded to all users who are subscribed to that tag
  1. Subscribe (new) (+ Unsubscribe)
  • After performing a series of checks, subscribes the user to the tag
  1. Spaces (from Cheng Yao) - I have made some edits based on the comments that you gave him two days ago. I have worked on some of them and the rest I will talk to Cheng Yao about it.
  2. help, feedback, cbs
    Fixed the repeated user bug! Sadly that took me some time.
  3. NUS Bus (new) - NUS Bus Timings.

Features added:
Implemented a cache.
Cache is a field under cinnabot so I can refer to it. It is used in cb.Router only cache last function called.

Current problems:

[I am unsure about my approach to testing as we have not talked about it specifically for these functions before]
1) Problems with unit test for bus and weather
I attempted the use the technique in section 1: Monkey Patching.(Refer to https://husobee.github.io/golang/testing/unit-test/2015/06/08/golang-unit-testing.html) This is to "monkey-patch" the http request as I felt that it was not important to that test. However, the monkey patch requires me to declare a variable. (Refer to line before func() main in image.)

image

If I do not declare a separate variable:
image

Hence, is it right for me to monkey-patch so as to not make a URL request? And is monkey-patch the right way to do it?

2) Problems with unit test for broadcast [SOLVED]

Minor notes:

I should have committed early, often, and using lower-case.
I apologise for slightly messy code. I should have cleaned it up proper but I thought that getting your feedback on what I am doing so far is more crucial to the project's success.
I should have better error handling but Im still not sure about go's way of handling errors/exceptions(or the lack of it).

@@ -0,0 +1,39 @@
FROM golang:1.9
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we don't need this file anymore.

main/.#main.go Outdated
@@ -0,0 +1 @@
[email protected]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this file to gitignore!

model/db.go Outdated
CheckTagExists (id int, tag string) bool
CheckSubscribed (id int, tag string) bool
UpdateTag (id int, tag string, flag string) error

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

model/user.go Outdated
@@ -2,13 +2,42 @@ package model

import (
"github.com/jinzhu/gorm"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdlib should appear before 3rd party

model/user.go Outdated
UserID int `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
//DeletedAt time.Time
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the commented line?

}

// About returns a link to Cinnabot's source code.
func (cb *Cinnabot) About(msg *message) {
cb.SendTextMessage(msg.From.ID, "Touch me: https://github.com/varunpatro/Cinnabot")
cb.SendTextMessage(int(msg.Chat.ID), "Touch me: https://github.com/varunpatro/Cinnabot")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add yourself here too! Say it was developed by you too (before showing the link)

//Link returns useful resources
func (cb *Cinnabot) Resources(msg *message) {
resources := make(map[string]string)
resources["usplife"] = "[fb page](https://www.facebook.com/groups/usplife/)"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use const for strings

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean:
const usplife = "testString"
resources["usplife"] = usplife

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but more descriptive const names such as

const (
    uspFacebookUrl := "https://wwww.facebook.com/groups/usplife/"
    uspSpacesUrl := "http://www.nususc.com/Spaces.aspx"
)

}

//Default loc: Cinnamon
loc := &tgbotapi.Location{Latitude: 1.306671, Longitude: 103.773556}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

Copy link
Author

@pengnam pengnam Mar 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant can only be declared on character, string, boolean values, so I can only make the latitude and longitude values a constant value.
Since this is the only only use of it in this file, where should I declare the constant? And how should I do that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare two consts at the top of the file:

const (
    cinnamonLat := 1.306671
    cinnamonLong := 103.773556
)

client := &http.Client{}

req, _ := http.NewRequest("GET", "https://api.data.gov.sg/v1/environment/2-hour-weather-forecast", nil)
req.Header.Set("api-key", "d1Y8YtThOpkE5QUfQZmvuA3ktrHa1uWP")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh. Cardinal sin here. Do not check in sensitive info (api keys) into VCS

basic.go Outdated
"*Subscribe status*\n" + "(sub status) tag: description\n"
for i := 0; i < len(cb.allTags); i += 2 {
if cb.db.CheckSubscribed(msg.From.ID, cb.allTags[i]) {
listText += "✅" + cb.allTags[i] + " : " + cb.allTags[i+1] + "\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the emoji would be great!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me, a green cross is not the best for unsubscribed status. What about the following pairs:

a/ ✅❌
b/ ✅✖️
c/ ✔️✖️

seanngpengnam and others added 27 commits February 22, 2018 12:15
…ot-go"

This reverts commit 52c832c, reversing
changes made to 955b68a.
Update handle for study groups resources
Created a dining hall survey function
* Add function for bot stats

* Refactor code to separate files for feedback and broadcast

* Add feedback for OHS

* Refine and finish stats function

* Remove useless log print in echo

* Better responses for stats function
…innabot into cinnabot-go"

This reverts commit cbfbfd8, reversing
changes made to bcd4e7c.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants