예제 #1
0
    def test_query_all(self):
        expected = set(self.approaches)
        self.assertGreater(len(expected), 0)

        filters = create_filters()
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #2
0
    def test_query_approaches_in_march_with_distance_and_velocity_bounds(self):
        start_date = datetime.date(2020, 3, 1)
        end_date = datetime.date(2020, 3, 31)
        distance_max = 0.4
        distance_min = 0.1
        velocity_max = 20
        velocity_min = 10

        expected = set(approach for approach in self.approaches
                       if start_date <= approach.time.date() <= end_date
                       and distance_min <= approach.distance <= distance_max
                       and velocity_min <= approach.velocity <= velocity_max)
        self.assertGreater(len(expected), 0)

        filters = create_filters(start_date=start_date,
                                 end_date=end_date,
                                 distance_min=distance_min,
                                 distance_max=distance_max,
                                 velocity_min=velocity_min,
                                 velocity_max=velocity_max)
        received = set(self.db.query(filters))

        #print(str(len(expected)),str(len(received)))
        self.assertEqual(expected,
                         received,
                         msg="Computed results do not match expected results.")
예제 #3
0
    def test_query_approaches_in_spring_with_all_bounds_and_not_potentially_hazardous_neos(self):
        start_date = datetime.date(2020, 3, 1)
        end_date = datetime.date(2020, 5, 31)
        distance_max = 0.5
        distance_min = 0.05
        velocity_max = 25
        velocity_min = 5
        diameter_max = 1.5
        diameter_min = 0.5

        expected = set(
            approach for approach in self.approaches
            if start_date <= approach.time.date() <= end_date
            and distance_min <= approach.distance <= distance_max
            and velocity_min <= approach.velocity <= velocity_max
            and diameter_min <= approach.neo.diameter <= diameter_max
            and not approach.neo.hazardous
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(
            start_date=start_date, end_date=end_date,
            distance_min=distance_min, distance_max=distance_max,
            velocity_min=velocity_min, velocity_max=velocity_max,
            diameter_min=diameter_min, diameter_max=diameter_max,
            hazardous=False
        )
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #4
0
    def test_query_with_conflicting_date_bounds(self):
        start_date = datetime.date(2020, 10, 1)
        end_date = datetime.date(2020, 4, 1)

        expected = set()

        filters = create_filters(start_date=start_date, end_date=end_date)
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #5
0
    def test_query_with_max_diameter_and_min_diameter_conflicting(self):
        diameter_max = 0.5
        diameter_min = 1.5

        expected = set()

        filters = create_filters(diameter_min=diameter_min, diameter_max=diameter_max)
        received = set(self.db.query(filters))

        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #6
0
    def test_query_with_max_velocity_and_min_velocity_conflicting(self):
        velocity_max = 10
        velocity_min = 20

        expected = set()

        filters = create_filters(velocity_min=velocity_min, velocity_max=velocity_max)
        received = set(self.db.query(filters))

        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #7
0
    def test_query_with_not_hazardous(self):
        expected = set(
            approach for approach in self.approaches
            if not approach.neo.hazardous
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(hazardous=False)
        received = set(self.db.query(filters))

        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #8
0
    def test_query_approaches_before_july(self):
        end_date = datetime.date(2020, 6, 30)

        expected = set(
            approach for approach in self.approaches
            if approach.time.date() <= end_date
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(end_date=end_date)
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #9
0
    def test_query_approaches_on_march_2(self):
        date = datetime.date(2020, 3, 2)

        expected = set(
            approach for approach in self.approaches
            if approach.time.date() == date
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(date=date)
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #10
0
    def test_query_approaches_after_april(self):
        start_date = datetime.date(2020, 4, 1)

        expected = set(
            approach for approach in self.approaches
            if start_date <= approach.time.date()
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(start_date=start_date)
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #11
0
    def test_query_with_min_distance(self):
        distance_min = 0.1

        expected = set(approach for approach in self.db.get_approaches_list()
                       if distance_min <= approach.distance)
        self.assertGreater(len(expected), 0)

        filters = create_filters(distance_min=distance_min)
        received = set(self.db.query(filters))

        self.assertEqual(expected,
                         received,
                         msg="Computed results do not match expected results.")
예제 #12
0
    def test_query_with_max_velocity(self):
        velocity_max = 20

        expected = set(approach for approach in self.db.get_approaches_list()
                       if approach.velocity <= velocity_max)
        self.assertGreater(len(expected), 0)

        filters = create_filters(velocity_max=velocity_max)
        received = set(self.db.query(filters))

        self.assertEqual(expected,
                         received,
                         msg="Computed results do not match expected results.")
예제 #13
0
    def test_query_approaches_in_march(self):
        start_date = datetime.date(2020, 3, 1)
        end_date = datetime.date(2020, 3, 31)

        expected = set(approach for approach in self.db.get_approaches_list()
                       if start_date <= approach.time.date() <= end_date)
        self.assertGreater(len(expected), 0)

        filters = create_filters(start_date=start_date, end_date=end_date)
        received = set(self.db.query(filters))
        self.assertEqual(expected,
                         received,
                         msg="Computed results do not match expected results.")
예제 #14
0
    def test_query_with_max_diameter(self):
        diameter_max = 1.5

        expected = set(approach for approach in self.db.get_approaches_list()
                       if approach.neo.diameter <= diameter_max)
        self.assertGreater(len(expected), 0)

        filters = create_filters(diameter_max=diameter_max)
        received = set(self.db.query(filters))

        self.assertEqual(expected,
                         received,
                         msg="Computed results do not match expected results.")
예제 #15
0
    def test_query_with_min_diameter(self):
        diameter_min = 0.5

        expected = set(
            approach for approach in self.approaches
            if diameter_min <= approach.neo.diameter
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(diameter_min=diameter_min)
        received = set(self.db.query(filters))

        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #16
0
    def test_query_with_min_velocity(self):
        velocity_min = 10

        expected = set(
            approach for approach in self.approaches
            if velocity_min <= approach.velocity
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(velocity_min=velocity_min)
        received = set(self.db.query(filters))

        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #17
0
    def test_query_with_max_distance(self):
        distance_max = 0.4

        expected = set(
            approach for approach in self.approaches
            if approach.distance <= distance_max
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(distance_max=distance_max)
        received = set(self.db.query(filters))

        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #18
0
def query(database, args):
    """Perform the `query` subcommand.

    Create a collection of filters with `create_filters` and supply them to
    the database's `query` method to produce a stream of matching results.

    If an output file wasn't given, print these results to stdout, limiting
    to 10 entries if no limit was specified. If an output file was given,
    use the file's extension to infer whether the file should hold CSV
    or JSON data, and then write the results to the output file in
    that format.

    :param database: The `NEODatabase` containing data on NEOs and
    their close approaches.
    :param args: All arguments from the command line, as parsed by
    the top-level parser.
    """
    # Construct a collection of filters from arguments supplied at
    # the command line.
    filters = create_filters(
        date=args.date,
        start_date=args.start_date,
        end_date=args.end_date,
        distance_min=args.distance_min,
        distance_max=args.distance_max,
        velocity_min=args.velocity_min,
        velocity_max=args.velocity_max,
        diameter_min=args.diameter_min,
        diameter_max=args.diameter_max,
        hazardous=args.hazardous,
    )
    # Query the database with the collection of filters.
    results = database.query(filters)

    if not args.outfile:
        # Write the results to stdout, limiting to 10 entries if
        # not specified.
        for result in limit(results, args.limit or 10):
            print(result)
    else:
        # Write the results to a file.
        if args.outfile.suffix == ".csv":
            write_to_csv(limit(results, args.limit), args.outfile)
        elif args.outfile.suffix == ".json":
            write_to_json(limit(results, args.limit), args.outfile)
        else:
            print(
                "Please use output file that ends with `.csv` or `.json`.",
                file=sys.stderr,
            )
예제 #19
0
    def test_query_approaches_on_march_2_with_min_distance(self):
        date = datetime.date(2020, 3, 2)
        distance_min = 0.1

        expected = set(
            approach for approach in self.approaches
            if approach.time.date() == date
            and distance_min <= approach.distance
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(date=date, distance_min=distance_min)
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #20
0
    def test_query_with_bounds_and_a_specific_date(self):
        start_date = datetime.date(2020, 2, 1)
        date = datetime.date(2020, 3, 2)
        end_date = datetime.date(2020, 4, 1)

        expected = set(
            approach for approach in self.approaches
            if approach.time.date() == date
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(date=date, start_date=start_date, end_date=end_date)
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #21
0
    def test_query_approaches_in_march_with_min_distance_and_max_distance(self):
        start_date = datetime.date(2020, 3, 1)
        end_date = datetime.date(2020, 3, 31)
        distance_max = 0.4
        distance_min = 0.1

        expected = set(
            approach for approach in self.approaches
            if start_date <= approach.time.date() <= end_date
            and distance_min <= approach.distance <= distance_max
        )
        self.assertGreater(len(expected), 0)

        filters = create_filters(
            start_date=start_date, end_date=end_date,
            distance_min=distance_min, distance_max=distance_max,
        )
        received = set(self.db.query(filters))
        self.assertEqual(expected, received, msg="Computed results do not match expected results.")
예제 #22
0
def query(database, args):
    """Perform the `query` subcommand.

    :param database: The `NEODatabase` containing data on NEOs and their close approaches.
    :param args: All arguments from the command line, as parsed by the top-level parser.
    """
    # Construct a collection of filters from arguments supplied at the command line.
    filters = create_filters(date=args.date,
                             start_date=args.start_date,
                             end_date=args.end_date,
                             distance_min=args.distance_min,
                             distance_max=args.distance_max,
                             velocity_min=args.velocity_min,
                             velocity_max=args.velocity_max,
                             diameter_min=args.diameter_min,
                             diameter_max=args.diameter_max,
                             hazardous=args.hazardous)

    results = database.query(filters)

    if not args.outfile:
        # Write the results to stdout, limiting to 10 entries if not specified.
        for result in limit(results, args.limit or 10):
            try:
                print(result)
            except StopIteration:
                break
    else:
        # Write the results to a file.
        if args.outfile.suffix == '.csv':
            write_to_csv(limit(results, args.limit), args.outfile)
        elif args.outfile.suffix == '.json':
            write_to_json(limit(results, args.limit), args.outfile)
        else:
            print(
                "Please use an output file that ends with `.csv` or `.json`.",
                file=sys.stderr)