<commit_before>// +build integration 

package factual_test

import (
  "os"
  "encoding/json"
  "testing"
  "net/url"

  "github.com/ainsleyc/factual"
)

const testValidPath = "/t/place-categories"
var testEmptyParams = url.Values{} 

type testConfig struct {
  Key string 
  Secret string
}

func getTestConfig() (conf testConfig, err error) {
  config := testConfig{}
  file, err := os.Open("conf.json")
  if err != nil {
    return config, err
  }

  decoder := json.NewDecoder(file)
  err = decoder.Decode(&config)
  if err != nil {
    return config, err
  }

  return config, nil
}

func TestGet_ConfigFile_ShouldExist(t *testing.T) {
  _, err := getTestConfig()
  if err != nil {
    switch err.(type) {
    default:
      t.Error("conf.json has an unknown error")
    case *os.PathError:
      t.Error("conf.json does not exist")
    case *json.SyntaxError:
      t.Error("conf.json is not a valid json")
    }
  }
}

func TestGet_ConfigFile_ShouldHaveRequiredFields(t *testing.T) {
  config, _:= getTestConfig()
  if config.Key == "" {
    t.Error("conf.json is missing Key")
  }
  if config.Secret == "" {
    t.Error("conf.json is missing Secret")
  }
}

func TestGet_InvalidCredentials_ShouldReturnError(t *testing.T) {
  client := factual.NewClient("blah", "blah")
  _, err := client.Get(testValidPath, testEmptyParams)
  if err == nil {
    t.Error("Did not return error for invalid credentials")
  }
}

func TestGet_ValidUrl_ShouldNotReturnError(t *testing.T) {
  config, _:= getTestConfig()
  client := factual.NewClient(config.Key, config.Secret) 
  _, err := client.Get(testValidPath, testEmptyParams)
  if err != nil {
    t.Error("Get returned error for valid url, Factual API may be unavailable")
  }
}

func TestRead_ValidFields_ShouldReturnData(t *testing.T) {
  config, _:= getTestConfig()
  client := factual.NewClient(config.Key, config.Secret) 
  opts := factual.ReadOpts{
    "places-us", 
    factual.NewFilter(
      "name", 
      factual.Eq, 
      "Factual", 
    ),
  }
  resp, err := client.Read(opts)
  if err != nil {
    t.Error("Read returned error for valid parameters, Factual API may be unavailable")
  }
  t.Error("TBD", resp)<commit_msg>Added read sample<commit_after>// +build integration 

package factual_test

import (
  "os"
  "encoding/json"
  "testing"
  "net/url"

  "github.com/ainsleyc/factual"
  "github.com/bitly/go-simplejson"
)

const testValidPath = "/t/place-categories"
var testEmptyParams = url.Values{} 

type testConfig struct {
  Key string 
  Secret string
}

func getTestConfig() (conf testConfig, err error) {
  config := testConfig{}
  file, err := os.Open("conf.json")
  if err != nil {
    return config, err
  }

  decoder := json.NewDecoder(file)
  err = decoder.Decode(&config)
  if err != nil {
    return config, err
  }

  return config, nil
}

func TestGet_ConfigFile_ShouldExist(t *testing.T) {
  _, err := getTestConfig()
  if err != nil {
    switch err.(type) {
    default:
      t.Error("conf.json has an unknown error")
    case *os.PathError:
      t.Error("conf.json does not exist")
    case *json.SyntaxError:
      t.Error("conf.json is not a valid json")
    }
  }
}

func TestGet_ConfigFile_ShouldHaveRequiredFields(t *testing.T) {
  config, _:= getTestConfig()
  if config.Key == "" {
    t.Error("conf.json is missing Key")
  }
  if config.Secret == "" {
    t.Error("conf.json is missing Secret")
  }
}

func TestGet_InvalidCredentials_ShouldReturnError(t *testing.T) {
  client := factual.NewClient("blah", "blah")
  _, err := client.Get(testValidPath, testEmptyParams)
  if err == nil {
    t.Error("Did not return error for invalid credentials")
  }
}

func TestGet_ValidUrl_ShouldNotReturnError(t *testing.T) {
  config, _:= getTestConfig()
  client := factual.NewClient(config.Key, config.Secret) 
  _, err := client.Get(testValidPath, testEmptyParams)
  if err != nil {
    t.Error("Get returned error for valid url, Factual API may be unavailable")
  }
}

func TestGet_Read_ShouldReturnResults(t *testing.T) {
  config, _:= getTestConfig()
  client := factual.NewClient(config.Key, config.Secret) 
  path := "/t/places-us"
  params := url.Values{}
  params.Set("q", "starbucks")
  resp, err := client.Get(path, params)
  if err != nil {
    t.Error("Read returned error for valid parameters, Factual API may be unavailable")
  }
  json, _ := simplejson.NewJson(resp)
  data := json.Get("response").Get("data")
  rows := json.Get("response").Get("included_rows")
  t.Error("TBD", data, rows) 
{'fraction_non_alphanumeric': 0.1359590565198042, 'fraction_numerical': 0.0, 'mean_word_length': 3.526686807653575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2805251', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1498, 'n_words': 367}