"""The little flags, and where a country comes from. A flag at twelve pixels by eight is not a rendering of the real thing, so what is checked here is what it has to get right to be worth drawing: the right shape of arrangement, the right colours, and never a flag that belongs to somebody else. """ import numpy as np import pytest from bandsaunter import flags def test_every_flag_is_the_size_it_says_it_is(): for code, rows in flags.FLAGS.items(): assert len(rows) == flags.FLAG_H, code assert all(len(row) == flags.FLAG_W for row in rows), code def test_every_flag_uses_colours_that_exist(): """A typo in a flag would otherwise come out as silent white.""" used = {letter for rows in flags.FLAGS.values() for row in rows for letter in row} assert used <= set(flags.COLOURS), used - set(flags.COLOURS) def test_the_colours_are_a_fixed_order(): """An index into the animation's palette has to mean one colour for the life of the file it is written into.""" assert flags.COLOUR_ORDER == tuple(flags.COLOURS) assert len(flags.COLOUR_ORDER) == len(set(flags.COLOUR_ORDER)) def test_a_country_with_no_flag_here_gets_none_rather_than_a_wrong_one(): assert flags.flag_for("ZZ") is None assert flags.pixels_for("ZZ") is None assert flags.known("ZZ") is False assert flags.known("us") is True # case does not matter def test_a_flag_comes_back_as_pixels(): picture = flags.pixels_for("JP") assert picture.shape == (flags.FLAG_H, flags.FLAG_W, 3) assert picture.dtype == np.uint8 # White at the corner, red in the middle: that is the flag of Japan. assert tuple(picture[0, 0]) == flags.COLOURS["w"] assert tuple(picture[4, 6]) == flags.COLOURS["r"] @pytest.mark.parametrize("code,hoist,middle,fly", [ ("FR", "b", "w", "r"), # blue at the hoist, white, red at the fly ("IT", "g", "w", "r"), ("IE", "g", "w", "o"), ("BE", "k", "y", "r"), ]) def test_a_vertical_tricolour_runs_left_to_right(code, hoist, middle, fly): picture = flags.pixels_for(code) assert tuple(picture[4, 0]) == flags.COLOURS[hoist] assert tuple(picture[4, 6]) == flags.COLOURS[middle] assert tuple(picture[4, 11]) == flags.COLOURS[fly] @pytest.mark.parametrize("code,top,middle,bottom", [ ("DE", "k", "r", "y"), # black over red over gold ("NL", "r", "w", "b"), ("RU", "w", "b", "r"), ("HU", "r", "w", "g"), ]) def test_a_horizontal_tricolour_runs_top_to_bottom(code, top, middle, bottom): picture = flags.pixels_for(code) assert tuple(picture[0, 6]) == flags.COLOURS[top] assert tuple(picture[4, 6]) == flags.COLOURS[middle] assert tuple(picture[7, 6]) == flags.COLOURS[bottom] def test_the_two_ways_round_are_not_the_same_flag(): """Ireland and Italy are the same three colours in the same order; the Netherlands and Russia are the same three the other way up.""" assert flags.flag_for("IE") != flags.flag_for("IT") assert flags.flag_for("NL") != flags.flag_for("RU") def test_a_nordic_cross_is_off_towards_the_hoist(): """It is what makes those five flags recognisable at any size.""" rows = flags.flag_for("SE") upright = [x for x in range(flags.FLAG_W) if rows[0][x] == "y"] assert upright, "no cross at all" assert max(upright) < flags.FLAG_W // 2 + 2, upright def test_the_two_countries_most_likely_to_be_confused_are_not(): """The United States and Malaysia really do look alike; they must at least differ here.""" assert flags.flag_for("US") != flags.flag_for("MY") def test_the_flags_a_receiver_actually_needs_are_all_here(): for code in ("US", "CA", "MX", "GB", "IE", "FR", "DE", "NL", "ES", "IT", "PT", "CH", "AT", "DK", "NO", "SE", "FI", "PL", "RU", "TR", "GR", "JP", "CN", "KR", "IN", "AU", "NZ", "BR", "AR", "ZA", "AE", "QA", "SA", "IL", "EG", "SG", "TH", "PH"): assert flags.known(code), code # --------------------------------------------------------------------------- # Which country an airport is in # --------------------------------------------------------------------------- @pytest.mark.parametrize("code,country", [ ("KSEA", "US"), ("KATL", "US"), ("CYYZ", "CA"), ("EGLL", "GB"), ("EIDW", "IE"), ("LFPG", "FR"), ("EDDF", "DE"), ("EHAM", "NL"), ("LEMD", "ES"), ("LIRF", "IT"), ("RJTT", "JP"), ("ZBAA", "CN"), ("YSSY", "AU"), ("NZAA", "NZ"), ("SBGR", "BR"), ("OMDB", "AE"), ("VIDP", "IN"), ("WSSS", "SG"), ("MMMX", "MX"), ("FAOR", "ZA"), ]) def test_an_airport_code_says_which_country_it_is_in(code, country): """Some routes arrive as nothing but a pair of codes, and the code is enough: the first letter or two is a region.""" assert flags.country_of_icao(code) == country def test_the_longer_prefix_wins(): """K is the United States and KE is not a prefix at all, but LE is Spain while L on its own is nothing.""" assert flags.country_of_icao("LEMD") == "ES" assert flags.country_of_icao("KMIA") == "US" @pytest.mark.parametrize("code", ["", "XX", "XXXX", "K", "1234", "KSE", None]) def test_something_that_is_not_an_airport_code_says_nothing(code): assert flags.country_of_icao(code) == "" def test_a_country_with_no_flag_still_has_a_code_to_fall_back_on(): """The point of the fallback: an airport in a country not drawn here is still labelled, just in letters.""" country = flags.country_of_icao("FQMA") # Mozambique assert country == "MZ" assert flags.flag_for(country) is None