895                       collected_at, valid_since, valid_until):
  896    """
  897    test add_condition()
  898 
  899    :param cdb_api: API
  900    :param detector_id: String identifying the detector to which the
  901    condition will be added (i.e. 'muonflux/straw_tubes').
  902    :param name: String specifying the name of the condition (e.g. 'strawPositions').
  903    :param values: Dictionary containing the values of the condition.
  904    :param test_type: String specifying the type of condition (e.g. 'calibration').
  905    :param tag: String specifying a tag for the condition. Must be unique
  906    for the same condition name.
  907    :param collected_at: Timestamp specifying the date/time the condition
  908    was acquired. Must be unique w.r.t. the condition name.
  909    :param valid_since: Timestamp specifying the date/time as of when the
  910    condition is valid.
  911    :param valid_until: Timestamp specifying the date/time the
  912    condition up until the condition is valid.
  913    """
  914 
  915    has_correct_parameter_type = True
  916    if type(detector_id) != str or detector_id == "" or type(name) != str or name == "" or \
  917            (type(test_type) != str and type(test_type) is not None) or \
  918            type(tag) != str or tag == "" or values == "" or values is None:
  919        has_correct_parameter_type = False
  920        
  921        
  922        with pytest.raises(TypeError):
  923            assert cdb_api.add_condition(detector_id, name, tag, values, test_type,
  924                                         collected_at, valid_since, valid_until)
  925 
  926    if (type(collected_at) != str and type(collected_at) != datetime.datetime and collected_at is not None) or \
  927            (type(valid_since) != str and type(valid_since) != datetime.datetime and valid_since is not None) or \
  928            (type(valid_until) != str and type(valid_until) != datetime.datetime and valid_until is not None):
  929        has_correct_parameter_type = False
  930        
  931        with pytest.raises(TypeError):
  932            assert cdb_api.add_condition(detector_id, name, tag, values, test_type,
  933                                         collected_at, valid_since, valid_until)
  934 
  935    if (type(collected_at) == str or type(valid_until) == str or type(valid_since) == str) and \
  936            has_correct_parameter_type:
  937        
  938        
  939        if not has_correct_format(collected_at) or not has_correct_format(valid_until) or \
  940                not has_correct_format(valid_since):
  941            has_correct_parameter_type = False
  942            with pytest.raises(ValueError):
  943                assert cdb_api.add_condition(detector_id, name, tag, values, test_type,
  944                                             collected_at, valid_since, valid_until)
  945 
  946    
  947    if (type(valid_since) == datetime.datetime and type(valid_until) == datetime.datetime) and \
  948            valid_since > valid_until:
  949        has_correct_parameter_type = False
  950        with pytest.raises(ValueError):
  951            assert cdb_api.add_condition(detector_id, name, tag, values, test_type, collected_at,
  952                                         valid_since, valid_until)
  953 
  954    
  955    if (type(valid_since) == str and type(valid_until) == str) and has_correct_parameter_type:
  956        if valid_since == "" or valid_until == "":
  957            has_correct_parameter_type = False
  958            with pytest.raises(ValueError):
  959                assert cdb_api.add_condition(detector_id, name, tag, values, test_type,
  960                                             collected_at, valid_since, valid_until)
  961        else:
  962            datetime_valid_since = __convert_str_to_datetime(valid_since)
  963            datetime_valid_until = __convert_str_to_datetime(valid_until)
  964            if datetime_valid_since > datetime_valid_until:
  965                has_correct_parameter_type = False
  966                with pytest.raises(ValueError):
  967                    assert cdb_api.add_condition(detector_id, name, tag, values, test_type,
  968                                                 collected_at, valid_since, valid_until)
  969 
  970    
  971    if detector_id == "detector_id_not_exist" or \
  972            detector_id == "detector_id_exist/sub_detector_not_exist":
  973        has_correct_parameter_type = False
  974        with pytest.raises(ValueError):
  975            assert cdb_api.add_condition(detector_id, name, tag, values, test_type,
  976                                         collected_at, valid_since, valid_until)
  977 
  978    
  979    if detector_id == "detector_id_exist" and name == "condition_exist" and tag == "tag":
  980        has_correct_parameter_type = False
  981        with pytest.raises(ValueError):
  982            assert cdb_api.add_condition(detector_id, name, tag, values, test_type,
  983                                         collected_at, valid_since, valid_until)
  984 
  985    if has_correct_parameter_type:
  986        
  987        cdb_api.add_condition(detector_id, name, tag, values, test_type,
  988                              collected_at, valid_since, valid_until)
  989        detector = cdb_api.get_detector(detector_id)
  990 
  991        
  992        assert detector["name"] == detector_id, "the detector has different name"
  993 
  994        
  995        conditions = detector["conditions"]
  996        new_condition = None
  997        for condition in conditions:
  998            if condition["tag"] == tag and condition["name"] == name:
  999                new_condition = condition
 1000                break
 1001 
 1002        
 1003        assert new_condition["tag"] == tag, "tag is not correct"
 1004 
 1005        
 1006        assert new_condition["values"] == values, "value is not correct"
 1007 
 1008        
 1009        assert new_condition["type"] == test_type, "type is not correct"
 1010 
 1011        
 1012        if type(collected_at) == datetime.datetime:
 1013            assert __convert_str_to_datetime(new_condition["collected_at"]) \
 1014                   == collected_at.replace(microsecond=0), \
 1015                "collected_at is not correct"
 1016        elif type(collected_at) == str:
 1017            assert __convert_str_to_datetime(new_condition["collected_at"]) == \
 1018                   __convert_str_to_datetime(collected_at).replace(microsecond=0), \
 1019                "collected_at is not correct"
 1020 
 1021        
 1022        if type(valid_until) == datetime.datetime:
 1023            assert __convert_str_to_datetime(new_condition["valid_until"]) \
 1024                   == valid_until.replace(microsecond=0), \
 1025                "valid_until is not correct"
 1026        elif type(valid_until) == str:
 1027            assert __convert_str_to_datetime(new_condition["valid_until"]) == \
 1028                   __convert_str_to_datetime(valid_until).replace(microsecond=0), \
 1029                "valid_until is not correct"
 1030 
 1031 
 1032@pytest.mark.parametrize("detector_id, name, tag, test_type, valid_since, valid_until", [
 1033    
 1034    (None, None, None, None, None, None),
 1035    
 1036    (999, 999, 999, 999, 999, 999),
 1037    
 1038    ("", "", "", "", "", ""),
 1039    
 1040    ("detector_id_exist", "condition_exist", "tag", "type",
 1041     "2020-04-16 12:07:30", "2020-04-17 12:07:30"),
 1042    
 1043    ("detector_id_exist", "condition_exist", "tag", "type",
 1044     "2020-04-16 12:07:30:129910", "2020-04-17 12:07:30:129910"),
 1045    
 1046    ("detector_id_exist", "condition_exist", "tag", "type",
 1047     datetime.datetime(2020, 4, 16, 12, 7, 30),
 1048     datetime.datetime(2020, 4, 17, 12, 7, 30)),
 1049    
 1050    ("detector_id_exist", "condition_exist", "tag", "type",
 1051     datetime.datetime(2020, 4, 16, 12, 7, 30, 129910),
 1052     datetime.datetime(2020, 4, 17, 12, 7, 30, 129910)),
 1053    
 1054    ("detector_id_exist", "condition_exist", "tag", "type", "2020-04-16 12:07:30",
 1055     datetime.datetime(2020, 4, 17, 12, 7, 30)),
 1056    
 1057    ("detector_id_exist", "condition_exist", "tag", "type",
 1058     datetime.datetime(2020, 4, 13), "2020-04-16 12:07:30"),
 1059    
 1060    ("detector_id_exist", "condition_exist", "tag", "type", "2020-24-16 12:07:30",
 1061     datetime.datetime(2020, 4, 17, 12, 7, 30)),
 1062    
 1063    ("detector_id_exist", "condition_exist", "tag", "type",
 1064     datetime.datetime(2020, 4, 20, 12, 7, 30),
 1065     datetime.datetime(2020, 4, 17, 12, 7, 30)),
 1066    
 1067    ("detector_id_exist", 999, 999, 999, 999, 999),
 1068    
 1069    ("detector_id_not_exist", "condition_exist", "tag", "type",
 1070     datetime.datetime(2020, 4, 16, 12, 7, 30),
 1071     datetime.datetime(2020, 4, 17, 12, 7, 30)),
 1072    
 1073    ("detector_id_exist/sub_detector_id_not_exist", "condition_exist", "tag", "type",
 1074     datetime.datetime(2020, 4, 16, 12, 7, 30),
 1075     datetime.datetime(2020, 4, 17, 12, 7, 30)),
 1076    
 1077    ("detector_id_exist", "condition_not_exist", "tag", "type",
 1078     datetime.datetime(2020, 4, 16, 12, 7, 30),
 1079     datetime.datetime(2020, 4, 17, 12, 7, 30)),
 1080])