Geoip = require './../index'
chai = require 'chai'

chai.should()

describe 'Geoip', ->
	describe 'lookup', ->
		@timeout 30000

		tests = [
			{ 
				ip: '80.62.117.90'
				country: 
					name: 'Denmark'
					code: 'DK'
				city: 'Copenhagen'
			}
			{ 
				ip: '87.60.160.68'
				country: 
					name: 'Denmark'
					code: 'DK'
				city: 'Copenhagen'
			}
			{
				ip: '185.24.169.72'
				country:
					name: 'Denmark'
					code: 'DK'
				city: 'Ballerup'
			}
		]

		tests.forEach (test) ->
			do (test) ->
				it "should return #{test.country.name} and #{test.city} upon ip #{test.ip}", (done) ->
					Geoip.lookup test.ip, (err, data, info) ->
						(err == null).should.be.true

						(data.country.name).should.equal(data.country.name)
						(data.country.code).should.equal(data.country.code) if data.country.code
						(data.country.region).should.equal(data.region) if data.country.region
						(data.country.city).should.equal(data.city) if data.country.city

						console.log test.ip, 'returns', 'country:', data.country.name or 'N/A', 'country code:', data.country.code or 'N/A', 'region:', data.region or 'N/A', 'city:', data.city or 'N/A', 'source:', info.source, 'time:', info.time

						done()


				it "should use the cache when queried more than once", (done) ->
					Geoip.lookup test.ip, (err, data, info) ->
						Geoip.lookup test.ip, (err, data, info) ->
							(err == null).should.be.true

							(data.country.name).should.equal(data.country.name)
							(data.country.code).should.equal(data.country.code) if data.country.code
							(data.country.region).should.equal(data.region) if data.country.region
							(data.country.city).should.equal(data.city) if data.country.city

							(info.fromCache).should.be.true

							console.log test.ip, 'returns', 'country:', data.country.name or 'N/A', 'country code:', data.country.code or 'N/A', 'region:', data.region or 'N/A', 'city:', data.city or 'N/A', 'source:', info.source, 'time:', info.time

							done()