コード例 #1
0
func TestScanCountries(t *testing.T) {
	Convey("Test scan countries", t, func() {
		db := h.CreateDB(t)
		ds.CreateCountriesBucket(db)

		Convey("When cities files exists", func() {
			filename := h.CreateTempfile(t, "# Comment\nWS\tWSM\t882\tWS\tSamoa\tApia\t2944\t192001\tOC\t.ws\tWST\tTala\t685\t\t\tsm,en-WS\t4034894\t\t")

			count, err := scanCountries(db, filename)

			Convey("Stores parsed countries to the db", func() {
				actual := h.ReadFromBucket(t, db, ds.CountriesBucketName, "4034894")
				So(actual, ShouldEqual, "WS\tSamoa\ten|Samoa")
			})

			Convey("Returns number of scanned records", func() {
				So(count, ShouldEqual, 1)
			})

			Convey("Returns no error", func() {
				So(err, ShouldBeNil)
			})
		})

		Convey("When the file has invalid data", func() {
			filename := h.CreateTempfile(t, "crap\ncrap\ncrap")
			count, err := scanCountries(db, filename)

			Convey("Returns a zero number of scanned records", func() {
				So(count, ShouldEqual, 0)
			})

			Convey("Returns an error", func() {
				So(err, ShouldNotBeNil)
			})
		})

		Convey("When some locale has blank id", func() {
			filename := h.CreateTempfile(t, "WS\tWSM\t882\tWS\tSamoa\tApia\t2944\t192001\tOC\t.ws\tWST\tTala\t685\t\t\tsm,en-WS\t\t\t")
			count, _ := scanCountries(db, filename)

			Convey("Returns a zero number of scanned records", func() {
				So(count, ShouldEqual, 0)
			})
		})

		Convey("When countries file does not exist", func() {
			count, err := scanCountries(db, "fake.txt")

			Convey("Returns a zero number of scanned records", func() {
				So(count, ShouldEqual, 0)
			})

			Convey("Returns an error", func() {
				So(err, ShouldNotBeNil)
			})
		})
	})
}
コード例 #2
0
func TestOptions(t *testing.T) {
	Convey("Load options from config file", t, func() {
		Convey("When config file does not exist", func() {
			defaultOptions := buildDefault()
			options := Load("foo.json")

			Convey("Returns default options", func() {
				So(options.Port, ShouldEqual, defaultOptions.Port)
				So(options.Timeout, ShouldEqual, defaultOptions.Timeout)
				So(options.CORSOrigins, ShouldResemble, defaultOptions.CORSOrigins)
				So(options.Locales, ShouldResemble, defaultOptions.Locales)
				So(options.MinPopulation, ShouldResemble, defaultOptions.MinPopulation)
				So(options.CitiesFile, ShouldEqual, defaultOptions.CitiesFile)
				So(options.CountriesFile, ShouldEqual, defaultOptions.CountriesFile)
				So(options.AlternateNamesFile, ShouldEqual, defaultOptions.AlternateNamesFile)
			})
		})

		Convey("When config file exists", func() {
			filename := h.CreateTempfile(
				t,
				`{
					 "Port": "3000",
					 "Timeout": 20,
					 "CORSOrigins": ["localhost", "example.com"],
					 "Locales": ["ru", "uk", "en"],
					 "MinPopulation": 10000,
					 "CountriesFile": "files/countries.txt",
					 "CitiesFile": "files/cities.txt",
					 "AlternateNamesFile": "files/alternate.txt"
				 }`,
			)

			options := Load(filename)

			Convey("Takes values from the config file", func() {
				So(options.Port, ShouldEqual, "3000")
				So(options.Timeout, ShouldEqual, 20)
				So(
					options.CORSOrigins, ShouldResemble,
					[]string{"localhost", "example.com"},
				)
				So(options.Locales, ShouldResemble, []string{"ru", "uk", "en"})
				So(options.MinPopulation, ShouldEqual, 10000)
				So(options.CountriesFile, ShouldEqual, "files/countries.txt")
				So(options.CitiesFile, ShouldEqual, "files/cities.txt")
				So(options.AlternateNamesFile, ShouldEqual, "files/alternate.txt")
			})
		})

		Convey("Panics when config file has wrong format", func() {
			filename := h.CreateTempfile(t, `"Port":}`)
			So(func() { Load(filename) }, ShouldPanic)
		})

		Convey("When the CONFIG env variable is set", func() {
			filename := h.CreateTempfile(t, `{"Port": "3001"}`)
			os.Setenv("CONFIG", filename)
			options := Load("foo.json")

			Convey("Options should be read from the env file", func() {
				So(options.Port, ShouldEqual, "3001")
			})
		})

		Convey("When the PORT env variable is set", func() {
			os.Setenv("PORT", "3030")
			options := Load("foo.json")

			Convey("Options should be read from the env file", func() {
				So(options.Port, ShouldEqual, "3030")
			})
		})
	})
}
コード例 #3
0
func TestScanAlternateNames(t *testing.T) {
	Convey("Test scan alternate names", t, func() {
		db := h.CreateDB(t)
		ds.CreateCitiesBucket(db)
		ds.CreateCityNamesBucket(db)
		ds.CreateCountriesBucket(db)

		h.PutToBucket(t, db, ds.CitiesBucketName, "1", "Montreal\t\t\t\t\t")
		h.PutToBucket(t, db, ds.CitiesBucketName, "2", "Moscow\t\t\t\t\t")
		h.PutToBucket(t, db, ds.CountriesBucketName, "3", "DE\tGermany\ten|Germany")

		locales := []string{"de", "ru"}

		Convey("When alternate names file exists", func() {
			filename := h.CreateTempfile(
				t,
				"10\t1\tfr\tMontréal\t\t\t\t\n11\t2\tde\tMoskau\t\t\t\t\n12\t2\tru\tМосква\t\t\t\t13\t9\tde\tMünchen\t\t\t\t\n"+
					"14\t3\tde\tDeutschland\t\t\t\t\n15\t3\ten\tWest Germany\t\t\t\t\n16\t3\tit\tGermania\t\t\t\t",
			)

			count, err := scanAlternateNames(db, filename, locales)
			country, _ := ds.FindCountry(db, "3")

			Convey("Returns number of scanned records", func() {
				So(count, ShouldEqual, 2)
			})

			Convey("When the locale is supported", func() {
				Convey("Stores the record if the city exists", func() {
					actual := h.ReadFromBucket(t, db, ds.CityNamesBucketName, "moskau")
					So(actual, ShouldEqual, "Moskau\t2\tde\t0")
				})

				Convey("Doesn't store the record if the city doesn't exist", func() {
					actual := h.ReadFromBucket(t, db, ds.CityNamesBucketName, "münchen")
					So(actual, ShouldEqual, "")
				})

				Convey("Adds translations for countries", func() {
					So(country.Translations["de"], ShouldEqual, "Deutschland")
				})

				Convey("Doesn't override en names for countries", func() {
					So(country.Translations["en"], ShouldEqual, "Germany")
				})
			})

			Convey("When the locale is not supported", func() {
				Convey("Doesn't store the record", func() {
					actual := h.ReadFromBucket(t, db, ds.CityNamesBucketName, "montréal")
					So(actual, ShouldEqual, "")
				})

				Convey("Doesn't add translations for countries", func() {
					So(country.Translations["it"], ShouldEqual, "")
				})
			})

			Convey("Returns no error", func() {
				So(err, ShouldBeNil)
			})
		})

		Convey("When alternate names file does not exist", func() {
			count, err := scanAlternateNames(db, "fake.txt", locales)

			Convey("Returns a zero number of scanned records", func() {
				So(count, ShouldEqual, 0)
			})

			Convey("Returns an error", func() {
				So(err, ShouldNotBeNil)
			})
		})
	})
}
コード例 #4
0
ファイル: scan_test.go プロジェクト: lebedev-yury/cities-api
func TestScan(t *testing.T) {
	Convey("Test scan", t, func() {
		db := h.CreateDB(t)
		locales := []string{"ru", "en"}

		countriesFilename := h.CreateTempfile(t, "WS\tWSM\t882\tWS\tSamoa\tApia\t2944\t192001\tOC\t.ws\tWST\tTala\t685\t\t\tsm,en-WS\t4034894\t\t")
		citiesFilename := h.CreateTempfile(t, "890516\tGwanda\tGwanda\tJawunda\t-20.93333\t29\tP\tPPLA\tZW\t\t07\t\t\t\t14450\t\t982\tAfrica/Harare\t2009-06-30")
		alternateNamesFilename := h.CreateTempfile(t, "10\t890516\tru\tГуанда\t\t\t\t")

		Convey("When both files are present and valid", func() {
			done := make(chan bool, 1)
			Scan(db, done, locales, 2000, countriesFilename, citiesFilename, alternateNamesFilename)

			Convey("Does not panics", func() {
				done := make(chan bool, 1)

				So(func() {
					Scan(db, done, locales, 2000, countriesFilename, citiesFilename, alternateNamesFilename)
				}, ShouldNotPanic)
			})

			Convey("Writes countries count to the statistics bucket", func() {
				actual := h.ReadFromBucket(t, db, ds.StatisticsBucketName, "countries_count")
				So(actual, ShouldEqual, "1")
			})

			Convey("Writes cities count to the statistics bucket", func() {
				actual := h.ReadFromBucket(t, db, ds.StatisticsBucketName, "cities_count")
				So(actual, ShouldEqual, "1")
			})

			Convey("Writes citynames count to the statistics bucket", func() {
				actual := h.ReadFromBucket(t, db, ds.StatisticsBucketName, "city_names_count")
				So(actual, ShouldEqual, "2")
			})
		})

		Convey("When the countries file does not exist", func() {
			Convey("Panics", func() {
				done := make(chan bool, 1)

				So(func() {
					Scan(db, done, locales, 2000, "fake.txt", citiesFilename, alternateNamesFilename)
				}, ShouldPanic)
			})
		})

		Convey("When the cities file does not exist", func() {
			Convey("Panics", func() {
				done := make(chan bool, 1)

				So(func() {
					Scan(db, done, locales, 2000, countriesFilename, "fake.txt", alternateNamesFilename)
				}, ShouldPanic)
			})
		})

		Convey("When the alternate names file does not exist", func() {
			Convey("Panics", func() {
				done := make(chan bool, 1)

				So(func() {
					Scan(db, done, locales, 2000, countriesFilename, citiesFilename, "fake.txt")
				}, ShouldPanic)
			})
		})
	})
}
コード例 #5
0
func TestScanCities(t *testing.T) {
	Convey("Test scan cities", t, func() {
		db := h.CreateDB(t)
		ds.CreateCitiesBucket(db)
		ds.CreateCityNamesBucket(db)

		Convey("When cities files exists", func() {
			filename := h.CreateTempfile(
				t,
				"890516\tGwanda\tGwanda\tJawunda\t-20.93333\t29\tP\tPPLA\tZW\t\t07\t\t\t\t14450\t\t982\tAfrica/Harare\t2009-06-30\n"+
					"890983\tGokwe\tGokwe\tGokwe\t-18.20476\t28.9349\tP\tPPL\tZW\t\t02\t\t\t\t18942\t\t1237\tAfrica/Harare\t2012-05-05\n"+
					"890984\tSmall city\tGokwe\tJawunda\t-18.20576\t29.9349\tP\tPPL\tZW\t\t02\t\t\t\t1942\t\t1237\tAfrica/Harare\t2012-05-05",
			)

			count, err := scanCities(db, filename, 2000)

			Convey("Stores parsed cities to the db", func() {
				actual := h.ReadFromBucket(t, db, ds.CitiesBucketName, "890516")
				So(actual, ShouldEqual, "Gwanda\tZW\t14450\t-20.93333\t29\tAfrica/Harare")

				actual = h.ReadFromBucket(t, db, ds.CitiesBucketName, "890983")
				So(actual, ShouldEqual, "Gokwe\tZW\t18942\t-18.20476\t28.9349\tAfrica/Harare")
			})

			Convey("Stores parsed city names to the db", func() {
				actual := h.ReadFromBucket(t, db, ds.CityNamesBucketName, "gwanda")
				So(actual, ShouldEqual, "Gwanda\t890516\ten\t14450")

				actual = h.ReadFromBucket(t, db, ds.CityNamesBucketName, "gokwe")
				So(actual, ShouldEqual, "Gokwe\t890983\ten\t18942")
			})

			Convey("Returns number of scanned records", func() {
				So(count, ShouldEqual, 2)
			})

			Convey("Returns no error", func() {
				So(err, ShouldBeNil)
			})
		})

		Convey("When the file has invalid data", func() {
			filename := h.CreateTempfile(t, "crap\ncrap\ncrap")
			count, err := scanCities(db, filename, 2000)

			Convey("Returns a zero number of scanned records", func() {
				So(count, ShouldEqual, 0)
			})

			Convey("Returns an error", func() {
				So(err, ShouldNotBeNil)
			})
		})

		Convey("When cities file does not exist", func() {
			count, err := scanCities(db, "fake.txt", 2000)

			Convey("Returns a zero number of scanned records", func() {
				So(count, ShouldEqual, 0)
			})

			Convey("Returns an error", func() {
				So(err, ShouldNotBeNil)
			})
		})
	})
}